Destroy Transportation system

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
http://acm.hdu.edu.cn/showproblem.php?pid=4940

Problem Description
Tom is a commander, his task is destroying his enemy’s
transportation system.

Let’s represent his enemy’s transportation system
as a simple directed graph G with n nodes and m edges. Each node is a city and
each directed edge is a directed road. Each edge from node u to node v is
associated with two values D and B, D is the cost to destroy/remove such edge, B
is the cost to build an undirected edge between u and v.

His enemy can
deliver supplies from city u to city v if and only if there is a directed path
from u to v. At first they can deliver supplies from any city to any other
cities. So the graph is a strongly-connected graph.

He will choose a
non-empty proper subset of cities, let’s denote this set as S. Let’s denote the
complement set of S as T. He will command his soldiers to destroy all the edges
(u, v) that u belongs to set S and v belongs to set T.

To destroy an
edge, he must pay the related cost D. The total cost he will pay is X. You can
use this formula to calculate X:

After that, all the edges from S to
T are destroyed. In order to deliver huge number of supplies from S to T, his
enemy will change all the remained directed edges (u, v) that u belongs to set T
and v belongs to set S into undirected edges. (Surely, those edges exist because
the original graph is strongly-connected)

To change an edge, they must
remove the original directed edge at first, whose cost is D, then they have to
build a new undirected edge, whose cost is B. The total cost they will pay is Y.
You can use this formula to calculate Y:

At last, if Y>=X, Tom will
achieve his goal. But Tom is so lazy that he is unwilling to take a cup of time
to choose a set S to make Y>=X, he hope to choose set S randomly! So he asks
you if there is a set S, such that Y<X. If such set exists, he will feel
unhappy, because he must choose set S carefully, otherwise he will become very
happy.

 
Input
There are multiply test cases.

The first line
contains an integer T(T<=200), indicates the number of cases.

For
each test case, the first line has two numbers n and m.

Next m lines
describe each edge. Each line has four numbers u, v, D, B.
(2=<n<=200,
2=<m<=5000, 1=<u, v<=n, 0=<D, B<=100000)

The meaning of
all characters are described above. It is guaranteed that the input graph is
strongly-connected.

 
Output
For each case, output "Case #X: " first, X is the case
number starting from 1.If such set doesn’t exist, print “happy”, else print
“unhappy”.
 
Sample Input
2
3 3
1 2 2 2
2 3 2 2
3 1 2 2
3 3
1 2 10 2
2 3 2 2
3 1 2 2
 
Sample Output
Case #1: happy
Case #2: unhappy
 
Hint

In first sample, for any set S, X=2, Y=4. In second sample. S= {1}, T= {2, 3}, X=10, Y=4.

 
题意:给出一个有向强连通图,每条边有两个值:破坏该边的代价a 和 把该边建成无向边的代价b
问是否存在一个集合S和S的补集T,满足 S到T的割边的 a的总和 > T到S的 割边的 a+b的总和
若存在 输出unhappy, 不存在,输出happy 以a为下界,a+b为上界,判断是否存在无源汇上下界可行流
因为如果存在,流量总和>=下界,<=上界
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define N 210
#define M 15000
using namespace std;
int m,n,src,dec,sum,tot;
int a[N];
int front[N],to[M],nextt[M],cap[M];
int lev[N],cur[N];
queue<int>q;
void add(int u,int v,int w)
{
to[++tot]=v; nextt[tot]=front[u]; front[u]=tot; cap[tot]=w;
to[++tot]=u; nextt[tot]=front[v]; front[v]=tot; cap[tot]=;
}
bool bfs()
{
for(int i=src;i<=dec;i++) cur[i]=front[i],lev[i]=-;
while(!q.empty()) q.pop();
lev[src]=;
q.push(src);
int now;
while(!q.empty())
{
now=q.front(); q.pop();
for(int i=front[now];i;i=nextt[i])
if(cap[i]>&&lev[to[i]]==-)
{
lev[to[i]]=lev[now]+;
if(to[i]==dec) return true;
q.push(to[i]);
}
}
return false;
}
int dfs(int now,int flow)
{
if(now==dec) return flow;
int rest=,delta;
for(int & i=cur[now];i;i=nextt[i])
if(cap[i]>&&lev[to[i]]>lev[now])
{
delta=dfs(to[i],min(flow-rest,cap[i]));
if(delta)
{
cap[i]-=delta; cap[i^]+=delta;
rest+=delta; if(rest==flow) break;
}
}
if(rest!=flow) lev[now]=-;
return rest;
}
int dinic()
{
int tmp=;
while(bfs()) tmp+=dfs(src,2e9);
return tmp;
}
int main()
{
int T;
scanf("%d",&T);
for(int k=;k<=T;k++)
{
memset(a,,sizeof(a));
memset(front,,sizeof(front));
sum=; tot=;
scanf("%d%d",&n,&m);
src=; dec=n+;
int u,v,c,d;
for(int i=;i<=m;i++)
{
scanf("%d%d%d%d",&u,&v,&c,&d);
a[v]+=c; a[u]-=c;
add(u,v,d);
}
for(int i=;i<=n;i++)
if(a[i]<) add(i,dec,-a[i]);
else if(a[i]>) {add(src,i,a[i]); sum+=a[i];}
if(dinic()==sum) printf("Case #%d: happy\n",k);
else printf("Case #%d: unhappy\n",k);
}
}
 

