HDU 4940 Destroy Transportation system(图论)

这道题目当时做的时候想的是,如果找到一个点他的d值之和大于 d+b值之和,就可以。竟然就这么过了啊。不过题解上还有一种做法,好像有点难。以后在补一补那种做法吧。

Destroy Transportation system

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

Total Submission(s): 289    Accepted Submission(s): 181

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.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)

const int maxn = 2010;

using namespace std;

struct node
{
    int x, y;
} f[maxn];

int main()
{
    int T;
    cin >>T;
    int Case = 1;
    while(T--)
    {
        int n, m;
        cin >>n>>m;
        int x, y;
        int d, b;
        for(int i = 0; i <= n; i++) f[i].x = f[i].y = 0;
        for(int i = 0; i < m; i++)
        {
            scanf("%d %d %d %d",&x, &y, &d, &b);
            f[x].y += d;
            f[y].x += (d+b);
        }
        int flag = 0;
        for(int i = 1; i <= n; i++)
        {
            if(f[i].x < f[i].y)
            {
                flag = 1;
                break;
            }
        }
        cout<<"Case #"<<Case++<<": ";
        if(flag) cout<<"unhappy"<<endl;
        else cout<<"happy"<<endl;
    }
    return 0;
}

HDU 4940 Destroy Transportation system(图论)

时间: 2024-11-05 22:40:38

HDU 4940 Destroy Transportation system(图论)的相关文章

HDU 4940 Destroy Transportation system 规律题

答案只有2种情况,所以ans = rand()%2; if(ans)puts("happy") else puts("unhappy"); == 想过无源汇的网络流,还是比较麻烦的,然后没往下想... 设s点集有一些点, 多加一个点一定是y增加比较快_(:зゝ∠)_ 然后设s点集只有一个点 #include <cstdio> #include <map> #include <set> #include <algorithm&

hdu 4940 Destroy Transportation system(水过)

题目链接: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

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

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 transportatio

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

题意:有n个点和m条有向边构成的网络.每条边有两个花费: d:毁坏这条边的花费 b:重建一条双向边的花费 寻找这样两个点集,使得点集s到点集t满足 毁坏全部S到T的路径的费用和 > 毁坏全部T到S的路径的费用和 + 重建这些T到S的双向路径的费用和. 思路1: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center&quo

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

题意:有n个点和m条有向边构成的网络,每条边有两个花费: d:毁坏这条边的花费 b:重建一条双向边的花费 寻找这样两个点集,使得点集s到点集t满足 毁坏所有S到T的路径的费用和 > 毁坏所有T到S的路径的费用和 + 重建这些T到S的双向路径的费用和. 思路1: 然后这个无源汇带上下界网络流的可行流问题的求解方法见这里~~ 建图就是上面说的那样啦~最后判断有没有可行流就是求一下我们所构造的这个新的网络的最大流~然后判断一下这个最大流是否满流~(即判断最大流是否和附加源点的流出总量相等~~) cod

【HDU 4940】Destroy Transportation system(数据水/无源无汇带上下界可行流)

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 directe

HDU 4940(杭电多校#7 1006) Destroy Transportation system(瞎搞)

题目地址:HDU 4940 当时这个题一看就看出来了是网络流的最小割,然后就一直在想建图..然后突然发现,应该要让T集合的数目最少,不然只要有两个,那这两个的每一个都可以跑到S集合,使得T集合变小.那就只能是1个了.然后..枚举就好了..但是虽然觉得这么做肯定没错..但是不敢敲..因为当时都3个小时了才只有10个队过了...后来又想了几遍后觉得这样没错,就写完交上了.果然AC... 代码如下: #include <iostream> #include <cstdio> #inclu

hdu4940 Destroy Transportation system

给你一个强连通的有向简单图,每条边有D, B两个权值,设S为点集的一个非空真子集 问:是否对于任意的集合S,都有sum (D(i, j))<= sum(D(j, i) + B(j, i)),(i, j)是点集S和S'的割边 比赛的时候完全没有思路啊...最大流果然还是白板 思路:将D值作为边的下界,D + B作为边的上界,如果存在可行流,那么对于任意集合S 都有流量小于等于边的容量上界,大于等于边的容量下界,即D(i, j) <= f(i, j) <= D(j, i)+B(j, i) 这

HDU——T 3072 Intelligence System

http://acm.hdu.edu.cn/showproblem.php?pid=3072 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2909    Accepted Submission(s): 1259 Problem Description After a day, ALPCs finally complete their