zoukankan      html  css  js  c++  java
  • POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)

    题目连接:

    http://poj.org/problem?id=2387

    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

    Hint

    INPUT DETAILS: 

    There are five landmarks. 

    OUTPUT DETAILS: 

    Bessie can get home by following trails 4, 3, 2, and 1.
    题意描述:
    最短路水题。
    解题思路:
    处理数据,使用迪杰斯特拉算法。
    AC代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 int e[1010][1010],dis[1010],bk[1010];
     4 int main()
     5 {
     6     int i,j,min,t,t1,t2,t3,n,u,v;
     7     int inf=99999999;
     8     while(scanf("%d%d",&t,&n)!=EOF)
     9     {
    10         for(i=1;i<=n;i++)
    11         {
    12             for(j=1;j<=n;j++)
    13             {
    14                 if(i==j)
    15                 e[i][j]=0;
    16                 else
    17                 e[i][j]=inf;
    18             }
    19         }
    20         for(i=1;i<=t;i++)
    21         {
    22             scanf("%d%d%d",&t1,&t2,&t3);
    23             if(e[t1][t2]>t3)
    24             {
    25                 e[t1][t2]=t3;
    26                 e[t2][t1]=t3;
    27             }
    28         }
    29         for(i=1;i<=n;i++)
    30             dis[i]=e[1][i];
    31         memset(bk,0,sizeof(bk));
    32         bk[1]=1;
    33         for(i=1;i<=n-1;i++)
    34         {
    35             min=inf;
    36             for(j=1;j<=n;j++)
    37             {
    38                 if(bk[j]==0&&dis[j]<min)
    39                 {
    40                     min=dis[j];
    41                     u=j;
    42                 }
    43             }
    44             bk[u]=1;
    45             for(v=1;v<=n;v++)
    46             {
    47                 if(e[u][v]<inf && dis[v]>dis[u]+e[u][v])
    48                         dis[v]=dis[u]+e[u][v];
    49             }
    50         }
    51         printf("%d
    ",dis[n]);
    52     }
    53     return 0;
    54 }
     
  • 相关阅读:
    动态页面技术----EL技术、JSTL技术,javaEE的开发模式
    动态页面技术----JSP技术
    会话技术Cookie&Session
    HttpServletRequest
    设计模式和抽象类、方法
    类的继承
    类与对象
    面向对象和面向过程的区别
    PHP-错误处理
    PHP-文件加载
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/7387613.html
Copyright © 2011-2022 走看看