【HDU 4940】Destroy Transportation system(无源无汇带上下界可行流)
Description
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
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
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
In first sample, for any set S, X=2, Y=4. In second sample. S= {1}, T= {2, 3}, X=10, Y=4.
Source
每个点单独作为S集合时,如果存在满足Y<X,就输出unhappy。否则输出happy。
为什么错呢?输出happy时,即两个点单独作为S时,都有Y1>=X1,Y2>=X2,如果两个点之间没有边,它们一起作为S时,X=X1+X2,Y=Y1+Y2,则Y>=X;但是如果两个点有边相连,X=X1+X2-(S1到S2的D)-(S2到S1的D),Y=Y1+Y2-(S1到S2的D+B)-(S2到S1的D+B),那么Y+B12+B21>=X,就有可能是Y<X了。当输入
1
4 5
1 4 10 2
4 1 6 2
2 1 1 1
4 3 6 2
3 2 4 2
时,输出的是happy。可是实际上,选择1、4节点,Y=2,X=8,显然是unhappy。
附上非正解代码:(AC了只能说明题目数据太水了)
#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=;cas<=t;cas++){
memset(x,,sizeof x);
memset(y,,sizeof y);
printf("Case #%d: ",cas);
int n,m;
sf(n);
sf(m);
for(int i=;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=;
for(int i=;i<=n;i++)
if(x[i]>y[i])ok=;
if(ok)puts("happy");
else puts("unhappy");
}
}
正解是无源无汇带上下界判断是否有可行流。
求s到t的最大流,如果源点汇点连接的边全部满流则有可行解。
参考国家集训队论文《一种简易的方法求解流量有上下界的网络中网络流问题》
#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 head[N],cnt;
int gap[N],dep[N],cur[N];
void init(){
cnt=;
memset(head, -, sizeof head);
}
void add(int u,int v,int w,int rw=){
e[cnt]=(edge){v,head[u],w,};
head[u]=cnt++;
e[cnt]=(edge){u,head[v],rw,};
head[v]=cnt++;
}
int q[N];
void bfs(int st,int ed){
memset(dep,-,sizeof dep);
memset(gap,,sizeof gap);
gap[]=;
int front=,rear=;
dep[ed]=;
q[rear++]=ed;
while(front!=rear){
int u=q[front++];
for(int i=head[u];~i;i=e[i].next){
int v=e[i].to;
if(dep[v]!=-)continue;
q[rear++]=v;
dep[v]=dep[u]+;
gap[dep[v]]++;
}
}
}
int s[N];
int sap(int st,int ed,int n){
bfs(st,ed);
memcpy(cur,head,sizeof head);
int top=;
int u=st;
int ans=;
while(dep[st]<n){
if(u==ed){
int Min=inf;
int inser;
for(int i=;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=;i<top;i++){
e[s[i]].flow+=Min;
e[s[i]^].flow-=Min;
}
ans+=Min;
top=inser;
u=e[s[top]^].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]+==dep[u]){
flag=true;
cur[u]=i;
break;
}
}
if(flag){
s[top++]=cur[u];
u=v;
continue;
}
int Min=n;
for(int i=head[u];~i;i=e[i].next)
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+]++;
if(u!=st)u=e[s[--top]^].to;
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
for(int cas=;cas<=t;cas++){
printf("Case #%d: ",cas);
int n,m;
scanf("%d%d",&n,&m);
int in[N];
int st=,ed=n+;
memset(in,,sizeof in);
init();
for(int i=;i<=m;i++){
int u,v,d,b;
scanf("%d%d%d%d",&u,&v,&d,&b);
add(u,v,b);
in[v]+=d;
in[u]-=d;
}
int need=;
for(int i=;i<=n;i++){
if(in[i]>){
add(st,i,in[i]);
need+=in[i];
}
else add(i,ed,-in[i]);
}
int ans=sap(st, ed, ed+);
if(need==ans)puts("happy");
else puts("unhappy");
}
}
【HDU 4940】Destroy Transportation system(无源无汇带上下界可行流)的更多相关文章
- hdu 4940 Destroy Transportation system( 无源汇上下界网络流的可行流推断 )
题意:有n个点和m条有向边构成的网络.每条边有两个花费: d:毁坏这条边的花费 b:重建一条双向边的花费 寻找这样两个点集,使得点集s到点集t满足 毁坏全部S到T的路径的费用和 > 毁坏全部T到 ...
- HDU 4940 Destroy Transportation system(无源汇上下界网络流)
Problem Description Tom is a commander, his task is destroying his enemy’s transportation system. Le ...
- SGU 194 Reactor Cooling 无源汇带上下界可行流
Reactor Cooling time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard output ...
- POJ 2396 有源有汇有上下界可行流问题
题意:给一个矩阵,给出每行每列之和,附加一些条件,如第i行第j列数必需大于(小于)多少. 思路题解:矩阵模型,模拟网络流,行.列标号为结点,构图,附加s,t,s连行标(容量上下限每行之和(必需以这个 ...
- HDU 4940 Destroy Transportation system(2014 Multi-University Training Contest 7)
思路:无源汇有上下界可行流判定, 原来每条边转化成 下界为D 上界为 D+B ,判断是否存在可行流即可. 为什么呢? 如果存在可行流 那么说明对于任意的 S 集合流出的肯定等于 流入的, ...
- [ACdream 1211 Reactor Cooling]无源无汇有上下界的可行流
题意:无源无汇有上下界的可行流 模型 思路:首先将所有边的容量设为上界减去下界,然后对一个点i,设i的所有入边的下界和为to[i],所有出边的下界和为from[i],令它们的差为dif[i]=to[i ...
- LOJ [#115. 无源汇有上下界可行流](https://loj.ac/problem/115)
#115. 无源汇有上下界可行流 先扔个板子,上下界的东西一点点搞,写在奇怪的合集里面 Code: #include <cstdio> #include <cstring> # ...
- 2018.08.20 loj#115. 无源汇有上下界可行流(模板)
传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源 ...
- [loj#115] 无源汇有上下界可行流 网络流
#115. 无源汇有上下界可行流 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据 题 ...
随机推荐
- Emacs杂谈(一)Emacs环境 c++ 快捷键
最近头脑发热(抽),重装了电脑,改成linux的ubuntu系统,熟悉一下环境,顺便转载相关emacs知识. //插播一则通知:似乎linux上vector不能用,会内存炸错,若有人可以解答,请用评论 ...
- MVC5 条件查询异步刷新
学校要做一个数据查询,按条件并且能下载 20160312 使用HTML.ajax异步刷新 视图层 @Html.Partial("_men") <div class=&quo ...
- 使用SQL如何把用逗号等字符隔开的字符串转换成列表(转)
如何把用逗号等字符隔开的字符串转换成列表,下面依逗号分隔符为例: 比如有一个字符串,其值为:香港,张家港,北京,上海用SQL把这个字符串转换成列表的方法是: 1.方法一 WITH A AS (SELE ...
- TFS命令tf:undo(强制签入签出文件)
由于修改计算机名称或不同电脑上操作忘记签入,则需要强制签入文件 具体步骤如下: 1.在命令行中输入"cd C:\Program Files\Microsoft Visual Studio ...
- FFT入门
这篇文章会讲讲FFT的原理和代码. 先贴picks博客(又名FFT从入门到精通):http://picks.logdown.com/posts/177631-fast-fourier-transfor ...
- PAT 1029. 旧键盘(20)
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及实际被输入的文字,请你列出肯定坏掉的那些键. 输入格式: 输入在2行中分别给出应该输入的文字.以及实际 ...
- T138
这一列车. 十年前送我去西安, 十年后搭我返故乡. 十年前手拉着手儿, 十年后独对着车窗. 这一列车. 装饰着坚毅的中国蓝, 却失去了往日光环. 只有通往偏远.落后的地方, 只有没赶上高铁动车的行 ...
- 数据库 SQL语法二
聚合函数 -SUM([DISTINCT] FIELDNAME) 求指定列之和,[DISTINCT]选项表示剔除重复记录 例如:SELECT SUM(age) FROM TABLE1; SELECT S ...
- Spring 依赖注入方式详解
平常的Java开发中,程序员在某个类中需要依赖其它类的方法. 通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由 ...
- Scala之OOP
/** * 1,在Scala中定义类是用class关键字: * 2,可以使用new ClassName的方式构建出类的对象: * 3, 如果名称相同,则object中的内容都是class的静态内容,也 ...