zoukankan      html  css  js  c++  java
  • 3386/1752: [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家

    3386/1752: [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 39  Solved: 14
    [Submit][Status][Discuss]

    Description

        贝茜在谷仓外的农场上,她想回到谷仓,在第二天早晨农夫约翰叫她起来挤奶之前尽可能多地睡上一觉.由于需要睡个好觉,贝茜必须尽快回到谷仓.农夫约翰的农场上有N(2≤N≤1000)个路标,每一个路标都有唯一的编号(1到N).路标1是谷仓,路标N是贝茜一整天呆在那里的果树园.农场的所有路标之间共有T(1≤T≤2000)条不同长度的供奶牛走的有向小路.贝茜对她识别方向的能力不是很自信,所以她每次总是从一条小路的头走到尾,再以这条路的尾作为下一条路的头开始走.  现给出所有路标之间的小路,要求输出贝茜回到谷仓的最短路程(每组输入数据都保证有解).

    Input

        第1行:2个整数T和N.
        第2到T+1行:每行用空格隔开的三个整数描述一条小路.前两个整数是这条小路的尾和头,
    第三个整数是这条小路的长度(不大于100).

    Output

     
        一个整数,表示贝茜从路标N到路标1所经过的最短路程

    Sample Input

    5 5
    1 2 20
    2 3 30
    3 4 20
    4 5 20
    1 5 100

    Sample Output

    90


    共有5个路标,贝茜可以依次经过路标4,3,2,1到家

    HINT

     

    Source

    Gold

    题解:么么哒又是一道双倍经验水题= =(HansBug:还有话说这不是最短路模板题么= =)

    按照惯例,我还是贴两个代码,我只笑笑不说话

     1 /**************************************************************
     2     Problem: 3386
     3     User: HansBug
     4     Language: Pascal
     5     Result: Accepted
     6     Time:24 ms
     7     Memory:796 kb
     8 ****************************************************************/
     9  
    10 type
    11     point=^node;
    12     node=record
    13                g,w:longint;
    14                next:point;
    15     end;
    16 VAR
    17    i,j,k,l,m,n,f,r:longint;
    18    a:array[0..10000] of point;
    19    c,g:array[0..10000] of longint;
    20    d:array[0..100000] of longint;
    21    p:point;
    22 procedure add(x,y,z:longint);
    23           var p:point;
    24           begin
    25                new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p;
    26           end;
    27 begin
    28      readln(m,n);
    29      for i:=1 to n do a[i]:=nil;
    30      for i:=1 to m do
    31          begin
    32               readln(j,k,l);
    33               add(j,k,l);add(k,j,l);
    34          end;
    35      fillchar(g,sizeof(g),0);
    36      fillchar(c,sizeof(c),-1);
    37      c[1]:=0;f:=1;r:=2;d[1]:=1;g[1]:=1;
    38      while f<r do
    39            begin
    40                 p:=a[d[f]];
    41                 while p<>nil do
    42                       begin
    43                            if (c[p^.g]=-1) or (c[p^.g]>(c[d[f]]+p^.w)) then
    44                               begin
    45                                    c[p^.g]:=c[d[f]]+p^.w;
    46                                    if g[p^.g]=0 then
    47                                       begin
    48                                            g[p^.g]:=1;
    49                                            d[r]:=p^.g;
    50                                            inc(r);
    51                                       end;
    52                               end;
    53                            p:=p^.next;
    54                       end;
    55                 g[d[f]]:=0;inc(f);
    56            end;
    57      writeln(c[n]);
    58      readln;
    59 end.    
     1 /**************************************************************
     2     Problem: 1752
     3     User: HansBug
     4     Language: Pascal
     5     Result: Accepted
     6     Time:24 ms
     7     Memory:796 kb
     8 ****************************************************************/
     9  
    10 type
    11     point=^node;
    12     node=record
    13                g,w:longint;
    14                next:point;
    15     end;
    16 VAR
    17    i,j,k,l,m,n,f,r:longint;
    18    a:array[0..10000] of point;
    19    c,g:array[0..10000] of longint;
    20    d:array[0..100000] of longint;
    21    p:point;
    22 procedure add(x,y,z:longint);
    23           var p:point;
    24           begin
    25                new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p;
    26           end;
    27 begin
    28      readln(m,n);
    29      for i:=1 to n do a[i]:=nil;
    30      for i:=1 to m do
    31          begin
    32               readln(j,k,l);
    33               add(j,k,l);add(k,j,l);
    34          end;
    35      fillchar(g,sizeof(g),0);
    36      fillchar(c,sizeof(c),-1);
    37      c[1]:=0;f:=1;r:=2;d[1]:=1;g[1]:=1;
    38      while f<r do
    39            begin
    40                 p:=a[d[f]];
    41                 while p<>nil do
    42                       begin
    43                            if (c[p^.g]=-1) or (c[p^.g]>(c[d[f]]+p^.w)) then
    44                               begin
    45                                    c[p^.g]:=c[d[f]]+p^.w;
    46                                    if g[p^.g]=0 then
    47                                       begin
    48                                            g[p^.g]:=1;
    49                                            d[r]:=p^.g;
    50                                            inc(r);
    51                                       end;
    52                               end;
    53                            p:=p^.next;
    54                       end;
    55                 g[d[f]]:=0;inc(f);
    56            end;
    57      writeln(c[n]);
    58      readln;
    59 end.    
  • 相关阅读:
    codeforces C. Fixing Typos 解题报告
    codeforces B. The Fibonacci Segment 解题报告
    codeforces B. Color the Fence 解题报告
    codeforces B. Petya and Staircases 解题报告
    codeforces A. Sereja and Bottles 解题报告
    codeforces B. Levko and Permutation 解题报告
    codeforces B.Fence 解题报告
    tmp
    API 设计 POSIX File API
    分布式跟踪的一个流行标准是OpenTracing API,该标准的一个流行实现是Jaeger项目。
  • 原文地址:https://www.cnblogs.com/HansBug/p/4428321.html
Copyright © 2011-2022 走看看