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
Author: ZHAO, Liqiang
Source: ZOJ Monthly, June 2014
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXN 57 //点
#define MAXM 4007//边
#define inf 0x3f3f3f3f
using namespace std;
struct E
{
int from,v,next;
int cap;
} edge[MAXM];
int num;
int head[MAXN],dis[MAXN],gap[MAXN];
int nn,n,m;//代表点的个数
void init()
{
num=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
edge[num].from=u;
edge[num].v=v;
edge[num].cap=w;
edge[num].next=head[u];
head[u]=num++;
edge[num].from=v;
edge[num].v=u;
edge[num].cap=0;
edge[num].next=head[v];
head[v]=num++;
}
void BFS(int start,int end)
{
memset(dis,-1,sizeof(dis));
memset(gap,0,sizeof(gap));
gap[0]=1;
int que[MAXN],front,rear;
front=rear=0;
dis[end]=0;
que[rear++]=end;
while(front!=rear)
{
int u=que[front++];
if(front==MAXN)front=0;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(dis[v]!=-1)continue;
que[rear++]=v;
if(rear==MAXN)rear=0;
dis[v]=dis[u]+1;
++gap[dis[v]];
}
}
}
int SAP(int start,int end)
{
int res=0;
nn=n;
BFS(start,end);
int cur[MAXN],S[MAXN];
memcpy(cur,head,sizeof(head));
int u=start,i,vp=0;
while(dis[start]<nn)
{
if(u==end)
{
int temp=inf;
int inser;
for(i=0; i<vp; i++)
if(temp>edge[S[i]].cap)
{
temp=edge[S[i]].cap;
inser=i;
}
for(i=0; i<vp; i++)
{
edge[S[i]].cap-=temp;
edge[S[i]^1].cap+=temp;
}
res+=temp;
vp=inser;
u=edge[S[vp]].from;
}
if(u!=end&&gap[dis[u]-1]==0)
break;
for(i=cur[u]; i!=-1; i=edge[i].next)
if(edge[i].cap!=0&&dis[u]==dis[edge[i].v]+1)
break;
if(i!=-1)
{
cur[u]=i;
S[vp++]=i;
u=edge[i].v;
}
else
{
int min=nn;
for(i=head[u]; i!=-1; i=edge[i].next)
{
if(edge[i].cap==0)continue;
if(min>dis[edge[i].v])
{
min=dis[edge[i].v];
cur[u]=i;
}
}
--gap[dis[u]];
dis[u]=min+1;
++gap[dis[u]];
if(u!=start)u=edge[S[--vp]].from;
}
}
return res;
}
int main()
{
int s,t,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&n,&m,&s,&t);
init();
int u,v,c,sum=0;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&c);
addedge(u,v,c*1000+1);
addedge(v,u,c*1000+1);
sum+=c;
}
int ans=SAP(s,t);
if(!ans){printf("Inf\n");continue;}
double a,b;
if(ans%1000==0){b=1000;ans-=1000;}
else b=ans%1000;
a=sum-ans/1000;
printf("%.2lf\n",1.0*a/b);
}
return 0;
}