欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ - 2387 Til the Cows Come Home 最短路模板(邻接表+优先队列的dijkstra)

程序员文章站 2022-03-22 22:18:23
...

GDUT 2020寒假训练 图论 L

原题链接

题目

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.
样例

input

5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100

output
90

题目大意

贝西在田里,想回到谷仓,在农场主约翰叫醒她早晨挤奶之前,尽可能多地睡一觉。贝西需要睡美容觉,所以她想尽快回来。

农场主约翰的田里有N(2<=N<=1000)个地标,唯一编号为1…N。地标1是谷仓;贝西整天站在其中的苹果树丛是地标N。奶牛在田里用地标之间不同长度的T(1<=T<=2000)双向奶牛径行进。贝西对自己的导航能力不太自信,所以一旦开始,她总是从头到尾都在一条小道上。

根据地标之间的路线,确定贝西返回谷仓所需的最小步行距离。可以保证存在这样的路线。

思路

dijkstra
决定用邻接表存图,总体上还是不变的。
两个结构体,Edge表示边,边的末尾是y 边权为w;Node表示点,表示端点为x的点,距离起点的距离为w
其实和邻接矩阵类似,只不过邻接矩阵的数组开法要开1000*1000 而用vector可以实现动态的长度,而且边的总个数不会超过2000 用邻接矩阵会有空间上的冗余。
用vector的邻接表的存法,是开辟vector的二维数组mp,mp[x]表示以x为起点的所有边的数组,这里要注意,要想访问以x为起点的所有边要这样访问

for(int i=0;i<mp[x].size();i++)
{
	cout<<x<<"->"<<mp[x][i].y<<"="<<mp[x][i].w<<endl;
}

一定要注意这里的数组下标是从0开始的。
然后就是这样的无向图,建边的时候是两组边。
再就是正常的广搜过程,Node记录的是点,我们把更新过的点和起点到这个点的距离放到有限队里里面,每一次都拿距离最短的点出来,让更新这个点与他所连接的点的距离,然后把更新的点再放到优先队列里面。如此往复。
注意,取点的时候,一定要看这个点有没有被更新过,防止重复的更新。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<vector>
#include<memory.h>
#include<algorithm>
using namespace std;
struct Edge{
	int y,w;
};
struct Node{//与dis数组作用类似 表示点x距离起点的距离为w 
	int x,w;
	bool operator < (const Node &rsh)const
	{
		return w>rsh.w;
	}
};
const int maxn=1000;
vector<Edge>mp[maxn];
int dis[maxn];
bool vis[maxn]; 
int main()
{
	int n,t;
	cin>>n>>t;
	for(int i=1;i<=t;i++)
	{
		int x,y,w;
		cin>>x>>y>>w;
		Edge tmp;
		tmp.y=y;tmp.w=w;
		mp[x].push_back(tmp);
		tmp.y=x;
		mp[y].push_back(tmp);
		
	}
	/*for(int i=1;i<=n;i++)
	{
		for(int j=0;j<mp[i].size();j++)//vector访问 一定要注意下标为0开始啊 
		{
			cout<<i<<" "<<mp[i][j].y<<" "<<mp[i][j].w<<endl;
		}
	}*/
	//fill(dis,dis+n+1,0x3f3f3f3f);
	memset(vis,false,sizeof(vis));
	memset(dis,0x3f,sizeof(dis));
	dis[1]=0;
	priority_queue<Node>open;
	Node st={1,0};
	open.push(st);
	Node noww;
	while(!open.empty())
	{
		noww=open.top();
		open.pop();
	//	cout<<noww.x<<" "<<noww.w<<endl;
		if(vis[noww.x]==true)continue;
		vis[noww.x]=true;
		for(int i=0;i<mp[noww.x].size();i++)
		{
			Edge to=mp[noww.x][i];
		//	cout<<"#debug"<<to.y<<" "<<to.w<<endl;
			if(dis[to.y]>dis[noww.x]+to.w)
			{
				dis[to.y]=dis[noww.x]+to.w;
				Node tmp;
				tmp.x=to.y;tmp.w=dis[to.y];
				open.push(tmp);
			}
		}
	}
	cout<<dis[n]<<endl;
	return 0;
}