hdu 4940 Destroy Transportation system (无源汇上下界可行流)的更多相关文章

  1. HDU 4940 Destroy Transportation system(无源汇上下界网络流)

    Problem Description Tom is a commander, his task is destroying his enemy’s transportation system. Le ...

  2. hdu 4940 Destroy Transportation system( 无源汇上下界网络流的可行流推断 )

    题意:有n个点和m条有向边构成的网络.每条边有两个花费: d:毁坏这条边的花费 b:重建一条双向边的花费 寻找这样两个点集,使得点集s到点集t满足 毁坏全部S到T的路径的费用和 > 毁坏全部T到 ...

  3. ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 The terrorist group leaded by ...

  4. zoj 2314 Reactor Cooling (无源汇上下界可行流)

    Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...

  5. ZOJ2314 Reactor Cooling(无源汇上下界可行流)

    The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...

  6. zoj2314 无源汇上下界可行流

    题意:看是否有无源汇上下界可行流,如果有输出流量 题解:对于每一条边u->v,上界high,下界low,来说,我们可以建立每条边流量为high-low,那么这样得到的流量可能会不守恒(流入量!= ...

  7. 有源汇上下界可行流(POJ2396)

    题意:给出一个n*m的矩阵的每行和及每列和,还有一些格子的限制,求一组合法方案. 源点向行,汇点向列,连一条上下界均为和的边. 对于某格的限制,从它所在行向所在列连其上下界的边. 求有源汇上下界可行流 ...

  8. 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]

    题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...

  9. poj2396有源汇上下界可行流

    题意:给一些约束条件,要求算能否有可行流,ps:刚开始输入的是每一列和,那么就建一条上下界相同的边,这样满流的时候就一定能保证流量相同了,还有0是该列(行)对另一行每个点都要满足约束条件 解法:先按无 ...

随机推荐

  1. 【leetcode】54.Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  2. beta阶段评语

    首先我说一下自己心中的排序 1.俄罗斯方块 2 连连看 3 考试管理系统 4 食物链教学软件 5 约跑App 6 礼物挑选小工具 我的理由: 新峰的俄罗斯的方块,虽然当初的亮点没做出来,但是整体流程完 ...

  3. Java容器深入浅出之Collection与Iterator接口

    Java中用于保存对象的容器,除了数组,就是Collection和Map接口下的容器实现类了,包括用于迭代容器中对象的Iterator接口,构成了Java数据结构主体的集合体系.其中包括: 1. Co ...

  4. utuntu 安装python3.5

    如果想要升级Utuntu系统中的python版本,请不要卸载原先的版本. 桌面环境中的需要依赖于python相关,卸载之后会出现意想不到问题. (1)sudo add-apt-repository p ...

  5. 在js和C#中split应用和去除字符串分组后的空值

    如字符串 string answer="A,B,D,",在 js和 C#按","分成数组 js: , useranswer.length - ).split(& ...

  6. Hadoop基于Protocol Buffer的RPC实现代码分析-Server端--转载

    原文地址:http://yanbohappy.sinaapp.com/?p=110 最新版本的Hadoop代码中已经默认了Protocol buffer(以下简称PB,http://code.goog ...

  7. OGG内部进程介绍

    1.首先看看什么是OGG,以及OGG的用途       简单的来讲 Oracle Golden Gate (简称OGG)是一种基于日志的结构化数据复制备份软件,它通过解析源数据库在线日志或归档日志获得 ...

  8. Robotframework SSHLibrary库关键字

    1.连接远程机器 两种方法,一种用户名密码登录连接,一种是通过密钥连接 (1)用户名密码连接 (2)密钥连接 大概介绍下密钥:首先由用户生成一对密码,公钥与私钥:私钥是给需要连接的用户,连接时使用私钥 ...

  9. python中括号的使用

    1. 列表list是用[ ]包住的以逗号分隔的数据集合 所有对列表的解析均采用[ ],不论是元素引用或取值 [ ]表示空列表 2. 字典由键-值(key-value)对构成,一般可采用{ }表示 取字 ...

  10. elasticsearch 第五篇(文档操作接口)

    INDEX API 示例: 1 2 3 4 5 PUT /test/user/1 { "name": "silence", "age": 2 ...