zoj2760 dijkstra+dinic

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.

然后一遍dinic,最大流就是最大条数。因为这些边的边权为1,所以从源点流向汇点一点流量,对应这条路径将会全部消失,尽管可能会被反向增广(感觉多次经过这条边),但从最后结果来看,这些增广路径可以拆解成不相交的路径,其实就是看每条边的流量就可以了(不需要关心中间增广过程)。

接下来一个问题:如何判定此条边是否在最短路上。

我们假设用最短路算法得到了,源点到任一点的最短路和任一点到汇点的最短路,记在dist数组里,那么我们枚举边(i,j),假设边权为adj[i][j],那么在最短路上必须满足dist[src][i]+adj[i][j]+diat[j][t]=dist[src][t](最优子结构),反之,满足dist[src][i]+adj[i][j]+diat[j][t]=dist[src][t]这个式子,一定在最短路上。

记下来就可以任意发挥了,这里采用了dijkstra算法。

注意此题输入数据边(i,i)不一定为0,因此要强制置0.极其坑爹!

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#define Maxn 110
using namespace std;

const int inf=0x3f3f3f3f;
int adj[Maxn][Maxn];
int vis[Maxn],dist1[Maxn],dist2[Maxn];
void dijkstra(int u,int n,int *dist){
    for(int i=1;i<=n;i++)
        dist[i]=adj[u][i],vis[i]=0;
    vis[u]=1,dist[u]=0;
    for(int i=1;i<n;i++){
        int minn=inf,v=0;
        for(int j=1;j<=n;j++)
            if(!vis[j]&&dist[j]<minn)
                minn=dist[j],v=j;
        if(v==0) break;
        vis[v]=1;
        for(int j=1;j<=n;j++)
            if(!vis[j]&&dist[v]+adj[v][j]<dist[j])
                dist[j]=dist[v]+adj[v][j];
    }
}
struct line{
    int to,next,cap;
}p[Maxn*Maxn*2];
int head[Maxn];
int q[Maxn];
int d[Maxn];
int tot;
int src,t;
int n,m;
void addedge(int a,int b,int c){
    p[tot].to=b;
    p[tot].next=head[a];
    p[tot].cap=c;
    head[a]=tot++;
}
void insert(int a,int b,int c){
    addedge(a,b,c);
    addedge(b,a,0);
}
bool bfs(){
    memset(d,-1,sizeof d);
    int s=0,e=-1;
    q[++e]=src;
    d[src]=0;
    while(s<=e){
        int u=q[s++];
        for(int i=head[u];i!=-1;i=p[i].next){
            int v=p[i].to;
            if(d[v]==-1&&p[i].cap){
                d[v]=d[u]+1;
                q[++e]=v;
            }
        }
    }
    return d[t]!=-1;
}
int dfs(int u,int alpha){
    if(u==t) return alpha;
    int w,used=0;
    for(int i=head[u];i!=-1&&used<alpha;i=p[i].next){
        int v=p[i].to;
        if(p[i].cap&&d[v]==d[u]+1){
            w=dfs(v,min(alpha-used,p[i].cap));
            used+=w;
            p[i].cap-=w;
            p[i^1].cap+=w;
        }
    }
    if(!used) d[u]=-1;
    return used;
}
int dinic(){
    int ans=0;
    while(bfs())
        ans+=dfs(src,inf);
    return ans;
}
int main()
{
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++){
                scanf("%d",&adj[i][j]);
                if(i==j) adj[i][j]=0;
                if(adj[i][j]==-1) adj[i][j]=inf;
            }
        scanf("%d%d",&src,&t);
        if(src==t) {puts("inf");continue;}
        src++,t++;
        dijkstra(src,n,dist1);
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                swap(adj[i][j],adj[j][i]);
        dijkstra(t,n,dist2);
        memset(head,-1,sizeof head);
        tot=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i!=j&&adj[j][i]!=inf&&dist1[i]+dist2[j]+adj[j][i]==dist1[t])
                    insert(i,j,1);
        printf("%d\n",dinic());
    }
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值