zoukankan      html  css  js  c++  java
  • 网络流(最小费用最大流):POJ 2135 Farm Tour

    Farm Tour

    Time Limit: 1000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 2135
    64-bit integer IO format: %lld      Java class name: Main
     
    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到点n的路径,同时要求路程最短。
      于是贴最小费用最大流模板就AC啦。
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <queue>
     5 using namespace std;
     6 const int INF=233333333;
     7 const int maxn=1010,maxm=40010;
     8 int cnt,fir[maxn],nxt[maxm],to[maxm],cap[maxm],val[maxm],dis[maxn],path[maxn];
     9 
    10 void addedge(int a,int b,int c,int v)
    11 {
    12     nxt[++cnt]=fir[a];to[cnt]=b;cap[cnt]=c;val[cnt]=v;fir[a]=cnt;
    13 }
    14 int S,T;
    15 int Spfa()
    16 {
    17     queue<int>q;
    18     memset(dis,127,sizeof(dis));
    19     q.push(S);dis[S]=0;
    20     while(!q.empty())
    21     {
    22         int node=q.front();q.pop();
    23         for(int i=fir[node];i;i=nxt[i])
    24             if(cap[i]&&dis[node]+val[i]<dis[to[i]]){
    25                 dis[to[i]]=val[i]+dis[node];
    26                 path[to[i]]=i;
    27                 q.push(to[i]);
    28             }
    29     }
    30     return dis[T]==dis[T+1]?0:dis[T]; 
    31 }
    32 
    33 int Aug()
    34 {
    35     int p=T,f=INF;
    36     while(p!=S)
    37     {
    38         f=min(f,cap[path[p]]);
    39         p=to[path[p]^1];
    40     }
    41     p=T;
    42     while(p!=S)
    43     {
    44         cap[path[p]]-=f;
    45         cap[path[p]^1]+=f;
    46         p=to[path[p]^1];
    47     }
    48     return f;
    49 }
    50 
    51 int MCMF()
    52 {
    53     int ret=0,d;
    54     while(d=Spfa())
    55         ret+=Aug()*d;
    56     return ret;    
    57 }
    58 
    59 void Init(int n)
    60 {
    61     cnt=1;S=0;T=n+1;
    62     for(int i=1;i<=n;i++)fir[i]=0;
    63 }
    64 
    65 int main()
    66 {
    67     int n,m;
    68     while(~scanf("%d%d",&n,&m))
    69     {
    70         Init(n);
    71         int a,b,v;
    72         for(int i=1;i<=m;i++)
    73         {
    74             scanf("%d%d%d",&a,&b,&v);
    75             addedge(a,b,1,v);
    76             addedge(b,a,0,-v);
    77             addedge(b,a,1,v);
    78             addedge(a,b,0,-v);
    79         }
    80         addedge(S,1,2,0);
    81         addedge(1,S,0,0);
    82         addedge(n,T,2,0);
    83         addedge(T,n,0,0);
    84         printf("%d
    ",MCMF());
    85     }
    86     return 0;
    87 }

    尽最大的努力,做最好的自己!
  • 相关阅读:
    【python】新创建一个列表,并向内添加元素 and 列表中查询最大值位置和最小值位置
    【python】python从命令行读入参数
    【开发环境安装笔记 】微软Visual Studio2012中文旗舰版(完美破解版)
    【Python笔记】Python读取word文本操作详解
    【安装---WIN7--cnpm】win7实测安装npm,cnpm
    【安装---WIN7--Vuejs】Vue.js 安装及其环境搭建
    【安装---WIN7--nodejs】win7安装 nodejs,npm
    【安装--达梦】Linux(中科方德服务器版4.0)安装达梦数据库V8
    【安装—ES】ES数据库安装6.6b版本
    【centos系统--安装--异常】server重装系统后,远程登录提示主机密钥验证失败的解决办法
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5222539.html
Copyright © 2011-2022 走看看