码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ3792_Romantic Value(网络流/最小割=最大流/找割边)

时间:2014-08-18 20:34:12      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   strong   

解题报告

题目传送门

题意:

给出一个无向图,以及起点与终点。要删除一些边使得起点与终点不连通,在删掉边的权值之和最小的情况下要求删除的边数尽量少。

求出一个比值:剩余边数权值和/删除的边数。

思路:

明显的让起点终点达不到就是一个最小割,用最大流可以求出。

但是求割边边数就不会了,没做过最小割的求割边问题。

割边一定是残留网络中零流的边,但零流不一定是割边。

飞神的想法很奇特。链接传送

可以把残留网络的零流的边设成容量为1,其他设成无穷,再求一次最大流。最后流量一定等于割边边数

另外:

还有一种求割边的办法。

因为每次我们求出来最大流都是割边的流量,那么,我们可以把原先边的容量×10000+1,那么求出来的最大流/10000不会影响原先的答案,而最大流%10000则是割边的数目。orz,,,,,,

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 0x3f3f3f3f
using 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

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

Sample Output

2.50

ZOJ3792_Romantic Value(网络流/最小割=最大流/找割边),布布扣,bubuko.com

ZOJ3792_Romantic Value(网络流/最小割=最大流/找割边)

标签:des   style   blog   http   color   os   io   strong   

原文地址:http://blog.csdn.net/juncoder/article/details/38664691

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!