POJ Farm Tour

Source: Internet
Author: User

Farm Tour

Topic:

John has n plots, home at number 1th, and N is a warehouse. There are M-roads in the farm (bidirectional), road I connects the AI and bi, and the length is CI.

John wanted to go to the warehouse after a few visits from home. And then go back home. Suppose that a round trip cannot go through the same road two times, the minimum value of the total length of the route is obtained.

Algorithm Analysis:

Solve with shortest path and then delete the edge in the first minimum to solve a short circuit at a time. Is this feasible? A counter-example should be found immediately to prove that the method does not always get the best results.

So we give up the idea of the question as a go and back, and instead use the question as a way to find two without a common edge from vertex 1th to n vertex? After this conversion. is not to ask for a flow of 2 of the minimum cost, because the road is bidirectional.

#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <cstdio > #include <cstring>using namespace std;/* flow limit is F. Solving the minimum cost time complexity: O (F mlogn) */typedef pair<int,int> p;const int INF = 1 << 30;const int maxn = + + 10;str   UCT edge{int To,cap,cost,rev;   Edge () {}; Edge (int _to,int _cap,int _cost,int _rev): to (_to), Cap (_CAP), Cost (_cost), rev (_rev) {};};        int V;     Vertex int n,m,s,t;vector<edge> G[maxn];int H[MAXN];  The potential of the vertex int DIST[MAXN];  Shortest distance int PREVV[MAXN],PREVE[MAXN]; The shortest path in the German precursor node and the corresponding side void init () {S = 1; T = N;   V = T + 1; for (int i = 0;i <= v;++i) g[i].clear ();} Add an edge void Addedge (int from,int to,int cap,int cost) {G[from].push_back (edge) (to,cap,cost,g[to    ].size ())); G[to].push_back (Edge (From,0,-cost,g[from].size ()-1));} Solve the minimum cost flow from S to T flow to f//assume no flow for F.    Then return -1int min_cost_flow (int s,int t,int f) {//s: start T: End F: Flow limit int res = 0; Fill (h,h +v,0); Initialize h while (F > 0) {//update H PRIORITY_QUEUE&LT;P,VECTOR&LT;P&GT;,GREATER&LT;P&GT using Dijkstra algorithm ;        > Q;        Fill (dist,dist + v,inf);        Dist[s] = 0;        Q.push (P (0,s)); while (! Q.empty ()) {p p = q.top ();            Q.pop ();            int v = p.second;            if (Dist[v] < P.first) continue;                for (int i = 0;i < G[v].size (); ++i) {edge& e = g[v][i];                int tmp = Dist[v] + e.cost + h[v]-h[e.to];                    if (E.cap > 0 && dist[e.to] > tmp) {dist[e.to] = tmp;                    Prevv[e.to] = v;                    Preve[e.to] = i;                Q.push (P (dist[e.to],e.to));        }}}//while//cannot be augmented if (dist[t] = = INF) {return-1;        } for (int v = 1;v <= v;++v) h[v] + = Dist[v];        int d = f; for (int v = t;v! = S;v = Prevv[v]) {d = min (d,g[prevv[V]]        [Preve[v]].cap];        } F-= D;        Res + = d * H[t];            for (int v = t; v! = S;v = Prevv[v]) {edge& e = g[prevv[v]][preve[v]];            E.cap-= D;        G[v][e.rev].cap + = D; }} return res;}    int main () {//freopen ("Input.txt", "R", stdin);        while (~SCANF ("%d%d", &n,&m)) {init ();        int a,b,c;            for (int i = 0;i < M;++i) {scanf ("%d%d%d", &a,&b,&c);            Addedge (A,B,1,C);        Addedge (B,A,1,C);    } printf ("%d\n", Min_cost_flow (s,t,2)); } return 0;}


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

POJ Farm Tour

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.