poj-1325
来源:互联网 发布:数据分组分析法案例 编辑:程序博客网 时间:2024/04/30 09:59
// 428K 0MS G++ #include <stdio.h>#include <string.h>#define MAX 110#define JOB_MAX 1020int G[MAX][MAX];int n;int m;int k;int V1[MAX];int V2[MAX];int waitingMove[MAX];char getPair(int beginId) { for (int i = 0; i < m; i++) { if (G[beginId][i]) { // check the connected V2 node if (V2[i] == -1) { // this node is not assigned yet V1[beginId] = i; V2[i] = beginId; return 1; } else { if (!waitingMove[V2[i]]) { // if i not waiiting for move waitingMove[V2[i]] = 1; //indicate i is waitting for move if (getPair(V2[i])) { // if move successfully V1[beginId] = i; V2[i] = beginId; return 1; } } } } } return 0;}int solve() { int restartTime = 0; for (int i = 0; i < n; i++) { memset(waitingMove, 0, sizeof(waitingMove)); if (getPair(i)) { restartTime++; } } printf("%d\n", restartTime);}int main() { while(1){ scanf("%d", &n); if (n == 0) { return 1; } scanf("%d %d", &m, &k); memset(G, 0, sizeof(G)); for (int i = 0; i < MAX; i++) { V1[i] = -1; V2[i] = -1; } for (int i = 0; i < k; i++) { int id; int M1Mode; int M2Mode; scanf("%d %d %d", &id, &M1Mode, &M2Mode); if (M1Mode * M2Mode != 0) { // if mode is 0, can process without restart G[M1Mode][M2Mode] = 1; } } solve(); }}
要不是做过3041,估计二分图根本建立不了,不过尽管如此,还是费了些功夫才想到3041的模式。
题的转化难度比较高,题目求的是最少的restart time,一开始感觉是很难和二分图扯上关系的,不知道restart time在图中如何表示,点和边似乎都不合适。
后来看到机器是两台,和3041的激光射击模式只有两种对应了起来,于是便迅速的解出来了:
二分图两个点集,V1 V2, V1中的点Ax代表在模式x下的机器1,同样V2中的点By代表在模式y下的机器2
那么每一个job,都化成了V1 V2间的一条边,比如,一个job可以被模式2的机器1处理,也可以被模式3的机器2处理,那么这个job就是一条连接了
A2和B3的边(有可能会有多条同样的边,但是对本题没有影响),到了这里,似乎还是得不到最少的restart time,但是观察注意, 每个点代表着一个机器1/2的模式,而机器最开始是从模式0开始的,
也就是说,如果这些点里面没有A0和B0, 那么,有几个点,就代表需要restart机器1/2 几次,比如 有A2 A3 B5 B8, 那么不管如何操作,最少都需要四次的restart才能让机器
1/2经历过这些状态,那么只要点最少,同时保证每条边都被点所覆盖,就是最少的restart time了,这时候,就变成最小点覆盖的问题了,直接等于最大匹配,匈牙利解法。
至于如何将A0/B0(初始的模式0)去掉,也简单,之前做一次预处理即可,如果job里有能在A0/B0下被处理的,那么不需要restart就可以将其处理掉,
完全不必将其带入到最大匹配的过程中去,所以,只需要在最开始根据输入建图的时候,如果有能在A0/B0下处理的job,直接忽略即可,这样就去掉了初始mode0的影响.
二分图真是NB,但是转化的难度也够大.
- POJ 1325
- poj-1325
- poj 1325
- POJ 1325 Machine Schedule
- POJ 1325 Machine Schedule
- POJ 1325 Machine Schedule
- poj 1325 Machine Schedule
- Poj 1325 Machine Schedule
- POJ 1325-Machine Schedule
- POJ 1325 Machine Schedule
- POJ 1325 Machine Schedule
- POJ-1325-Machine Schedule
- poj 1325 Machine Schedule
- poj 1325 二分匹配
- hdu 1325 poj 1308
- POJ 1325 Machine Schedule
- poj 1325
- POJ 1325 Machine Schedule
- BUPT Summer Journey #test1 B
- Android中的onTouch()事件如何区分单击事件和滑动事件
- vc真彩工具条的实现
- single number
- 微信加入“微社区” Discuz!发力移动社交
- poj-1325
- 寻找汉密尔顿回路(java版)
- vsnprintf 打印变长参数
- JQuery了。。。
- 前台创建多个name 一样的名称 用String[] 传到 控制器
- Android工程打包第三方jar包和so方法
- (一)无显示器无键盘无路由器,最简方式配置树莓派
- The declared package "" does not match the expected package "javaControlSql"
- 直升机思维