标签:void one imu return cert tom otto ber rip
1 #include<stdio.h> 2 #include<string> 3 #include<string.h> 4 #include<vector> 5 #include<queue> 6 using namespace std; 7 #pragma comment(linker, "/STACK:1024000000,1024000000") 8 const int maxn = 1e5 + 10; 9 const int INF = 999999999; 10 struct node { 11 int from, to, cap, flow; 12 }; 13 struct Dinic { 14 int n, m, s, t; 15 vector<node>nodes; 16 vector<int>g[maxn]; 17 int vis[maxn]; 18 int d[maxn]; 19 int cur[maxn]; 20 void clearall(int n) { 21 for (int i = 0 ; i < n ; i++) g[i].clear(); 22 nodes.clear(); 23 } 24 void clearflow() { 25 int len = nodes.size(); 26 for (int i = 0 ; i < len ; i++) nodes[i].flow = 0; 27 } 28 void add(int from, int to, int cap) { 29 nodes.push_back((node) { 30 from, to, cap, 0 31 }); 32 nodes.push_back((node) { 33 to, from, cap, 0 34 }); 35 m = nodes.size(); 36 g[from].push_back(m - 2); 37 g[to].push_back(m - 1); 38 } 39 bool bfs() { 40 memset(vis, 0, sizeof(vis)); 41 queue<int>q; 42 q.push(s); 43 d[s] = 0; 44 vis[s] = 1; 45 while(!q.empty()) { 46 int x = q.front(); 47 q.pop(); 48 int len = g[x].size(); 49 for (int i = 0 ; i < len ; i++) { 50 node &e = nodes[g[x][i]]; 51 if (!vis[e.to] && e.cap > e.flow ) { 52 vis[e.to] = 1; 53 d[e.to] = d[x] + 1; 54 q.push(e.to); 55 } 56 } 57 } 58 return vis[t]; 59 } 60 int dfs(int x, int a) { 61 if (x == t || a == 0) return a; 62 int flow = 0, f, len = g[x].size(); 63 for (int &i = cur[x] ; i < len ; i++) { 64 node & e = nodes[g[x][i]]; 65 if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0 ) { 66 e.flow += f; 67 nodes[g[x][i] ^ 1].flow -= f; 68 flow += f; 69 a -= f; 70 if (a == 0) break; 71 } 72 } 73 return flow; 74 } 75 int maxflow(int a, int b) { 76 s = a; 77 t = b; 78 int flow = 0; 79 while(bfs()) { 80 memset(cur, 0, sizeof(cur)); 81 flow += dfs(s, INF); 82 } 83 return flow; 84 } 85 vector<int>mincut() { 86 vector<int>ans; 87 int len = nodes.size(); 88 for (int i = 0 ; i < len ; i++) { 89 node & e = nodes[i]; 90 if ( vis[e.from] && !vis[e.to] && e.cap > 0 ) ans.push_back(i); 91 } 92 return ans; 93 } 94 void reduce() { 95 int len = nodes.size(); 96 for (int i = 0 ; i < len ; i++) nodes[i].cap -= nodes[i].flow; 97 } 98 } f; 99 int main() { 100 int n, m, t; 101 scanf("%d", &t); 102 while(t--) { 103 scanf("%d%d", &n, &m); 104 f.clearall(n); 105 f.clearflow(); 106 int left = INF, right = -INF, s = 1, t = 1; 107 for (int i = 1 ; i <= n ; i++) { 108 int x, y; 109 scanf("%d%d", &x, &y); 110 if (x < left) { 111 left = x; 112 s = i; 113 } 114 if (x > right) { 115 right = x; 116 t = i; 117 } 118 } 119 for (int i = 0 ; i < m ; i++) { 120 int u, v, c; 121 scanf("%d%d%d", &u, &v, &c); 122 f.add(u, v, c); 123 } 124 printf("%d\n", f.maxflow(s, t)); 125 } 126 return 0; 127 }
(hdu-4280)Island Transport~测试网络流模板速度~要加挂才能过啊
标签:void one imu return cert tom otto ber rip
原文地址:https://www.cnblogs.com/qldabiaoge/p/8878123.html