zoukankan      html  css  js  c++  java
  • poj2135 Farm Tour(费用流)

    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

    USACO 2003 February Gree
     
     
    费用流,建图还是比较好想的。据说题中数据范围不对要当成2000个点20000条边才能过。
    题目大意及题解:

     1 program rrr(input,output);
     2 const
     3   inf=987654321;
     4 type
     5   etype=record
     6      t,c,w,next,rev:longint;
     7   end;
     8 var
     9   e:array[0..40040]of etype;
    10   a,q,dis,fre,frv:array[0..2020]of longint;
    11   inq:array[0..2020]of boolean;
    12   n,m,i,x,y,w,cnt,h,t,ans:longint;
    13 procedure ins(x,y,c,w:longint);
    14 begin
    15    inc(cnt);e[cnt].t:=y;e[cnt].c:=c;e[cnt].w:=w;e[cnt].next:=a[x];a[x]:=cnt;
    16 end;
    17 procedure add(x,y,w:longint);
    18 begin
    19    ins(x,y,1,w);e[cnt].rev:=cnt+1;
    20    ins(y,x,0,-w);e[cnt].rev:=cnt-1;
    21 end;
    22 procedure spfa;
    23 begin
    24    for i:=1 to n do dis[i]:=inf;
    25    fillchar(inq,sizeof(inq),false);
    26    h:=0;t:=1;dis[1]:=0;q[1]:=1;inq[1]:=true;
    27    while h<>t do
    28       begin
    29          inc(h);if h>1000 then h:=1;
    30          i:=a[q[h]];
    31          while i<>0 do
    32             begin
    33                if (e[i].c>0) and (dis[q[h]]+e[i].w<dis[e[i].t]) then
    34                   begin
    35                      dis[e[i].t]:=dis[q[h]]+e[i].w;
    36                      fre[e[i].t]:=i;frv[e[i].t]:=q[h];
    37                      if not inq[e[i].t] then
    38                         begin
    39                            inc(t);if t>1000 then t:=1;
    40                            q[t]:=e[i].t;inq[e[i].t]:=true;
    41                         end;
    42                   end;
    43               i:=e[i].next;
    44             end;
    45          inq[q[h]]:=false;
    46       end;
    47 end;
    48 begin
    49    assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
    50    readln(n,m);
    51    fillchar(a,sizeof(a),0);cnt:=0;
    52    for i:=1 to m do begin readln(x,y,w);add(x,y,w);add(y,x,w); end;
    53    ans:=0;
    54    spfa;
    55    i:=n;while i<>1 do begin e[fre[i]].c:=0;e[e[fre[i]].rev].c:=1;ans:=ans+e[fre[i]].w;i:=frv[i]; end;
    56    spfa;
    57    i:=n;while i<>1 do begin ans:=ans+e[fre[i]].w;i:=frv[i]; end;
    58    write(ans);
    59    close(input);close(output);
    60 end.
  • 相关阅读:
    vue-cli脚手架工具构建&初始化vue项目
    2020/05/17 Github加速-DNS配置
    同时将本地git仓库提交到gitee和github
    git远程仓库在本地看不到&push 到远程仓库失败的解决方案
    stylus的使用-变量和函数
    webpack使用4-HRM热替换&SourceMap&解析es语法
    webpack使用3-plugin插件的使用
    webpack使用2-常用的几个loader的使用
    webpack基本使用
    spring框架里面的注入?
  • 原文地址:https://www.cnblogs.com/Currier/p/6670670.html
Copyright © 2011-2022 走看看