zoukankan      html  css  js  c++  java
  • POJ 2135 Farm Tour

    Farm Tour
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 18599   Accepted: 7198

    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

    题解:源点向1流一条cap为2的边,剩下的边cap为1,费用为时间,就只能跑一次了,跑一次最大费用最小流量;
    注意无向图要建双向边,这个地方wa了好几次
    #include <cstdio>
    #include <cstdlib>
    #include <queue>
    #include <iostream>
    #include <cstring>
    using namespace std;
    const int maxn = 1019, maxm = 4*10000 +150,inf = 1e9+5;
    int head[maxn],S,T,dis[maxn],pre[maxn],pree[maxn],mincost;
    bool inq[maxn];
    struct edge{int to,f,w,nxt;}G[maxm];
    int tot = 1;
    void add(int u,int v,int w,int f){
        G[++tot].nxt = head[u];
        G[tot].to = v;
        G[tot].f = f;
        G[tot].w = w;
        head[u] = tot;
    
        G[++tot].nxt = head[v];
        G[tot].to = u;
        G[tot].f = 0;
        G[tot].w = -w;
        head[v] = tot;
    
    }
    bool spfa(){
        memset(inq,0,sizeof(inq));
        memset(dis,127/3,sizeof(dis));
        queue <int> Q;
        Q.push(S);
        dis[S] = 0;
        inq[S] = 1;
    
        while(!Q.empty()){
            int u = Q.front();
            Q.pop();
            inq[u] = 0;
    
            for(int i = head[u]; i; i = G[i].nxt){
                int v = G[i].to;
                if(G[i].f && dis[v] > dis[u] + G[i].w){
                    dis[v] = dis[u] + G[i].w;
                    pre[v] = u;
                    pree[v] = i;
                    if(!inq[v]){
                        Q.push(v);
                        inq[v] = 1;
                    }
                }
    
            }
        }
        return dis[T] < dis[0];
    }
    void augment(){
        int u = T, delta = inf;
        while(u != S){
            delta = min(delta, G[pree[u]].f);
            u = pre[u];
        }
        u = T;
        while(u != S){
            G[pree[u]].f -= delta;
            G[pree[u]^1].f += delta;
            u = pre[u];
        }
        mincost += delta * dis[T];
    }
    int main()
    {
        int N,M,ans = 0;
        scanf("%d%d",&N,&M);
        for(int i = 1; i <= M; i++){
            int u, v, w;
            scanf("%d%d%d",&u,&v,&w);
            add(u, v, w, 1);
            add(v, u, w, 1);
        }
        S = N + 1 , T = N + 2;
        add(S, 1, 0, 2);
        add(1, S, 0, 2);
        add(N, T, 0, 2);
        add(T, N, 0, 2);
        while(spfa())augment();
        printf("%d
    ",mincost);
    
        return 0;
    }
     
    
    
  • 相关阅读:
    技巧和诀窍;在VS 2005里优化ASP.NET 2.0Web项目的Build性能
    [资源]Flex 中文帮助
    13 Awesome Javascript CSS Menus
    兼容FF\IE的事件获得方法
    MSVCR80D.dll not found解决方案
    [转]一种革命性的自绘菜单实现
    [ZZ]马化腾关于产品设计与用户体验的培训
    [ZZ]WatiN:在.NET中测试Web应用程序
    How to Recommender Systems?
    淘宝第三届D2在上海举办
  • 原文地址:https://www.cnblogs.com/EdSheeran/p/8459809.html
Copyright © 2011-2022 走看看