题干:

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.

解题报告:

单源最短路裸题。

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>using namespace std;
const int MAX = 100000 + 5;
const int INF = 0x3f3f3f3f ;
int vis[MAX];
int maze[1005][1005];
int n,m;
int dis[MAX];//表示从出发点开始到该点的最短距离。 void Dijkstra(int u,int v) {dis[u] = 0;
//  vis[u] = 1;        //这步还真不能加上。。。。。 int minw = INF,minv;for(int k = 1;k<= n;k++) {minw = INF;for(int i = 1; i<=n; i++) {if(vis[i] ) continue;if(dis[i] < minw) {minv = i; minw = dis[i];}}vis[minv] = 1;if(minv == v) break;for(int i = 1; i<=n; i++) {if(vis[i]) continue;if(maze[minv][i] == 0) continue;dis[i] = min(dis[i], dis[minv] + maze[minv][i]);}}printf("%d\n",dis[v]);} int main()
{int a,b,w;while(~scanf("%d %d",&m,&n) ) {memset(dis,0x3f3f3f3f,sizeof(dis) ) ;memset(maze,0x3f3f3f3f,sizeof(maze) );memset(vis,0,sizeof(vis) ) ;for(int i = 1; i<=m; i++) {scanf("%d%d%d",&a,&b,&w);if(w<maze[a][b])maze[a][b] = maze[b][a] = w;}Dijkstra(1,n); }return 0 ;} 

AC代码2:(Bellman-Ford算法)

//*bellman算法:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 2010
#define MAX 99999999
using namespace std ;
struct node {int a , b , w ;
} edge[N];
int n , m ;
void bell() {int i , j ;int  d[N];for(int i =1 ; i<=n; i++) { //*距离初始化为无穷;d[i]=MAX;}d[1]=0;//*初始地点为0;for(i=1; i<=n; i++) {for(j=1; j<=m; j++) { //*按点-边搜,顺便解决了重边问题;if(d[edge[j].a]>d[edge[j].b]+edge[j].w) d[edge[j].a]= d[edge[j].b]+edge[j].w;if(d[edge[j].b]>d[edge[j].a]+edge[j].w) d[edge[j].b]= d[edge[j].a]+edge[j].w;}}printf("%d\n",d[n]);
}
int main() {int i , a   , b ,c;while(cin>>m>>n) {for(int i =1 ; i<=m; i++) { //*结构体存边和权cin>>a>>b>>c;edge[i].a=a;edge[i].b=b;edge[i].w=c;}bell();}return 0 ;
}

总结:

别忘处理一下重边!

【POJ - 2387】 Til the Cows Come Home(单源最短路Dijkstra算法)相关推荐

  1. 单源最短路——dijkstra算法

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. 问 ...

  2. 单源最短路 Dijkstra算法 和 SPFA算法

    单源最短路 •从一个点出发,到达其他顶点的最短路径的长度. •基本操作:松弛 •d[u]+map[u, v]< d[v]这样的边(u,v)称为紧的(tense),可以对它进行松弛(relax): ...

  3. 最短路问题之单源最短路-Dijkstra算法

    一个点(源点)到其余各个顶点的最短路径,叫做单源最短路经. 例如求下图中的1号顶点到2,3,4,5,6号顶点的最短路径. 使用二维数组e来存储顶点之间边的关系,初始值如下表. e 1 2 3 4 5 ...

  4. 单源最短路 dijkstra算法模板

    链接: 模板题 不能处理带负边权的情况 /* 邻接矩阵存图 1.从源点开始每次选取一个离点集距离最近的点t 添加到集合中 2.利用t点对集合中的点进行松弛操作,进行更新 时间复杂度o(n²) */ # ...

  5. Poj 2387 Til the Cows Come Home 迪杰斯特拉(普通+优化)

    Til the Cows Come Home 迪杰斯特拉(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回来. 农场主约翰的田里有n( ...

  6. 图的单源最短路径:Dijkstra算法实现

    本文介绍的是图的非负权值的单源最短路径问题.问题的提出是,对于有权图D,t提供源点v,要找到从v到其他所有点的最短路径,即单源最短路径问题,在本文中,解决这一问题,是普遍比较熟悉的Dijkstra算法 ...

  7. dijkstra算法PHP,单源最短路径(dijkstra算法)php实现

    做一个医学项目,其中在病例评分时会用到单源最短路径的算法.单源最短路径的dijkstra算法的思路如下: 如果存在一条从i到j的最短路径(Vi.....Vk,Vj),Vk是Vj前面的一顶点.那么(Vi ...

  8. 算法百题斩其三: 单源最短路与算法——其一

    算法百题斩其三: 单源最短路与算法--其一 写在前面:何所谓"斩"? 斩,即快速而有力地切断,指我们用最精简的语言,一针见血地点破算法题的核心难点.斩需三思而后行:斩需借助外力.旁 ...

  9. C语言基本数据结构之三(图的广度及深度遍历,求单源最短路径的Dijkstra算法)

    上一篇主要讲了二叉树的先序,中序,后序遍历算法以及深度和节点的算法,这篇就讲一讲图的基本算法. 一.图的基本概念 1.1有向图G1: 有向图G是由两个集合V(G)和E(G)组成的,其中:V(G)是顶点 ...

最新文章

  1. ue编辑器拖拽上传图片_为百度UE编辑器上传图片添加水印功能
  2. vue.js 三(数据交互)isomorphic-fetch
  3. 基层数字化治理困境如何破局?
  4. @responseBody注解的使用
  5. 世界顶尖品牌的经典广告词欣赏
  6. 政府大数据治理的挑战及对策
  7. MySQL读锁的区别和应用场景分析
  8. Broadcom获得65亿美元过度贷款以收购Brocade
  9. (94)FPGA 两个触发器时序分析模型中,涉及到哪些参数?,面试必问(十八)(第19天)
  10. 论文Real-Time Lane and Obstacle Detection on the global System
  11. 主表和附表的关联关系,普通字段就可以实现为什么还要有主键外键?之间有什么关系
  12. 斯坦福NLP名课带学详解 | CS224n 第14讲 - Transformers自注意力与生成模型(NLP通关指南·完结)
  13. tpadmin的坑收集 nginx下配置tp5失败
  14. 第十八届全国大学生智能车竞赛竞速比赛规则(讨论版)
  15. 深圳市已获取支付牌照公司
  16. 纯前端项目文件部署到远程服务器
  17. 【懒懒的Python学习笔记八】
  18. Facebook广告如何精准投放呢?Facebook广告投放方案
  19. 1-2BP神经网络--Keras实现
  20. 海致大数据京信_God-Of-BigData

热门文章

  1. [剑指offer]面试题第[45]题[JAVA][把数组排成最小的数][快排][ Comparator][PriorityQueue]
  2. php 汉字分割,php支持中文字符串分割的函数
  3. python enumerate_Python中enumerate用法详解
  4. 北京交通大学计算机系2018年录取情况,北京交通大学2018年高招录取分数线汇总...
  5. js中报错 ajax不存在,AJAX
  6. git 合并冲突_GIT提交记录和Revert commit过程分析
  7. display:inline-block之用法
  8. linux中将hdfs数据导入hbase,将数据文件导入到HBase中
  9. redis 多线程_唬人的Redis多线程,也就那么回事
  10. linux组成,Linux学习笔记之Linux组成及初识