ZOJ 3792 Romantic Value 最小割

版权声明:本文为博主原创文章,转载请说明出处。 https://blog.csdn.net/xianpingping/article/details/82946209

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

题意:

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

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

思路:

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

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

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

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

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

就是:

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

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

另外:

还有一种求割边的办法。

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

代码:

#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;
}

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/82946209
ZOJ
今日推荐