# 【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 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.

## Source

2014 Multi-University Training Contest 7

1 4 5 1 4 10 2 4 1 6 2 2 1 1 1 4 3 6 2 3 2 4 2

#include <cstdio>
#include <cstring>
#define N 205
#define sf(x) scanf("%d",&x)
int x[N],y[N];
int main(){
int t;
sf(t);
for(int cas=1;cas<=t;cas++){
memset(x,0,sizeof x);
memset(y,0,sizeof y);
printf("Case #%d: ",cas);
int n,m;
sf(n);
sf(m);
for(int i=1;i<=m;i++){
int u,v,d,b;
sf(u);sf(v);sf(d);sf(b);
x[u]+=d;
y[v]+=d+b;
}
int ok=1;
for(int i=1;i<=n;i++)
if(x[i]>y[i])ok=0;
if(ok)puts("happy");
else puts("unhappy");
}
}

$$\sum_{\substack{u\in S \\ v\in \overline {S}}} f_{uv} = \sum_{\substack{u\in S \\ v\in \overline {S}}}f_{uv}\\D_{uv} \leq f_{uv} \leq D_{uv}+B_{uv}$$

$$\sum_{\substack{u\in S \\ v\in \overline {S}}} D_{uv} \leq \sum_{\substack{u\in S \\ v\in \overline {S}}}D_{uv}+ B_{uv}$$

in>out则s 到 i 连边，流量为in-out；

in<out则 i 到 t 连边，流量为out-in。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 500
#define M 200001
#define inf 0x3f3f3f3f
struct edge{
int to,next,cap,flow;
}e[M];
int gap[N],dep[N],cur[N];
void init(){
cnt=0;
}
void add(int u,int v,int w,int rw=0){
}
int q[N];
void bfs(int st,int ed){
memset(dep,-1,sizeof dep);
memset(gap,0,sizeof gap);
gap[0]=1;
int front=0,rear=0;
dep[ed]=0;
q[rear++]=ed;
while(front!=rear){
int u=q[front++];
int v=e[i].to;
if(dep[v]!=-1)continue;
q[rear++]=v;
dep[v]=dep[u]+1;
gap[dep[v]]++;
}
}
}
int s[N];
int sap(int st,int ed,int n){
bfs(st,ed);
int top=0;
int u=st;
int ans=0;
while(dep[st]<n){
if(u==ed){
int Min=inf;
int inser;
for(int i=0;i<top;i++)
if(Min>e[s[i]].cap-e[s[i]].flow){
Min=e[s[i]].cap-e[s[i]].flow;
inser=i;
}
for(int i=0;i<top;i++){
e[s[i]].flow+=Min;
e[s[i]^1].flow-=Min;
}
ans+=Min;
top=inser;
u=e[s[top]^1].to;
continue;
}
bool flag=false;
int v;
for(int i=cur[u];~i;i=e[i].next){
v=e[i].to;
if(e[i].cap-e[i].flow&&dep[v]+1==dep[u]){
flag=true;
cur[u]=i;
break;
}
}
if(flag){
s[top++]=cur[u];
u=v;
continue;
}
int Min=n;
if(e[i].cap-e[i].flow &&dep[e[i].to]<Min){
Min=dep[e[i].to];
cur[u]=i;
}
gap[dep[u]]--;
if(!gap[dep[u]])return ans;
gap[dep[u]=Min+1]++;
if(u!=st)u=e[s[--top]^1].to;
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++){
printf("Case #%d: ",cas);
int n,m;
scanf("%d%d",&n,&m);
int in[N];
int st=0,ed=n+1;
memset(in,0,sizeof in);
init();
for(int i=1;i<=m;i++){
int u,v,d,b;
scanf("%d%d%d%d",&u,&v,&d,&b);
in[v]+=d;
in[u]-=d;
}
int need=0;
for(int i=1;i<=n;i++){
if(in[i]>0){
need+=in[i];
}
}
int ans=sap(st, ed, ed+1);
if(need==ans)puts("happy");
else puts("unhappy");
}
}

0 条评论

• ### 【CodeForces 730H】Car Repair Shop

模拟，先看从s[i]时刻开始修理，和之前i-1个是否冲突。如果冲突，就枚举每个s[j]+d[j]时刻开始，看是否冲突，再从中选择最小的时刻。

• ### 堆排序

2.把根节点和最后一个节点交换，,把堆长度-1，也就不考虑放最后的最大的元素了，再构建最大堆

• ### 【UVALive - 6534 】Join two kingdoms （树的直径的期望）

给两棵树，分别有 n，m 个节点(1 ≤ N, Q ≤ 4 × 10^4)，等概率连接属于不同树的两个节点，求新树的直径（最远两点的距离）的期望。

• ### 盘点今年秋招那些“送命”的算法面试题

随着 2019 年校招结束，“金九银十”的跳槽季也已经接近尾声，不知道在裁员、消减HC、只招中高级岗位等等“悲观”情绪下，你是否已经如愿入职心意的企业？或者还是...

• ### HDU 2121 Ice_cream’s world II(最小树形图+虚根)

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

• ### PAT 甲级 1021 Deepest Root (并查集，树的遍历)

1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B ...

• ### 玩具谜题 未完成

#include<iostream> #include<cstdio> using namespace std; struct node { int zt;/...

• ### 最短网络Agri-Net

问题描述】 　　农民约翰被选为他们镇的镇长！他其中一个竞选承诺就是在镇上建立起互联网，并连接到所有的农场。当然，他需要你的帮助。约翰已经给他的农场安排了一条高速...

• ### 1807. [NOIP2014]寻找道路P2296 寻找道路

题目描述 在有向图G 中，每条边的长度均为1 ，现给定起点和终点，请你在图中找一条从起点到终点的路径，该路径满足以下条件： 1 ．路径上的所有点的出边所指向的点...