您的位置:首页 > 其它

HDU4940 Destroy Transportation system

2014-08-12 17:45 253 查看

Destroy Transportation system

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 8 Accepted Submission(s): 6

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

题意:求选出一个集合,如果这个集合的出边的d之和比这个集合的入边的d和b之和大,就unhappy,其他都happy。

这题很简单。。比赛无脑交了一发,稀里糊涂的就过了。。用out表示一个点的出边,in表示入边(d,b之后)。

这题就是要找出看存不存在out大于in的。对于同一条边,in肯定要大于这条边的out。所以如果两个相邻的点都不能当做S,那么这两个点进入S也不可以(稍微画下图就知道了),两个不相邻的点单个不满足合在一起也是不可以的(这个很显然),所以我们只要判断一个点能不能满足out大于in就可以了,如果存在这样的一个点就是unhappy了。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXE=5010;
const int MAXN=210;
struct EDGE
{
int v,next;
int d,b;
}edge[MAXE];
int head[MAXN],size;
void init()
{
memset(head,-1,sizeof(head));
size=0;
}
void add_edge(int u,int v,int d,int b)
{
edge[size].v=v;
edge[size].next=head[u];
edge[size].d=d;
edge[size].b=b;
head[u]=size++;
}
int in[MAXN],out[MAXN];
int main()
{
int t,n,m,u,v,d,b,i,x=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
while(m--)
{
scanf("%d%d%d%d",&u,&v,&d,&b);
add_edge(u,v,d,b);
}
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(u=1;u<=n;u++)
{
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
in[v]+=edge[i].d;
in[v]+=edge[i].b;
out[u]+=edge[i].d;
}
}
int flag=1;
for(i=1;i<=n;i++)
{
if(out[i]>in[i])
{
flag=0;
break;
}
}
printf("Case #%d: ",x++);
if(flag)
printf("happy\n");
else
printf("unhappy\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: