Farmer John is a diligent man. He spent a lot of time building roads between his farms. From his point of view, every road is romantic because the scenery along it is very harmonious and beautiful. Recently, John is immersed in poetry, he wants stay alone and enjoy the wonderful words. But his little brother always disturbs him. This night, fortunately, his little brother does not stay the same farm with him. So, he wants to destroy some roads to keep himself quiet for a few days(then no route exist between John and his brother). Of course, John love his romantic roads, so he want to separate him and his brother with least romantic cost.
There are N farms(numbered from 1 to N) and M undirected roads each with a romantic value c(indicate how much Farmer John loves it). Now John stays in farm p and his little brother stay in farm q. John wants to first minimize the romantic value lost, then to destroy as few roads as possible. Help him to calculate the ratio of [sum of the remainder roads' value]/[the amount of removed roads](not necessary to maximisation this ratio) when he achieves his goal.
The first line is a single integer T, indicate the number of testcases. Then follows T testcase. For each testcase, the first line containsfour integers N M p q(you can assume p and q are unequal), then following M lines each contains three integer a b c which means there is an undirected road between farm a andfarm b with romantic value c. (2<=N<=50, 0<=M<=1000, 1<=c<1000, 1<=p,q<=N)
For each test case, print the ratio in a single line(keep two decimal places). If p and q exist no route at the start, then output "Inf".
1 4 5 1 4 1 2 1 1 3 1 2 4 2 3 4 2 2 3 1
2.50
Source: ZOJ Monthly, June 2014
题目链接:ZOJ 3792 Romantic Value
题目大意:给你一个无向图,求最小割,再求最小割中边最少的最小割。
题目分析:
跑一次最大流,如果不存在s到t的流,输出Inf。
否则,将所有的关键边容量设为1,其余边容量设为oo,再跑一次最大流,这次的流量即最小割中的最少边。
#include <stdio.h> #include <string.h> #include <algorithm> #define clear(A, X) memset(A, X, sizeof A) #define copy(A, B) memcpy(A, B, sizeof A) const int maxE = 1000000; const int maxN = 100; const int oo = 0x3f3f3f3f; struct Edge{ int v, c, n; }; Edge edge[maxE]; int adj[maxN], cntE; int d[maxN], num[maxN], cur[maxN], pre[maxN]; int Q[maxE], head, tail; int s, t, nv; int n, m; void addedge(int u, int v, int c){ edge[cntE].v = v; edge[cntE].c = c; edge[cntE].n = adj[u]; adj[u] = cntE++; edge[cntE].v = u; edge[cntE].c = c; edge[cntE].n = adj[v]; adj[v] = cntE++; } void REV_BFS(){ clear(num, 0); clear(d, -1); d[t] = 0; num[0] = 1; head = tail = 0; Q[tail++] = t; while(head != tail){ int u = Q[head++]; for(int i = adj[u]; ~i; i = edge[i].n){ int v = edge[i].v; if(~d[v]) continue; d[v] = d[u] + 1; num[d[v]]++; Q[tail++] = d[v]; } } } int ISAP(){ copy(cur, adj); REV_BFS(); int flow = 0, u = pre[s] = s, i; while(d[s] < nv){ if(u == t){ int f = oo, neck; for(i = s; i != t; i = edge[cur[i]].v){ if(f > edge[cur[i]].c){ f = edge[cur[i]].c; neck = i; } } for(i = s; i != t; i = edge[cur[i]].v){ edge[cur[i]].c -= f; edge[cur[i] ^ 1].c += f; } flow += f; u = neck; } for(i = cur[u]; ~i; i = edge[i].n) if(edge[i].c && d[u] == d[edge[i].v] + 1) break; if(~i){ cur[u] = i; pre[edge[i].v] = u; u = edge[i].v; } else{ if(0 == (--num[d[u]])) break; int mind = nv; for(i = adj[u]; ~i; i = edge[i].n){ if(edge[i].c && mind > d[edge[i].v]){ cur[u] = i; mind = d[edge[i].v]; } } d[u] = mind + 1; num[d[u]]++; u = pre[u]; } } return flow; } void work(){ int u, v, c, sum = 0; clear(adj, -1); cntE = 0; scanf("%d%d%d%d", &n, &m, &s, &t); nv = n + 1; for(int i = 0; i < m; ++i){ scanf("%d%d%d", &u, &v, &c); addedge(u, v, c); sum += c; } int flow = ISAP(); if(!flow){ printf("Inf\n"); return; } sum -= flow; for(int i = 0; i < cntE; i += 2){ if(edge[i].c == 0){ edge[i].c = 1; edge[i ^ 1].c = oo; } else if(edge[i ^ 1].c == 0){ edge[i].c = oo; edge[i ^ 1].c = 1; } else{ edge[i].c = edge[i ^ 1].c = oo; } } int d = ISAP(); printf("%.2f\n", 1.0 * sum / d); } int main(){ int t; scanf("%d", &t); while(t--) work(); return 0; }