题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4940

Destroy Transportation system

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 21    Accepted Submission(s): 17

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.
 
Author
UESTC
 
Source

官方题解:http://blog.sina.com.cn/s/blog_6bddecdc0102uzka.html

寻找是否存在能单独作为S集合的点!

代码例如以下:

//#pragma warning (disable:4786)
#include <cstdio>
#include <cstring>
typedef long long LL;
#define N 1017
LL X[N], Y[N];
void init()
{
memset(X, 0, sizeof(X));
memset(Y, 0, sizeof(Y));
}
int main()
{
int n, m;
int u, v, D, B;
int t;
int cas = 0;
int flag = 0;
int i;
scanf("%d", &t);
while(t--)
{
init();
flag = 0;
scanf("%d%d", &n,&m);
for(i=1; i<=m; i++)
{
scanf("%d%d%d%d", &u, &v, &D, &B);
X[u]+=D;
Y[v]+=(D+B); }
for(i = 1; i <= n; i++)
{
if(Y[i] < X[i])
{
flag = 1;
break;
}
}
if(flag)
printf("Case #%d: unhappy\n",++cas);
else
printf("Case #%d: happy\n",++cas);
}
return 0;
}
贴一发官方题解:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 209
#define maxm 20000
#define INF 1e9
using namespace std;
struct Edge
{
int v,cap,next;
}edge[maxm];
int n,tot,src,des;
int head[maxn],h[maxn],gap[maxn],B[maxn];
void addedge(int u,int v,int cap)
{
edge[tot].v=v;
edge[tot].cap=cap;
edge[tot].next=head[u];
head[u]=tot++;
edge[tot].v=u;
edge[tot].cap=0;
edge[tot].next=head[v];
head[v]=tot++;
}
int dfs(int u,int cap)
{
if(u==des)return cap;
int minh=n-1;
int lv=cap,d;
for(int e=head[u];e!=-1;e=edge[e].next)
{
int v=edge[e].v;
if(edge[e].cap>0)
{
if(h[v]+1==h[u])
{
d=min(lv,edge[e].cap);
d=dfs(v,d);
edge[e].cap-=d;
edge[e^1].cap+=d;
lv-=d;
if(h[src]>=n)return cap-lv;
if(lv==0)
break;
}
minh=min(minh,h[v]);
}
}
if(lv==cap)
{
--gap[h[u]];
if(gap[h[u]]==0)
h[src]=n;
h[u]=minh+1;
++gap[h[u]];
}
return cap-lv;
}
int sap()
{
int flow=0;
memset(gap,0,sizeof(gap));
memset(h,0,sizeof(h));
gap[0]=n;
while(h[src]<n)flow+=dfs(src,INF);
return flow;
}
int main()
{
int N,M;
int cot=1;
int CAS;
scanf("%d", &CAS);
while(CAS--)
{
scanf("%d%d",&N,&M);
memset(head,-1,sizeof(head));
memset(B,0,sizeof(B));
tot=0;src=0;des=N+1;n=des+1;
for(int i=1;i<=M;i++)
{
int u,v,b,l;
scanf("%d%d%d%d",&u,&v,&b,&l);
addedge(u,v,l);
B[v]+=b;B[u]-=b;
}
int sum=0;
for(int i=1;i<=N;i++)
{
if(B[i]>0)
{
addedge(src,i,B[i]);
sum+=B[i];
}
else if(B[i]<0)
{
addedge(i,des,-B[i]);
}
}
int flow=sap();
if(flow==sum)
{
printf("Case #%d: happy\n",cot++);
}
else
{
printf("Case #%d: unhappy\n",cot++);
}
}
return 0;
}

