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 ...
随机推荐
- 代码混淆防止APP被反编译指南
本文来自网易云社区 安卓App安全包含很多内容,包括混淆代码.整体Dex加固.拆分 Dex 加固.虚拟机加固等方面.事实上,这些内容也是国内近几年Android App安全保护的一种主要趋势. 混淆代 ...
- ThinkPHP开启设置子域名笔记
一.ThinkPHP框架里 common下的config文件 'APP_SUB_DOMAIN_DEPLOY' => 1, // 开启子域名配置 'APP_SUB_DOMAIN_RULES' =& ...
- SpringMVC+mybatis+maven+Ehcache缓存实现
所谓缓存,就是将程序或系统经常要调用的对象存在内存中,以便其使用时可以快速调用,不必再去创建新的重复的实例.这样做可以减少系统开销,提高系统效率. 缓存主要可分为二大类: 一.通过文件缓存,顾名思义文 ...
- lesson 19 A very dear cat
lesson 19 A very dear cat dear adj. 亲爱的:尊敬的:昂贵的 表示几乎不,很少的词语 rarely hardly seldom scarcely flat = apa ...
- SqlServer的两种插入方式效率对比
protected void button1_Click(object sender, EventArgs e) { DataTable dtSource = new DataTable(); dtS ...
- lintcode 二叉树前序遍历
二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. / ...
- [Clr via C#读书笔记]Cp11事件
Cp11事件 类型之所以提供事件通知功能,是因为类型维护了一个已登记方法的列表,事件发生后,类型将通知列表登记的所有方法: 事件模型建立在委托的基础上.委托是调用回调方法的一种类型安全的方式. 设计事 ...
- 【Linux 运维】linux系统查看版本信息
查看linux系统版本信息: [root@kvm-host~]# cat /proc/version (Linux查看当前操作系统版本信息)Linux version 3.10.0-514 ...
- 如何在线测试Exchange的速度
最新碰到了客户需要比较国内版和国际版的Office365的速度问题,微软提供在线工具测试 这里以Exchange 测试为例子,请参考. PS Onenote贴过来只能至图片,各位看官只能将就了 这里有
- Notes of the scrum meeting(12.11)
meeting time:19:30~20:30p.m.,December 11th,2013 meeting place:3号公寓一层 attendees: 顾育豪 ...