题目:https://vjudge.net/problem/POJ-2387

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位置,输出最短路,其权值是多少?

分析:要充分理解最短路,图的概念。当从1位置到1位置时,此时的权值为0。其他无法到达的位置可以设为无穷大 INF

ac代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>using namespace std;const int maxn = 2*1e3+5;
const int INF = 1e9+7;int val[maxn][maxn];
int d[maxn];
bool used[maxn];
int T, N;int pre[maxn];//dijkstra模板
void dijkstra(int s){fill(d, d+N+1, INF);fill(used, used+N+1, false);fill(pre, pre+N+1, -1);d[s] = 0;while(true){int v = -1;for(int u = 1; u <= N; u++){if(!used[u]&&(v == -1||d[u] < d[v])) v=u;}if(v == -1) break;used[v] = true;for(int u = 0; u <= N; u++){d[u] = min(d[u], d[v] + val[v][u]);}}
}int main()
{scanf("%d%d", &T, &N);for(int i = 0; i <= N; i++){for(int j = 0; j <= N; j++){val[i][j] = INF;    //假设所有位置都无法到达 }val[i][i] = 0;         //在原地打转,其权值为0 }for(int i = 0; i < T; i++){int u, v, w;scanf("%d%d%d", &u, &v, &w);if(w < val[u][v]){val[u][v] = w;      //该图为无向图 val[v][u] = w;} }dijkstra(N);printf("%d\n", d[1]);return 0;
}

记录路径:用一个数组来记录路径,每次记录当前节点的前驱。然后反着输出。

而此题的最后一个节点是1,因此还原的时候从最后一个节点开始。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>using namespace std;const int maxn = 2*1e3+5;
const int INF = 1e9+7;int val[maxn][maxn];
int d[maxn];
bool used[maxn];
int T, N;int pre[maxn];//dijkstra模板
void dijkstra(int s){fill(d, d+N+1, INF);fill(used, used+N+1, false);fill(pre, pre+N+1, -1);d[s] = 0;while(true){int v = -1;for(int u = 1; u <= N; u++){if(!used[u]&&(v == -1||d[u] < d[v])) v = u;}if(v == -1) break;used[v] = true;for(int u = 0; u <= N; u++){if(d[u] > d[v] + val[v][u]){d[u] = d[v] + val[v][u];pre[u] = v;     //记录每一个节点的前驱 } }}
}//路径还原
vector<int> get_path(int t){vector<int>path;for(; t != -1; t = pre[t]){path.push_back(t);} reverse(path.begin(), path.end());return path;
}int main()
{scanf("%d%d", &T, &N);for(int i = 0; i <= N; i++){for(int j = 0; j <= N; j++){val[i][j] = INF;    //假设所有位置都无法到达 }val[i][i] = 0;         //在原地打转,其权值为0 }for(int i = 0; i < T; i++){int u, v, w;scanf("%d%d%d", &u, &v, &w);if(w < val[u][v]){val[u][v] = w;      //该图为无向图 val[v][u] = w;} }dijkstra(N);printf("%d\n", d[1]);//路径输出 vector<int>path;path = get_path(1);for(int i = 0; i < path.size(); i++){printf("%d ", path[i]);} return 0;
}

Til the Cows Come Home (最短路问题, 模板)相关推荐

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

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

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

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

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

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

  4. Til the Cows Come Home(dijkstra)

    题目连接: Til the Cows Come Home 题目: Bessie is out in the field and wants to get back to the barn to get ...

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

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

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

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

  7. 【最短路问题】Til The Cows Come Hone HDU 2387

    题意:给一张图,1为起点,n为终点 求有权图单源最短路. 解题思路:dijkstra+堆优化.用优先队列来实现最小堆.新学到了优先队列和pair的用法 优先队列: priority_queue < ...

  8. 【POJ2387】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 bef ...

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

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

最新文章

  1. 及cp含义_当我们谈论CP时,我们在谈论什么?
  2. NLP入门竞赛,搜狗新闻文本分类
  3. android 网络调试 源代码,一个步骤教你调试Android系统源代码
  4. 使用snoopy logger记录用户命令
  5. VUE初长成【部分小记】
  6. 两台linux之间实现共享文件夹挂载实例,linux之间实现共享文件夹挂载实力
  7. python——数据类型
  8. SmartCode.ETL 这不是先有鸡还是蛋的问题!
  9. cubic-bezier_带CSS中的示例的cube-bezier()函数
  10. php 判断苹果还是安卓,PHP简单判断iPhone、iPad、Android及PC设备的方法
  11. nginx php上传大小设置
  12. Day8_误差反向传播
  13. Cocos2d-3.x Android环境搭建
  14. 类String的比较
  15. iOS底层探索之多线程(三)—初识GCD
  16. 优秀课件笔记——财政学1
  17. Python:实现random forest regressor随机森林回归器算法(附完整源码)
  18. 【段子来袭(已笑晕)】cv领域会议段子
  19. 且听风吟,王者峡谷英雄汇,让我们探一探英雄背后的故事
  20. 2D物理引擎--谁碰了我的奶酪

热门文章

  1. 正则表达式下——相关方法
  2. 生态篇-HBase 生态介绍
  3. node项目发送邮件失败
  4. Linux系统中网络配置详解
  5. OAF_开发系列11_实现OAF通过DataBoundValues动态显示表列的左右对齐
  6. 【数据挖掘笔记八】分类:基本概念
  7. 【正一专栏】梅西、内马尔分开明天会更好
  8. Windows上 万能的串口调试助手
  9. linux 分区 var,Ubuntu下移动/var目录到单独分区后出现的一些问题
  10. python concat函数 多张表_教你用python递归函数求n的阶乘,优缺点及递归次数设置方式