HDU 4940 destroy Transportation System (graph theory)

Source: Internet
Author: User

What I thought at the time of this question was that if the sum of the D value of a vertex is greater than the sum of the D + B value, we can. That's it. However, there is another way to solve the problem, which seems a little difficult. In the future, make up that practice.

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 descriptiontom 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 stronugly-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 stronugly-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.
Inputthere 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 stronugly-connected.
Outputfor 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
23 31 2 2 22 3 2 23 1 2 23 31 2 10 22 3 2 23 1 2 2
 
Sample output
Case #1: happyCase #2: unhappyHintIn 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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.