• 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
    ", cost);
        }
        return 0;
    }
    


  • 相关阅读:
    第二阶段冲刺第五天(6月4号)
    第二阶段冲刺第四天(6月3号)
    第二次阶段冲刺第三天(6月2号)
    第二次阶段冲刺第二天(6月1号)
    第二次阶段冲刺第一天(5月31号)
    第十周学习进度
    第十一周学习进度
    第十二周学习进度
    javascript 将递归转化为循环
    创建数据库,并设置外部访问权限
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3266466.html
Copyright © 2020-2023  润新知