UVA 10806 Dijkstra, Dijkstra.(费用流)

n个点的无向带权图,求1->n的最短往返路径,不走重复边。

这里涉及到一个知识点:求无向图上s->t的最短路,其实就是费用流。

而求1->n最短往返路径呢?增加源点s,由s到1加弧,容量为2(往返两次),费用为0;而对于原图中的边<u, v>,分别由u到v,由v到u增加容量为1(往返不能走重边),费用为边权的弧。然后跑费用流得到的最小费用便是答案。如果最后求得的最大流小于2,则说明无解。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<bitset>
#include<vector>
#include<string>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define debug puts("**debug**")
#define LL long long
#define PB push_back
using namespace std;

const int maxn = 111;
const int INF = 1e9;
int n, m, s, t, d[maxn], p[maxn], a[maxn], inq[maxn];
int flow, cost;
struct Edge
{
    int from, to, cap, flow, cost;
};
vector<Edge> edges;
vector<int> G[maxn];

inline void init()
{
    flow = cost = s = 0, t = n;
    REP(i, t+1) G[i].clear(); edges.clear();
}

void add(int from, int to, int cap, int cost)
{
    edges.PB((Edge){from, to, cap, 0, cost});
    edges.PB((Edge){to, from, 0, 0, -cost});
    int nc = edges.size();
    G[from].PB(nc-2);
    G[to].PB(nc-1);
}

bool spfa(int& flow, int& cost)
{
    REP(i, t+1) d[i] = INF;
    CLR(inq, 0);
    d[s] = 0, inq[s] = 1, p[s] = 0, a[s] = INF;
    queue<int> q; q.push(s);
    while(!q.empty())
    {
        int u = q.front(); q.pop();
        inq[u] = 0;
        int nc = G[u].size();
        REP(i, nc)
        {
            Edge& e = edges[G[u][i]];
            if(e.cap > e.flow && d[e.to] > d[u] + e.cost)
            {
                d[e.to] = d[u] + e.cost;
                p[e.to] = G[u][i];
                a[e.to] = min(a[u], e.cap - e.flow);
                if(!inq[e.to]) q.push(e.to), inq[e.to] = 1;
            }
        }
    }
    if(d[t] == INF) return false;
    flow += a[t], cost += d[t] * a[t];
    int u = t;
    while(u != s)
    {
        edges[p[u]].flow += a[t];
        edges[p[u]^1].flow -= a[t];
        u = edges[p[u]].from;
    }
    return true;
}

int main()
{
    while(scanf("%d", &n), n)
    {
        scanf("%d", &m);
        init();
        int a, b, c;
        add(s, 1, 2, 0);
        while(m--)
        {
            scanf("%d%d%d", &a, &b, &c);
            add(a, b, 1, c);
            add(b, a, 1, c);
        }
        while(spfa(flow, cost));
        if(flow < 2) puts("Back to jail");
        else printf("%d\n", cost);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值