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

解题报告

题目传送门

题意:

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

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

思路:

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

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

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

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

可以把残留网络的零流的边设成容量为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(网络流/最小割=最大流/找割边)

时间: 2024-08-06 03:41:37

ZOJ3792_Romantic Value(网络流/最小割=最大流/找割边)的相关文章

ZOJ 3792 Romantic Value(网络流之最小割)(找割边)

题目地址:ZOJ 3792 最小割做的太少..这题很明显是找割边.找割边就是判断正向弧是否是0.如果跑完一次最小割后正向弧流量为0的话,那就说明这个边为一条割边.但是找到了割边后再怎么办呢..中午睡觉的时候突然来了灵感..再利用这些割边求一次最大流不就行了..把割边的流量都设为1,其他的都为正无穷.那最后的流量就是最少需要的割边了.然后计算就可以了. 这次又把上限值直接设为sink+1了...导致WA了12发.....sad...以后得注意... 代码如下: #include <iostream

POJ3469_Dual Core CPU(网络流/最小割=最大流/模版)----Dinic模版2.0

解题报告 题目传送门 题意: 双核CPU,n个模块,每个模块必须运行在某个CPU核心上,每个模块在cpu单核的消耗A和B,M对模块要共享数据,如果在同一个核心上不用消耗,否则需要耗费.安排N个模块,使得总耗费最小 思路: 将两个cpu核心看成源点和汇点,其他模块分别与源点汇点连线(表示每个模块可以在任意cpu上运行),m对模块分别连双向边,要使得模块只能在一个cpu上运行,就是找到一个割,源点和汇点必不联通,耗费最少就是最小割,最小割最大流原理转换成求最大流. 这题数据大,没优化TLE了,加了两

二分图/网络流/最小割/最大流/最小费用最大流等等 模板

二分图匹配: 1.匈牙利算法  O(n * m)  n为二分图左侧点数  m为二分图右侧点数 #include<bits/stdc++.h> using namespace std; const int N=1e7; struct node{ int from,to,nxt; }e[N]; int head[N],cnt; int n; int v[N],ans,A,B,d[N]; void add(int from,int to){ e[++cnt].nxt=head[from]; e[cn

【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点

1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1685  Solved: 724[Submit][Status][Discuss] Description A,B两个国家正在交战,其中A国的物资运输网中有N个中转站,M条单向道路.设其中第i (1≤i≤M)条道路连接了vi,ui两个中转站,那么中转站vi可以通过该道路到达ui中转站,如果切断这条道路,需要代价ci.现在B国想找出一个路径切断方案

【bzoj3144】[Hnoi2013]切糕 网络流最小割

题目描述 输入 第一行是三个正整数P,Q,R,表示切糕的长P. 宽Q.高R.第二行有一个非负整数D,表示光滑性要求.接下来是R个P行Q列的矩阵,第z个 矩阵的第x行第y列是v(x,y,z) (1≤x≤P, 1≤y≤Q, 1≤z≤R). 100%的数据满足P,Q,R≤40,0≤D≤R,且给出的所有的不和谐值不超过1000. 输出 仅包含一个整数,表示在合法基础上最小的总不和谐值. 样例输入 2 2 2 1 6 1 6 1 2 6 2 6 样例输出 6 题目大意 给定一个p行q列的矩阵,每个位置可以

HDU 2435 There is a war (网络流-最小割)

There is a war Problem Description There is a sea. There are N islands in the sea. There are some directional bridges connecting these islands. There is a country called Country One located in Island 1. There is another country called Country Another

HDU 4289 Control (网络流-最小割)

Control Problem Description You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to another one (the destination). You know their

二分图&amp;网络流&amp;最小割等问题的总结

二分图基础: 最大匹配:匈牙利算法 最小点覆盖=最大匹配 最小边覆盖=总节点数-最大匹配 最大独立集=点数-最大匹配 网络流: 带下界网络流 最小割问题的总结: *意义 1.加inf的边表示不能被割,通常用于体现某个点必须属于某个集合 连边(s,u,w)代表如果u不在s割的话需要付出代价w 2.连边(u,v,w)代表如果u在s割,v在t割需要付出代价w 但注意,如果u在t割,v在s割是不需要付出代价的. 那么如果连边(u,v,w)以及(v,u,w)则说明当u与v所属割不同的时候需要付出代价w *

【bzoj3630】[JLOI2014]镜面通道 对偶图+计算几何+网络流最小割

题目描述 在一个二维平面上,有一个镜面通道,由镜面AC,BD组成,AC,BD长度相等,且都平行于x轴,B位于(0,0).通道中有n个外表面为镜面的光学元件,光学元件α为圆形,光学元件β为矩形(这些元件可以与其他元件和通道有交集,具体看下图).光线可以在AB上任一点以任意角度射入通道,光线不会发生削弱.当出现元件与元件,元件和通道刚好接触的情况视为光线无法透过(比如两圆相切).现在给出通道中所有元件的信息(α元件包括圆心坐标和半径xi,yi,ri,β元件包括左下角和右上角坐标x1,y1,x2,y2