poj 3436 最大流算法(dinic+虚拟源汇+拆点+残留网络求流量)

ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4855 Accepted: 1657 Special Judge

Description

As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory.

Every ACM computer consists of P parts. When all these parts are present, the computer is ready and can be shipped to one of the numerous ACM contests.

Computer manufacturing is fully automated by using N various machines. Each machine removes some parts from a half-finished computer and adds some new parts (removing of parts is sometimes necessary as the parts cannot be added to a computer in arbitrary order). Each machine is described by its performance (measured in computers per hour), input and output specification.

Input specification describes which parts must be present in a half-finished computer for the machine to be able to operate on it. The specification is a set ofP numbers 0, 1 or 2 (one number for each part), where 0 means that corresponding part must not be present, 1 — the part is required, 2 — presence of the part doesn't matter.

Output specification describes the result of the operation, and is a set of P numbers 0 or 1, where 0 means that the part is absent, 1 — the part is present.

The machines are connected by very fast production lines so that delivery time is negligibly small compared to production time.

After many years of operation the overall performance of the ACM Computer Factory became insufficient for satisfying the growing contest needs. That is why ACM directorate decided to upgrade the factory.

As different machines were installed in different time periods, they were often not optimally connected to the existing factory machines. It was noted that the easiest way to upgrade the factory is to rearrange production lines. ACM directorate decided to entrust you with solving this problem.

Input

Input file contains integers P N, then N descriptions of the machines. The description ofith machine is represented as by 2P + 1 integersQi Si,1Si,2...Si,PDi,1Di,2...Di,P, whereQi specifies performance,Si,j — input specification for partj,Di,k — output specification for partk.

Constraints

1 ≤ P ≤ 10, 1 ≤ N ≤ 50, 1 ≤ Qi ≤ 10000

Output

Output the maximum possible overall performance, then M — number of connections that must be made, thenM descriptions of the connections. Each connection between machinesA andB must be described by three positive numbersA B W, whereW is the number of computers delivered fromA toB per hour.

If several solutions exist, output any of them.

Sample Input

Sample input 1
3 4
15  0 0 0  0 1 0
10  0 0 0  0 1 1
30  0 1 2  1 1 1
3   0 2 1  1 1 1
Sample input 2
3 5
5   0 0 0  0 1 0
100 0 1 0  1 0 1
3   0 1 0  1 1 0
1   1 0 1  1 1 0
300 1 1 2  1 1 1
Sample input 3
2 2
100  0 0  1 0
200  0 1  1 1

Sample Output

Sample output 1
25 2
1 3 15
2 3 10
Sample output 2
4 5
1 3 3
3 5 3
1 2 1
2 4 1
4 5 1
Sample output 3
0 0

Hint

Bold texts appearing in the sample sections are informative and do not form part of the actual data.

Source

Northeastern Europe 2005, Far-Eastern Subregion


很不错的模板题。
题目中,需要先将输入数据预处理成有向图。思路就是判断第i个机器后面能否连第j个机器。也就是看i的输出与j的输入是否“匹配”。“匹配”的定义题目中已经给出:j要求的i必须给,j不要的i必须不能给,j无所谓的i随便。
输入中没有1的结点,是源点。(注意!!这里只需要没有1就可以,并不需要都为0)。
输出都为1的结点,是汇点。
发现这是一个 多源多汇问题。自然再增加一个虚拟源点和一个虚拟汇点,转化为单源单汇问题。将虚拟源点连向所有源点,将所有汇点连向虚拟汇点。
可是这题每个 结点还有流量限制(这个机器的最大功率)。自然想到拆点。将一个结点拆成两个点,用这台机器的流量限制作为这两个点的连线的cap。这里用到的小技巧是,所有偶数点只进不出,所有奇数点只出不进。一个点i对应2*i和2*i+1两个新点。
至此可以求最大流。

题目还需要输出最大流的网络图中每条边的流量。

