题意:现在有n个城市,m条路,现在要把整个图分成2部分,编号1,2的城市分成在一部分中,拆开每条路都需要花费,现在问达成目标的花费最少要隔开那几条路。
题解:建图直接按给你的图建一下,然后呢跑一下最大流,我们就知道了最小割是多少,答案就是最小割了 。
现在要求输出割法。我们从s开始往前跑,如果某条正向边有流量,我们就按着这条边继续往外走,知道无法再走,把所有经历过的点都染一下色。最后看所有的边,是不是有一头是染色了,另一头没有染色,如果是,这条边就是割边,输出就好了。
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); 4 #define LL long long 5 #define ULL unsigned LL 6 #define fi first 7 #define se second 8 #define pb emplace_back 9 #define lson l,m,rt<<1 10 #define rson m+1,r,rt<<1|1 11 #define lch(x) tr[x].son[0] 12 #define rch(x) tr[x].son[1] 13 #define max3(a,b,c) max(a,max(b,c)) 14 #define min3(a,b,c) min(a,min(b,c)) 15 typedef pair<int,int> pll; 16 const int inf = 0x3f3f3f3f; 17 const LL INF = 0x3f3f3f3f3f3f3f3f; 18 const LL mod = (int)1e9+7; 19 const int N = 200; 20 const int M = N*N; 21 int head[N], deep[N], cur[N]; 22 int w[M], to[M], nx[M]; 23 int tot; 24 void add(int u, int v, int val){ 25 w[tot] = val; to[tot] = v; 26 nx[tot] = head[u]; head[u] = tot++; 27 28 w[tot] = 0; to[tot] = u; 29 nx[tot] = head[v]; head[v] = tot++; 30 } 31 int bfs(int s, int t){ 32 queue<int> q; 33 memset(deep, 0, sizeof(deep)); 34 q.push(s); 35 deep[s] = 1; 36 while(!q.empty()){ 37 int u = q.front(); 38 q.pop(); 39 for(int i = head[u]; ~i; i = nx[i]){ 40 if(w[i] > 0 && deep[to[i]] == 0){ 41 deep[to[i]] = deep[u] + 1; 42 q.push(to[i]); 43 } 44 } 45 } 46 return deep[t] > 0; 47 } 48 int Dfs(int u, int t, int flow){ 49 if(u == t) return flow; 50 for(int &i = cur[u]; ~i; i = nx[i]){ 51 if(deep[u]+1 == deep[to[i]] && w[i] > 0){ 52 int di = Dfs(to[i], t, min(w[i], flow)); 53 if(di > 0){ 54 w[i] -= di, w[i^1] += di; 55 return di; 56 } 57 } 58 } 59 return 0; 60 } 61 62 int n, m; 63 int Dinic(int s, int t){ 64 int ans = 0, tmp; 65 while(bfs(s, t)){ 66 for(int i = 1; i <= n; i++) cur[i] = head[i]; 67 while(tmp = Dfs(s, t, inf)) ans += tmp; 68 } 69 return ans; 70 } 71 int vis[N]; 72 int top; 73 int ans[M][2]; 74 void init(){ 75 memset(head, -1, sizeof(head)); 76 memset(vis, 0, sizeof(vis)); 77 top = tot = 0; 78 } 79 void solve(int u){ 80 vis[u] = 1; 81 for(int i = head[u]; ~i; i = nx[i]){ 82 if(vis[to[i]]) continue; 83 if(w[i]){ 84 solve(to[i]); 85 } 86 } 87 } 88 int main(){ 89 while(~scanf("%d%d", &n, &m) && n + m){ 90 int u, v, w; 91 init(); 92 for(int i = 1; i <= m; i++){ 93 scanf("%d%d%d", &u, &v, &w); 94 add(u, v, w); 95 add(v, u, w); 96 } 97 Dinic(1, 2); 98 solve(1); 99 for(int i = 1; i <= n; i++){ 100 for(int j = head[i]; ~j; j = nx[j]){ 101 if(!(j&1)){ 102 v = to[j]; 103 if(vis[i] != vis[v] && i < v) 104 printf("%d %d\n", i, v); 105 } 106 } 107 } 108 puts(""); 109 } 110 return 0; 111 }