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

    问题描述

    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.

    输入

    * 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.

    输出

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

    样例输入

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

    样例输出

    90

    dijkstra的模板
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 const int N=1001;
     6 const int inf=1<<29;
     7 int n,m,u,v,c;
     8 int w[N][N],d[N];
     9 bool vis[N];
    10 void dij()
    11 {
    12     for(int i=1; i<=n; i++)
    13     {
    14         d[i]=inf;
    15     }
    16     d[1]=0;
    17     for(int i=0; i<n; i++)
    18     {
    19         int now=inf;
    20         int x;
    21         for(int j=1; j<=n; j++)
    22         {
    23             if(!vis[j]&&now>d[j])
    24             {
    25                 now=d[j];
    26                 x=j;
    27             }
    28         }
    29         vis[x]=1;
    30         for(int j=1; j<=n; j++)
    31         {
    32             if(!vis[j]&&d[j]>d[x]+w[x][j])
    33             {
    34                 d[j]=d[x]+w[x][j];
    35             }
    36         }
    37     }
    38 }
    39 int main()
    40 {
    41     while(scanf("%d%d",&m,&n)!=EOF)
    42     {
    43         memset(vis,0,sizeof(vis));
    44         for(int i=1; i<=n; i++)
    45         {
    46             for(int j=1; j<=n; j++)
    47             {
    48                 w[i][j]=inf;
    49             }
    50         }
    51         for(int i=1; i<=m; i++)
    52         {
    53             scanf("%d%d%d",&u,&v,&c);
    54             if(c<w[u][v])
    55                 w[u][v]=w[v][u]=c;
    56         }
    57         dij();
    58         printf("%d
    ",d[n]);
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    Windows核心编程笔记:同步设备I/O与异步设备I/O 200404
    最顶层窗口
    Windows环境变量
    如何使用Beyond Compare比较两个文件夹的差异
    c++生成缩略图
    MD5加密算法
    c++读写注册表
    通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号
    Hive中HSQL中left semi join和INNER JOIN、LEFT JOIN、RIGHT JOIN、FULL JOIN区别
    postgreSQL格式化时间的函数详解
  • 原文地址:https://www.cnblogs.com/tianmin123/p/4783931.html
Copyright © 2011-2022 走看看