zoukankan      html  css  js  c++  java
  • POJ-2387-Til the Cows Come Home(最短路)

    Description

    Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. 

    Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it. 

    Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
    Input

    * Line 1: Two integers: T and N 

    * Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
    Output

    * Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
    Sample Input

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

    90


    思路:点对点最短路问题,dijkstra算法就OK;

    坑点:

    1.两棵树之间可能有多条路径,输入时取最小的。

    2.路均是双向路,所以输出时需  G[v][u]=G[u][v]=w;


     1 #include<cstdio>
     2 #include<cstring>
     3 using namespace std;
     4 const int Inf=0x3f3f3f3f,N=1e3+5;
     5 
     6 int G[N][N],dis[N],mark[N];
     7 int n,m;
     8     
     9 void Getmap(){
    10     memset(G,Inf,sizeof(G));
    11     for(int i=0;i<=n;i++)
    12         G[i][i]=0;
    13         
    14     for(int i=0;i<m;i++){
    15         int u,v,w;
    16         scanf("%d%d%d",&u,&v,&w);
    17         if(G[u][v]>w)//两城之间可能有多条路,输入最短的 
    18             G[v][u]=G[u][v]=w;//双向路 
    19     }
    20 }
    21 
    22 void Dijk(){
    23     memset(mark,0,sizeof(mark));
    24     for(int i=1;i<=n;i++)
    25         dis[i]=G[1][i];
    26     mark[1]=1;
    27     for(int k=1;k<=n;k++){
    28          int mini=Inf;
    29          int u;
    30         for(int i=1;i<=n;i++)
    31             if(!mark[i]&&dis[i]<mini){
    32                 mini=dis[i];
    33                 u=i;
    34             }
    35         mark[u]=1;
    36         for(int i=1;i<=n;i++)
    37             if(dis[i]>dis[u]+G[u][i])
    38                 dis[i]=dis[u]+G[u][i];
    39     }     
    40 }
    41 
    42 int main(){
    43     while(~scanf("%d%d",&m,&n)){
    44         
    45     Getmap();
    46     Dijk();
    47     printf("%d
    ",dis[n]);
    48     
    49     }
    50     return 0;
    51 } 
  • 相关阅读:
    开发者最好的推广平台
    [ERR] 2006
    PS通道
    PS图层样式
    PS 图层 蒙版
    科研狗的基本绘图技巧 | PS | AI
    memcached的常规操作:增删改查【转】
    mysql:pt-online-schema-change 在线修改表、删除表数据【转】
    HAProxy的四层与七层的区别及透传IP实战案例【转】
    【springBoot】SpringBoot修改启动logo图案
  • 原文地址:https://www.cnblogs.com/yzhhh/p/9977746.html
Copyright © 2011-2022 走看看