zoj 2760 How Many Shortest Path

本文介绍了一种算法,用于解决在一个带权有向图中找出从起点到终点的最大数量的非重叠最短路径的问题。通过使用Floyd算法预处理最短路径,并结合最大流算法来确定非重叠路径的数量。

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

http://www.elijahqi.win/2018/01/08/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<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
#define N 110
using 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(){
    queue<int>q;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<n;++i)
            for (int j=0;j<n;++j){
                mp1[i][j]=read();
                if (i==j) {mp1[i][j]=0;continue;}
                if (mp1[i][j]==-1) mp1[i][j]=inf;
                else insert2(i,j,mp1[i][j]);
            }
        for (int k=0;k<n;++k)
            for (int i=0;i<n;++i)
                for (int j=0;j<n;++j){
                    mp1[i][j]=min(mp1[i][j],mp1[i][k]+mp1[k][j]);
                }
        S=read();T=read();

        /*for (int i=0;i<n;++i){
            for (int j=0;j<n;++j) printf("%d ",mp1[i][j]);puts("");

        }*/if (S==T) {puts("inf");continue;}
    //  for (int i=1;i<=nm;++i) printf("%d %d %d\n",ed[i].x,ed[i].y,ed[i].z);
        for (int i=1;i<=nm;++i){
            int x=ed[i].x,y=ed[i].y,z=ed[i].z;
            if(mp1[S][x]+z+mp1[y][T]==mp1[S][T]) insert1(x,y,1);
        }
        int ans=0;
    //  for (int i=2;i<=num;++i) printf("%d %d %d\n",data[i].x,data[i].y,data[i].z);printf("%d %d\n",S,T);
        while(bfs()) memcpy(cur,h,sizeof(h)),ans+=dfs(S,inf);printf("%d\n",ans);
    }
    return 0;
}
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
#define N 110
using 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(){
    queue<int>q;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<n;++i)
            for (int j=0;j<n;++j){
                mp[i][j]=read();if (i==j) mp[i][j]=0;if (mp[i][j]==-1) mp[i][j]=inf;mp1[i][j]=mp[i][j];
            }
        for (int k=0;k<n;++k)
            for (int i=0;i<n;++i)
                for (int j=0;j<n;++j){
                    if (i==j) continue;mp1[i][j]=min(mp1[i][j],mp1[i][k]+mp1[k][j]);
                }
        S=read();T=read();

        /*for (int i=0;i<n;++i){
            for (int j=0;j<n;++j) printf("%d ",mp1[i][j]);puts("");
        }*/if (S==T) {puts("inf");continue;}
        int st=mp1[S][T];
        for (int i=0;i<n;++i){
            for (int j=0;j<n;++j){
                if (i==j||mp[i][j]==inf) continue;if (st==mp1[S][i]+mp[i][j]+mp1[j][T]) insert1(i,j,1);
            }
        }int ans=0;
        //for (int i=2;i<=num;++i) printf("%d %d %d\n",data[i].x,data[i].y,data[i].z);printf("%d %d\n",S,T);
        while(bfs()) memcpy(cur,h,sizeof(h)),ans+=dfs(S,inf);printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值