求最小割的值, 以及割边最少的情况的边数。
先求一遍最小割, 然后把所有割边的权值变为1, 其他边变成inf, 在求一遍最小割, 此时求出的就是最少边数。
Inf打成inf WA了好几发............
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 #define ll long long 5 #define mk(x, y) make_pair(x, y) 6 #define lson l, m, rt<<1 7 #define mem(a) memset(a, 0, sizeof(a)) 8 #define rson m+1, r, rt<<1|1 9 #define mem1(a) memset(a, -1, sizeof(a)) 10 #define mem2(a) memset(a, 0x3f, sizeof(a)) 11 #define rep(i, a, n) for(int i = a; i<n; i++) 12 #define ull unsigned long long 13 typedef pair<int, int> pll; 14 const double PI = acos(-1.0); 15 const double eps = 1e-8; 16 const int mod = 1e9+7; 17 const int inf = 1e4; 18 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; 19 const int maxn = 2e5+5; 20 int num, q[maxn*5], head[maxn*2], dis[maxn*2], s, t; 21 struct node 22 { 23 int to, nextt, c; 24 node(){} 25 node(int to, int nextt, int c):to(to), nextt(nextt), c(c){} 26 }e[maxn*2]; 27 int bfs() { 28 mem(dis); 29 int st = 0, ed = 0; 30 dis[s] = 1; 31 q[ed++] = s; 32 while(st<ed) { 33 int u = q[st++]; 34 for(int i = head[u]; ~i; i = e[i].nextt) { 35 int v = e[i].to; 36 if(!dis[v]&&e[i].c) { 37 dis[v] = dis[u]+1; 38 if(v == t) 39 return 1; 40 q[ed++] = v; 41 } 42 } 43 } 44 return 0; 45 } 46 int dfs(int u, int limit) { 47 int cost = 0; 48 if(u == t) 49 return limit; 50 for(int i = head[u]; ~i; i = e[i].nextt) { 51 int v = e[i].to; 52 if(e[i].c&&dis[v] == dis[u]+1) { 53 int tmp = dfs(v, min(e[i].c, limit-cost)); 54 if(tmp>0) { 55 e[i].c -= tmp; 56 e[i^1].c += tmp; 57 cost += tmp; 58 if(cost == limit) 59 break; 60 } else { 61 dis[v] = -1; 62 } 63 } 64 } 65 return cost; 66 } 67 int dinic() { 68 int ans = 0; 69 while(bfs()) { 70 ans += dfs(s, inf); 71 } 72 return ans; 73 } 74 void add(int u, int v, int c) { 75 e[num] = node(v, head[u], c); head[u] = num++; 76 e[num] = node(u, head[v], c); head[v] = num++; 77 } 78 void init() { 79 mem1(head); 80 num = 0; 81 } 82 int main() 83 { 84 int T, m, n, x, y, q, p, z; 85 cin>>T; 86 while(T--) { 87 init(); 88 int sum = 0; 89 scanf("%d%d%d%d", &n, &m, &p, &q); 90 s = p, t = q; 91 while(m--) { 92 scanf("%d%d%d", &x, &y, &z); 93 add(x, y, z); 94 sum += z; 95 } 96 int ans = dinic(); 97 if(ans == 0) { 98 cout<<"Inf"<<endl; 99 continue; 100 } 101 sum -= ans; 102 for(int i = 0; i<num; i+=2) { 103 if(e[i].c == 0) { 104 e[i].c = 1; 105 e[i^1].c = inf; 106 } else if(e[i^1].c==0) { 107 e[i].c = inf; 108 e[i^1].c = 1; 109 } else { 110 e[i].c = e[i^1].c = inf; 111 } 112 } 113 ans = dinic(); 114 printf("%.2f ", 1.0*sum/ans); 115 } 116 }