POJ - 2387:Til the Cows Come Home

Til the Cows Come Home

来源:POJ

标签:最短路径问题,Dijkstra算法

参考资料:

相似题目:

题目

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.

输入

  • 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.

输出

  • Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.

输入样例

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

输出样例

90

样例解释

INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.

题目大意

N个结点,T条边的无向加权图,计算点N到点1的最短距离。

参考代码

#include
#include
#include
#include
using namespace std;

const int MAXN=1005; //顶点数
const int INF=0x3f3f3f3f;

struct Dijkstra { //求解单源点最短距离问题。适用有向图,且边的权值为负。
	struct Edge {
		int u; //边的一个顶点
		int v; //边的另一个顶点
		int w; //边的权值
		Edge(int u,int v,int w):u(u),v(v),w(w) {}
		bool operator < (const Edge &e) const {
			return w>e.w;
		}
	};

	struct Dist { //源点当前到点u的距离w
		int u;
		int w;
		Dist(int u,int w):u(u),w(w) {}
		bool operator < (const Dist &d) const {
			return w>d.w;
		}
	};

	vector<Edge> adj[MAXN];
	priority_queue<Dist> pq;
	int distTo[MAXN];
	bool vis[MAXN];

	void dijkstra(int s) {
		distTo[s]=0;
		pq.push(Dist(s,0));
		while(!pq.empty()) {
			int u=pq.top().u; pq.pop();
			if(vis[u]==true) continue;
			relax(u);
		}
	}

	void init() {
		for(int i=0; i<MAXN; i++) adj[i].clear();
		while(!pq.empty()) pq.pop();
		memset(distTo,INF,sizeof(distTo));
		memset(vis,0,sizeof(vis));
	}

	void addEdge(int u, int v, int w) {
		adj[u].push_back(Edge(u,v,w));
	}

	void relax(int u) {
		vis[u]=true;
		for(int i=0; i<adj[u].size(); i++) {
			int v=adj[u][i].v;
			if(distTo[v]>distTo[u]+adj[u][i].w) {
				distTo[v]=distTo[u]+adj[u][i].w;
				pq.push(Dist(v,distTo[v]));
			}
		}
	}

};

Dijkstra dijkstra;
int T,N;

int main() {
	dijkstra.init();
	int u,v,w;
	scanf("%d%d",&T,&N);
	for(int i=0; i<T; i++) {
		scanf("%d%d%d",&u, &v, &w);
		dijkstra.addEdge(u,v,w);
		dijkstra.addEdge(v,u,w);
	}
	dijkstra.dijkstra(1);
	printf("%d\n",dijkstra.distTo[N]);
	return 0;
}

你可能感兴趣的:(【记录】算法题解)