• 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;
    }
  • 相关阅读:
    使用netcraft在线查看网站使用的操作系统和服务器
    Terracotta Web Sessions Tutorial
    JPA2.0和Spring的集成配置方式
    Maven笔记(5) Eclipse和Maven集成
    Maven笔记(2) 常用命令和标准的Maven项目结构
    Maven笔记(4) 构建一个Web Project
    Linux 技巧:让进程在后台可靠运行的几种方法
    You are currently running the HMaster without HDFS append support enabled. This may result in data loss. Please see the
    xtrabackup 安装及应用
    CentOS 6.2 X64上64位Oracle11gR2 静默安装,静默设置监听,静默建库亲自实践记录
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8146600.html
Copyright © 2020-2023  润新知