题目描述

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

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

Sample Output

  1. 90

Hint

INPUT DETAILS:

There are five landmarks.

OUTPUT DETAILS:

Bessie can get home by following trails 4, 3, 2, and 1.

 
 
大意就是求1到n之间的最短路径,由于点的个数只有1000,O(N^2)dijkstra的算法即可解决。
代码如下
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<iostream>
  5. #include<string>
  6. #include<vector>
  7. #include<stack>
  8. #include<bitset>
  9. #include<cstdlib>
  10. #include<cmath>
  11. #include<set>
  12. #include<list>
  13. #include<deque>
  14. #include<map>
  15. #include<queue>
  16. using namespace std;
  17.  
  18. const int INF=2e9;
  19. int V,E;
  20. int d[];
  21. int cost[][];
  22. int used[];
  23. void dijkstra(int s)
  24. {
  25.  
  26. for(int i=;i<=V;i++)
  27. {
  28. used[i]=;
  29. d[i]=INF;
  30. }
  31. d[s]=;
  32. int minn,v;
  33. for(int i=;i<=V;i++)
  34. {
  35. minn=INF;
  36. v=-;
  37. for(int j=;j<=V;j++)
  38. {
  39. if(!used[j]&&d[j]<minn)
  40. {
  41. minn=d[j];
  42. v=j;
  43. }
  44. }
  45. used[v]=;
  46. for(int j=;j<=V;j++)
  47. {
  48. if((!used[j])&&(d[j]>cost[v][j]+d[v]))
  49. {
  50. d[j]=cost[v][j]+d[v];
  51. }
  52. }
  53. }
  54. }
  55. int main()
  56. {
  57. cin>>E>>V;
  58. for(int i=;i<=;i++)
  59. {
  60. for(int j=;j<=;j++)
  61. {
  62. cost[i][j]=INF;
  63. if(i==j)
  64. cost[i][j]=;
  65. }
  66. }
  67. for(int i=;i<=E;i++)
  68. {
  69. int s,t,cos;
  70. cin>>s>>t>>cos;
  71. if(cos<cost[s][t])
  72. {
  73. cost[s][t]=cos;
  74. cost[t][s]=cos;
  75. }
  76. }
  77. dijkstra();
  78. printf("%d\n",d[V]);
  79. return ;
  80. }

poj2387- Til the Cows Come Home(最短路板子题)的更多相关文章

  1. POJ2387 Til the Cows Come Home (最短路 dijkstra)

    AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...

  2. POJ-2387 Til the Cows Come Home ( 最短路 )

    题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...

  3. Til the Cows Come Home(最短路模板题)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Bessie is ...

  4. POJ 2387 Til the Cows Come Home --最短路模板题

    Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...

  5. Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)

    Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...

  6. POj2387——Til the Cows Come Home——————【最短路】

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

  7. POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

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

  8. (Dijkstra) POJ2387 Til the Cows Come Home

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

  9. POJ-2387Til the Cows Come Home,最短路坑题,dijkstra+队列优化

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K       http://poj.org/problem?id=238 ...

  10. POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)

    原题链接:Til the Cows Come Home 题目大意:有  个点,给出从  点到  点的距离并且  和  是互相可以抵达的,问从  到  的最短距离. 题目分析:这是一道典型的最短路径模版 ...

随机推荐

  1. linux传输文件-sftp

    SFTP sftp登陆远程服务器 sftp username@ip 例如:sftp mqadmin@10.10.1.150 然后输入password即可   put:上传文件 例如:put iosta ...

  2. electron 安装过程出现未成功地运行

    问题 正文 产生问题得原因? 是因为之前安装了该程序,但是卸载的时候可能人为的直接删除了卸载程序. 这时候安装包会触发找到注册表中,该appid相同地址的卸载程序位置,然后进行调用,如果没有的话,只会 ...

  3. 10.Android-SharedPreferences使用

    1.SharedPreferences介绍 SharedPreferences,它是一个轻量级的配置文件类,用于保存软件配置参数. 采用xml文件形式存储在/data/data/包名/shared_p ...

  4. Android 有关在ListView RecycleView 中使用EditText Checkbox的坑

    这是一篇文字超多的博客,哈哈哈,废话自行过滤··· 遇到问题 在开发中我们常会在ListView , RecycleView 列表中添加EditText输入框,或者checkbox复选框.   复选框 ...

  5. Python——模块和包

    一.概念 """模块():一个python文件,以 .py 结尾,包含python对象定义和语句.模块可以定义函数.类.变量,也可包含可执行文件 导入模块: 1.impo ...

  6. python提取图片内容并转换成对应表格的markdown代码

    本节我们将介绍使用python识别一张图片中的内容,并试着得到一张表格,当然并不是类似于Excel的表格,而是该表格的markdown代码. 注:原创内容,转载请标明出处! 相关工具的安装 本次实验环 ...

  7. Codeforces Round #340 (Div. 2) E XOR and Favorite Number 莫队板子

    #include<bits/stdc++.h> using namespace std; <<; struct node{ int l,r; int id; }q[N]; in ...

  8. 利用 serviceStack 搭建web服务器

    1,资料地址 参考资料 https://docs.servicestack.net/ https://docs.servicestack.net/create-your-first-webservic ...

  9. 浅谈python的第三方库——numpy(一)

    python作为广受欢迎的一门编程语言,其中很重要的一个原因便是它可以使用很多第三方库. 对第三方库的理解,在笔者看来就是一些python爱好者和专门的研发机构,为满足某一特定应用领域的需要,使用py ...

  10. opencv —— imread、namedWindow & imshow、cvtColor、imwrite 加载、显示、修改、保存图像

    加载图像:imread 函数 Mat imread(const string& filename, int flags = 1): filename:需要载入的图像的路径名. flags:加载 ...