zoukankan      html  css  js  c++  java
  • poj2135 Farm Tour

    Farm Tour
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 18334   Accepted: 7095

    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

    大致题意:从1到n求两条不相交的路径,使得边权和最大.
    分析:第一次做有点难想到.如果不要求不相交,那么直接最短路就好了.要求不相交,每条边最多只能走一次,那么可以给每个边加一个容量1,最后求起点到终点流量为2的最小费用流.套模板.思路比较巧妙。
    #include <queue>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 500010,inf = 0x3f3f3f3f;
    int n,m,head[maxn],to[maxn],nextt[maxn],tot = 2,pre[maxn],w[maxn],c[maxn],ans;
    int d[maxn],vis[maxn],S,T,vis2[maxn];
    
    void add(int x,int y,int z,int p)
    {
        w[tot] = z;
        c[tot] = p;
        to[tot] = y;
        nextt[tot] = head[x];
        head[x] = tot++;
    }
    
    bool spfa()
    {
        memset(d,inf,sizeof(d));
        memset(vis2,0,sizeof(vis2));
        d[S] = 0;
        queue <int> q;
        q.push(S);
        memset(vis,0,sizeof(vis));
        vis[S] = 1;
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            vis[u] = 0;
            for(int i = head[u];i;i = nextt[i])
            {
                int v = to[i];
                if (w[i] && d[v] > d[u] + c[i])
                {
                    d[v] = d[u] + c[i];
                    if (!vis[v])
                    {
                        vis[v] = 1;
                        q.push(v);
                    }
                }
            }
        }
        return d[T] < inf;
    }
    
    int dfs(int u,int f)
    {
        if (u == T)
        {
            ans += f * d[T];
            return f;
        }
        int res = 0;
        vis2[u] = 1;
        for (int i = head[u];i;i = nextt[i])
        {
            int v = to[i];
            if (!vis2[v] && w[i] && d[v] == d[u] + c[i])
            {
                int temp = dfs(v,min(f - res,w[i]));
                w[i] -= temp;
                w[i ^ 1] += temp;
                res += temp;
                if (res == f)
                    return f;
            }
        }
        return res;
    }
    
    void dinic()
    {
        while(spfa())
            dfs(S,inf);
    }
    
    int main()
    {
        scanf("%d%d",&n,&m);
        S = 0,T = n + 1;
        for (int i = 1; i <= m; i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,1,c);
            add(b,a,0,-c);
            add(b,a,1,c);
            add(a,b,0,-c);
        }
            add(S,1,2,0);
            add(1,S,0,0);
            add(n,T,2,0);
            add(T,n,0,0);
        dinic();
        printf("%d
    ",ans);
    
        return 0;
    }
  • 相关阅读:
    node.js爬虫杭州房产销售及数据可视化
    webpack1.x 升级到 webpack2.x 英文文档翻译
    一机双屏和双屏通信方案总结
    Linux 利用hosts.deny 防止暴力破解ssh
    Linux 之rsyslog+LogAnalyzer 日志收集系统
    count和distinct
    排序算法 — 冒泡排序
    排序算法 — 插入排序
    排序算法 — 选择排序
    CRLF will be replaced by LF in XXX when git commit
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8146600.html
Copyright © 2011-2022 走看看