zoukankan      html  css  js  c++  java
  • Til the Cows Come Home(最短路)

    Til the Cows Come Home
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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.
     1 #include <iostream>
     2 #include <string>
     3 #include <queue>
     4 #include <cstdio>
     5 #include <cmath>
     6 #include <cstring>
     7 #include <algorithm>
     8 #include <deque>
     9 #include <vector>
    10 #define LL long long
    11 #define MAXI 2147483647
    12 #define MAXL 9223372036854775807
    13 #define dg(i) cout << "*" << i << endl;
    14 using namespace std;
    15 
    16 /**
    17 *spfa算法:邻接矩阵+SLF策略
    18 *顶点标号:1..n
    19 *源点:1,目的点:n
    20 */
    21 
    22 const int sz = 1005;  //size--最大顶点数
    23 const int inf = 0x3f3f3f3f;  //最大值
    24 int w[sz][sz];  //w[i][j]--边(i,j)的权值,若(i,j)不存在,w[i][j]为inf
    25 int dis[sz];  //dis[i]--源点到顶点i的最短距离,初始化为inf
    26 bool in[sz];  //in[i]--标记顶点i是否在队列中,在为true
    27 int n, m;  //n--顶点数,m--边数
    28 
    29 int main()
    30 {
    31     while(scanf("%d %d", &m, &n) != EOF)
    32     {
    33         int u, v, d;
    34         memset(dis, inf, sizeof(dis));  //0x3f3f3f3f是可以用memset初始化的
    35         dis[1] = 0;  //
    36         memset(w, inf, sizeof(w));
    37         while(m--)
    38         {
    39             scanf("%d %d %d", &u, &v, &d);
    40             w[u][v] = w[v][u] = min(d, w[u][v]);  //为避免平行边,取min
    41         }
    42         memset(in, false, sizeof(in));
    43         deque<int> que;
    44         que.push_front(1);  //源点进队
    45         in[1] = true;
    46         while(!que.empty())
    47         {
    48             int cur = que.front();
    49             que.pop_front();
    50             in[cur] = false;  //撤销进队标志
    51             for(int i = 1; i <= n; i++)  //对与cur相邻的点都进行松弛计算
    52             {
    53                 if(dis[cur] + w[cur][i] < dis[i])
    54                 {
    55                     dis[i] = dis[cur] + w[cur][i];  //更新最短距离
    56                     if(!in[i])  //对于最短距离被更新的点,若不在队列则进队
    57                     {
    58                         //此处执行SLF策略,小的尽量放前面。不采取这个策略而直接让i进队也可以
    59                         if(!que.empty() && dis[i] < dis[que.front()])
    60                             que.push_front(i);
    61                         else que.push_back(i);
    62                         in[i] = true;
    63                     }
    64                 }
    65             }
    66         }
    67         printf("%d
    ", dis[n]);
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    ectouch第四讲 之缓存文件的生成
    ectouch第三讲之加载调用机制
    Ubuntu 16.04下安装sublime Text的插件
    有关于Git操作(持续更新)
    MongoDB简单查询语句<平时使用语录,持续更新>
    Ruby小白入门笔记之<Rubymine工具的快捷键>
    Ruby小白入门笔记之 <Gemfile 文件>
    Ubuntu 16.04安装、卸载mysql及怎么使用SQL常用操作语句
    Git中.gitignore忽略文件(maven项目)
    Git入门之常用命令(转载阮一峰老师)
  • 原文地址:https://www.cnblogs.com/cszlg/p/3299555.html
Copyright © 2011-2022 走看看