(Cost Stream)

Source: Internet
Author: User

For the undirected Weighted Graph of n points, find the shortest round-trip path of 1-> n without repeating the edge.

Here is a knowledge point: finding the shortest short circuit of s-> t on an undirected graph is actually a cost flow.

What about the 1-> n shortest round-trip path? Add the Source Vertex s, from s to 1, and add an arc. The capacity is 2 (two round trips), and the cost is 0. For the edges in the source image, <u, v>, the capacity increases from u to v, and from v to u to 1 (heavy edge cannot be taken during round-trip). The cost is the arc of Edge Weight. The answer is the minimum fee obtained by running the billing flow. If the final maximum stream is less than 2, there is no solution.

 

#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_backusing 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;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.