poj 3068 "Shortest" pair of paths
来源:互联网 发布:淘宝 废铁战士 编辑:程序博客网 时间:2024/06/11 12:37
题目连接:“Shortest” pair of paths
题目大意:给你一个有向图,有边权,现在需要你找出两条从0到n-1的路径并且两条路径无公共点而且需要使得边权和最小,不存在输出No possible,存在输出两条路径的边权和
题目思路:这题我们可以想到去跑网络流,然后看最大流是不是2,我只给2的流量从超级源点流出,为了方便起见,我们将所有点位置加1,变成求1到n的最小费用最大流,超级源点为0,连接1,容量为2,单位费用为0,然后图中给的边容量为1(因为不能有公共点),费用为题目给的边权,然后第n个点连接超级汇点n+1,容量为2,费用为0,然后跑最小费用最大流就好了,判断满流以确定是不是有这样的两条路径
#include <map>#include <set>#include <cmath>#include <queue>#include <stack>#include <vector>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;const int INF = 0x3f3f3f3f;const int MaxNode = 205;const int MaxEdge = 40005;struct Edge{ int to,vol,cost,next;}Edges[MaxEdge];int Pre[MaxNode],Path[MaxNode],Dist[MaxNode],Head[MaxNode],EdgeCount;void addedge(int u, int v, int vol, int cost){ Edges[EdgeCount].to = v; Edges[EdgeCount].vol = vol; Edges[EdgeCount].cost = cost; Edges[EdgeCount].next = Head[u]; Head[u] = EdgeCount++; Edges[EdgeCount].to = u; Edges[EdgeCount].vol = 0; Edges[EdgeCount].cost = -cost; Edges[EdgeCount].next = Head[v]; Head[v] = EdgeCount++;}bool Spfa(int s, int t){ memset(Dist, 0x3f3f3f3f, sizeof(Dist)); memset(Pre, -1, sizeof(Pre)); Dist[s] = 0; queue<int>Q; Q.push(s); while (!Q.empty()){ int u = Q.front(); Q.pop(); for (int e = Head[u]; e != -1; e = Edges[e].next){ int v = Edges[e].to; if (Edges[e].vol > 0 && Dist[v] > Dist[u] + Edges[e].cost){ Dist[v] = Dist[u] + Edges[e].cost; Pre[v] = u; Path[v] = e; Q.push(v); } } } return Pre[t] != -1;}int MCMF(int s, int t){ int cost = 0; int max_flow = 0; int u, v, e; while (Spfa(s, t)){ int f = INF; for (u = t; u != s; u = Pre[u]){ f = min(f, Edges[Path[u]].vol); } for (u = t; u != s; u = Pre[u]){ e = Path[u]; Edges[e].vol -= f; Edges[e^1].vol += f; } max_flow += f; cost += f*Dist[t]; } if(max_flow == 2) return cost; return -1;}void init(){ memset(Head,-1,sizeof(Head)); EdgeCount = 0;}int main(){ int n,m,Case = 1; while(~scanf("%d%d",&n,&m)&&n+m){ init(); int u,v,cost; while(m--){ scanf("%d%d%d",&u,&v,&cost); addedge(u+1,v+1,1,cost); } addedge(0,1,2,0); addedge(n,n+1,2,0); printf("Instance #%d: ",Case++); int cos = MCMF(0,n+1); if(cos == -1) puts("Not possible"); else printf("%d\n",cos); } return 0;}
阅读全文
0 0
- POJ-3068-"Shortest" pair of paths
- poj 3068 "Shortest" pair of paths
- "Shortest" pair of paths - POJ 3068 费用流
- POJ 3068 "Shortest" pair of paths 已翻译
- poj 3068 "Shortest" pair of paths(最小费用流)
- POJ3068 "Shortest" pair of paths
- POJ 3068 "Shortest" pair of paths(最小费用最大流-mcmf)
- Poj 3068 "Shortest" pair of paths【拆点+最小费用最大流】
- poj3068 "Shortest" pair of paths 费用流
- POJ3068: "Shortest" pair of paths 题解
- POJ3068 "Shortest" pair of paths最小费用流
- POJ3068--"Shortest" pair of paths(最小费用流)
- (Shortest Paths Algorithm)
- Double Shortest Paths CSU
- ABC 051D Candidates of No Shortest Paths 最短路(Floyd)
- Shortest paths and matrix multiplication
- uva 12821 Double Shortest Paths
- Double Shortest Paths 网络流
- android.view.View.getImportantForAccessibility()
- 官方Property Animation翻译(全部)以及批注
- scroll-view实现上拉加载(tab切换多页数据加载)
- Android常用自定义形状方式
- maptask 个数(并行度)决定机制
- poj 3068 "Shortest" pair of paths
- MySQL的命令学习
- Mentor Graphics FloEFD v16.2
- 欢迎使用CSDN-markdown编辑器
- 获取 url 中的参数
- getFilePath ZipOutputStream
- JAVA编程思想学习 --- 第二章 (一切都是对象)
- javaSSM框架详解
- Timer 控件中的Elapsed事件与tick事件的区别