这个只需要用原图减去残留网络就可以了(不算新增的虚拟源汇边,和结点内部边)。

提交记录:
1.Accepted!

#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
        
        
#include 
        
        
          #include 
         
           #include 
          
            #define MAX_V 500 #define oo 0x3f3f3f3f using namespace std; struct edge {int to, cap, rev;}; vector 
           
             G[MAX_V]; struct machine { int time; int in[20], out[20]; }; machine m[100]; int level[MAX_V];//顶点到源点的距离编号 int iter[MAX_V];//当前弧,在其之前的边不用再考虑 void add_edge(int from, int to, int cap) { G[from].push_back((edge){to, cap, G[to].size()}); G[to].push_back((edge){from, 0, G[from].size()-1}); } //bfs用来计算从源点出发所有点的距离编号 void bfs(int s) { memset(level, -1, sizeof(level)); queue 
            
              que; level[s] = 0; que.push(s); while(!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } //通过DFS寻找当前的最短的增广路 int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < G[v].size(); i++) {//这里用引用,巧妙地修改了iter数组 edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) {//level[v] < level[e.to]这个条件保证了当前的增广路是最短的 int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap-=d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; for(;;) { bfs(s); if (level[t] < 0) return flow; memset(iter, 0, sizeof(iter)); int f; while (( f = dfs(s, t, oo)) > 0) { flow += f; } } } int main() { int P, N; //while (cin >> n >> np >> nc >> m) { while (EOF != scanf("%d%d", &P, &N)) { int i, j, k; for (i = 0; i < MAX_V; i++) G[i].clear(); for (i = 0; i < N; i++) { scanf("%d", &m[i].time); for (j = 0; j < P; j++) { scanf("%d", &m[i].in[j]); } for (j = 0; j < P; j++) { scanf("%d", &m[i].out[j]); } } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { if (i != j) { for (k = 0; k < P; k++) { if ((m[i].out[k] == 1 && m[j].in[k] == 0) || (m[i].out[k] == 0 && m[j].in[k] == 1)) { break; } } if (k == P) { add_edge(2*i+1, 2*j, oo); } } } add_edge(2*i, 2*i+1, m[i].time);//结点本身有流量限制,将结点展开成两个点,然后用流量链接一下就好 for (k = 0; k < P; k++) { if (m[i].in[k] == 1) break; } if (k == P) { add_edge(2*N, 2*i, oo); } for (k = 0; k < P; k++) { if (m[i].out[k] == 0) break; } if (k == P) add_edge(2*i+1, 2*N+1, oo); } vector 
             
               G_origin[MAX_V];//将原图保存下来 for (i = 0; i <= 2*N+1; i++) { G_origin[i] = G[i]; } printf("%d ", max_flow(2*N, 2*N+1));//max_flow后,网络G变成残余网络!! int count = 0; for (i = 0; i <= 2 * N + 1; i++) { if (i != 2 * N && i % 2 != 0) {//源点不是虚拟源点和新添加的结点本身的边 for (j = 0; j < G_origin[i].size(); j++) { edge &e = G_origin[i][j]; if (e.to % 2 != 1 && e.to != 2 * N + 1 && e.cap > G[i][j].cap) {//汇点不是虚拟汇点和新添加的结点本身的边 count ++;//先数个数 } } } } printf("%d\n", count); for (i = 0; i <= 2 * N + 1; i++) { if (i != 2 * N && i % 2 != 0) { for (j = 0; j < G_origin[i].size(); j++) { edge &e = G_origin[i][j]; if (e.to % 2 != 1 && e.to != 2 * N + 1 && e.cap > G[i][j].cap) { printf("%d %d %d\n", i/2+1, e.to/2+1, e.cap - G[i][j].cap);//正式输出!! //原网络流量减去残余网络流量,其实就是最终的流量选择 } } } } } return 0; } 
              
             
            
           
          
        
       
       
      
      
     
     
    
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值