zoj 2760 How Many Shortest Path //MAXFLOW

本文介绍了一种利用网络流算法解决寻找多个不重叠最短路径的问题。通过FLOYED算法确定每条边是否位于最短路径上,并借助最大流算法计算最多可找到多少条这样的路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 


Author: SHEN, Guanghao
Source: ZOJ Monthly, September 2006

 

这道题学到不少东西。

以前做过求S-->T中的路中最长边最小的路径的数目这一类问题,当时的解法是二分+网络流

这道题目是求最短路的数量,竟然也可以用网络流做

首先以前没想到的,可以用FLOYED去算出I-->J这条边是否在最短路上,接着若是在则标记为1

注意,FLOYED的过程容易错,并且不要改变原来图中变量,因为可能原本I->J是没有边的,但你FLOYED后可能I-->J就有边了,你可能把I-->J这条边加入,那就错了

接着就可以用求最大流做了

 

#include<stdio.h>

#include<string.h>

#include<algorithm>

using namespace std;

const int inf=100000000;

 

int n,s,t;

int flow[110][110],mat[110][110],gap[110],dist[110];

int map[110][110];

 

int find_path(int p,int limit=1<<30-1)

{

    if(p==n-1) return limit;

    for(int i=0;i<n;i++)

    {

        if(dist[p]==dist[i]+1&&map[p][i]>0)

        {

            int t=find_path(i,min(limit,map[p][i]));

            if(t<0) return t;

            if(t>0)

            {

                map[p][i]-=t;

                map[i][p]+=t;

                return t;

            }

        }

    }

    int label=n;

    for(int i=0;i<n;i++) if(map[p][i]>0)  label=min(label,dist[i]+1);

    if(--gap[dist[p]]==0||dist[0]>=n)  return -1;

    ++gap[dist[p]=label];

    return 0;

}

 

int iSAP()

{

    gap[0]=n;

    int maxflow=0,t=0;

    while((t=find_path(0))>=0)  maxflow+=t;

    return maxflow;

}

 

void buildgraph()

{

    memcpy(mat,flow,sizeof(flow));

    for(int k=1;k<=n;k++)

      for(int i=1;i<=n;i++)

      {

           if(mat[i][k]==inf)  continue;

           for(int j=1;j<=n;j++)

           {

               if(mat[k][j]==inf) continue;

               mat[i][j]=min(mat[i][j],mat[i][k]+mat[k][j]);

           }

      }

    memset(map,0,sizeof(map));

    for(int i=1;i<=n;i++)

    {

      if(mat[s][i]==inf) continue;

      for(int j=1;j<=n;j++)

      {

          if(mat[j][t]==inf) continue;

          map[i][j]=(flow[i][j]!=inf && mat[s][i]+flow[i][j]+mat[j][t]==mat[s][t]);

      }

    }

}

 

int main()

{

    while(scanf("%d",&n)!=EOF)

    {

        memset(flow,0,sizeof(flow));

        memset(gap,0,sizeof(gap));

        memset(dist,0,sizeof(dist));

        for(int i=1;i<=n;i++)

          for(int j=1;j<=n;j++)

          {

              scanf("%d",&flow[i][j]);

              if(i==j) flow[i][j]=0;

              else if(flow[i][j]==-1) flow[i][j]=inf;

          }

        scanf("%d%d",&s,&t);

        s++;t++;

        if(s==t) printf("inf/n");

        else

        {

            buildgraph();

            map[0][s]=inf;

            map[t][n+1]=inf;

            n=n+2;

            printf("%d/n",iSAP());

        }

    }

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值