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

    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.
     
    题意:有n个点,给出几组数据(两点 和两点之间的距离),求从1 到其他点之间的最短距离。输入:第一行,t,n.t表示给出的数据,n表示共有几个点,接下来t行每行为两个点和两点之间的距离,。输出从1到n之间的最短距离。
     
    思路:最短路问题,题中无负权,所以可以用Floyd算法和Dijkstra算法,但是数据要求为1000,比较大,因此用Dijkstra算法。
     
    代码
     1 #include <cstdio>
     2 #include <fstream>
     3 #include <algorithm>
     4 #include <cmath>
     5 #include <deque>
     6 #include <vector>
     7 #include <queue>
     8 #include <string>
     9 #include <cstring>
    10 #include <map>
    11 #include <stack>
    12 #include <set>
    13 #include <sstream>
    14 #include <iostream>
    15 #define mod 998244353
    16 #define eps 1e-6
    17 #define ll long long
    18 #define INF 0x3f3f3f3f
    19 using namespace std;
    20 
    21 int t,n;
    22 //ma[i][j]存放i到j点之间的距离,dis数组存放1到各点之间的距离
    23 int ma[1010][1010],dis[1010];
    24 void dijkstra(int m)
    25 {
    26     //vis数组标记已经找过的最短路
    27     bool vis[1010];
    28     memset(vis,0,sizeof(vis));
    29     vis[1]=1;
    30     for(int i=1;i<m;i++)
    31     {
    32         //mi记录当前到1距离最小的值
    33         int mi=INF;
    34         //k表示到1距离最小的点
    35         int k=1;
    36         for(int j=1;j<=m;j++)
    37         {
    38             //当未标记这个点,并且这个点到1的距离时最小的时侯成立,
    39             if(!vis[j]&&mi>dis[j])
    40             {
    41                 mi=dis[j];
    42                 k=j;
    43             }
    44         }
    45         //已找到1到k点的最小值,所以标记这个点
    46         vis[k]=1;
    47         for(int j=1;j<=m;j++)
    48         {
    49             //当未标记这个点,且j点到1的距离比1到k的距离加上k到j点的距离小时,更换dis的数据
    50             if(!vis[j]&&dis[j]>(dis[k]+ma[k][j]))
    51             {
    52                 dis[j]=dis[k]+ma[k][j];
    53             }
    54         }
    55     }
    56 }
    57 int main()
    58 {
    59     scanf("%d %d",&t,&n);
    60     //初始化ma数组
    61     for(int i=1;i<=n;i++)
    62     {
    63         //i点和i点之间距离为0
    64         ma[i][i]=0;
    65         for(int j=1;j<i;j++)
    66         {
    67             //初始两点之间距离为INF,
    68             ma[i][j]=ma[j][i]=INF;
    69         }
    70     }
    71     int a,b,c;
    72     for(int i=1;i<=t;i++)
    73     {
    74         scanf("%d %d %d",&a,&b,&c);
    75         //如果输入的两点有重复,则保存小的
    76         if(c<ma[a][b])
    77         {
    78             ma[a][b]=ma[b][a]=c;
    79         }
    80     }
    81     for(int i=1;i<=n;i++)
    82     {
    83         //dis数组保存从1到其他点之间的距离
    84         dis[i]=ma[1][i];
    85     }
    86     dijkstra(n);
    87     printf("%d
    ",dis[n]);
    88 }
  • 相关阅读:
    rocketmq详解
    rocketmq的windows版客户端的启动步骤
    postman提交数组格式方式
    关于springmvc的包含list提交的格式
    数据库字段和实体类和map中对应类型
    怎么连接两个数据库和怎么停止上一个线程
    IDEA 创建类是自动添加注释和创建方法时快速添加注释
    centOs7 安装mysql8
    CentOS7安装Nginx及配置
    windows安装mysql8详解
  • 原文地址:https://www.cnblogs.com/mzchuan/p/11468937.html
Copyright © 2011-2022 走看看