zoukankan      html  css  js  c++  java
  • poj2135--Farm Tour(最小费用最大流)

    Farm Tour
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14219   Accepted: 5421

    Description

    When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000. 

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

    He wants his tour to be 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 that 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 length. 

    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
    

    Source

     
    农场主有N块地, 求从房子出发到某地,  再回到房子, 所用最短路程(每条边走一次)。 建源点和汇点 跑最短路。
    数组开小为毛判T;
    #include <queue>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    typedef int Type;
    const int MAXNODE = 1010;
    const int MAXEDGE = 10010;
    const int INF = 0x3f3f3f3f;
    struct Edge
    {
        int u, v, next;
        Type cap, flow, cost;
        Edge() {}
        Edge(int u, int v, Type cap, Type flow, Type cost, int next):  u(u), v(v), cap(cap), flow(flow), cost(cost), next(next) {}
    };
    struct MXMF
    {
        int n, m, s, t;
        Edge edges[MAXEDGE*2];
        int head[MAXNODE];
        int p[MAXNODE];
        Type dis[MAXNODE];
        Type a[MAXNODE];
        bool vis[MAXNODE];
        void init(int n)
        {
            this->n=n;
            memset(head, -1, sizeof(head));
            m = 0;
        }
        void addEdge(int u, int v, Type cap, Type cost)
        {
            edges[m]=Edge(u, v, cap, 0, cost, head[u]);
            head[u]=m++;
            edges[m]=Edge(v, u, cap, 0, cost, head[v]);
            head[v]=m++;
        }
        bool BellmanFord(int s, int t, Type &flow, Type &cost)
        {
            for(int i=0; i<n; i++) dis[i]=INF;    //0 -> n+1;
            memset(vis, 0, sizeof(vis));
            dis[s]=0; vis[s]=true; p[s]=0; a[s]=INF;
            queue<int> Q;
            Q.push(s); 
            
            while(!Q.empty())
            {
                int u=Q.front(); Q.pop();
                vis[u]=false;
                for(int i=head[u]; i!=-1; i=edges[i].next)
                {
                    Edge &e=edges[i];
                    if(e.cap>e.flow && dis[e.v]>dis[u]+e.cost)
                    {
                        dis[e.v]=dis[u]+e.cost;
                        p[e.v]=i;
                        a[e.v]=min(a[u], e.cap-e.flow);
                        if(!vis[e.v]) { Q.push(e.v); vis[e.v]=true;}
                    }
                }
            }
            if(dis[t]== INF) return false;
            flow += a[t];
            cost += a[t]*dis[t];
            int u=t;
            while(u != s)
            {
                edges[p[u]].flow += a[t];
                edges[p[u] ^ 1].cost = -edges[p[u] ^ 1].cost;
                u = edges[p[u]].u;
            }
            return true;
        }
        Type minCost(int s, int t)
        {
            this->s=s; this->t;
            Type flow=0, cost=0;
            while(BellmanFord(s, t, flow, cost));
            return cost;
        }
    } mcmf; 
    
    int n, m;
    
    void init()
    {
        int source=1, sink=n;   //imp; 
        mcmf.init(n+2);
        int u, v, c;
        for(int i=0; i< m; i++)
        {
            scanf("%d%d%d", &u, &v, &c);
            mcmf.addEdge(u, v, 1, c);
        }
        mcmf.addEdge(0, source, 2, 0);
        mcmf.addEdge(sink, n+1, 2, 0);
        printf("%d
    ", mcmf.minCost(0, n+1));
    }
    int main()
    {
        while(scanf("%d%d", &n, &m) != EOF){
            init();
        }
        return 0;
    }
     
  • 相关阅读:
    axios全局配置
    014 Javascript(148
    013 Javascript(134
    axios的基本使用
    axios简介
    012 Javascript(126
    016 HTML+CSS(Class198
    015 HTML+CSS(Class184
    014 HTML+CSS(Class165
    LCA题目选讲2
  • 原文地址:https://www.cnblogs.com/soTired/p/5312455.html
Copyright © 2011-2022 走看看