zoukankan      html  css  js  c++  java
  • zoj 2760(网络流+floyed)

    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
    
    题意:给定源点和汇点的情况下边不相交最短路的条数
    题解:先用floyed算法将图中的点全部两点之间的距离全部算一遍,求出最短路,然后枚举原来图中的边,如果在最短路上,则连一条容量为1的边,算出最大流即可。
    //zoj 2760 
    #include <stdio.h>
    #include <algorithm>
    #include <queue>
    #include <string.h>
    #include <math.h>
    #include <iostream>
    #include <math.h>
    using namespace std;
    const int N = 205;
    const int INF = 999999999;
    struct Edge{
        int v,next;
        int w;
    }edge[2*N*N];
    int head[N];
    int level[N];
    int graph[N][N],dis[N][N];
    int tot;
    void init(){
        memset(head,-1,sizeof(head));
        tot=0;
    }
    void addEdge(int u,int v,int w,int &k){
        edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
        edge[k].v = u,edge[k].w=0,edge[k].next=head[v],head[v]=k++;
    }
    int BFS(int src,int des){
        queue<int >q;
        memset(level,0,sizeof(level));
        level[src]=1;
        q.push(src);
        while(!q.empty()){
            int u = q.front();
            q.pop();
            if(u==des) return 1;
            for(int k = head[u];k!=-1;k=edge[k].next){
                int v = edge[k].v;
                int w = edge[k].w;
                if(level[v]==0&&w!=0){
                    level[v]=level[u]+1;
                    q.push(v);
                }
            }
        }
        return -1;
    }
    int dfs(int u,int des,int increaseRoad){
        if(u==des) return increaseRoad;
        int ret=0;
        for(int k=head[u];k!=-1;k=edge[k].next){
            int v = edge[k].v;
            int w = edge[k].w;
            if(level[v]==level[u]+1&&w!=0){
                int MIN = min(increaseRoad-ret,w);
                w = dfs(v,des,MIN);
                if(w>0){
                edge[k].w -=w;
                edge[k^1].w+=w;
                ret+=w;
                if(ret==increaseRoad) return ret;
                }else level[v] = -1;
            }
        }
        return ret;
    }
    int Dinic(int src,int des){
        int ans = 0;
        while(BFS(src,des)!=-1) ans+=dfs(src,des,INF);
        return ans;
    }
    void floyd(int n){
        for(int k=0;k<n;k++){
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    dis[i][j] = min(dis[i][k]+dis[k][j],dis[i][j]);
                }
            }
        }
    }
    void build(int s,int t,int N){
        int i,j;
        for(i=0;i<N;i++){
            if(dis[s][i]==INF)continue;
            for(j=0;j<N;j++){
                if(i==j||dis[j][t]==INF||dis[i][j]==INF)continue;
                if(dis[s][t]==dis[s][i]+graph[i][j]+dis[j][t]){
                    addEdge(i,j,1,tot);
                }
            }
        }
    }
    int main(){
        int n;
        while(scanf("%d",&n)!=EOF){
            init();
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    int w;
                    scanf("%d",&graph[i][j]);
                    if(graph[i][j]==-1) graph[i][j]=INF;
                    if(i==j) graph[i][j] = 0;
                    dis[i][j] = graph[i][j];
                }
            }
            int src,des;
            scanf("%d%d",&src,&des);
            if(src==des) {
                printf("inf
    ");continue;
            }
            floyd(n);
            build(src,des,n);
            int ans = Dinic(src,des);
            printf("%d
    ",ans);
        }
    }
  • 相关阅读:
    解决弹出的窗口window.open会被浏览器阻止的问题(自定义open方法)
    js call
    说说JSON和JSONP,也许你会豁然开朗
    按需加载/懒加载
    Sublime Text2 中Emmet(之前叫Zencoding)插件安装以及使用
    求数组中的最小值以及最小值的序列号
    软件工程概论作业03--将随机产生的表达式导入数据库
    软件工程作业01--四则运算1
    梦断代码阅读笔记
    学习进度条--第二周
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5556311.html
Copyright © 2011-2022 走看看