ZOJ 3792 Romantic Value

Romantic Value

Time Limit: 2000ms
Memory Limit: 65536KB
This problem will be judged on ZJU. Original ID: 3792
64-bit integer IO format: %lld      Java class name: Main
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.

 

Input

The first line is a single integer T, indicate the number of testcases. Then follows T testcase. For each testcase, the first line contains four 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 and farm b with romantic value c. (2<=N<=50, 0<=M<=1000, 1<=c<1000, 1<=p,q<=N)

 

Output

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".

 

Sample Input

1
4 5 1 4
1 2 1
1 3 1
2 4 2
3 4 2
2 3 1

Sample Output

2.50
 
 

Source

 

Author

ZHAO, Liqiang
 
解题:比较巧的构图方式!求去掉一些边,使给出的两个点不连通,切剩下的边和除以去掉的边数 最大
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int INF = ~0U>>2;
 4 const int maxn = 100;
 5 int head[maxn],gap[maxn],d[maxn],tot,S,T,n,m;
 6 struct arc{
 7     int to,flow,next;
 8     arc(int x = 0,int y = 0,int z = -1){
 9         to = x;
10         flow = y;
11         next = z;
12     }
13 }e[maxn*maxn];
14 void add(int u,int v,int w){
15     e[tot] = arc(v,w,head[u]);
16     head[u] = tot++;
17     e[tot] = arc(u,0,head[v]);
18     head[v] = tot++;
19 }
20 int dfs(int u,int low){
21     if(u == T) return low;
22     int tmp = 0,minH = n - 1;
23     for(int i = head[u]; ~i; i = e[i].next){
24         if(e[i].flow){
25             if(d[e[i].to] + 1 == d[u]){
26                 int a = dfs(e[i].to,min(low,e[i].flow));
27                 e[i].flow -= a;
28                 e[i^1].flow += a;
29                 tmp += a;
30                 low -= a;
31                 if(!low) break;
32                 if(d[S] >= n) return tmp;
33             }
34             if(e[i].flow) minH = min(minH,d[e[i].to]);
35         }
36     }
37     if(!tmp){
38         if(--gap[d[u]] == 0) d[S] = n;
39         ++gap[d[u] = minH + 1];
40     }
41     return tmp;
42 }
43 int sap(int ret = 0){
44     memset(d,0,sizeof d);
45     memset(gap,0,sizeof gap);
46     gap[S] = n;
47     while(d[S] < n) ret += dfs(S,INF);
48     return ret;
49 }
50 int main(){
51     int kase,ret,sum,u,v,w;
52     scanf("%d",&kase);
53     while(kase--){
54         scanf("%d%d%d%d",&n,&m,&S,&T);
55         memset(head,-1,sizeof head);
56         for(int i = tot = sum = 0; i < m; ++i){
57             scanf("%d%d%d",&u,&v,&w);
58             add(u,v,w*1234 + 1);
59             add(v,u,w*1234 + 1);
60             sum += w;
61         }
62         int ret = sap();
63         if(ret == 0) puts("Inf");
64         else printf("%.2f\n",double(sum - ret/1234)/(ret%1234));
65     }
66     return 0;
67 }
View Code

 

posted @ 2015-11-14 13:32  狂徒归来  阅读(170)  评论(0编辑  收藏  举报