POJ 3068 "Shortest" pair of paths(费用流)
【题目链接】 http://poj.org/problem?id=3068
【题目大意】
给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过
每条路都有一定的费用,求最小费用
【题解】
题目等价于求两条无交叉最短路,可用流量为2的费用流求解
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int INF=0x3f3f3f3f;
struct edge{int to,cap,cost,rev;};
const int MAX_V=10000;
int V,dist[MAX_V],prevv[MAX_V],preve[MAX_V];
vector<edge> G[MAX_V];
void add_edge(int from,int to,int cap,int cost){
G[from].push_back((edge){to,cap,cost,G[to].size()});
G[to].push_back((edge){from,0,-cost,G[from].size()-1});
}
int min_cost_flow(int s,int t,int f){
int res=0;
while(f>0){
fill(dist,dist+V,INF);
dist[s]=0;
bool update=1;
while(update){
update=0;
for(int v=0;v<V;v++){
if(dist[v]==INF)continue;
for(int i=0;i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>0&&dist[e.to]>dist[v]+e.cost){
dist[e.to]=dist[v]+e.cost;
prevv[e.to]=v;
preve[e.to]=i;
update=1;
}
}
}
}
if(dist[t]==INF)return -1;
int d=f;
for(int v=t;v!=s;v=prevv[v]){
d=min(d,G[prevv[v]][preve[v]].cap);
}f-=d;
res+=d*dist[t];
for(int v=t;v!=s;v=prevv[v]){
edge &e=G[prevv[v]][preve[v]];
e.cap-=d;
G[v][e.rev].cap+=d;
}
}return res;
}
void clear(){for(int i=0;i<V;i++)G[i].clear();}
int N,M;
int cas=0;
void solve(){
int s=N,t=s+1;
V=t+1; clear();
add_edge(s,0,2,0);
add_edge(N-1,t,2,0);
for(int i=0;i<M;i++){
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
add_edge(u,v,1,cost);
}
int ans=min_cost_flow(s,t,2);
printf("Instance #%d: ", ++cas);
if(ans==-1)puts("Not possible");
else printf("%d\n",ans);
}
int main(){
while(~scanf("%d%d",&N,&M),N&&M){
solve();
}return 0;
}
POJ 3068 "Shortest" pair of paths(费用流)的更多相关文章
- poj 3068 "Shortest" pair of paths
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1407 ...
- [poj] 3068 "Shortest" pair of paths || 最小费用最大流
[原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ...
- 2018.06.27"Shortest" pair of paths(费用流)
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...
- POJ3068 "Shortest" pair of paths 【费用流】
POJ3068 "Shortest" pair of paths Description A chemical company has an unusual shortest pa ...
- "Shortest" pair of paths[题解]
"Shortest" pair of paths 题目大意 给出 \(n\) 个点,\(m\) 条边,除第一个点和最后一个点外,其他所有的点都只能被经过一次,要求找到两条从第一个点 ...
- POJ 3686 The Windy's (费用流)
[题目链接] http://poj.org/problem?id=3686 [题目大意] 每个工厂对于每种玩具的加工时间都是不同的, 并且在加工完一种玩具之后才能加工另一种,现在求加工完每种玩具的平均 ...
- poj 3422 Kaka's Matrix Travels 费用流
题目链接 给一个n*n的矩阵, 从左上角出发, 走到右下角, 然后在返回左上角,这样算两次. 一共重复k次, 每个格子有值, 问能够取得的最大值是多少, 一个格子的值只能取一次, 取完后变为0. 费用 ...
- POJ 2677 旅行商问题 双调dp或者费用流
Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3408 Accepted: 1513 Description ...
- POJ3068 "Shortest" pair of paths
嘟嘟嘟 题目大意:一个有向图,每一条边有一个边权,求从节点\(0\)到\(n - 1\)的两条不经过同一条边的路径,并且边权和最小. 费用流板子题. 发个博客证明一下我写了这题. #include&l ...
随机推荐
- C语言指针大杂烩
By francis_hao Oct 31,2016 指针数组和数组指针 指针数组本身是个数组,数组的内容是指针.形如char *pa[].由于[]优先级高于*,pa先于[]结合表示pa是一个数组,p ...
- yaf学习网站
http://www.01happy.com/php-yaf-ext-business/
- Sencha Touch MVC 中 store 的使用
I have a UserStore that I want to load after succesful login of a user. I can't get this to work i.e ...
- BootStrap弹出框插件popover简单实例
1.网上实例地址 http://www.runoob.com/bootstrap/bootstrap-popover-plugin.html 2.具体demo $("#pieId&q ...
- codeforce C. Okabe and Boxes
题目传送门 这道题 每次删除一个点 那么这个点必然在栈里面 那么如果堆顶不是他 我们就需要一次操作使得堆合理 这时我们可以把他删除然后把他下面的点打个标记表示这下面的点以后想怎么排就怎么排以后都不需要 ...
- [POJ2774]Long Long Message 解题报告
Long Long Message Description The little cat is majoring in physics in the capital of Byterland. A p ...
- javascript学习3
javascript数据类型.函数传参 javascript语言核心:ECMAScript 定义js的语法:基本对象.数据类型 js的数据类型 typeof运算符 判断数据类型 字符串.数字.布尔. ...
- 【洛谷 P3304】[SDOI2013]直径(树的直径)
题目链接 题意,求一棵树被所有直径经过的边的条数. 这题是我们8.25KS图论的最后一题,当时我果断打了暴力求所有直径然后树上差分统计的方法,好像有点小问题,boom0了. 考完改这题,改了好久,各种 ...
- TLS回调函数
@author: dlive TLS (Thread Local Storage 线程局部存储 )回调函数常用于反调试. TLS回调函数的调用运行要先于EP代码执行,该特性使它可以作为一种反调试技术使 ...
- UVA 10385 Duathlon
Problem HDuathlonInput: standard inputOutput: standard outputTime Limit: 15 seconds A duathlon is a ...