您的位置:首页 > 其它

poj-2387-Til the Cows Come Home(Dijkstra优先队列实现)

2016-08-07 17:00 465 查看
Til the Cows Come Home

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 44206 Accepted: 15033
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
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息