[原题](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. MyString类的实现--基础中的基础C语言

    MyString 类是学习 C++ 的过程中一个很重要的例子,涉及到面向对象的封装.堆内存申请和释放.函数的重载以及 C++ 的 “Big Three”.本例子重点在于复习和理解上述的 C++ 特性, ...

  2. 微信小程序日期选择器

    /* JS代码部分 */ const date = new Date() const years = [] const months = [] const days = [] const hours ...

  3. Latex 使用笔记,取消目录

    不使用标准模板(如ieee或者acm的模板)的前提下: \usepackage{hyperref} \hypersetup{bookmarks={false}} 或者 \usepackage[book ...

  4. 在 publicId 和 systemId 之间需要有空格。

    spring  applicationContext_datasource.xml中约束错误 org.springframework.beans.factory.xml.XmlBeanDefiniti ...

  5. Mysql语句 AND 和 OR 的运用

    数据记录 查询方式1 -- 查询info=1或者info=2同时bookname=java的数据 AND bookname='java'; 查询方式2 -- 查询info=3同时bookname=ja ...

  6. Ubuntu 下安装GIMP

    1.Add GIMP PPA Open terminal from Unity Dash, App launcher, or via Ctrl+Alt+T shortcut key. When it ...

  7. html颜色实体符号表示汇总

    颜色的表示方法有许多种,列如black,#000000,rgb(0,0,0)都表示黑色.这三种表示方法分别为英文,十六进制,rgb格式.拥有下列颜色,足以使你的网页充满生机. 颜色名 十六进制颜色值 ...

  8. git push 时 fatal: Unable to create 'D:/phpStudy/WWW/green_tree/.git/index.lock': File exists.解决办法

    找到自己的项目,找到.git文件夹,进去把目标文件删除即可 或者使用rm -rf 命令(如果没有那个文件件或者文件,将隐藏文件打开就可以看到了)

  9. 百度MIP校验错误整理与解决方法

    MIP校验工具地址: https://www.mipengine.org/validator/validate 1.强制性标签缺失或错误 错误提示:line 1,col 1: 强制性标签'<sc ...

  10. 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 ...