poj 3068 "Shortest" pair of paths

题目连接:“Shortest” pair of paths

题目大意:给你一个有向图,有边权,现在需要你找出两条从0到n-1的路径并且两条路径无公共点而且需要使得边权和最小,不存在输出No possible,存在输出两条路径的边权和

题目思路:这题我们可以想到去跑网络流,然后看最大流是不是2,我只给2的流量从超级源点流出,为了方便起见,我们将所有点位置加1,变成求1到n的最小费用最大流,超级源点为0,连接1,容量为2,单位费用为0,然后图中给的边容量为1(因为不能有公共点),费用为题目给的边权,然后第n个点连接超级汇点n+1,容量为2,费用为0,然后跑最小费用最大流就好了,判断满流以确定是不是有这样的两条路径

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
const int INF = 0x3f3f3f3f;
const int MaxNode = 205;
const int MaxEdge = 40005;

struct Edge{
    int to,vol,cost,next;
}Edges[MaxEdge];

int Pre[MaxNode],Path[MaxNode],Dist[MaxNode],Head[MaxNode],EdgeCount;

void addedge(int u, int v, int vol, int cost){
    Edges[EdgeCount].to = v;
    Edges[EdgeCount].vol = vol;
    Edges[EdgeCount].cost = cost;
    Edges[EdgeCount].next = Head[u];
    Head[u] = EdgeCount++;

    Edges[EdgeCount].to = u;
    Edges[EdgeCount].vol = 0;
    Edges[EdgeCount].cost = -cost;
    Edges[EdgeCount].next = Head[v];
    Head[v] = EdgeCount++;
}

bool Spfa(int s, int t){
    memset(Dist, 0x3f3f3f3f, sizeof(Dist));
    memset(Pre, -1, sizeof(Pre));
    Dist[s] = 0;
    queue<int>Q;
    Q.push(s);
    while (!Q.empty()){
        int u = Q.front();
        Q.pop();
        for (int e = Head[u]; e != -1; e = Edges[e].next){
            int v = Edges[e].to;
            if (Edges[e].vol > 0 && Dist[v] > Dist[u] + Edges[e].cost){
                Dist[v] = Dist[u] + Edges[e].cost;
                Pre[v] = u;
                Path[v] = e;
                Q.push(v);
            }
        }
    }
    return Pre[t] != -1;
}

int MCMF(int s, int t){
    int cost = 0;
    int max_flow = 0;
    int u, v, e;
    while (Spfa(s, t)){
        int f = INF;
        for (u = t; u != s; u = Pre[u]){
            f = min(f, Edges[Path[u]].vol);
        }

        for (u = t; u != s; u = Pre[u]){
            e = Path[u];
            Edges[e].vol -= f;
            Edges[e^1].vol += f;
        }
        max_flow += f;
        cost += f*Dist[t];
    }
    if(max_flow == 2) return cost;
    return -1;
}

void init(){
    memset(Head,-1,sizeof(Head));
    EdgeCount = 0;
}

int main(){
    int n,m,Case = 1;
    while(~scanf("%d%d",&n,&m)&&n+m){
        init();
        int u,v,cost;
        while(m--){
            scanf("%d%d%d",&u,&v,&cost);
            addedge(u+1,v+1,1,cost);
        }
        addedge(0,1,2,0);
        addedge(n,n+1,2,0);
        printf("Instance #%d: ",Case++);
        int cos = MCMF(0,n+1);
        if(cos == -1) puts("Not possible");
        else printf("%d\n",cos);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值