ZOJ 3792 Romantic Value 最小割

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 containsfour 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 andfarm 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

题目链接:ZOJ 3792  Romantic Value

题目大意:给你一个无向图,求最小割,再求最小割中边最少的最小割。

题目分析:

跑一次最大流,如果不存在s到t的流,输出Inf。

否则,将所有的关键边容量设为1,其余边容量设为oo,再跑一次最大流,这次的流量即最小割中的最少边。


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define clear(A, X) memset(A, X, sizeof A)
#define copy(A, B) memcpy(A, B, sizeof A)
const int maxE = 1000000;
const int maxN = 100;
const int oo = 0x3f3f3f3f;
struct Edge{
    int v, c, n;
};
Edge edge[maxE];
int adj[maxN], cntE;
int d[maxN], num[maxN], cur[maxN], pre[maxN];
int Q[maxE], head, tail;
int s, t, nv;
int n, m;
void addedge(int u, int v, int c){
    edge[cntE].v = v; edge[cntE].c = c; edge[cntE].n = adj[u]; adj[u] = cntE++;
    edge[cntE].v = u; edge[cntE].c = c; edge[cntE].n = adj[v]; adj[v] = cntE++;
}
void REV_BFS(){
    clear(num, 0);
    clear(d, -1);
    d[t] = 0;
    num[0] = 1;
    head = tail = 0;
    Q[tail++] = t;
    while(head != tail){
        int u = Q[head++];
        for(int i = adj[u]; ~i; i = edge[i].n){
            int v = edge[i].v;
            if(~d[v]) continue;
            d[v] = d[u] + 1;
            num[d[v]]++;
            Q[tail++] = d[v];
        }
    }
}
int ISAP(){
    copy(cur, adj);
    REV_BFS();
    int flow = 0, u = pre[s] = s, i;
    while(d[s] < nv){
        if(u == t){
            int f = oo, neck;
            for(i = s; i != t; i = edge[cur[i]].v){
                if(f > edge[cur[i]].c){
                    f = edge[cur[i]].c;
                    neck = i;
                }
            }
            for(i = s; i != t; i = edge[cur[i]].v){
                edge[cur[i]].c -= f;
                edge[cur[i] ^ 1].c += f;
            }
            flow += f;
            u = neck;
        }
        for(i = cur[u]; ~i; i = edge[i].n) if(edge[i].c && d[u] == d[edge[i].v] + 1) break;
        if(~i){
            cur[u] = i;
            pre[edge[i].v] = u;
            u = edge[i].v;
        }
        else{
            if(0 == (--num[d[u]])) break;
            int mind = nv;
            for(i = adj[u]; ~i; i = edge[i].n){
                if(edge[i].c && mind > d[edge[i].v]){
                    cur[u] = i;
                    mind = d[edge[i].v];
                }
            }
            d[u] = mind + 1;
            num[d[u]]++;
            u = pre[u];
        }
    }
    return flow;
}
void work(){
    int u, v, c, sum = 0;
    clear(adj, -1);
    cntE = 0;
    scanf("%d%d%d%d", &n, &m, &s, &t);
    nv = n + 1;
    for(int i = 0; i < m; ++i){
        scanf("%d%d%d", &u, &v, &c);
        addedge(u, v, c);
        sum += c;
    }
    int flow = ISAP();
    if(!flow){
        printf("Inf\n");
        return;
    }
    sum -= flow;
    for(int i = 0; i < cntE; i += 2){
        if(edge[i].c == 0){
            edge[i].c = 1;
            edge[i ^ 1].c = oo;
        }
        else if(edge[i ^ 1].c == 0){
            edge[i].c = oo;
            edge[i ^ 1].c = 1;
        }
        else{
            edge[i].c = edge[i ^ 1].c = oo;
        }
    }
    int d = ISAP();
    printf("%.2f\n", 1.0 * sum / d);
}
int main(){
    int t;
    scanf("%d", &t);
    while(t--) work();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值