• poj 2387 Dijkstra 模板


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

    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

    直接求最短路:
    代码:
    View Code
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 using namespace std;
     5 
     6 const int INF=9999999;
     7 
     8 int map[1005][1005],vis[1005];
     9 int dis[1005];
    10 int t,n;
    11 
    12 void dijkstra(int s,int t)
    13 {
    14     int i,j,k,min;
    15     for(i=0;i<n;i++)
    16         dis[i]=INF;
    17     dis[s]=0;
    18     for(i=0;i<n;i++)
    19     {
    20         min=INF;
    21         k=-1;
    22         for(j=0;j<n;j++)
    23         {
    24             if(dis[j]<min && vis[j]==0)
    25             {
    26                 min=dis[j];
    27                 k=j;
    28             }
    29         }
    30         vis[k]=1;
    31         if(k==t)
    32         {
    33             printf("%d\n",dis[t]);
    34             return ;
    35         }
    36         for(j=0;j<n;j++)
    37         {
    38             if(vis[j]==0 && dis[k]+map[k][j]<dis[j])
    39                 dis[j]=dis[k]+map[k][j];
    40         }
    41     }
    42 }
    43 
    44 int main()
    45 {
    46     while(scanf("%d%d",&t,&n)!=EOF)
    47     {
    48         int i,j;
    49         int x,y,d;
    50         for(i=0;i<n;i++)
    51         {
    52             for(j=0;j<n;j++)
    53             {
    54                 map[i][j]=INF;
    55             }
    56             vis[i]=0;
    57         }
    58         for(i=0;i<t;i++)
    59         {
    60             scanf("%d%d%d",&x,&y,&d);
    61             if(map[x-1][y-1]>d)
    62             {
    63                 map[x-1][y-1]=d;
    64                 map[y-1][x-1]=d;
    65             }
    66         }
    67         dijkstra(0,n-1);
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    Jlist的用法
    Swing中Timer定时器的使用
    埃氏筛法
    ACM排序题
    《C语言程序设计》9.6
    从字符串中提取数
    字符串排序
    树—线索二叉树的创建&二叉树的后序遍历&中序线索化&中序遍历线索二叉树
    《大话数据结构》中介绍的三种树的存储结构的表示方法的总结
    day08
  • 原文地址:https://www.cnblogs.com/shenshuyang/p/2619326.html
Copyright © 2020-2023  润新知