ZOJ 3792 Romantic Value(最小割问题)

来源:互联网 发布:mac电脑上的游戏 编辑:程序博客网 时间:2024/05/17 06:15

题目的意思很扯,但是题意很好理解就是让你求出那个很神奇的公式,需要求出给出的p,q点之间的最小割,还有就是割边的数目。

在统计割边的数目的时候,把权值乘了一个大数这样的话取余就是割边的数目了啊。

需要用dinic算法求出最大流。

基本上就是模版题啊。

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
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-8#define M 1000100//#define LL __int64#define LL long long#define INF 0x7ffffff#define PI 3.1415926535898const int maxn = 10010;using namespace std;int pre[maxn];int head[maxn];int cnt;int n, m;int deep[maxn];struct node{    int v, next;    int w;};node f[maxn];void init(){    cnt = 0;    memset(head, -1, sizeof(head));}void add(int u, int v, int w){    f[cnt].v = v;    f[cnt].w = w;    f[cnt].next = head[u];    head[u] = cnt++;    f[cnt].v = u;    f[cnt].w = 0;    f[cnt].next = head[v];    head[v] = cnt++;}bool bfs(int s, int t){    memset(deep, -1, sizeof(deep));    queue<int>q;    while(!q.empty())        q.pop();    q.push(s);    deep[s] = 0;    while(!q.empty())    {        int temp = q.front();        q.pop();        int p = head[temp];        while(p != -1)        {            int v = f[p].v;            if(deep[v] == -1 && f[p].w > 0)            {                deep[v] = deep[temp]+1;                q.push(v);            }            p = f[p].next;        }    }    return deep[t] != -1;}int dfs(int s, int a, int t){    if(s == t || a == 0)        return a;    int flow = 0, ff;    for(int kk = head[s]; kk != -1; kk = f[kk].next)    {        node e;        e.next = f[kk].next;        e.v = f[kk].v;        e.w = f[kk].w;        if(deep[e.v] == deep[s]+1 && (ff = dfs(e.v, min(a, e.w), t)) > 0)        {            f[kk].w -= ff;            f[kk^1].w += ff;            flow += ff;            a -= ff;            if(a == 0)                return flow;        }    }    deep[s] = -1;    return flow;}int main(){    int T;    cin >>T;    while(T--)    {        int s, t;        init();        cin >>n>>m>>s>>t;        int u, v, w;        int sum = 0;        for(int i = 1; i <= m; i++)        {            cin >>u>>v>>w;            sum += w;            add(u, v, w*10000+1);            add(v, u, w*10000+1);        }        int flow = 0;        while(bfs(s,t))            flow += dfs(s, INF, t);        if(flow == 0)        {            cout<<"Inf"<<endl;            continue;        }        int ans = flow/10000;        int ans2 = flow%10000;        double res = (double)(sum-ans)/ans2;        printf("%.2lf\n",res);    }}


0 0
原创粉丝点击