zoj 2760(边不相交最短路的条数)

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

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2760

分析:这题要求边不相交的最短路条数,我们可以想到把最短路上的边都加到网络里,且容量为1,这样边就不会相交了,最大流就是答案,问题即转化为求哪些边是最短路上的,一开始我用dijstra做最短路,然后枚举边e[i][j]满足dis[i]+e[i][j]==dis[j]的边为最短路上的,但是wa了,后来看了一些人的做法都是floyd求出最短路d[i][j],然后枚举边e[i][j]满足d[s][i]+e[i][j]+d[j][t]==d[s][t]的边即最短路上的边(之前那种为什么错呢。。。),这样图就建完了,不过要注意不要用d[s][t]==0来判断是否输出inf,我因此wa了几次,还有d[i][i]都要赋值为0,数据很坑人阿~~~

代码:

#include<cstdio>
using namespace std;
const int mm=222222;
const int mn=222;
const int oo=1000000000;
int node,src,dest,edge;
int ver[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
int map[mn][mn],d[mn][mn];
inline int min(int a,int b)
{
    return a<b?a:b;
}
inline void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0; i<node; ++i)head[i]=-1;
    edge=0;
}
inline void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0; i<node; ++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0; l<r; ++l)
        for(i=head[u=q[l]]; i>=0; i=next[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp; i>=0; i=next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0; i<node; ++i)work[i]=head[i];
        while(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}
int main()
{
    int i,j,k,n,s,t;
    while(scanf("%d",&n)!=-1)
    {
        for(i=0; i<n; ++i)
            for(j=0; j<n; ++j)
            {
                scanf("%d",&map[i][j]);
                if(i==j)map[i][j]=0;
                if(map[i][j]<0)map[i][j]=oo;
                d[i][j]=map[i][j];
            }
        scanf("%d%d",&s,&t);
        if(s!=t)
        {
            for(k=0; k<n; ++k)
                for(i=0; i<n; ++i)
                    if(d[i][k]<oo)for(j=0; j<n; ++j)
                            if(d[k][j]<oo)d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
            prepare(n,s,t);
            for(i=0; i<n; ++i)
                if(d[s][i]<oo)for(j=0; j<n; ++j)
                        if(d[j][t]<oo&&map[i][j]<oo&&d[s][i]+map[i][j]+d[j][t]==d[s][t])addedge(i,j,1);
            printf("%d\n",Dinic_flow());
        }
        else printf("inf\n");
    }
    return 0;
}


你可能感兴趣的:(zoj 2760(边不相交最短路的条数))