POJ2387-Til the Cows Come Home Dijkstra

19 篇文章 0 订阅
3 篇文章 0 订阅

Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 68691 Accepted: 23025

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.

链接

一、题意

        一个农场有N个结点,节点之间有T条双向边。问从结点N到达结点1的最短路。

二、思路

        题目中没有说,但是输入是存在重边的,所以应该选择重边中最短的路,然后跑一边最短路算法就行了。

三、代码

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;
typedef long long ll;
const int MAXN = 2020;
const int MOD7 = 1000000007;
const int MOD9 = 1000000009;
const int INF = 2000000000;//0x7fffffff
const double EPS = 1e-9;
const double PI = 3.14159265358979;
const int dir_4r[] = { -1, 1, 0, 0 };
const int dir_4c[] = { 0, 0, -1, 1 };
const int dir_8r[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
const int dir_8c[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

struct Node {
	int v;
	int c;
	Node() {}
	Node(int vv, int cc) :v(vv), c(cc) {}
};

bool operator<(const Node &a, const Node &b) {
	return a.c > b.c;
}

int mat[MAXN][MAXN];
int dis[MAXN];
bool visited[MAXN];
priority_queue<Node> que;

void Dijkstra(int beg, int n) {
	memset(visited, false, sizeof(visited));
	memset(dis, -1, sizeof(dis));
	while (!que.empty()) {
		que.pop();
	}

	dis[beg] = 0;
	que.push(Node(beg, dis[beg]));
	while (!que.empty()) {
		int cur = que.top().v;
		que.pop();
		if (visited[cur]) {
			continue;
		}

		visited[cur] = true;
		for (int i = 0; i < n; ++i) {
			if (!visited[i] && mat[cur][i] != -1) {
				if (dis[i] == -1 || dis[i] > dis[cur] + mat[cur][i]) {
					dis[i] = dis[cur] + mat[cur][i];
					que.push(Node(i, dis[i]));
				}
			}
		}
	}
}

int main() {
	int t, n, u, v, c;

	memset(mat, -1, sizeof(mat));
	scanf("%d%d", &t, &n);
	for (int i = 0; i < t; ++i) {
		scanf("%d%d%d", &u, &v, &c);
		if (mat[u - 1][v - 1] == -1 || mat[u - 1][v - 1] > c) {
			mat[u - 1][v - 1] = mat[v - 1][u - 1] = c;
		}
	}

	Dijkstra(n - 1, n);

	printf("%d\n", dis[0]);

	//system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值