Poj_2135_farm Tour (minimum cost flow)

Source: Internet
Author: User
Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13539 Accepted: 5138

Description when FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= n <=) fields numbered 1..N, the first of which contains he house and the Nth of WHI CH contains the Big barn. A Total M (1 <= m <= 10000) Paths this connect the fields in various ways. Each path connects the different fields and have a nonzero length smaller than 35,000.

to show off his farm on the best-of-the-walks, he-a tour of that starts at his house, potentially travels through some Fields, and ends at the barn. Later, he returns (potentially through some fields) the back to his house again.

He wants his tour to being as short as possible, however he doesn ' t want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure this some tour exists for any given farm.

Input * Line 1:two space-separated integers:n and M.

* Lines 2..m+1:three space-separated integers that define a path:the starting field, the End field, and the path ' s lengt H.

Output A single line containing the length of the shortest tour.

Sample Input

4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

Sample Output

6


Test instructions: Give you a pair of diagram, ask from 1->n->1 so walk a back and forth the shortest path is how much, each side can only walk once.

Analysis: Minimum cost flow problem. The length of the side as a fee, each edge capacity of 1, because it is no direction, so each side to be processed two times, that is u->v,v->u to add in. After the diagram is built, run the minimum cost flow with a flow rate of 2 to draw the minimum cost.

Title Link: http://poj.org/problem?id=2135

Code Listing:

#include <map> #include <set> #include <cmath> #include <queue> #include <stack> #include <ctime> #include <cctype> #include <string> #include <cstdio> #include <cstring> #include

<cstdlib> #include <iostream> #include <algorithm> using namespace std;
#define END () return 0 typedef long Long ll;
typedef unsigned int uint;



typedef unsigned long long ull;
const INT MAXN = 4000 + 5;

const int INF = 0x7f7f7f7f;
    struct edge{int from,to,cap,flow,cost;

Edge (int u,int v,int c,int f,int W): From (U), to (v), Cap (c), Flow (f), Cost (W) {}};
    struct mcmf{int n,m; vector<edge>edge; Twice times the number of sides vector<int>g[maxn]; adjacency table, G[i][j] indicates the ordinal of the J edge of I in the e array, int INQ[MAXN]; Whether in queue int D[MAXN]; Bellman-ford int P[MAXN]; Previous arc int A[MAXN];
        An improved amount of void init (int n) {this, n = n;
        for (int i=0;i<=n;i++) g[i].clear ();
    Edge.clear (); } void Addedge (int from,int to,intCap,int cost) {Edge.push_back (Edge (From,to,cap,0,cost));
        Edge.push_back (Edge (to,from,0,0,-cost));
        M=edge.size ();
        G[from].push_back (m-2);
    G[to].push_back (m-1);
        } bool Bellmanford (int s,int t,int& flow,int& cost) {memset (d,inf,sizeof (d));
        memset (inq,0,sizeof (INQ)); 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;
                for (int i=0;i<g[u].size (); i++) {edge& e=edge[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];
        if (flow==2) return false;
            for (int u=t;u!=s;u=edge[p[u]].from) {edge[p[u]].flow+=a[t];
        EDGE[P[U]^1].FLOW-=A[T];
    } return true;
        }//need to ensure that the initial network does not have a negative weight ring int mincostmaxflow (int s,int t) {int flow=0,cost=0;
        while (Bellmanford (s,t,flow,cost));
    return cost;

}
};
int n,m;
int a,b,c;

MCMF MCMF;
    void input () {scanf ("%d%d", &n,&m);
    Mcmf.init (N);
        for (int i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&c);
        Mcmf.addedge (A,B,1,C);
    Mcmf.addedge (B,A,1,C); }} void Solve () {printf ("%d\n", MCMF.
Mincostmaxflow (1,n));
    } int main () {input ();
    Solve ();
End ();
 }


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.