题面
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
题目链接
参考链接
最小割定理:百科百度 最大流问题 最小割如同瓶颈一般,即使是最大流也无法超过最小割。
Ford-Fulkerson 方法——最大流问题 Author:廖少少
网络流【最大流&&最小割&&费用流】——一篇简单易懂的博文 Author: XSamsara
模板:最大流问题的几种经典解法综述 Author: qiucz
题解:「zoj2760」How Many Shortest Path
题目简述
求相互不重叠的最短路有多少条。
程序
/**
初学网络流
https://www.jianshu.com/p/efb2d79e2b0f
https://www.cnblogs.com/qiucz/p/4601241.html
http://hzwer.com/5855.html
**/
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define inf 0x3f3f3f3f
#define maxn 105
int n,S,T;
int cnt;
int MAP[maxn][maxn];
int dis[maxn][maxn];
int last[maxn],h[maxn],q[maxn],cur[maxn];
struct Edge
{
int to,next,v;
Edge(int T,int N,int V):to(T),next(N),v(V){}
Edge(){}
void change(int T,int N,int V)
{
to=T;
next=N;
v=V;
}
}e[maxn*maxn*2];
void Insert(int u,int v,int w)//前向星加入边
{
++cnt;
e[cnt].change(v,last[u],w);
last[u]=cnt;
++cnt;
e[cnt].change(u,last[v],0);
last[v]=cnt;
}
bool bfs()
{
int head=0,tail=1;
memset(h,-1,sizeof(h));
q[0]=S;
h[S]=0;
while(head!=tail)
{
int now=q[head];
head++;
for(int i=last[now];i;i=e[i].next)
if(e[i].v&&h[e[i].to]==-1)//未饱和,e[i].to未访问过
{
h[e[i].to]=h[now]+1;//bfs树搜索层数
q[tail++]=e[i].to;
}
}
return h[T]!=-1;//终点是否可达
}
int dfs(int x,int f)//f为还可以增加的最大流量
{
if(x==T)
return f;
int w,used=0;
for(int i=cur[x];i;i=e[i].next)
if(h[e[i].to]==h[x]+1)//存在于bfs搜索树的下一层
{
w=dfs(e[i].to,min(f-used,e[i].v));
e[i].v-=w;/**更新正向边、反相边**/
e[i^1].v+=w;
if(e[i].v)/**还有空余流量**/
cur[x]=i;
used+=w;//增加的流量
if(used==f)
return f;
}
if(!used)//这个分支没流量,从bfs搜索树中删除
h[x]=-1;
return used;
}
int dinic()
{
int answer=0;
while(bfs())
{
for(int i=1;i<=n;i++)//复制last数组到cur数组
cur[i]=last[i];
answer+=dfs(S,inf);//最小流量
}
return answer;
}
void floyd()
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
void build()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(MAP[i][j]!=-1&&dis[S][i]+MAP[i][j]+dis[j][T]==dis[S][T])
Insert(i,j,1);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
cnt=1;
memset(last,0,sizeof(last));
memset(dis,127/3,sizeof(dis));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&MAP[i][j]);
if(MAP[i][j]!=-1)
dis[i][j]=MAP[i][j];
}
dis[i][i]=0;
}
scanf("%d%d",&S,&T);
S++;
T++;
if(S==T)
{
printf("inf\n");
continue;
}
floyd();
build();
printf("%d\n",dinic());
}
return 0;
}