TZOJ 4712 Double Shortest Paths(最小费用最大流)
描述
Alice and Bob are walking in an ancient maze with a lot of caves and one-way passages connecting them. They want to go from cave 1 to cave n. All the passages are difficult to pass. Passages are too small for two people to walk through simultaneously, and crossing a passage can make it even more difficult to pass for the next person. We define di as the difficulty of crossing passage i for the first time, and ai as the additional difficulty for the second time (e.g. the second person's difficulty is di+ai).
Your task is to find two (possibly identical) routes for Alice and Bob, so that their total difficulty is minimized.
For
example, in figure 1, the best solution is 1->2->4 for both Alice
and Bob, but in figure 2, it's better to use 1->2->4 for Alice
and 1->3->4 for Bob.
输入
There
will be at most 200 test cases. Each case begins with two integers n, m
(1<=n<=500, 1<=m<=2000), the number of caves and passages.
Each of the following m lines contains four integers u, v, di and ai
(1<=u,v<=n, 1<=di<=1000, 0<=ai<=1000). Note that there
can be multiple passages connecting the same pair of caves, and even
passages connecting a cave and itself.
输出
For each test case, print the case number and the minimal total difficulty.
样例输入
4 4
1 2 5 1
2 4 6 0
1 3 4 0
3 4 9 1
4 4
1 2 5 10
2 4 6 10
1 3 4 10
3 4 9 10
样例输出
Case 1: 23
Case 2: 24
题意
找两条从1到N的路使得总花费最小,一条路第一次走花费d,第二次走花费d+a
题解
每个点建两条边一条流量1花费d,一条流量1花费d+a
源点S=0,和1连一条边流量2花费0
汇点T=n+1,和n连一条边流量2花费0
然后跑一边最小费用最大流
代码
#include<bits/stdc++.h>
using namespace std; const int N=1e5+;
const int M=2e5+;
const int INF=0x3f3f3f3f; int FIR[N],FROM[M],TO[M],CAP[M],FLOW[M],COST[M],NEXT[M],tote;
int pre[N],dist[N],q[];
bool vis[N];
int n,m,S,T;
void init()
{
tote=;
memset(FIR,-,sizeof(FIR));
}
void addEdge(int u,int v,int cap,int cost)
{
FROM[tote]=u;
TO[tote]=v;
CAP[tote]=cap;
FLOW[tote]=;
COST[tote]=cost;
NEXT[tote]=FIR[u];
FIR[u]=tote++; FROM[tote]=v;
TO[tote]=u;
CAP[tote]=;
FLOW[tote]=;
COST[tote]=-cost;
NEXT[tote]=FIR[v];
FIR[v]=tote++;
}
bool SPFA(int s, int t)
{
memset(dist,INF,sizeof(dist));
memset(vis,false,sizeof(vis));
memset(pre,-,sizeof(pre));
dist[s] = ;vis[s]=true;q[]=s;
int head=,tail=;
while(head!=tail)
{
int u=q[++head];vis[u]=false;
for(int v=FIR[u];v!=-;v=NEXT[v])
{
if(dist[TO[v]]>dist[u]+COST[v]&&CAP[v]>FLOW[v])
{
dist[TO[v]]=dist[u]+COST[v];
pre[TO[v]]=v;
if(!vis[TO[v]])
{
vis[TO[v]] = true;
q[++tail]=TO[v];
}
}
}
}
return pre[t]!=-;
}
void MCMF(int s, int t, int &cost, int &flow)
{
flow=;
cost=;
while(SPFA(s,t))
{
int Min = INF;
for(int v=pre[t];v!=-;v=pre[TO[v^]])
Min = min(Min, CAP[v]-FLOW[v]);
for(int v=pre[t];v!=-;v=pre[TO[v^]])
{
FLOW[v]+=Min;
FLOW[v^]-=Min;
cost+=COST[v]*Min;
}
flow+=Min;
}
}
int main()
{
int ca=;
while(scanf("%d%d",&n,&m)!= EOF)
{
init();
for(int i=,u,v,d,a;i<m;i++)
{
scanf("%d%d%d%d",&u,&v,&d,&a);
addEdge(u,v,,d);
addEdge(u,v,,d+a);
}
S=,T=n+;
addEdge(S,,,);
addEdge(n,T,,);
int cost,flow;
MCMF(S,T,cost,flow);
printf("Case %d: %d\n",++ca,cost);
}
return ;
}
TZOJ 4712 Double Shortest Paths(最小费用最大流)的更多相关文章
- SGU 185.Two shortest (最小费用最大流)
时间限制:0.25s 空间限制:4M 题意: 在n(n<=400)个点的图中,找到并输出两条不想交的最短路.不存在输出“No sulotion”: Solution: 最小费用最大流 建图与po ...
- [poj] 3068 "Shortest" pair of paths || 最小费用最大流
[原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ...
- SGU185 Two shortest(最小费用最大流/最大流)
题目求一张图两条边不重复的最短路. 一开始我用费用流做. 源点到1连容量2费用0的边:所有边,连u到v和v到u容量1费用cost的边. 总共最多会增广两次,比较两次求得的费用,然后输出路径. 然而死M ...
- CSU 1506 Problem D: Double Shortest Paths(最小费用最大流)
题意:2个人从1走到n,假设一条路第一次走则是价值di,假设第二次还走这条路则须要价值di+ai,要你输出2个人到达终点的最小价值! 太水了!一条边建2次就OK了.第一次价值为di,第二次为ai+di ...
- TZOJ 1513 Farm Tour(最小费用最大流)
描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 &l ...
- CSU 1506 Double Shortest Paths
1506: Double Shortest Paths Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 49 Solved: 5 Description ...
- UVA 12821 Double Shortest Paths
Double Shortest PathsAlice and Bob are walking in an ancient maze with a lot of caves and one-way pa ...
- CSU 1506(最小费用最大流)
传送门:Double Shortest Paths 题意:有两个人:给出路径之间第一个人走所需要的费用和第二个人走所需要的费用(在第一个人所需的 费用上再加上第二次的费用):求两个人一共所需要的最小费 ...
- poj 2351 Farm Tour (最小费用最大流)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17230 Accepted: 6647 Descri ...
随机推荐
- chattr 改变文件、目录属性 (chmod、passwd等涉及文件修改的命令提示Operation not permitted)
与chmod这个命令相比,chmod只是改变文件的读写.执行权限,更底层的属性控制是由chattr来改变的. lsattr查看文件或目录属性 chattr命令的用法:chattr [ -RVf ] [ ...
- Python2 错误记录1File "<string>", line 1, in <module> NameError: name 'f' is not defined
Python 2下 count = 0 while count < 3: user = input('>>>') pwd = input('>>>') if ...
- Zeosdbo-Query使用
with DataModule1.Zlxz_zy_Query do begin Close; SQL.Clear; SQL.Add( ...
- js总结001
JSTL 表达式与 EL 语言 http://leon906998248.iteye.com/blog/1502569 2 jquery中$each()方法的使用指南 http: ...
- Android事件拦截机制 - 两句话
模拟情形:ViewGroupA ->ViewGroupB->View False往下走,True就停下.(适用于事件传递和事件处理)
- mysql 回顾
主键可以是一个或者是多个列,但所有的列(或者是列的组合)必须是唯一的,非空的 关键字distinct 可以去重,实现该效果还可以使用group by limit 默认从 0 开始,limit 5 其实 ...
- flume 详细介绍
http://blog.csdn.net/a2011480169/article/details/51544664 配有详细的例子. http://www.cnblogs.com/gongxijun/ ...
- 无法加载ISAPI 筛选器 当前配置只支持加载为 AMD64 处理器体系结构创建的映像
无法加载ISAPI 筛选器 当前配置只支持加载为 AMD64 处理器体系结构创建的映像 2011-11-9 0:18:49来源:本站原创作者:清晨320我要评论(0) 今天服务器的伪静态死活加载不上去 ...
- Python unindent dese not match any out indentation level 问题
今天写个小程序出现 “unindent dese not match any out indentation level”. 一直没找到原因,经过仔细对比发现实际上是缩进的问题. 上下两行的缩进用的 ...
- C# Excel转换为Json
demo:https://files.cnblogs.com/files/guxingy/Excel%E8%BD%AC%E6%8D%A2%E4%B8%BAJson%E5%AF%B9%E8%B1%A1. ...