Zoj1__romantic Value (network stream/min cut = max stream/find cut edge)

Source: Internet
Author: User

Zoj1__romantic Value (network stream/min cut = max stream/find cut edge)

Solution report

Question Portal

Question:

Returns an undirected graph with the start and end points. To delete some edges so that the start point and the end point are not connected, the number of edges to be deleted should be as few as possible when the sum of the right values of the deleted edges is the smallest.

Obtain a ratio: the number of remaining edges and the number of deleted edges.

Ideas:

Obviously, a minimum cut is used to locate the starting point and ending point, which can be obtained using the maximum stream.

However, the number of cut edge does not exist, and the problem of minimum cut is not solved.

The cutting edge must be the edge of the residual network with zero flow, but the zero flow is not necessarily the cutting edge.

The idea of feishen is very strange. Link Transfer

You can set the zero-flow edge of the residual network to 1, and set the other to infinity, and then calculate the maximum stream. The traffic must be equal to the number of cut edges.

In addition:

There is also a way to cut edges.

Because every time we find that the maximum flow is the traffic of the edge Cutting, we can set the capacity of the original edge to X 10000 + 1, the obtained maximum flow/10000 does not affect the original answer, while the maximum flow % 10000 is the number of cut edges. Orz ,,,,,,

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #define inf 0x3f3f3f3fusing namespace std;struct node {    int v,w,next;} edge[500000];int head[5000],cnt,n,m,l[5000],s,t,_hash[5000];void add(int u,int v,int w) {    edge[cnt].v=v;    edge[cnt].w=w;    edge[cnt].next=head[u];    head[u]=cnt++;    edge[cnt].v=u;    edge[cnt].w=0;    edge[cnt].next=head[v];    head[v]=cnt++;}int bfs() {    queue
     
      Q;    Q.push(s);    memset(l,-1,sizeof(l));    l[s]=0;    while(!Q.empty()) {        int u=Q.front();        Q.pop();        for(int i=head[u]; i!=-1; i=edge[i].next) {            int v=edge[i].v;            if(l[v]==-1&&edge[i].w) {                l[v]=l[u]+1;                Q.push(v);            }        }    }    return l[t]>0;}int dfs(int x,int f) {    if(x==t)return f;    int a;    for(int i=head[x]; i!=-1; i=edge[i].next) {        int v=edge[i].v;        if(edge[i].w&&l[v]==l[x]+1&&(a=dfs(v,min(edge[i].w,f)))) {            edge[i].w-=a;            edge[i^1].w+=a;            return a;        }    }    l[x]=-1;    return 0;}int main() {    int i,j,u,v,w,k=1,T,q,p;    scanf("%d",&T);    while(T--) {        scanf("%d%d%d%d",&n,&m,&p,&q);        memset(head,-1,sizeof(head));        cnt=0;        s=p;        t=q;        int sum=0;        for(i=1; i<=m; i++) {            scanf("%d%d%d",&u,&v,&w);            add(u,v,w);            add(v,u,w);            sum+=w;        }        if(!bfs()) {            printf("Inf\n");            continue;        }        int ans=0,a;        while(bfs())            while(a=dfs(s,inf))                ans+=a;        for(i=0; i
      
       

Romantic Value Time Limit: 2 Seconds Memory Limit: 65536 KB

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
14 5 1 41 2 11 3 12 4 23 4 22 3 1
Sample Output
2.50

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.