zoj 2760 How Many Shortest Path
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
真tm得吐槽这个数据在我下午这么困的时候数据有这样的问题gg
注意当i,j相同的时候原题数据不一定给的是0 需要我们特判一下
首先floyd求下最短路 然后呢根据noip2017 day1t3的经验我们枚举一下每条边 然后验证他是否存在于最短路上 然后如果存在 因为只能经过一回 所以就连一条容量为1的边 跑最大流即可 注意输出inf是在如果起点和中点相同的时候输出inf注意看清题面 为什么直接最大流即可因为仔细想一想我最大流每次增广的一定是最短路 不可能存在其他情况 我曾经试想过 如果一个点左右两边分别都是一大一小两种权值 然后正好两种最短路均可 但是仔细想想这明明两个小的组合起来就是最优的情况啊我好菜啊%%%icefox orz
#include#include#include#include#define ll long long#define inf 0x3f3f3f3f#define N 110using namespace std;inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();} while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=getchar(); return x*f;}struct node{ int y,z,next,x;}data[N*N*2],ed[N*N];int num=1,S,T,h[N],level[N],mp1[N][N],cur[N],n,nm;inline void insert1(int x,int y,int z){ data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;data[num].x=x; data[++num].y=x;data[num].z=0;data[num].next=h[y];h[y]=num;data[num].x=y;}inline void insert2(int x,int y,int z){ ed[++nm].x=x;ed[nm].y=y;ed[nm].z=z;}inline bool bfs(){ queueq;q.push(S);memset(level,0,sizeof(level));level[S]=1; while(!q.empty()){ int x=q.front();q.pop(); for (int i=h[x];i;i=data[i].next){ int y=data[i].y,z=data[i].z; if(level[y]||!z) continue;level[y]=level[x]+1;if (y==T) return 1;q.push(y); } }return 0;}inline int dfs(int x,int s){ if (x==T) return s;int ss=s; for (int &i=cur[x];i;i=data[i].next){ int y=data[i].y,z=data[i].z; if (level[x]+1==level[y]&&z){ int xx=dfs(y,min(z,s));if (!xx) level[y]=0; s-=xx;data[i].z-=xx;data[i^1].z+=xx;if(!s) return ss; } }return ss-s;}int main(){// freopen("zoj2760.in","r",stdin); while(~scanf("%d",&n)){ memset(h,0,sizeof(h));num=1;nm=0; for (int i=0;i#include#include#include#include#define ll long long#define inf 0x3f3f3f3f#define N 110using namespace std;inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();} while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=getchar(); return x*f;}struct node{ int y,z,next,x;}data[N*N*2];int num=1,S,T,h[N],level[N],mp[N][N],mp1[N][N],cur[N],n;inline void insert1(int x,int y,int z){ data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;data[num].x=x; data[++num].y=x;data[num].z=0;data[num].next=h[y];h[y]=num;data[num].x=y;}inline bool bfs(){ queueq;q.push(S);memset(level,0,sizeof(level));level[S]=1; while(!q.empty()){ int x=q.front();q.pop(); for (int i=h[x];i;i=data[i].next){ int y=data[i].y,z=data[i].z; if(level[y]||!z) continue;level[y]=level[x]+1;if (y==T) return 1;q.push(y); } }return 0;}inline int dfs(int x,int s){ if (x==T) return s;int ss=s; for (int &i=cur[x];i;i=data[i].next){ int y=data[i].y,z=data[i].z; if (level[x]+1==level[y]&&z){ int xx=dfs(y,min(z,s));if (!xx) level[y]=0; s-=xx;data[i].z-=xx;data[i^1].z+=xx;if(!s) return ss; } }return ss-s;}int main(){ //freopen("zoj2760.in","r",stdin); while(~scanf("%d",&n)){ memset(h,0,sizeof(h));num=1; for (int i=0;i
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~