• 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.
  • 相关阅读:
    iMX6Q开发板的EIM接口的配置可以与FPGA通讯-交换数据-最常用的接口配置
    大受喜欢安卓触控一体机连接云端数据化管理提供例程DEMO
    iTOP-4418开发板所用核心板研发7寸/10.1寸安卓触控一体机
    i.MX6ULL终结者Buildoot文件系统构建篇buildroot添加支持第三方软件
    所有教程为迅为原创-3399开发板资料更新了
    如果使用4412开发板那么怎么搭建和测试TFTP服务器
    iMX6ULL开发板Linux 4G通信实验EC20 4G模块配置
    iMX6ULL终结者Linux WIFI驱动实验rtl8723 Wifi联网测试
    iTOP4412开发板Android5.1.1移植教程
    迅为iTOP3399开发板人工智能(图像分类)
  • 原文地址:https://www.cnblogs.com/Currier/p/6670670.html
Copyright © 2020-2023  润新知