Click to open link
Romantic Value Time limit: 2 Seconds Memory Limit: 65536 KB
farmer John was a diligent man. He spent a lot of time building roads between his farms. From its point of view, every road was 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 he little brother always disturbs him. This night, fortunately, he 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 him romantic roads, so he want to separate him and his brother with least romantic cost.
There is 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 he 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] (notnecessary to Maximisation this ratio) when he achieves his goal.
Input
The first line was 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 is unequal), then following M Lin Es each contains three integer a b c which means there are 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, places). If p and Q exist no route at the start, then output "INF".
Sample Input
14 5 1 41 2 11 3 12 4 23 4 22 3 1
Sample Output
2.50
Author:ZHAO, Liqiang
Source:ZOJ Monthly, June
an no-map, gives you the source and sink points, allowing you to ask for the smallest cut and cut edges.
min cut = maximum flow, For cutting edge words can be calculated according to the maximum flow is cut edge flow, so each side in the beginning of *1000+1, then the last flow/1000 is the maximum flow,%1000 is cut edge.
#include <stdio.h> #include <string.h> #include <algorithm> #define MAXN 57//Point # MAXM 4007//side # Define INF 0x3f3f3f3fusing 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;//represents the number of points of 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;}
ZOJ 3792 Romantic Value min cut + seek cut edge number