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
//
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=410;
const int M=50000;
const int inf=(1<<28);
int head[N];
struct Edge
{
int v,next,w;
int pw;//原图中u->v的边权
} edge[M];
int cnt,n,s,t;//n从0开始 0->n-1
void addedge(int u,int v,int w)
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].pw=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].v=u;
edge[cnt].w=0;
edge[cnt].pw=w;
edge[cnt].next=head[v];
head[v]=cnt++;
}
int sap()
{
int pre[N],cur[N],dis[N],gap[N];
int flow=0,aug=inf,u;
bool flag;
for(int i=0; i<n; i++)
{
cur[i]=head[i];
gap[i]=dis[i]=0;
}
gap[s]=n;
u=pre[s]=s;
while(dis[s]<n)
{
flag=0;
for(int &j=cur[u]; j!=-1; j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w>0&&dis[u]==dis[v]+1)
{
flag=1;
if(edge[j].w<aug) aug=edge[j].w;
pre[v]=u;
u=v;
if(u==t)
{
flow+=aug;
while(u!=s)
{
u=pre[u];
edge[cur[u]].w-=aug;
edge[cur[u]^1].w+=aug;
}
aug=inf;
}
break;
}
}
if(flag) continue;
int mindis=n;
for(int j=head[u]; j!=-1; j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w>0&&dis[v]<mindis)
{
mindis=dis[v];
cur[u]=j;
}
}
if((--gap[dis[u]])==0)
break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return flow;
}
//初始化 cnt=0;memset(head,-1,sizeof(head));
int a[200][200],b[200][200];
int main()
{
while(scanf("%d",&n)==1)
{
for(int i=0;i<n;i++) for(int j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==-1) a[i][j]=inf;
b[i][j]=a[i][j];
}
for(int i=0;i<n;i++) a[i][i]=b[i][i]=0;
scanf("%d%d",&s,&t);
if(s==t)
{
printf("inf\n");continue;
}
for(int k=0;k<n;k++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]>a[i][k]+a[k][j]) a[i][j]=a[i][k]+a[k][j];
}
}
}
cnt=0;
memset(head,-1,sizeof(head));
for(int i=0;i<n;i++)
{
if(a[s][i]==inf) continue;//important
for(int j=0;j<n;j++)
{
//b[i][j]!=inf&&a[j][t]!=inf important
if(b[i][j]!=inf&&a[j][t]!=inf&&a[s][t]==a[s][i]+b[i][j]+a[j][t])
addedge(i,j,1);
}
}
int ans=sap();
printf("%d\n",ans);
}
return 0;
}