标签:inpu http pos turn connect nal i++ str capacity
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 9151 Accepted Submission(s): 2958
1 //2017-08-24 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #pragma comment(linker, "/STACK:1024000000,1024000000") 9 10 using namespace std; 11 12 const int N = 100010; 13 const int INF = 0x3f3f3f3f; 14 int head[N], tot; 15 struct Edge{ 16 int next, to, w; 17 }edge[N<<4]; 18 19 void add_edge(int u, int v, int w){ 20 edge[tot].w = w; 21 edge[tot].to = v; 22 edge[tot].next = head[u]; 23 head[u] = tot++; 24 } 25 26 struct Dinic{ 27 int level[N], S, T; 28 void init(int _S, int _T){ 29 S = _S; 30 T = _T; 31 tot = 0; 32 memset(head, -1, sizeof(head)); 33 } 34 bool bfs(){ 35 queue<int> que; 36 memset(level, -1, sizeof(level)); 37 level[S] = 0; 38 que.push(S); 39 while(!que.empty()){ 40 int u = que.front(); 41 que.pop(); 42 for(int i = head[u]; i != -1; i = edge[i].next){ 43 int v = edge[i].to; 44 int w = edge[i].w; 45 if(level[v] == -1 && w > 0){ 46 level[v] = level[u]+1; 47 que.push(v); 48 } 49 } 50 } 51 return level[T] != -1; 52 } 53 int dfs(int u, int flow){ 54 if(u == T)return flow; 55 int ans = 0, fw; 56 for(int i = head[u]; i != -1; i = edge[i].next){ 57 int v = edge[i].to, w = edge[i].w; 58 if(!w || level[v] != level[u]+1) 59 continue; 60 fw = dfs(v, min(flow-ans, w)); 61 ans += fw; 62 edge[i].w -= fw; 63 edge[i^1].w += fw; 64 if(ans == flow)return ans; 65 } 66 if(ans == 0)level[u] = -2; 67 return ans; 68 } 69 int maxflow(){ 70 int flow = 0, tmp; 71 while(bfs()) 72 while((tmp = dfs(S, INF)) > 0) 73 flow += tmp; 74 return flow; 75 } 76 }dinic; 77 78 int main() 79 { 80 //std::ios::sync_with_stdio(false); 81 //freopen("inputG.txt", "r", stdin); 82 int T, n, m; 83 scanf("%d", &T); 84 while(T--){ 85 scanf("%d%d", &n, &m); 86 int x, y, s = 1, t = 1, mininum = INF, maxinum = -INF; 87 for(int i = 1; i <= n; i++){ 88 scanf("%d%d", &x, &y); 89 if(x < mininum){ 90 mininum = x; 91 s = i; 92 } 93 if(x > maxinum){ 94 maxinum = x; 95 t = i; 96 } 97 } 98 dinic.init(s, t); 99 int u, v, w; 100 for(int i = 0; i < m; i++){ 101 scanf("%d%d%d", &u, &v, &w); 102 add_edge(u, v, w); 103 add_edge(v, u, w); 104 } 105 printf("%d\n", dinic.maxflow()); 106 } 107 return 0; 108 }
标签:inpu http pos turn connect nal i++ str capacity
原文地址:http://www.cnblogs.com/Penn000/p/7422303.html