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

同上一道题,不过在我不知道这题要用最大流来做的情况下我是不会想到的:

关键是要构造出不等式,而且把不等式对应到可行流。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=4;
const int inf=;
int Laxt[maxn],Next[maxn],To[maxn],Cap[maxn],cnt;
int dis[maxn],nd[maxn],S,T,num,ans,q[maxn],qnum[maxn],top;
void init()
{
cnt=;ans=num=top=;
memset(Laxt,,sizeof(Laxt));
memset(dis,,sizeof(dis));
memset(nd,,sizeof(nd));
}
int add(int u,int v,int c)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
Cap[cnt]=c; Next[++cnt]=Laxt[v];
Laxt[v]=cnt;
To[cnt]=u;
Cap[cnt]=;
}
int sap(int u,int flow)
{
if(u==T||flow==) return flow;
int delta=,tmp;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(dis[v]+==dis[u]&&Cap[i]>){
tmp=sap(v,min(Cap[i],flow-delta));
delta+=tmp;
Cap[i]-=tmp;
Cap[i^]+=tmp;
if(flow==delta||dis[]>=T) return delta;
}
}
nd[dis[u]]--;
if(nd[dis[u]]==) dis[]=T;
nd[++dis[u]]++;
return delta;
}
int main()
{
int Case,n,i,j,m,u,v,x,y,k=;
scanf("%d",&Case);
while(Case--){
init();
scanf("%d%d",&n,&m);
S=;T=n+;
for(i=;i<=m;i++){
scanf("%d%d%d%d",&u,&v,&x,&y);
u++;v++;num+=x;
add(u,v,y);
q[++top]=cnt;
qnum[top]=x;
add(S,v,x);
add(u,T,x);
}
while(dis[S]<T) {
ans+=sap(S,inf);
}
printf("Case #%d: ",++k);
if(num!=ans) printf("unhappy\n");
else printf("happy\n");
}
return ;
}

(希望多遇到几个这样的模型,然后好好理解一下)

HDU4940 Destroy Transportation system(有上下界的最大流)的更多相关文章

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

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

  2. hdu4940 Destroy Transportation system(2014多校联合第七场)

    题意很容易转化到这样的问题:在一个强连通的有向图D中是否存在这样的集合划分S + T = D,从S到T集合的边权大于从T到S集合的边权. 即D(i, j)  > B(j, i) + D(j, i ...

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

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

  4. hdu 4940 Destroy Transportation system(水过)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4940 Destroy Transportation system Time Limit: 2000/1 ...

  5. ZOJ 2314 带上下界的可行流

    对于无源汇问题,方法有两种. 1 从边的角度来处理. 新建超级源汇, 对于每一条有下界的边,x->y, 建立有向边 超级源->y ,容量为x->y下界,建立有向边 x-> 超级 ...

  6. 【UVALive - 5131】Chips Challenge(上下界循环费用流)

    Description A prominent microprocessor company has enlisted your help to lay out some interchangeabl ...

  7. SGU 176.Flow construction (有上下界的最大流)

    时间限制:0.5s 空间限制:4M 题意: 有一个由管道组成的网络,有n个节点(n不大于100),1号节点可以制造原料,最后汇集到n号节点.原料通过管道运输.其中有一些节点有管道连接,这些管道都有着最 ...

  8. zoj3229 Shoot the Bullet(有源汇有上下界的最大流)

    题意: 一个屌丝给m个女神拍照,计划拍照n天,每一天屌丝给给定的C个女神拍照,每天拍照数不能超过D张,而且给每个女神i拍照有数量限制[Li,Ri],对于每个女神n天的拍照总和不能少于Gi,如果有解求屌 ...

  9. zoj 3229 有源汇有上下界的最大流模板题

    /*坑啊,pe的程序在zoj上原来是wa. 题目大意:一个屌丝给m个女神拍照.计划拍照n天,每一天屌丝最多个C个女神拍照,每天拍照数不能超过D张,并且给每一个女神i拍照有数量限制[Li,Ri], 对于 ...

随机推荐

  1. open函数and文件处理

    一 介绍 计算机系统分为:计算机硬件,操作系统,应用程序三部分 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,应用程序是无法操作 ...

  2. (转) FLASH吸血鬼的工作原理

    FLASH吸血鬼是众多网友用来从exe可执行文件中提取swf的利器,其直接读取内存,从内存中取出swf文件.经过分析,发现其原理还是比较简单的.第一步.通过GetWindowThreadProcess ...

  3. LeetCode:罗马数字转整数【13】

    LeetCode:罗马数字转整数[13] 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 10 ...

  4. C#:连接本地SQL Server语句

    一.Windows身份验证方式 SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source ...

  5. 每天一个Linux命令(62)rcp命令

        rcp代表"remote file copy"(远程文件拷贝).     (1)用法:     用法:  rcp [参数] [源文件] [目标文件]     (2)功能: ...

  6. 016_笼统概述MapReduce执行流程结合wordcount程序

    数据传输<key,value>     File-->  <key,value>  -->map(key,value)  --> mapResult<k ...

  7. sql中in和exists的区别效率问题 转

    in 和exists in是把外表和内表作hash 连接,而exists 是对外表作loop 循环,每次loop 循环再对内表进行查询. 一直以来认为exists 比in 效率高的说法是不准确的.如果 ...

  8. Linux文件系统管理 挂载命令mount

    概述 mount命令用来挂载Linux系统外的文件. Linux 中所有的存储设备都必须挂载之后才能使用,包括硬盘.U 盘和光盘(swap 分区是系统直接调用的,所以不需要挂载).不过,硬盘分区在安装 ...

  9. XML文件结构和基本语法

    XML文件的结构性内容,包括节点关系以及属性内容等等.元素是组成XML的最基本的单位,它由开始标记,属性和结束标记组成.就是一个元素的例子,每个元素必须有一个元素名,元素可以若干个属性以及属性值. x ...

  10. HTTP协议—常见的HTTP响应状态码解析

    常见的HTTP响应状态码解析 1XX Informational(信息性状态码) 2XX Success(成功状态码) 3XX Redirection(重定向状态码) 4XX Client Error ...