zoukankan      html  css  js  c++  java
  • POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387

    Til the Cows Come Home
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 45414   Accepted: 15405

    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
    题目大意:有N个节点,T条路(带权值),求1到N的最短路径。
    解题思路:最短路迪杰斯特拉算法(模板题)。
    AC代码:
     1 #include <stdio.h>
     2 #include <string.h>
     3 #define inf 999999999
     4 int visit[1010];
     5 int dis[1010];
     6 int p[1010][1010];
     7 int n;
     8 int dijkstra()
     9 {
    10     int i,j,pos = 0,minn;
    11     memset(visit,0,sizeof(visit));
    12     visit[1] = 1;
    13     dis[1] = 0;
    14     for (i = 2; i <= n; i ++)
    15         dis[i] = p[1][i];
    16     for (i = 1; i <= n; i ++)
    17     {
    18         minn = inf;
    19         for (j = 1; j <= n; j ++)
    20         {
    21             if (!visit[j] && dis[j] < minn)
    22             {
    23                 minn = dis[j];
    24                 pos = j;
    25             }
    26         }
    27         visit[pos] = 1;
    28         for (j = 1; j <= n; j ++)
    29             if (!visit[j] && dis[j] > dis[pos]+p[pos][j])
    30                 dis[j] = dis[pos]+p[pos][j];
    31     }
    32     return dis[n];
    33 }
    34 int main ()
    35 {
    36     int t,a,b,c,i,j;
    37     while (~scanf("%d%d",&t,&n))
    38     {
    39         for (i = 1; i <= n; i ++)
    40             for (j = 1; j <= n; j ++)
    41                 p[i][j] = inf;
    42 
    43         for (i = 0; i < t; i ++)
    44         {
    45             scanf("%d%d%d",&a,&b,&c);
    46             if (p[a][b] > c)
    47                 p[a][b] = p[b][a] = c;
    48         }
    49         printf("%d
    ",dijkstra());
    50     }
    51     return 0;
    52 }
    View Code
     
  • 相关阅读:
    LR遇到的问题
    常用的mysql操作
    mysql操作
    土地档案管理系统架构图,ER图,用例图
    土地档案管理系统需求分析
    Load data local infile
    Lamda Expression
    Domain Logic approaches
    Spring AOP Capabilities and Goals
    python
  • 原文地址:https://www.cnblogs.com/yoke/p/5862470.html
Copyright © 2011-2022 走看看