[原题](http://poj.org/problem?id=3068)

给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 。

//是不是像传纸条啊~

是否可行只要判断最后最大流是不是2就可以了

#include<cstdio>
#include<queue>
#include<cstring>
#define N 1010*1010
#define inf 0x3f3f3f3f
using namespace std;
int n,m,head[N],dis[N],cur[N],ans,cnt=2,s,t,ANS,T,cse;
queue <int> q;
bool vis[N];
struct hhh
{
int to,next,w,cost;
}edge[1000005]; int read()
{
int ans=0,fu=1;
char j=getchar();
for (;(j<'0' || j>'9') && j!='-';j=getchar()) ;
if (j=='-') fu=-1,j=getchar();
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
} void add(int u,int v,int w,int c)
{
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
edge[cnt].cost=c;
head[u]=cnt++;
} void addEdge(int u,int v,int w,int c)
{
add(u,v,w,c);
add(v,u,0,-c);
} bool bfs()
{
for (int i=s;i<=t;i++)
vis[i]=0,cur[i]=head[i],dis[i]=inf;
q.push(s);
dis[s]=0;
vis[s]=1;
while(!q.empty())
{
int r=q.front();
q.pop();
vis[r]=0;
for (int i=head[r],v;i;i=edge[i].next)
{
v=edge[i].to;
if (edge[i].w>0 && dis[r]+edge[i].cost<dis[v])
{
dis[v]=dis[r]+edge[i].cost;
if (!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
return dis[t]!=inf;
} int dfs(int x,int f)
{
if (x==t) return ANS+=f*dis[t],f;
int ha=0,now;
vis[x]=1;
for (int &i=cur[x],v;i;i=edge[i].next)
{
v=edge[i].to;
if (vis[v]) continue;
if (edge[i].w>0 && dis[v]==dis[x]+edge[i].cost)
{
now=dfs(v,min(f-ha,edge[i].w));
if (now)
{
ha+=now;
edge[i].w-=now;
edge[i^1].w+=now;
}
}
if (ha==f) return ha;
}
return ha;
} void init()
{
memset(head,0,sizeof(head));
cnt=2;
ANS=0;
ans=0;
} int main()
{
while (~scanf("%d%d",&n,&m))
{
if (!n && !m) break;
++cse;
printf("Instance #%d: ",cse);
s=0;
t=n-1;
init();
for (int i=1,a,b,c;i<=m;i++)
{
a=read();
b=read();
c=read();
addEdge(a,b,1,c);
}
while (bfs()) if (ans==2) break;else ans+=dfs(s,inf);
if (ans==2) printf("%d\n",ANS);
else printf("Not possible\n");
}
return 0;
}

[poj] 3068 "Shortest" pair of paths || 最小费用最大流的更多相关文章

  1. POJ 3068 "Shortest" pair of paths(费用流)

    [题目链接] http://poj.org/problem?id=3068 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ...

  2. poj 3068 "Shortest" pair of paths

    "Shortest" pair of paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1407 ...

  3. poj 2195 二分图带权匹配+最小费用最大流

    题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...

  4. 2018.06.27"Shortest" pair of paths(费用流)

    "Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...

  5. POJ3068 "Shortest" pair of paths 【费用流】

    POJ3068 "Shortest" pair of paths Description A chemical company has an unusual shortest pa ...

  6. TZOJ 4712 Double Shortest Paths(最小费用最大流)

    描述 Alice and Bob are walking in an ancient maze with a lot of caves and one-way passages connecting ...

  7. POJ 2195:Going Home(最小费用最大流)

    http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...

  8. POJ 2195 & HDU 1533 Going Home(最小费用最大流)

    这就是一道最小费用最大流问题 最大流就体现到每一个'm'都能找到一个'H',但是要在这个基础上面加一个费用,按照题意费用就是(横坐标之差的绝对值加上纵坐标之差的绝对值) 然后最小费用最大流模板就是再用 ...

  9. POJ 3422Kaka's Matrix Travels(最小费用最大流)

                                                            Kaka's Matrix Travels Time Limit: 1000MS   M ...

随机推荐

  1. python基础数据类型之字符串操作

    1.字符串切片ps:字符串是不可变的对象, 所以任何操作对原字符 是不会有任何影响的 s1 = "python最简洁" print(s1[0]) print(s1[1]) prin ...

  2. 【vlan-端口配置】

    搭建好拓扑图如下: 分别配置两台终端的ip地址 创建vlan把e0/4/0接口加入到新的vlan中 连通性失败 . 同理在把e0/4/1加入到vlan视图中 连通性成功 : 搭建好拓扑图如下 进入e0 ...

  3. 【转载】最长回文字符串(manacher算法)

    原文转载自:http://blog.csdn.net/lsjseu/article/details/9990539 偶然看见了人家的博客发现这么一个问题,研究了一下午, 才发现其中的奥妙.Stupid ...

  4. Redis 持久化操作

    hash类型 类比:mysql数据库存储数据 持久化操作 以本身的数据以文件形式保存到硬盘中 手动快照持久化 i 备份机制(频率) vi redis.conf save 900 1  900s如果一个 ...

  5. mysql 自定义不规则排序

    mysql按id的指定顺序进行排序,以前解决过一次,后来忘了,记录一下 SELECT * FROM `table` WHERE id<6 order by field(id,3,5,1,2,4)

  6. POJ 3254 状压DP(基础题)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17749   Accepted: 9342 Desc ...

  7. HDU 2222 AC自动机(模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  8. opencv中对图像的像素操作

    1.对灰度图像的像素操作: #include<iostream> #include<opencv2/opencv.hpp> using namespace std; using ...

  9. [KMP][BZOJ1355][Baltic2009]Radio Transmission

    题面 Description 给你一个字符串,它是由某个字符串不断自我连接形成的. 但是这个字符串是不确定的,现在只想知道它的最短长度是多少. Input 第一行给出字符串的长度,\(1 < L ...

  10. python向多个邮箱发邮件--注意接收是垃圾邮件

    群发邮件注意:三处标红的地方 # -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText from emai ...