hdu 4940 Destroy Transportation system(水过)的更多相关文章

  1. hdu 4940 Destroy Transportation system (无源汇上下界可行流)

    Destroy Transportation system Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 ...

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

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

  3. HDU 4940 Destroy Transportation system(无源汇有上下界最大流)

    看不懂题解以及别人说的集合最多只有一个点..... 然后试了下题解的方法http://blog.sina.com.cn/s/blog_6bddecdc0102uzka.html 首先是无源汇有上下界最 ...

  4. HDU 4940 Destroy Transportation system(2014 Multi-University Training Contest 7)

    思路:无源汇有上下界可行流判定, 原来每条边转化成  下界为D  上界为 D+B   ,判断是否存在可行流即可. 为什么呢?  如果存在可行流  那么说明对于任意的 S 集合流出的肯定等于 流入的, ...

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

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

  6. 【HDU 4940】Destroy Transportation system(无源无汇带上下界可行流)

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

  7. HDU 4940(杭电更多的学校#7 1006) Destroy Transportation system(到处乱混)

    职务地址:pid=4940">HDU 4940 当时这个题一看就看出来了是网络流的最小割.然后就一直在想建图. .然后突然发现,应该要让T集合的数目最少,不然仅仅要有两个,那这两个的每 ...

  8. HDU4940 Destroy Transportation system(有上下界的最大流)

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

  9. HDU Destroy Transportation system(有上下界的可行流)

    前几天正看着网络流,也正研究着一个有上下界的网络流的问题,查看了很多博客,觉得下面这篇概括的还是相当精确的: http://blog.csdn.net/leolin_/article/details/ ...

随机推荐

  1. jQuery自定义组件——输入框设置默认提示文字

    if (window.jQuery || window.Zepto) { /** * 设置输入框提示信息 * @param options * @author rubekid */ var setIn ...

  2. C#中yield用法

    yield 关键字向编译器指示它所在的方法是迭代器块.编译器生成一个类来实现迭代器块中表示的行为.在迭代器块中,yield 关键字与 return 关键字结合使用,向枚举器对象提供值.这是一个返回值, ...

  3. MSSQL查询所有数据库表,指定数据库的字段、索引

    --查询所有数据库USE mastergoselect [name] from [sysdatabases] order by [name] --查询其中一个数据库test,就可以得到这个数据库中的所 ...

  4. android studio 改变主题及字体大小

    一丶修改主题背景 1.点击图中的小扳子图标(设置),或者点击file->setting-> 2.找到Appearance->在Theme中选择Darcula,即可改变成当前最主流的主 ...

  5. sql server 导出

    http://ssat.codeplex.com/SourceControl/latest 用于连接sql server

  6. 高放的python学习笔记之基本语法

    python与c++的不同之处 python的语句块不是用{}括起来的而是冒号后面跟一些与比当前语句多一的tab缩进的语句. 1.定义变量 python的变量类型不需要人为指出,会根据赋值的类型决定此 ...

  7. 你好,C++(22) 排排坐,吃果果——4.3.3 for循环:某个范围内…每个都…

    4.3.3  for循环:某个范围内…每个都… 既然while语句和do…while…语句都已经可以满足我们表达循环现象的需要,那为什么C++还要专门提供for语句来表达循环现象呢?在现实世界中,常常 ...

  8. Integer和int的详细比较(转)

    Integer与int的区别我们耳熟详的有两点:1.Integer是int的包装类.2.Integer的默认初始值是null,而int的默认初试值是0. 下面通过代码进行详细比较. public cl ...

  9. Java中 int和Integer的区别+包装类

    --今天用Integer 和Integer 比较 发现有问题,于是去查了查. 1.Java 中的数据类型分为基本数据类型和引用数据类型 int是基本数据类型,Integer是引用数据类型: Inget ...

  10. JS判断字符串是否为空、过滤空格、查找字符串位置等函数集

    这是一个由网上收集的JS代码段,用于判断指定字符串是否为空,过滤字符串中某字符两边的空格.查找指定字符串开始的位置.使用IsFloat函数判断一 个字符串是否由数字(int or long or fl ...