/*
题意:一个农场有n(1 ~ 1000)个landmarks,有t(1 ~ 2000)条道路连接,
问Bessie要从编号为 n 的landmarks到编号为 1 的landmarks,最少得走多少的路程?
*/
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <bitset>
#include <list>
#include <map>
#include <set>
#include <iterator>
#include <algorithm>
#include <functional>
#include <utility>
#include <sstream>
#include <climits>
#include <cassert>
#define BUG puts("here!!!");
using namespace std;
const int N = 1005;
const int INF = 0x7fffffff;
int data[N][N];
int lowc[N];
int vis[N];
int n, m;
void djst(int p) {
memset(vis, 0, sizeof(vis));
memset(lowc, 0, sizeof(lowc));
for(int i = 1; i <= n; i++) {
lowc[i] = data[p][i];
}
vis[p] = 1;
for(int i = 1; i <= n-1; i++) {
int mini = INF, c = 0;
for(int j = 1; j <= n; j++) {
if(!vis[j] && lowc[j] < mini) {
mini = lowc[j];
c = j;
}
}
if(c == 1) break;
vis[c] = 1;
for(int j = 1; j <= n; j++) {
if(!vis[j] && data[c][j] < INF && data[c][j] + mini < lowc[j]) {
lowc[j] = data[c][j] + mini;
}
}
}
cout << lowc[1] << endl;
}
int main() {
cin >> m >> n;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
data[i][j] = INF;
}
}
int u, v, w;
for(int i = 0; i < m; i++) {
cin >> u >> v >> w;
if(w < data[u][v]) {
data[u][v] = w;
data[v][u] = w;
}
}
djst(n);
}
转载于:https://www.cnblogs.com/robbychan/archive/2013/01/18/3787206.html