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

ZOJ 3792 Romantic Value 最小割+求割边的数量

时间:2015-03-21 14:04:52      阅读:140      评论:0      收藏: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

Author: ZHAO, Liqiang
Source: ZOJ Monthly, June 2014

一个无向图,给你源点和汇点,让你求在最小割和割边。
最小割=最大流,求割边的话可以根据求出的最大流即为割边的流量,所以每条边在一开始就*1000+1,那么最后的流量/1000就是最大流,%1000就是割边。
#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;
}



ZOJ 3792 Romantic Value 最小割+求割边的数量

标签:

原文地址:http://blog.csdn.net/crescent__moon/article/details/44515433

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