• POJ 2387 Til the Cows Come Home 单源最短路径


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

    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
    
     1 //POJ-2387 单源最短路径
     2 #include<stdio.h>
     3 #define Max 0x3fffffff
     4 int map[1005][1005];
     5 int dis[1005];
     6 
     7 void dijkstra(int n)
     8 {
     9     int visit[1001]={0};
    10     int min,i,j,k;
    11     visit[1]=1;
    12     for(i=1;i<n;++i)
    13     {
    14         min=Max;
    15         k=1;
    16         for(j=1;j<=n;++j)
    17         {
    18             if(!visit[j]&&min>dis[j])
    19             {
    20                 min=dis[j];
    21                 k=j;
    22             }
    23         }
    24         visit[k]=1;
    25         for(j=1;j<=n;++j)
    26         {
    27             if(!visit[j]&&dis[j]>dis[k]+map[k][j])
    28                 dis[j]=dis[k]+map[k][j];
    29         }
    30     }
    31     printf("%d\n",dis[n]);
    32 }
    33 
    34 int main()
    35 {
    36     int t,n,i,j,from,to,cost;
    37     while(scanf("%d%d",&t,&n)!=EOF)
    38     {
    39         for(i=1;i<=n;++i)
    40         {
    41             map[i][i]=0;
    42             for(j=1;j<i;++j)
    43                 map[i][j]=map[j][i]=Max;
    44         }
    45         for(i=1;i<=t;++i)
    46         {
    47             scanf("%d%d%d",&from,&to,&cost);
    48             if(cost<map[from][to])                //可能有多条路,只记录最短的
    49                 map[from][to]=map[to][from]=cost;
    50         }
    51         for(i=1;i<=n;++i)
    52             dis[i]=map[1][i];
    53         dijkstra(n);
    54     }
    55     return 0;
    56 }
    
    
    功不成,身已退
  • 相关阅读:
    Android Studio 快捷键、Debug的使用
    android 控件获取 获取焦点
    jquery里面获取div区块的宽度与高度
    C# ASP.NET MVC 配置允许跨域访问
    分页跳转计算公式
    在vue-cli3 中import引入一个没有export default{}的js文件
    vue中引入css文件
    Vue 导入文件import、路径@和.的区别
    npm 如何查看一个包的版本信息?
    Chrome浏览器常用键盘快捷键介绍
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2627215.html
Copyright © 2020-2023  润新知