POJ3068:"Shortest" pair of paths——题解
http://poj.org/problem?id=3068
题目大意:
从0~n-1找到两条边和点都不相同(除了0和n-1外)的最小费用路径。
————————————————————————————
POJ2135魔改版。
按照那题的思路并且把点拆成中间连一条容量为1的边即可。
切了切了。
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
typedef long long ll;
const int INF=1e9;
const int N=;
const int M=;
inline int read(){
int X=,w=;char ch=;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
struct node{
int nxt;
int to;
int w;
int b;
}edge[M];
int head[N],cnt=-;
void add(int u,int v,int w,int b){
cnt++;
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].b=b;
edge[cnt].nxt=head[u];
head[u]=cnt;
return;
}
int dis[N];
bool vis[N];
inline bool spfa(int s,int t,int n){
deque<int>q;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)dis[i]=INF;
dis[t]=;q.push_back(t);vis[t]=;
while(!q.empty()){
int u=q.front();
q.pop_front();vis[u]=;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
int b=edge[i].b;
if(edge[i^].w&&dis[v]>dis[u]-b){
dis[v]=dis[u]-b;
if(!vis[v]){
vis[v]=;
if(!q.empty()&&dis[v]<dis[q.front()]){
q.push_front(v);
}else{
q.push_back(v);
}
}
}
}
}
return dis[s]<INF;
}
int ans=;
int dfs(int u,int flow,int m){
if(u==m){
vis[m]=;
return flow;
}
int res=,delta;
vis[u]=;
for(int e=head[u];e!=-;e=edge[e].nxt){
int v=edge[e].to;
int b=edge[e].b;
if(!vis[v]&&edge[e].w&&dis[u]-b==dis[v]){
delta=dfs(v,min(edge[e].w,flow-res),m);
if(delta){
edge[e].w-=delta;
edge[e^].w+=delta;
res+=delta;
ans+=delta*b;
if(res==flow)break;
}
}
}
return res;
}
inline int costflow(int S,int T,int n){
int flow=;
while(spfa(S,T,n)){
do{
memset(vis,,sizeof(vis));
flow+=dfs(S,INF,T);
}while(vis[T]);
}
return flow;
}
void restart(){
memset(head,-,sizeof(head));
cnt=-;
ans=;
return;
}
int main(){
int ecnt=,n,m;
while(scanf("%d%d",&n,&m)!=EOF&&n&&m){
restart();
ecnt++;
int F=,E=n;
int S=n*+,T=S+;
add(S,F,,);add(F,S,,);
add(E,T,,);add(T,E,,);
for(int i=;i<=n-;i++){
add(i,i+n,,);
add(i+n,i,,);
}
for(int i=;i<=m;i++){
int u=read()+;
int v=read()+;
int b=read();
if(u!=&&u!=n)u+=n;
add(u,v,,b);
add(v,u,,-b);
}
int flow=costflow(S,T,T);
printf("Instance #%d: ",ecnt);
if(flow!=){
printf("Not possible\n");
}else printf("%d\n",ans);
}
return ;
}
POJ3068:"Shortest" pair of paths——题解的更多相关文章
- 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\) 条边,除第一个点和最后一个点外,其他所有的点都只能被经过一次,要求找到两条从第一个点 ...
- POJ3068 "Shortest" pair of paths
嘟嘟嘟 题目大意:一个有向图,每一条边有一个边权,求从节点\(0\)到\(n - 1\)的两条不经过同一条边的路径,并且边权和最小. 费用流板子题. 发个博客证明一下我写了这题. #include&l ...
- 2018.06.27"Shortest" pair of paths(费用流)
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...
- poj 3068 "Shortest" pair of paths
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1407 ...
- 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 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ...
- [poj] 3068 "Shortest" pair of paths || 最小费用最大流
[原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ...
- UVALIVE 2927 "Shortest" pair of paths
裸的费用流.一开始因为这句话还觉得要拆点 样例行不通不知道这句话干啥用的.Further, the company cannot place the two chemicals in same dep ...
随机推荐
- 【JUC源码解析】Exchanger
简介 Exchanger,并发工具类,用于线程间的数据交换. 使用 两个线程,两个缓冲区,一个线程往一个缓冲区里面填数据,另一个线程从另一个缓冲区里面取数据.当填数据的线程将缓冲区填满时,或者取数据的 ...
- Unity2017 经典游戏开发教程 算法分析与实现 (张帆 著)
https://meta.box.lenovo.com/link/view/82c451b41ce34e81a4b34cb46747d3d5 第1章 熟悉Unity软件的操作 第2章 打地鼠 (已看) ...
- SQL注入篇二------利用burp盲注,post注入,http头注入,利用burpsuit找注入点,宽字节注入
1.布尔盲注burpsuit的使用 先自己构造好注入语句,利用burpsuit抓包,设置变量,查出想要的信息. 比如----查数据库名的ascii码得到数据库构造好语句 http://123.206. ...
- python3读取csv文件
代码如下 import csv with open('D:\\abc\\userinfo.csv',newline='') as f: reader = csv.reader(f) for row i ...
- FastJson - 从HttpEntity到Json
在使用java + httpClient施行API自动化时,不可避免地遇到了如下问题: 1. 用Http Response数据做断言: 2. 用上一个请求的Response内容,作为下一个请求的参数: ...
- C 判断成绩是否及格
#include <stdio.h> int main(int argc, char **argv) { // 新建两个变量 pass代表及格分数的固定变量 score代表学生成绩的一个 ...
- 最全的Markdown语法
目录 Markdown语法 多级标题 引用与注释 插入代码 行内代码 代码段 图片 超链接 行内超链接 参数式超链接 字体 表格 分割线 多级列表 无序列表 有序列表 多选框 LaTeX公式 行内La ...
- metamask注记词
leaf orbit poet zebra toy day put dinosaur review cool pluck throw(m) 一个钱包地址 里面有多个账号 菲苾代表了不同网络
- ffmpeg实现mjpeg摄像头的采集-预览-拍照
摄像头输出是mjpeg格式的,需要实现在线预览功能,然后实现拍照功能 1.可以设置采集图像的分辨率,预览分辨率为640*480,可以自定义 2.ctrl+\ 拍照,ctrl+c 退出 void tes ...
- POJ 1815 Friendship(最大流最小割の字典序割点集)
Description In modern society, each person has his own friends. Since all the people are very busy, ...