ZOJ 2760 How Many Shortest Path (最小费用最大流做法)

Description

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

题意:给定一个带权有向图 G=(V, E)和源点 s、汇点 t,问 s-t 边不相交最短路最多有几条。 (1 <= N <= 100)

分析:这个题有个最大流的做法,分别从源点和汇点作一次 Dijkstra,然后为建图方便仍保留所有的点,但只加入满足 ds[u]+w[u][v]+dt[v]==dst 的边(u, v)(这样便保证网络中的任意一条 s-t 路都是最短路) ,容量为 1。求一次最大流即为结果。

但是我一开始没有想到这种高端的方法,就用了费用流水过了。因为费用流解决了流量和费用的问题,所以我们只需在模板里面记录结果就行了,没必要那么麻烦。

建图时每条边的容量还是1,费用为题目给的费用就行。

#include<cstdio>
#include<string.h>
#include<queue>
#include<algorithm>
#define maxn 110
#define INF 0x3f3f3f
using namespace std;
struct node
{
    int st;
    int en;
    int flow,cost;
    int next;
}E[101000];
int num;
int p[maxn];
void init()
{
    memset(p,-1,sizeof p);
    num=0;
}
void add(int st,int en,int flow,int cost)
{
    E[num].st=st;
    E[num].en=en;
    E[num].flow=flow;
    E[num].cost=cost;
    E[num].next=p[st];
    p[st]=num++;
    E[num].st=en;
    E[num].en=st;
    E[num].flow=0;
    E[num].cost=-cost;
    E[num].next=p[en];
    p[en]=num++;
}
int n;

int pre[maxn];
int dis[maxn];
bool fg[maxn];

bool spfa(int st,int en)
{
    for(int i=0;i<=maxn;i++)
        fg[i]=0,dis[i]=INF,pre[i]=-1;
    queue<int>q;
    q.push(st);
    fg[st]=1;
    dis[st]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        fg[u]=0;
        for(int i=p[u];i+1;i=E[i].next)
        {
            int v=E[i].en;
            if(E[i].flow&&dis[v]>dis[u]+E[i].cost)
            {
                dis[v]=dis[u]+E[i].cost;
                pre[v]=i;
                if(!fg[v])
                {
                    fg[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(dis[en]<INF)
        return 1;
    return 0;
}
int solve(int st,int en)
{
    int f=1,tem,sum=0;
    while(spfa(st,en))
    {
        int d=INF,ans=0;
        for(int i=pre[en];i+1;i=pre[E[i].st])
            d=min(d,E[i].flow);
        for(int i=pre[en];i+1;i=pre[E[i].st])
        {
            E[i].flow-=d;
            E[i^1].flow+=d;
            ans+=d*E[i].cost;
        }
        if(f)///此处处理答案,第一次跑完后是最小费用,看看有几次最小费用
        {
            sum++;
            tem=ans;
            f=0;
        }
        else if(tem==ans) sum++;
        else break;
    }
    return sum;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        init();
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            {
                int x;
                scanf("%d",&x);
                if(i==j) continue;
                if(x<0) continue;
                add(i,j,1,x);
            }
        int st,en;
        scanf("%d%d",&st,&en);
        if(st==en)
        {
            puts("inf");
            continue;
        }
        printf("%d\n",solve(st,en));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值