ZOJ 2760--How Many Shortest Path【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表示无路径。

思路:最短路处理出最短路径图,若dis[v] = dist[u] + w(u,v),则该路在最短路径中。把最短路的边加进最短路径图,边权值为1,构完图后dinic求最大流就ok了,还得判断起终点是否相等,是的话要输出inf。还有,太坑了,输入的矩阵对角线不一定都是0 



#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 200
#define maxm 80000+1000
#define INF 0x3f3f3f3f
using namespace std;

struct node {
    int u, v, cap, flow, next;
};

node edge[maxm];
int dist[maxn], vis[maxn];
int head[maxn], cur[maxn], cnt;
int head1[maxn], cnt1;
int n, st, ed;
int flag;

struct NODE {
    int u, v, w, next;
};
NODE str[maxm];

void initstr(){
    cnt1 = 0;
    memset(head1, -1, sizeof(head1));
}

void initedge(){
    cnt = 0;
    memset(head, -1, sizeof(head1));
}

void addstr(int u, int v, int w){
    str[cnt1] = {u, v, w, head1[u]};
    head1[u] = cnt1++;
}


void addedge(int u, int v, int w){
    edge[cnt] = {u, v, w, 0 ,head[u]};
    head[u] = cnt++;
    edge[cnt] = {v, u, 0, 0, head[v]};
    head[v] = cnt++;
}

void getstr(){
    int u;
    for(int i = 0; i < n; ++i)
    for(int j = 0; j < n; ++j){
        scanf("%d", &u);
        if(u != -1){
            if(i == j && u != 0)
                u = 0;
            addstr(i, j, u);
        }
    }
}

void SPFA(int st ,int ed){
    for(int i = 0 ; i < n; ++i){
        dist[i] = INF;
        vis[i] = 0;
    }
    queue<int>q;
    dist[st] = 0;
    vis[st] = 1;
    q.push(st);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for(int i = head1[u]; i != -1; i = str[i].next){
            int v = str[i].v;
            int w = str[i].w;
            if(dist[v] > dist[u] + w){
                dist[v] = dist[u] + w;
                if(!vis[v]){
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    if(dist[ed] == INF)
        flag = 0;
}

void getedge(){
    for(int i = 0; i < n; ++i)
    for(int j = head1[i]; j != -1; j = str[j].next){
        int v = str[j].v;
        int w = str[j].w;
        if(dist[v] == dist[i] + w)
            addedge(i, v, 1);
    }
}


bool BFS(int st, int ed){
    queue<int>q;
    memset(vis, 0, sizeof(vis));
    memset(dist, -1, sizeof(dist));
    vis[st] = 1;
    dist[st] = 0;
    q.push(st);
    while(!q.empty()){
        int u =q.front();
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            node E = edge[i];
            if(!vis[E.v] && E.cap > E.flow){
                vis[E.v] = 1;
                dist[E.v] = dist[u] + 1;
                if(E.v == ed) return true;
                q.push(E.v);
            }
        }
    }
    return false;
}

int DFS(int x, int ed, int a){
    if(x == ed || a == 0)
        return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next){
        node &E = edge[i];
        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
            E.flow += f;
            edge[i ^ 1].flow -= f;
            a -= f;
            flow += f;
            if(a == 0) break;
        }
    }
    return flow;
}

int maxflow(int st, int ed){
    int flowsum = 0;
    while(BFS(st, ed)){
        memcpy(cur, head, sizeof(head));
        flowsum += DFS(st, ed, INF);
    }
    return flowsum;
}

int main (){
    while(scanf("%d", &n) != EOF){
        initstr();
        getstr();
        scanf("%d%d", &st, &ed);
        if(st == ed){
            printf("inf\n");
            continue;
        }
        flag = 1;
        SPFA(st, ed);
        if(!flag){
            printf("0\n");
            continue;
        }
        initedge();
        getedge();
        printf("%d\n", maxflow(st, ed));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值