标签:hihocoder span void test i++ front include name hoc
题目:http://hihocoder.com/problemset/problem/1378
在求出最大流之后遍历一下最后一次标记的的点,即为S集合。
题目本身对最小割介绍的不错。
最小割模板:
1 #include "iostream" 2 #include "algorithm" 3 #include "stdlib.h" 4 #include "queue" 5 #include "memory.h" 6 using namespace std; 7 #define inf 0x3f3f3f3f 8 int n, m; 9 int pic[505][505], flow[505], pre[505], ans[505]; 10 11 int bfs(){ 12 int i, t; 13 queue<int> s; 14 memset(flow, 0, sizeof(flow)); 15 memset(pre, -1, sizeof(pre)); 16 s.push(1); 17 flow[1] = inf; 18 while(!s.empty()){ 19 t = s.front(); 20 if(t == n) return flow[n]; 21 s.pop(); 22 for(i = 2; i <= n; i++){ 23 if(pic[t][i]>0 && pre[i] == -1){ 24 flow[i] = min(flow[t], pic[t][i]); 25 pre[i] = t; 26 s.push(i); 27 } 28 } 29 } 30 return flow[n]; 31 } 32 void EK(){ 33 int i, maxflow = 0, cnt, add, now; 34 while(add = bfs()){ 35 maxflow += add; 36 now = n; 37 while(now != 1){ 38 pic[pre[now]][now] -= add; 39 pic[now][pre[now]] += add; 40 now = pre[now]; 41 } 42 } 43 cnt = 0; 44 for(i = 0 ;i <= n; i++){ 45 if(flow[i]) ans[cnt++] = i; 46 } 47 cout<<maxflow<<‘ ‘<<cnt<<endl; 48 for(i = 0 ;i < cnt; i++) cout<<ans[i]<<‘ ‘; 49 cout<<endl; 50 } 51 int main(){ 52 //freopen("test.txt", "r", stdin);///////////// 53 int u, v, c; 54 while(cin>>n>>m){ 55 memset(pic, 0, sizeof(pic)); 56 for(int i = 0; i < m; i++){ 57 cin>>u>>v>>c; 58 pic[u][v] += c; 59 } 60 EK(); 61 } 62 return 0; 63 }
标签:hihocoder span void test i++ front include name hoc
原文地址:https://www.cnblogs.com/zUotTe0/p/8878039.html