poj3068
来源:互联网 发布:windows 8.1 64位 iso 编辑:程序博客网 时间:2024/05/19 15:21
链接:点击打开链接
题意:求两条从0~n-1的路径,使两条路径没有公共点并且变得权值的和最小,没有则输出Not possible
代码:
#include <vector>#include <stdio.h>#include <stdlib.h>#include <iostream>#include <algorithm>using namespace std;const int INF=0x3f3f3f3f;struct node{ int to,cap,cost,rev;};int V;int dis[10005],prevv[10005],preve[10005];vector<node> G[10005];void addedge(int from,int to,int cap,int cost){ G[from].push_back((node){to,cap,cost,G[to].size()}); G[to].push_back((node){from,0,-cost,G[from].size()-1});}int min_cost_flow(int s,int t,int f){ int i,d,v,ans,sign; ans=0; while(f>0){ fill(dis,dis+V,INF); dis[s]=0; sign=1; while(sign){ sign=0; for(v=0;v<V;v++){ if(dis[v]==INF) continue; for(i=0;i<G[v].size();i++){ node &e=G[v][i]; if(e.cap>0&&dis[e.to]>dis[v]+e.cost){ dis[e.to]=dis[v]+e.cost; prevv[e.to]=v; preve[e.to]=i; sign=1; } } } } //沿费用最短路增广 if(dis[t]==INF) return -1; d=f; for(v=t;v!=s;v=prevv[v]) d=min(d,G[prevv[v]][preve[v]].cap); //找出当前流量 f-=d; ans+=d*dis[t]; for(v=t;v!=s;v=prevv[v]){ node &e=G[prevv[v]][preve[v]]; e.cap-=d; G[v][e.rev].cap+=d; //加反向边 } } return ans;} //最小费用流模板 int main(){ int n,m,i,j,u,v,w,ans,cas; cas=1; while(scanf("%d%d",&n,&m)!=EOF&&(n||m)){ for(i=0;i<=n+1;i++) G[i].clear(); addedge(0,1,2,0); //源点与第一个点容量为2,费用为0 addedge(n,n+1,2,0); //汇点与最后一个点容量也为2,费用为0 while(m--){ scanf("%d%d%d",&u,&v,&w); addedge(u+1,v+1,1,w); //建容量为1,权值为边权的边 } V=n+2; ans=min_cost_flow(0,V-1,2); if(ans==-1) printf("Instance #%d: Not possible\n",cas++); else printf("Instance #%d: %d\n",cas++,ans); } return 0;}
0 0
- poj3068
- POJ3068 "Shortest" pair of paths
- poj3068 "Shortest" pair of paths 费用流
- POJ3068: "Shortest" pair of paths 题解
- POJ3068 "Shortest" pair of paths最小费用流
- POJ3068--"Shortest" pair of paths(最小费用流)
- Adobe CC 2015全系列产品下载 PS DW AE等 附注册机补丁
- 页面片段缓存----使用esi标签
- 八皇后问题
- [Spring实战系列](18)注解切面
- textarea 换行
- poj3068
- USACO-Section 4.1-PROB Beef McNuggets
- 关于隐藏Windows控制台的办法
- CView视图类
- iptables进行端口转发
- hdu 1239 Calling Extraterrestrial Intelligence Again
- java 原型设计模式
- HDU——1303Doubles(水题,试手二分查找)
- 【bzoj1295】[SCOI2009]最长距离 最短路