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连接SMTP的TLS(587端口)发邮件python发邮件(带认证,587端口)202010
    JAVA抓取通过JS渲染的网站(动态)网页数据
    moviepy音视频剪辑:与大小相关的视频变换函数详解
    区块链知识博文1: 共识算法之争(PBFT,Raft,PoW,PoS,DPoS,Ripple)
    MoviePy v2.0.0.dev1尚不成熟,不建议大家使用
    区块链学习2:一些概念性的基础知识笔记
    老猿学5G:3GPP 5G规范中的URI资源概念
    区块链学习1:Merkle树(默克尔树)和Merkle根
    老猿Python博客文章目录索引
    老猿学5G:融合计费场景的Nchf_ConvergedCharging_Create、Update和Release融合计费消息交互过程
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5222539.html
Copyright © 2011-2022 走看看