题目连接: 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.

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

解题思路:

本题的算法是迪科斯彻的最短路径算法, 该算法是一种十分稳定且高效的算法.
其核心思想是一样的, 都是贪心的思维, 用一个dis数组来存放所有点到起始点的距离, 然后每次选出距离起始点最近的点, 一直维护dis数组, 直至找到所有点(或者所求点)到起始点的距离.

AC代码: 复杂度 O(n2)

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MAXN = 1005, INF = 0x3f3f3f3f;
int mp[MAXN][MAXN]; //图
int dis[MAXN]; //记录所有点与起始点的距离
bool vis[MAXN]; //记录起始点到该点是否已经距离最短
int t, n;
void initialize() {cin >> t >> n;memset(dis, 0x3f, sizeof(dis));memset(mp, 0x3f, sizeof(mp)); //默认所有点不可达memset(vis, 0, sizeof(vis)); vis[1] = 1;for (int i = 0; i < t; ++i) {int a, b, c;scanf("%d %d %d", &a, &b, &c);if (c < mp[b][a]) mp[b][a] = mp[a][b] = c;}for (int i = 2; i <= n; ++i) dis[i] = mp[1][i];
}
void dijkstra() {for (int i = 0; i < n - 1; ++i) {int index = 0, temp = INF;for (int j = 1; j <= n; ++j) { //找到起始点的所有可达点中 路径最短的if (dis[j] < temp && !vis[j]) index = j, temp = dis[j];}if (temp == INF || index == n) break; vis[index] = 1;for (int j = 1; j <= n; ++j) { //维护disdis[j] = min(dis[j], dis[index] + mp[index][j]);}}cout << dis[n] << endl;
}
int main(void)
{initialize();dijkstra();return 0;
}

END

Til the Cows Come Home(dijkstra)相关推荐

  1. POJ2387 Til the Cows Come Home -DIJKSTRA 练习

    题目大意是:有N个牛棚和T条边相连,每条边有个权值,问1号到N号牛棚之间的最短距离 本题是又是DIJKSTRA最短路水题,注意任何两个牛棚之间可能有多条路相连,输入时先输入边,再输入点,程序如下: # ...

  2. POJ - Til the Cows Come Home(Dijkstra)

    题意: 有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 分析: 典型的模板题,但是一定要注意有重边,因此需要对输入数据加以判断,保存较短的边,这样才能正确使用模板. ...

  3. poj 2387 Til the Cows Come Home dijkstra

    题意: 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回来. 农场主约翰的田里有n(2<=n<=1000)个地标,唯一编号为1-n.地标 ...

  4. Til the Cows Come Home-poj2387(dijkstra判断重边)

    题目链接 题目大意: 说的是,一只奶牛位于N号节点,输入N个节点和T对双向的边,求出由N到1的最短的距离,其实就是问的单源最短路问题. 两个点可能有多条路,选择最短的. #include<std ...

  5. Til the Cows Come Home(最短路-Dijkstra)

    Til the Cows Come Home(最短路-Dijkstra) judge:https://vjudge.net/contest/297882#problem/A Time limit:10 ...

  6. A - Til the Cows Come Home POJ - 2387

    A - Til the Cows Come Home POJ - 2387 最短路 #include<iostream> #include<cstdio> #include&l ...

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

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

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

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

  9. Til the Cows Come Home(简单的最短路)

    Til the Cows Come Home Bessie 在外地,想要在 Farmer John 叫醒她早上挤奶之前回到谷仓尽可能多地睡觉.Bessie 需要她的美容觉,所以她想尽快回来. Farm ...

最新文章

  1. Pinterest从OpenTSDB切换到他们自己的时间序列数据库
  2. linux入门与常用指令
  3. redis之闪电内幕
  4. python是不是高级语言_Python是什么语言?老男孩教育带你了解!
  5. Github Page创建个人主页以及绑定域名
  6. QT给文本添加链接事件
  7. Elon Musk赞同“火星经济将依靠加密货币运行”言论
  8. EnterpriseLibrary 介绍
  9. 数据结构笔记(十五)-- 数组原理
  10. checkbox选中与取消选择
  11. [[UIScreen mainScreen] bounds] 返回的屏幕尺寸不对
  12. 用于编译cm-12.0 的 local_manifest.xml文件
  13. 【Flutter】Dart中的类和对象
  14. 刷新代码IOS 上拉分页刷新--
  15. 一个失败软件项目的思考
  16. 基于51单片机中文汉字LCD12864滚动显示屏仿真(源码+仿真+全套资料)
  17. 2022南京商业贷款提前还款
  18. Python本地文件合并(csv)
  19. 如何在Chrome中自定义新标签页
  20. vue3 + ts + EsLint + Prettier 规范代码

热门文章

  1. Node Inspector 调试 Node.js 程序
  2. 教育培训行业营销推广方案
  3. 统计学之正态分布检验
  4. FCPX插件:56种高动态HDR视频调色预设HDR Look Effects
  5. 读写锁ReentrantReadWriteLock源码分析
  6. 机器学习(Machine learning: a probabilistic perspective) 第三章阅读笔记
  7. 前端HTML5视频_谷粒音乐实战-张晓飞-专题视频课程
  8. Windows安装lua,并使用SciTE进行编辑
  9. Marvolo Gaunt's Ring(类似于dp的做法)
  10. 2018年Java大企业面试问题