标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13273 Accepted Submission(s): 6288
1 #include <iostream> 2 #include <queue> 3 #include<string.h> 4 using namespace std; 5 #define arraysize 201 6 int maxData = 0x7fffffff; 7 int capacity[arraysize][arraysize]; //记录残留网络的容量 8 int flow[arraysize]; //标记从源点到当前节点实际还剩多少流量可用 9 int pre[arraysize]; //标记在这条路径上当前节点的前驱,同时标记该节点是否在队列中 10 int n,m; 11 queue<int> myqueue; 12 int BFS(int src,int des) 13 { 14 int i,j; 15 while(!myqueue.empty()) //队列清空 16 myqueue.pop(); 17 for(i=1;i<m+1;++i) 18 { 19 pre[i]=-1; 20 } 21 pre[src]=0; 22 flow[src]= maxData; 23 myqueue.push(src); 24 while(!myqueue.empty()) 25 { 26 int index = myqueue.front(); 27 myqueue.pop(); 28 if(index == des) //找到了增广路径 29 break; 30 for(i=1;i<m+1;++i) 31 { 32 if(i!=src && capacity[index][i]>0 && pre[i]==-1) 33 { 34 pre[i] = index; //记录前驱 35 flow[i] = min(capacity[index][i],flow[index]); //关键:迭代的找到增量 36 myqueue.push(i); 37 } 38 } 39 } 40 if(pre[des]==-1) //残留图中不再存在增广路径 41 return -1; 42 else 43 return flow[des]; 44 } 45 int maxFlow(int src,int des) 46 { 47 int increasement= 0; 48 int sumflow = 0; 49 while((increasement=BFS(src,des))!=-1) 50 { 51 int k = des; //利用前驱寻找路径 52 while(k!=src) 53 { 54 int last = pre[k]; 55 capacity[last][k] -= increasement; //改变正向边的容量 56 capacity[k][last] += increasement; //改变反向边的容量 57 k = last; 58 } 59 sumflow += increasement; 60 } 61 return sumflow; 62 } 63 int main() 64 { 65 int i,j; 66 int start,end,ci; 67 while(cin>>n>>m) 68 { 69 memset(capacity,0,sizeof(capacity)); 70 memset(flow,0,sizeof(flow)); 71 for(i=0;i<n;++i) 72 { 73 cin>>start>>end>>ci; 74 if(start == end) //考虑起点终点相同的情况 75 continue; 76 capacity[start][end] +=ci; //此处注意可能出现多条同一起点终点的情况 77 } 78 cout<<maxFlow(1,m)<<endl; 79 } 80 return 0; 81 }
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 #include <queue> 6 #include <vector> 7 #include <string.h> 8 using namespace std; 9 const int MAX = 210; 10 const int INF = 0x3f3f3f3f; 11 struct Edge 12 { 13 int from,to,cap,flow; 14 Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}; 15 }; 16 int n,m; 17 vector<Edge> edge; 18 vector<int> g[MAX]; 19 int a[MAX],p[MAX]; 20 void init() 21 { 22 for(int i = 0; i <= m; i++) 23 g[i].clear(); 24 edge.clear(); 25 } 26 int Maxflow(int s,int t) 27 { 28 int flow = 0; 29 while(true) 30 { 31 memset(a,0,sizeof(a)); 32 queue<int> q; 33 q.push(s); 34 a[s] = INF; 35 while(q.size()) 36 { 37 int x = q.front(); 38 q.pop(); 39 int len = g[x].size(); 40 for(int i = 0; i < len; i++) 41 { 42 Edge e = edge[ g[x][i] ]; 43 if(a[e.to] == 0 && e.cap > e.flow) 44 { 45 p[e.to] = g[x][i]; 46 a[e.to] = min(a[x],e.cap - e.flow); 47 q.push(e.to); 48 } 49 } 50 if(a[t]) 51 break; 52 } 53 if(a[t] == 0) 54 break; 55 for(int i = t; i != s; i = edge[ p[i] ].from) 56 { 57 edge[ p[i] ].flow += a[t]; 58 edge[ p[i] ^ 1 ].flow -= a[t]; 59 } 60 flow += a[t]; 61 } 62 return flow; 63 } 64 int main() 65 { 66 while(scanf("%d%d", &n,&m) != EOF) 67 { 68 int s,e,v,t; 69 init(); 70 for(int i = 1; i <= n; i++) 71 { 72 scanf("%d%d%d",&s,&e,&v); 73 edge.push_back(Edge(s,e,v,0)); 74 edge.push_back(Edge(e,s,0,0)); 75 t = edge.size(); 76 g[s].push_back(t - 2); 77 g[e].push_back(t - 1); 78 } 79 80 printf("%d\n",Maxflow(1,m)); 81 } 82 }
HD1532Drainage Ditches(最大流模板裸题 + 邻接表)
标签:
原文地址:http://www.cnblogs.com/zhaopAC/p/5018095.html