ZOJ 2760 How Many Shortest Path 边不相交最短路条数
2014-08-13 16:57
323 查看
How Many Shortest Path
Time Limit: 10 Seconds Memory Limit: 32768 KB
Given a weighted directed graph, we define the shortest path as the path who has the smallest length among all the path connecting the source vertex to the target vertex. And if two path
is said to be non-overlapping, it means that the two path has no common edge. So, given a weighted directed graph, a source vertex and a target vertex, we are interested in how many non-overlapping shortest path could we find out at most.
Input
Input consists of multiple test cases. The first line of each test case, there is an integer number N (1<=N<=100), which is the number of the vertices. Then follows an N * N matrix, represents
the directed graph. Each element of the matrix is either non-negative integer, denotes the length of the edge, or -1, which means there is no edge. At the last, the test case ends with two integer numbers S and T (0<=S, T<=N-1), that is, the starting and ending
points. Process to the end of the file.
Output
For each test case, output one line, the number of the the non-overlapping shortest path that we can find at most, or "inf" (without quote), if the starting point meets with the ending.
Sample Input
Sample Output
Author: SHEN, Guanghao
Source: ZOJ Monthly, September 2006
求不相交的最短路条数,可以把最短路上的边加到网络里,且容量为1,这样边就不会相交了
之后再枚举d[ss][i]+mp[i][j]+d[j][tt]==d[ss][tt] 连i,j建图 最大流即为答案
数据很坑。。
Time Limit: 10 Seconds Memory Limit: 32768 KB
Given a weighted directed graph, we define the shortest path as the path who has the smallest length among all the path connecting the source vertex to the target vertex. And if two path
is said to be non-overlapping, it means that the two path has no common edge. So, given a weighted directed graph, a source vertex and a target vertex, we are interested in how many non-overlapping shortest path could we find out at most.
Input
Input consists of multiple test cases. The first line of each test case, there is an integer number N (1<=N<=100), which is the number of the vertices. Then follows an N * N matrix, represents
the directed graph. Each element of the matrix is either non-negative integer, denotes the length of the edge, or -1, which means there is no edge. At the last, the test case ends with two integer numbers S and T (0<=S, T<=N-1), that is, the starting and ending
points. Process to the end of the file.
Output
For each test case, output one line, the number of the the non-overlapping shortest path that we can find at most, or "inf" (without quote), if the starting point meets with the ending.
Sample Input
4 0 1 1 -1 -1 0 1 1 -1 -1 0 1 -1 -1 -1 0 0 3 5 0 1 1 -1 -1 -1 0 1 1 -1 -1 -1 0 1 -1 -1 -1 -1 0 1 -1 -1 -1 -1 0 0 4
Sample Output
2 1
Author: SHEN, Guanghao
Source: ZOJ Monthly, September 2006
求不相交的最短路条数,可以把最短路上的边加到网络里,且容量为1,这样边就不会相交了
之后再枚举d[ss][i]+mp[i][j]+d[j][tt]==d[ss][tt] 连i,j建图 最大流即为答案
数据很坑。。
#include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <iostream> #include <sstream> #include <map> #include <set> #include <queue> #include <stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> using namespace std; #define PB push_back #define MP make_pair #define CLR(vis) memset(vis,0,sizeof(vis)) #define MST(vis,pos) memset(vis,pos,sizeof(vis)) #define MAX3(a,b,c) max(a,max(b,c)) #define MAX4(a,b,c,d) max(max(a,b),max(c,d)) #define MIN3(a,b,c) min(a,min(b,c)) #define MIN4(a,b,c,d) min(min(a,b),min(c,d)) #define PI acos(-1.0) #define INF 0x7FFFFFFF #define LINF 1000000000000000000LL #define eps 1e-8 typedef long long ll; typedef unsigned long long ull; int n; int d[222][222],mp[222][222]; const int mm=222222; const int mn=222; int node,s,t,edge,max_flow; int ver[mm],cap[mm],flow[mm],next[mm]; int head[mn],work[mn],dis[mn],q[mn]; inline void init(int _node,int _s,int _t) { node=_node, s=_s, t=_t; for(int i=0;i<node;++i) head[i]=-1; edge=max_flow=0; } inline void addedge(int u,int v,int c) { ver[edge]=v,cap[edge]=c,flow[edge]=0,next[edge]=head[u],head[u]=edge++; ver[edge]=u,cap[edge]=0,flow[edge]=0,next[edge]=head[v],head[v]=edge++; } bool Dinic_bfs() { int i,u,v,l,r=0; for(i=0;i<node;++i) dis[i]=-1; dis[ q[r++]=s ] = 0; for(l=0;l<r;l++) { for(i=head[ u=q[l] ]; ~i ;i=next[i]) if(flow[i]<cap[i] && dis[ v=ver[i] ]<0) { dis[ q[r++]=v ]=dis[u]+1; if(v==t) return 1; } } return 0; } int Dinic_dfs(int u,int exp) { if(u==t) return exp; for(int &i=work[u],v,temp; ~i ;i=next[i]) { if(flow[i]<cap[i] && dis[ v=ver[i] ]==dis[u]+1 && ( temp=Dinic_dfs(v,min(exp,cap[i]-flow[i])) )>0) { flow[i]+=temp; flow[i^1]-=temp; return temp; } } return 0; } int Dinic_flow() { int res,i; while(Dinic_bfs()) { for(i=0;i<node;++i) work[i]=head[i]; while( ( res=Dinic_dfs(s,INF) ) ) max_flow+=res; } return max_flow; } void floyd(int n) { int i,j,k; for(k=0;k<n;k++) for(i=0;i<n;i++) if(d[i][k]<INF) for(j=0;j<n;j++) if(d[k][j]<INF) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } int main() { while(scanf("%d",&n)!=EOF) { int ss,tt; int w; CLR(dis),CLR(mp); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { scanf("%d",&w); if(w<0) mp[i][j]=INF; if(w>=0) mp[i][j]=w; if(i==j) mp[i][j]=0; d[i][j]=mp[i][j]; } } scanf("%d%d",&ss,&tt); if(ss!=tt) { floyd(n); init(n,ss,tt); for(int i=0;i<n;i++) { if(d[ss][i]<INF) { for(int j=0;j<n;j++) { if(d[j][tt]<INF && mp[i][j]<INF && d[ss][i]+mp[i][j]+d[j][tt]==d[ss][tt]) addedge(i,j,1); } } } printf("%d\n",Dinic_flow()); } else printf("inf\n"); } return 0; }
相关文章推荐
- zoj How Many Shortest Path 网络流 不相交最短路条数
- zoj 2760 How Many Shortest Path 【最短路 + 最大流】 【求边不重复最短路径条数】
- ZOJ 2760--How Many Shortest Path【dinic最大流 && 最短路构图 && 求最短路径的条数】
- ZOJ 2760 How Many Shortest Path 最短路+最大流
- ZOJ 2760 How Many Shortest Path (不相交的最短路径个数)
- ZOJ 2760 How Many Shortest Path (不相交的最短路径个数)
- zoj 2760 How Many Shortest Path(Floyd+枚举判边是否在最短路上+最大流)
- ZOJ 2760 - How Many Shortest Path(最短路的个数) Floyd+最大流
- ZOJ 2760 How Many Shortest Path 最大流+floyd求最短路
- ZOJ 2760 How Many Shortest Path 最短路+最大流
- 解题报告 之 ZOJ2760 How Many Shortest Path
- ZOJ 2760 -- How Many Shortest Path
- ZOJ 2760 How Many Shortest Path(floyd + 最大流)
- ZOJ 2760 How Many Shortest Path 网络最大流
- ZOJ 2760 How Many Shortest Path(Dijistra + ISAP 最大流)
- ZOJ 2760 How Many Shortest Path
- 【zoj2760】【最大流】How Many Shortest Path
- ZOJ 2760 How Many Shortest Path
- ZOJ2760_How Many Shortest Path
- ZOJ 2760 How Many Shortest Path