【ZOJ2760】How Many Shortest Path

                                                                      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

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

 

解析:
       最大流。

       题目要求无边相交的不同的最短路径条数。

       首先跑最短路,对于一条边(u,v),如果有:

       dis[v]=dis[u]+len(u,v)

       则说明这条边在最短路上,就将边(u,v)加入新图中,容量为1,然后求最大流,就能保证每条边只能经过一次且答案最优。

 

代码:
 

#include <bits/stdc++.h>
using namespace std;

const int inf=1e9;
const int Max=100005;
int n,m,s,t,size=1,ans;
int f[105][105],mp[105][105],first[Max],tmp[Max],dep[Max];
struct shu{int to,next,len;};
shu edge[Max<<1];

inline void clean()
{
	size=1,ans=0;
	memset(f,0x3f,sizeof(f));
	memset(mp,0,sizeof(mp));
	memset(first,0,sizeof(first));
}

inline void build(int x,int y,int z)
{
	edge[++size].next=first[x],first[x]=size,edge[size].to=y,edge[size].len=z;
	edge[++size].next=first[y],first[y]=size,edge[size].to=x,edge[size].len=0;
}

inline void pre()
{
	for(int i=1;i<=n;i++)
	  for(int j=1;j<=n;j++)
	  {
	 	scanf("%d",&mp[i][j]);
	    if(~mp[i][j]) f[i][j]=mp[i][j];
	  }
	scanf("%d%d",&s,&t);s++,t++;
	if(s==t){puts("inf");return;}
	for(int i=1;i<=n;i++) f[i][i]=0;
	for(int k=1;k<=n;k++)
	  for(int i=1;i<=n;i++)
	    for(int j=1;j<=n;j++)
	      f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
	for(int i=1;i<=n;i++)
	  for(int j=1;j<=n;j++)
	    if(i!=j&&f[s][t]==f[s][i]+mp[i][j]+f[j][t]&&~mp[i][j]) build(i,j,1);
}

inline bool bfs()
{
	memset(dep,0,sizeof(dep));
	queue<int>q;q.push(s);dep[s]=1;
	while(q.size())
	{
	  int p=q.front();q.pop();
	  for(int u=first[p];u;u=edge[u].next)
	  {
	  	int to=edge[u].to;
	  	if(!dep[to]&&edge[u].len)
	  	{
	  	  dep[to]=dep[p]+1,q.push(to);
	  	  if(to==t) return 1;
		}
	  }
	}
	return 0;
}

inline int dinic(int p,int flow)
{
	if(p==t) return flow;
	int sum=0;
	for(int &u=tmp[p];u&&sum<flow;u=edge[u].next)
	{
	  int to=edge[u].to;
	  if(dep[to]==dep[p]+1&&edge[u].len)
	  {
	    int minn=dinic(to,min(flow-sum,edge[u].len));
	    if(!(sum-flow)) {dep[to]=0;break;}
	    edge[u].len-=minn,edge[u^1].len+=minn,sum+=minn;
	  }
	}
	return sum;
}

inline void solve()
{
	while(bfs())
	{
	  memcpy(tmp,first,sizeof(first));
	  ans+=dinic(s,inf);
	}
	printf("%d\n",ans);
}

int main()
{
	while(~scanf("%d",&n))
	{
	  clean();
	  pre();
	  if(s==t) continue;
	  solve();
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值