zoukankan      html  css  js  c++  java
  • POJ3068 "Shortest" pair of paths 【费用流】

    POJ3068 “Shortest” pair of paths


    Description

    A chemical company has an unusual shortest path problem.

    There are N depots (vertices) where chemicals can be stored. There are M individual shipping methods (edges) connecting pairs of depots. Each individual shipping method has a cost. In the usual problem, the company would need to find a way to route a single shipment from the first depot (0) to the last (N - 1). That’s easy. The problem they have seems harder. They have to ship two chemicals from the first depot (0) to the last (N - 1). The chemicals are dangerous and cannot safely be placed together. The regulations say the company cannot use the same shipping method for both chemicals. Further, the company cannot place the two chemicals in same depot (for any length of time) without special storage handling — available only at the first and last depots. To begin, they need to know if it’s possible to ship both chemicals under these constraints. Next, they need to find the least cost of shipping both chemicals from first depot to the last depot. In brief, they need two completely separate paths (from the first depot to the last) where the overall cost of both is minimal.

    Your program must simply determine the minimum cost or, if it’s not possible, conclusively state that the shipment cannot be made.

    Input

    The input will consist of multiple cases. The first line of each input will contain N and M where N is the number of depots and M is the number of individual shipping methods. You may assume that N is less than 64 and that M is less than 10000. The next M lines will contain three values, i, j, and v. Each line corresponds a single, unique shipping method. The values i and j are the indices of two depots, and v is the cost of getting from i to j. Note that these shipping methods are directed. If something can be shipped from i to j with cost 10, that says nothing about shipping from j to i. Also, there may be more than one way to ship between any pair of depots, and that may be important here.
    A line containing two zeroes signals the end of data and should not be processed.

    Output

    follow the output format of sample output.

    Sample Input

    2 1
    0 1 20
    2 3
    0 1 20
    0 1 20
    1 0 10
    4 6
    0 1 22
    1 3 11
    0 2 14
    2 3 26
    0 3 43
    0 3 58
    0 0

    Sample Output

    Instance #1: Not possible
    Instance #2: 40
    Instance #3: 73


    题意是有两个物品,需要把他们从0号节点运送到n-1号节点,其中他们经过的路径除了0和n-1两个节点是完全不相同的,每个边有边权,求最小化边权,不能满足输出Not possible

    然我我们发现每个点是有度数限制的,所以我们将一个点拆成两个点,中间连接一条cap为1的边,但是对于s和t(0和n-1),中间连接的边是度数为2,跑完最大流之后检查一下是不是流量为二,是二就存在可行流,否则不存在


    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<queue>
    #include<algorithm>
    using namespace std;
    #define N 100010
    #define INF 0x3f3f3f3f
    struct Edge{
        int u,v,cap,flow,cost;
        Edge(int xu,int xv,int xcap,int xflow,int xcost){
            u=xu;v=xv;cap=xcap;flow=xflow;cost=xcost;
        }
    };
    struct MCMF{
        int s,t;
        int d[N],f[N],p[N];
        bool inq[N];
        vector<Edge> E;
        vector<int> G[N];
        void clear(){
            E.clear();
            for(int i=0;i<N;i++)G[i].clear();
        }
        void add(int u,int v,int cap,int cost){
            E.push_back(Edge(u,v,cap,0,cost));
            E.push_back(Edge(v,u,0,0,-cost));
            int m=E.size();
            G[u].push_back(m-2);
            G[v].push_back(m-1);
        }
        bool SPFA(int &flow,int &cost){
            memset(inq,0,sizeof(inq));
            memset(d,0x3f,sizeof(d));
            queue<int> Q;Q.push(s);
            d[s]=0;f[s]=INF;
            while(!Q.empty()){
                int u=Q.front();Q.pop();
                inq[u]=0;
                for(int i=0;i<G[u].size();i++){
                    Edge e=E[G[u][i]];
                    if(d[e.v]>d[u]+e.cost&&e.cap>e.flow){
                        d[e.v]=d[u]+e.cost;
                        f[e.v]=min(f[u],e.cap-e.flow);
                        p[e.v]=G[u][i];
                        if(!inq[e.v]){
                            inq[e.v]=1;
                            Q.push(e.v);
                        }
                    }
                }
            }
            if(d[t]==INF)return false;
            flow+=f[t];cost+=d[t]*f[t];
            int u=t;
            while(u!=s){
                E[p[u]].flow+=f[t];
                E[p[u]^1].flow-=f[t];
                u=E[p[u]].u;
            }
            return true;
        }
        int Min_cost_Max_flow(){
            int flow=0,cost=0;
            while(SPFA(flow,cost));
            return (flow==2)?cost:-1;
        }
    }mcmf;
    int n,m,ind=0;
    int main(){
        while(scanf("%d%d",&n,&m)&&n&&m){
            mcmf.clear();
            mcmf.s=1;mcmf.t=n*2;
            mcmf.add(1,2,2,0);
            for(int i=1;i<n;i++)mcmf.add(i*2-1,i*2,1,0);
            mcmf.add(n*2-1,n*2,2,0);
            for(int i=1;i<=m;i++){
                int u,v,c;scanf("%d%d%d",&u,&v,&c);
                u++;v++;
                mcmf.add(u*2,v*2-1,1,c);
            }
            int cost=mcmf.Min_cost_Max_flow();
            if(cost==-1)printf("Instance #%d: Not possible
    ",++ind);
            else printf("Instance #%d: %d
    ",++ind,cost);
        }
        return 0;
    }
  • 相关阅读:
    【css系列】创建网页加载进度条
    【大数据系列】apache hive 官方文档翻译
    【大数据系列】问题汇总
    【大数据系列】hive修改默认的derby数据库
    【大数据系列】hive安装及启动
    【大数据系列】MapReduce详解
    【大数据系列】基于MapReduce的数据处理 SequenceFile序列化文件
    【大数据系列】windows下连接Linux环境开发
    【大数据系列】常用命令
    【大数据系列】hadoop脚本分析
  • 原文地址:https://www.cnblogs.com/dream-maker-yk/p/9676352.html
Copyright © 2011-2022 走看看