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 }

    尽最大的努力,做最好的自己!
  • 相关阅读:
    MyBatis参数传入集合之foreach动态sql
    【mybatis】多次查询缓存的问题
    svn is already locked解决方案
    Python 命令行之旅:使用 docopt 实现 git 命令
    Django 官方推荐的姿势:类视图
    这可能就是你苦苦寻找免费、高颜值、功能强大的 Markdown 编辑器(共5款)
    超级好用的 Java 数据可视化库:Tablesaw
    让你如“老”绅士般编写 Python 命令行工具的开源项目:docopt
    开发 Django 博客文章阅读量统计功能
    使用 Docker 让部署 Django 项目更加轻松
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5222539.html
Copyright © 2011-2022 走看看