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

题意:给出一个有向带权图,给定起始点与终点,求最短的条数。-1表示无路径。

思路:floyd求解最短路径。然后判断tmap[s][i]+map[i][j]+tmap[j][t]==tmap[s][t]?如果相等建一条边,权值为1,构完图后sap求最大流就ok了,还得判断起终点是否相等,是的话要输出inf。又错了六次啊,纠结啊,为什么呢,该思考一下,~~o(>_<)o ~~

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
const int N=420;
const int M=82000;
const int INF=99999999;
int n,m;
int gap[N],dis[N],pre[N],head[N],cur[N];
int map[N][N],tmap[N][N];
int NE,NV;
struct Node
{
    int pos,next;
    int c;
} E[M];
#define FF(i,NV) for(int i=0;i<NV;i++)
int sap(int s,int t)
{
    memset(dis,0,sizeof(int)*(NV+1));
    memset(gap,0,sizeof(int)*(NV+1));
    FF(i,NV) cur[i] = head[i];
    int u = pre[s] = s,maxflow = 0;
    int aug =INF;
    gap[0] = NV;
    while(dis[s] < NV)
    {
loop:
        for(int &i = cur[u]; i != -1; i = E[i].next)
        {
            int v = E[i].pos;
            if(E[i].c && dis[u] == dis[v] + 1)
            {
                aug=min(aug,E[i].c);
                pre[v] = u;
                u = v;
                if(v == t)
                {
                    maxflow += aug;
                    for(u = pre[u]; v != s; v = u,u = pre[u])
                    {
                        E[cur[u]].c -= aug;
                        E[cur[u]^1].c += aug;
                    }
                    aug =INF;
                }
                goto loop;
            }
        }
        if( (--gap[dis[u]]) == 0)   break;
        int mindis = NV;
        for(int i = head[u]; i != -1 ; i = E[i].next)
        {
            int v = E[i].pos;
            if(E[i].c && mindis > dis[v])
            {
                cur[u] = i;
                mindis = dis[v];
            }
        }
        gap[ dis[u] = mindis+1 ] ++;
        u = pre[u];
    }
    return maxflow;
}
void addEdge(int u,int v,int c )
{
    E[NE].c = c;
    E[NE].pos = v;
    E[NE].next = head[u];
    head[u] = NE++;

    E[NE].c = 0;
    E[NE].pos = u;
    E[NE].next = head[v];
    head[v] = NE++;
}

int main()
{
    int i,j,k,n,s,t;
    while(scanf("%d",&n)!=-1)
    {
        memset(head,-1,sizeof(head));
        NE=0;
        NV=n;
        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]=INF;
                tmap[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(tmap[i][k]<INF)for(j=0; j<n; ++j)
                            if(tmap[k][j]<INF)tmap[i][j]=min(tmap[i][j],tmap[i][k]+tmap[k][j]);
            
            for(i=0; i<n; ++i)
                if(tmap[s][i]<INF)for(j=0; j<n; ++j)
                        if(tmap[j][t]<INF&&map[i][j]<INF&&tmap[s][i]+map[i][j]+tmap[j][t]==tmap[s][t])addEdge(i,j,1);
            printf("%d\n",sap(s,t));
        }
        else printf("inf\n");
    }
    return 0;

}

/*
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
*/


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值