标签:script names his mon case direct define evel front
1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 #include <vector> 5 #include <queue> 6 #define INF 0x3f3f3f3f 7 using namespace std; 8 const int M = 210010; 9 struct Nod { 10 int to, cap, next, index; 11 }edge[M]; 12 int T, n, m, f, s, t; 13 int head[M], level[M], vis[M], iter[M], sum, num; 14 void init() { 15 memset(head, -1, sizeof(head)); 16 memset(vis, 0, sizeof(vis)); 17 num = 0; 18 sum = 0; 19 } 20 void add_edge(int u, int v, int cap, int index) { 21 edge[num].to = v; edge[num].cap = cap; edge[num].next = head[u]; edge[num].index = index; head[u] = num++; 22 edge[num].to = u; edge[num].cap = 0; edge[num].next = head[v]; edge[num].index = -1; head[v] = num++; 23 } 24 bool bfs(int s, int t) { 25 memset(level, -1, sizeof(level)); 26 level[s] = 0; 27 queue<int> que; 28 que.push(s); 29 while(!que.empty()) { 30 int v = que.front(); 31 que.pop(); 32 for(int i = head[v]; i != -1; i = edge[i].next) { 33 int u = edge[i].to; 34 if(level[u] < 0 && edge[i].cap) { 35 level[u] = level[v] + 1; 36 que.push(u); 37 } 38 } 39 } 40 return level[t] != -1; 41 } 42 int dfs(int v, int t, int f) { 43 if(v == t) return f; 44 for(int &i = iter[v]; i != -1; i = edge[i].next) { 45 int u = edge[i].to; 46 if(level[u] > level[v] && edge[i].cap > 0) { 47 int d = dfs(u, t, min(f, edge[i].cap)); 48 if(d > 0) { 49 edge[i].cap -= d; 50 edge[i^1].cap += d; 51 return d; 52 } 53 } 54 } 55 level[v] = -1; 56 return 0; 57 } 58 int max_flow(int s, int t) { 59 int flow = 0; 60 while(bfs(s, t)) { 61 for(int i = 0; i <= t + 10; i ++) iter[i] = head[i]; 62 int f = 0; 63 while((f = dfs(s, t, INF)) > 0) flow += f; 64 } 65 return flow; 66 } 67 void dfs(int u, int fa) { 68 for(int i = head[u]; i != -1; i = edge[i].next) { 69 int v = edge[i].to; 70 if(!vis[v] && edge[i].cap) { 71 vis[v] = true; 72 dfs(v, u); 73 } 74 } 75 } 76 int main() { 77 ios::sync_with_stdio(false); 78 // cin >> T; 79 scanf("%d", &T); 80 int cas = 1; 81 while(T--) { 82 // cin >> n >> m >> f; 83 scanf("%d %d %d", &n, &m, &f); 84 init(); 85 add_edge(s, 1, INF, -1); 86 s = 0, t = n + 1; 87 for(int i = 1; i <= m; i ++) { 88 int u, v, w; 89 scanf("%d %d %d", &u, &v, &w); 90 add_edge(u, v, w, i); 91 } 92 for(int i = 1; i <= f; i ++) { 93 int u, w; 94 scanf("%d %d", &u, &w); 95 add_edge(u, t, w, -1); 96 sum += w; 97 } 98 int ans = max_flow(s, t); 99 ans = sum - ans; 100 printf("Case %d: %d\n",cas++, ans); 101 vis[1] = true; 102 dfs(1, -1); 103 queue<int> que; 104 for(int i = 0; i < num; i += 2) {//0正1负 105 if(vis[edge[i^1].to] && !vis[edge[i].to] && edge[i].index != -1) que.push(edge[i].index); 106 } 107 printf("%d",(int)que.size()); 108 while(!que.empty()) { 109 printf(" %d",que.front()); 110 que.pop(); 111 } 112 printf("\n"); 113 } 114 return 0; 115 }
标签:script names his mon case direct define evel front
原文地址:http://www.cnblogs.com/xingkongyihao/p/7306127.html