[原题](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. zabbix服务端安装配置

    1.安装好httpd,mysql,php yum install httpd php mysql mysql-devel php-xmlwriter php-gd php-mbstring php-b ...

  2. python核心编程2 第五章 练习

    5-2 运算符(a) 写一个函数,计算并返回两个数的乘积(b) 写一段代码调用这个函数,并显示它的结果 def product(x, y): return x * y if __name__ == ' ...

  3. SpringBoot注入Mapper提示Could not autowire. No beans of 'xxxMapper' type found错误

    通过用Mabatis的逆向工程生成的Entity和Mapper.在Service层注入的时候一直提示Could not autowire. No beans of 'xxxMapper' type f ...

  4. java中的运算(2013-05-03-bd 写的日志迁移

    // ++自加 --自减 int a=9; a++; // a=a+1; System.out.println(a); // a=10 a--; // a=a-1 System.out.println ...

  5. 搭建一个简单的dns缓存服务器

    环境:linux 软件:bind97,bind97-utils, bind97-libs ip:192.168.192.130:192.168.192.131 -------------------- ...

  6. QToolBox学习笔记

    抽屉控件效果类似于QQ界面 最外面一层叫工具盒QToolBox QToolBox中装的是QGroupBox,分组的盒子 在分组的盒子QGroupBox中装的是QToolButton.

  7. 生成heap dump

    在查看内存泄露以及对内存问题中,要dump出当前内存堆存储快照,便于分析.有几种方法可以做,简介如下 一.intellij IDEA 由于我用的是intellij IDEA,所以没有介绍Eclipse ...

  8. 《Cracking the Coding Interview》——第14章:Java——题目6

    2014-04-26 19:11 题目:设计一个循环数组,使其支持高效率的循环移位.并能够使用foreach的方式访问. 解法:foreach不太清楚,循环移位我倒是实现了一个,用带有偏移量的数组实现 ...

  9. 打包成apk,生成apk文件,上传到网站服务器提供链接下载

    Android开发把项目打包成apk: 做完一个Android项目之后,如何才能把项目发布到Internet上供别人使用呢?我们需要将自己的程序打包成Android安装包文件--APK(Android ...

  10. USACO Section2.1 The Castle 解题报告

    castle解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...