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

Source: Internet
Author: User

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

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 <iostream>#include <cstdio>#include <cstring>#include <queue>#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<int >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<cnt; i+=2) {            if(edge[i].w==0)                edge[i].w=1;            else edge[i].w=inf;            edge[i^1].w=0;        }        int nnt=0;        while(bfs())            while(a=dfs(s,inf))                nnt+=a;        printf("%.2lf\n",(double )(sum-ans)/nnt);    }    return 0;}

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


What is the difference between the minimum cut in a network flow and the minimum cut in an undirected graph?

The network flow is a directed graph. For s in the directed graph, the t two points have the minimum cut of s-t, and the minimum cut of the directed graph is equal to the maximum flow of the network flow.

I don't know what the concept of undirected graph least cut is. We have the s-t least cut corresponding to the s and t points, which are done according to the directed graph,
If global minimum cut is enabled, the whole graph is cut by edge into a scheme with the smallest edge weight and the two parts, which is done by the SW algorithm,
If a cut point exists, the graph is not connected after the vertex is deleted, which is done by the tarjan algorithm,
If there is connectivity, it means that at least the number of vertices to be deleted is not connected. This is done by network flow.

These terms are currently not strictly used, so they are not conclusive and should be explained in general.

What is the minimum cut in a network stream?

First, explain the cutover set.
In a permission diagram, the Source Vertex is Vs, and the sink vertex is Vt. There are many paths to go from Vs to Vt. Each path contains several edges. These edges may belong to only one path or appear in both paths. If you remove some edges from this graph, you cannot reach Vt from Vs. The combination of these edges is called a cut set.

Minimum Cut explanation:
There are many cutover sets. The sum of the weights of each element in a cutover set is the capacity of the cutover set. Among all the cutover sets, the smallest cutover set is called the smallest cut.

^

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.