2018.06.27"Shortest" pair of paths(费用流)
“Shortest” pair of paths
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1589 Accepted: 708
Description
A chemical company has an unusual shortest path problem.
There are N depots (vertices) where chemicals can be stored. There are M individual shipping methods (edges) connecting pairs of depots. Each individual shipping method has a cost. In the usual problem, the company would need to find a way to route a single shipment from the first depot (0) to the last (N - 1). That’s easy. The problem they have seems harder. They have to ship two chemicals from the first depot (0) to the last (N - 1). The chemicals are dangerous and cannot safely be placed together. The regulations say the company cannot use the same shipping method for both chemicals. Further, the company cannot place the two chemicals in same depot (for any length of time) without special storage handling — available only at the first and last depots. To begin, they need to know if it’s possible to ship both chemicals under these constraints. Next, they need to find the least cost of shipping both chemicals from first depot to the last depot. In brief, they need two completely separate paths (from the first depot to the last) where the overall cost of both is minimal.
Your program must simply determine the minimum cost or, if it’s not possible, conclusively state that the shipment cannot be made.
Input
The input will consist of multiple cases. The first line of each input will contain N and M where N is the number of depots and M is the number of individual shipping methods. You may assume that N is less than 64 and that M is less than 10000. The next M lines will contain three values, i, j, and v. Each line corresponds a single, unique shipping method. The values i and j are the indices of two depots, and v is the cost of getting from i to j. Note that these shipping methods are directed. If something can be shipped from i to j with cost 10, that says nothing about shipping from j to i. Also, there may be more than one way to ship between any pair of depots, and that may be important here.
A line containing two zeroes signals the end of data and should not be processed.
Output
follow the output format of sample output.
Sample Input
2 1
0 1 20
2 3
0 1 20
0 1 20
1 0 10
4 6
0 1 22
1 3 11
0 2 14
2 3 26
0 3 43
0 3 58
0 0
Sample Output
Instance #1: Not possible
Instance #2: 40
Instance #3: 73
Source
Southeastern Europe 2006
题意简述:给你一个网络,让你找两条从000号点到n−1n-1n−1号点的两条不走相同边的最短路,走重边算走不同的路。显然是一道费用流,我们将除000号点和n−1n-1n−1号点以外的每个点拆点,并连一条容量为111的边来限制经过的路径条数,然后再建立源点,汇点分别与000号点和n−1n-1n−1号点连容量为222的边来保证最多走两条路,这样跑费用流,若跑出来最大流<2<2<2,说明没有两条满足条件的路径,输出NotpossibleNot possibleNotpossible,否则输出最小费用即可。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#define N 1000
#define M 100000
#define inf 0x3f3f3f3f
using namespace std;
inline long long read(){
long long ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
return ans;
}
struct Node{int v,next,c,w;}e[M];
int pos[N],d[N],flow[N],first[N],pred[N],n,m,s,t,cnt,tot=0;
bool in[N];
inline void add(int u,int v,int c,int w){
e[++cnt].v=v;
e[cnt].w=w;
e[cnt].c=c;
e[cnt].next=first[u];
first[u]=cnt;
e[++cnt].v=u;
e[cnt].w=-w;
e[cnt].c=0;
e[cnt].next=first[v];
first[v]=cnt;
}
inline bool spfa(){
queue<int>q;
memset(flow,inf,sizeof(flow));
memset(d,inf,sizeof(d));
memset(in,false,sizeof(in));
q.push(s),in[s]=true,d[s]=0,pred[t]=-1;
while(!q.empty()){
int x=q.front();
q.pop();
in[x]=false;
for(int i=first[x];i!=-1;i=e[i].next){
int v=e[i].v;
if(e[i].c>0&&d[v]>d[x]+e[i].w){
d[v]=d[x]+e[i].w,pred[v]=x,pos[v]=i,flow[v]=min(flow[x],e[i].c);
if(!in[v])in[v]=true,q.push(v);
}
}
}
return pred[t]!=-1;
}
inline void solve(){
int maxf=0,maxw=0;
while(spfa()){
maxf+=flow[t],maxw+=flow[t]*d[t];
int now=t;
while(now!=s){
e[pos[now]].c-=flow[t];
e[pos[now]^1].c+=flow[t];
now=pred[now];
}
}
printf("Instance #%d: ",++tot);
if(maxf<2)printf("Not possible\n");
else printf("%d\n",maxw);
}
int main(){
while(1){
n=read(),m=read(),s=0,t=(n<<1|1);
if(!n&&!m)break;
memset(e,0,sizeof(e));
memset(first,-1,sizeof(first));
cnt=-1;
add(s,1,2,0),add(n+n,t,2,0),add(1,1+n,2,0),add(n,n+n,2,0);
for(int i=2;i<n;++i)add(i,i+n,1,0);
for(int i=1;i<=m;++i){
int u=read()+1,v=read()+1,w=read();
add(u+n,v,1,w);
}
solve();
}
return 0;
}
2018.06.27"Shortest" pair of paths(费用流)的更多相关文章
- POJ3068 "Shortest" pair of paths 【费用流】
POJ3068 "Shortest" pair of paths Description A chemical company has an unusual shortest pa ...
- poj 3068 "Shortest" pair of paths
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1407 ...
- "Shortest" pair of paths[题解]
"Shortest" pair of paths 题目大意 给出 \(n\) 个点,\(m\) 条边,除第一个点和最后一个点外,其他所有的点都只能被经过一次,要求找到两条从第一个点 ...
- POJ 3068 "Shortest" pair of paths(费用流)
[题目链接] http://poj.org/problem?id=3068 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ...
- POJ3068 "Shortest" pair of paths
嘟嘟嘟 题目大意:一个有向图,每一条边有一个边权,求从节点\(0\)到\(n - 1\)的两条不经过同一条边的路径,并且边权和最小. 费用流板子题. 发个博客证明一下我写了这题. #include&l ...
- UVALIVE 2927 "Shortest" pair of paths
裸的费用流.一开始因为这句话还觉得要拆点 样例行不通不知道这句话干啥用的.Further, the company cannot place the two chemicals in same dep ...
- 2018.10.13 bzoj1070: [SCOI2007]修车(费用流)
传送门 费用流经典题目. 自我感觉跟TheWindy′sThe Windy'sTheWindy′s很像. 利用费用提前计算的思想来建图就行了. 代码: #include<bits/stdc++. ...
- UVALive - 2927 "Shortest" pair of paths(最小费用最大流)题解
题意:有n个机器,机器之间有m条连线,我们需要判断机器0到n-1是否存在两条线路,存在输出最小费用. 思路:我们把0连接超级源点,n-1连接超级汇点,两者流量都设为2,其他流量设为1,那么只要最后我们 ...
- [poj] 3068 "Shortest" pair of paths || 最小费用最大流
[原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ...
随机推荐
- AD操作
加泪滴 批量添加覆铜过孔(先铺铜以后,再批量添加过孔) 开槽 在KEPP—OUT层 部分区域 不敷铜 开窗
- RxJS之AsyncSubject
AsyncSubject 是另一个 Subject 变体,只有当 Observable 执行完成时(执行 complete()),它才会将执行的最后一个值发送给观察者. import { Compon ...
- 100-days: Two
Title: London HIV patient's remission spurs hope for curing AIDS HIV 艾滋病毒 human immunodeficiency vi ...
- selenium验证码和错误截图
验证码的识别: 1,破解验证码 OCR识别(一般使用tesseract-ocr) 人工智能(AI机器学习 TensorFlow,成本大) 2,绕过验证码 1, 让开发人员临时关闭验证码 2,提供万能验 ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- java NIO Buffer 详解(1)
1.java.io 最为核心的概念是流(stream),面向流的编程,要么输入流要么输出流,二者不可兼具: 2.java.nio 中拥有3个核心概念: Selector Channel, Buffe ...
- xml转Map,对象,Map转xml,inputs tram 转xml 字符串的工具类方法
众所周知,大家在微信开发工程中,由于微信开发文档中,对于消息的接收发送都是基础xml数据的(太坑了),所以我们需要对XML进行解析转换: 1.我们先引入所需要的依赖 dom4j (解析xml的),xs ...
- android显示通知栏Notification以及自定义Notification的View
遇到的最大的问题是监听不到用户清除通知栏的广播.所以是不能监听到的. 自定义通知栏的View,然后service运行时更改notification的信息. /** * Show a notificat ...
- Dockerfile里指定执行命令用ENTRYPOING和用CMD有何不同?
结论:运行时机不太一样. RUN是在Build时运行的,先于CMD和ENTRYPOINT.Build完成了,RUN也运行完成后,再运行CMD或者ENTRYPOINT. ENTRYPOINT和CMD的不 ...
- 同一台主机部署两个比特币钱包以及rpc服务的摘要
.bitcoin QA Test环境 启动指定参数: "C:\Program Files (x86)\Bitcoin\bitcoin-qt.exe" -testnet -serve ...