poj-2387-Til the Cows Come Home(Dijkstra优先队列实现)
2016-08-07 17:00
465 查看
Til the Cows Come Home
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
Sample Output
90
题意:
给出具有T条边,N个顶点的图,求N点到1点的最短距离。
题目链接:Til the Cows Come Home
解题思路:
题意很直接的点明可用Dijkstra算法,此处作为Dijkstra算法优先队列实现的练习。
Dijkstra算法思想是通过循环n次,每次从没有标记的顶点中,选出距离N点(起点)最短的点x,将x选定后,从x出发,更新其所有连接的边(x,y).
每次都要循环打擂,选出最小的,此处是可以优化的,借助C++ STL里面的priority_queue(优先队列),常常需要注意运算符重载下,保证所求元素是优先级最高的。
vector开的二维数组G[x][y],存顶点x的第y条边,结构体node存边的信息----终点和权值。
此题当处理完1号顶点后就可以跳出更新的操作了,总而言之,方法不错,值得学习!
代码:
通用解法:
理想解法:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 44206 | Accepted: 15033 |
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
题意:
给出具有T条边,N个顶点的图,求N点到1点的最短距离。
题目链接:Til the Cows Come Home
解题思路:
题意很直接的点明可用Dijkstra算法,此处作为Dijkstra算法优先队列实现的练习。
Dijkstra算法思想是通过循环n次,每次从没有标记的顶点中,选出距离N点(起点)最短的点x,将x选定后,从x出发,更新其所有连接的边(x,y).
每次都要循环打擂,选出最小的,此处是可以优化的,借助C++ STL里面的priority_queue(优先队列),常常需要注意运算符重载下,保证所求元素是优先级最高的。
vector开的二维数组G[x][y],存顶点x的第y条边,结构体node存边的信息----终点和权值。
此题当处理完1号顶点后就可以跳出更新的操作了,总而言之,方法不错,值得学习!
代码:
通用解法:
#include #include #include #include #include using namespace std; struct node { int ed,v; node(int f,int vv):ed(f),v(vv) {} node() {} }; bool operator < (const node &a,const node &b)//权值小的边优先 { return a.v > b.v; } vector > G; int N,vis[1010],dist[1010]; //dist 为点N到其余顶点的最短距离 const int INF = 2 * 1e5+10;//2000条边,每条边最长100,那么最长距离为INF void dijkstra(int v) { priority_queue qe; for(int i = 1;i <= N;i++) dist[i] = INF; //初始化 dist[v] = 0; memset(vis,0,sizeof(vis)); qe.push(node (v,0)); //起点入队 while(!qe.empty()) { node p = qe.top(); qe.pop(); if(vis[p.ed]) continue; //此点之前已选过,则跳过 vis[p.ed] = 1; for(int i = 0;i < G[p.ed].size();i++){ //遍历当前点的相邻顶点 node q = G[p.ed][i]; if(dist[q.ed] > dist[p.ed] + G[p.ed][i].v) {//更新当前最优点所连接点的dist值 dist[q.ed] = dist[p.ed] + G[p.ed][i].v; qe.push(node (q.ed,dist[q.ed])); } } } } int main() { int t,s,e,v; scanf("%d%d",&t,&N); G.resize(N + 1); node p; for(int i = 0;i < t;i++){ scanf("%d%d%d",&s,&e,&v); G[s].push_back(node (e,v)); //无向边 G[e].push_back(node (s,v)); } dijkstra(N); printf("%d\n",dist[1]); return 0; }
理想解法:
#include #include #include #include #include using namespace std; struct node { int ed,v; node(int f,int vv):ed(f),v(vv) {} node() {} }; bool operator < (const node &a,const node &b)//c++结构体运算符重载书写 { return a.v > b.v; } vector > G; priority_queue qe; int N,vis[1010]; int main() { int t,s,e,v; scanf("%d%d",&t,&N); G.resize(N + 1); node p; for(int i = 0;i < t;i++){ scanf("%d%d%d",&s,&e,&v); G[s].push_back(node (e,v)); G[e].push_back(node (s,v)); } qe.push(node (N,0)); memset(vis,0,sizeof(vis)); while(!qe.empty()){ p = qe.top(); qe.pop(); if(vis[p.ed]) continue; vis[p.ed] = 1; if(p.ed == 1) break; //当1号点已经处理过时,就可以结束了 for(int i = 0;i < G[p.ed].size();i++){ node q; q = G[p.ed][i]; if(!vis[q.ed]) { q.v = p.v + G[p.ed][i].v; qe.push(q); } } } printf("%d\n",p.v); return 0; }
相关文章推荐
- POJ 2387-Til the Cows Come Home(Dijkstra+堆优化)
- POJ2387--Til the Cows Come Home--Dijkstra算法裸题
- hdu-2544-最短路(Dijkstra + Dijkstra优先队列 + Bellman-ford + SPFA +Floyd) 纯模板题
- Til the Cows Come Home(最短路—Dijkstra算法)
- lua 点号与冒号操作符的区别
- [安卓开发] Glide图片加载库的简单封装使用
- ex7.py
- UVa 10780 幂和阶乘 求n!中某个因子的个数
- 举例详解Python中的split()函数的使用方法
- mysql数据库关键字
- 抽象工厂模式
- Gym 100507G The Debut Album (滚动数组dp)
- 缓冲区溢出攻击-入门例子
- ex6.py
- 通过软链接ln -s在home分区存储Tomcat的运行日志
- Python os模块os.system()返回值在windows和linux平台上不同
- Android 开发最佳实践
- ex5.py
- android面试(基础篇 8)
- 一. 开始 Xcode 7 编程之旅