# POJ 2387 Til the Cows Come Home （最短路径 模版题 三种解法）

// Dijkstra
#include <iostream>
using namespace std;

const int INTFY = 1 << 29;
const int MAX   = 1005;
int map[MAX][MAX];
int n,m;

void dijkstra() {
int min, v;
int d[MAX];
bool vis[MAX];

for(int i = 1; i <= n; i++) {
vis[i] = 0;
d[i]   = map[1][i];
}

for(int i = 1; i <= n; i++) {
min = INTFY;
for(int j = 1; j <= n; j++)
if(!vis[j] && d[j] < min) {
v   = j;
min = d[j];
}
vis[v] = 1;

for(int j = 1; j <= n; j++)
if(!vis[j] && d[j] > map[v][j] + d[v])
d[j] = map[v][j] + d[v];
}
cout << d[n] << endl;
}

int main() {
int a, b, c;
while(cin >> m >> n) {
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(i == j)
map[i][i] = 0;
else map[i][j] = map[j][i] = INTFY;

for(int i = 1; i <= m; i++) {
cin >> a >> b >> c;
if(map[a][b] > c) map[a][b] = map[b][a] = c;
}
dijkstra();
}
return 0;
}
// Bellman-ford
#include <iostream>
using namespace std;

const int INFTY  = 1 << 29;
const int MAXM = 2005;
const int MAXV = 1005;

typedef struct {
int a, b, w;
}Edge;

Edge edge[MAXM];
int n, m;

void bellman_ford() {
int d[MAXV];
for(int i = 2; i <= n; i++) d[i] = INFTY;
d[1] = 0;

for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(d[edge[j].a] > edge[j].w + d[edge[j].b]) d[edge[j].a] = edge[j].w + d[edge[j].b];
if(d[edge[j].b] > edge[j].w + d[edge[j].a]) d[edge[j].b] = edge[j].w + d[edge[j].a];
}
}
cout << d[n] << endl;
}

int main() {
int a, b, c;
while(cin >> m >> n) {
for(int i = 1; i <= m; i++) {
cin >> a >> b >> c;
edge[i].a = a;
edge[i].b = b;
edge[i].w = c;
}
bellman_ford();
}
return 0;
}
// Spfa
#include <iostream>
#include <queue>
using namespace std;

const int INFTY = 1 << 29;
const int MAXM = 4005;
const int MAXV = 1005;

typedef struct{
int a, b, w, next;
}Edge;

Edge edge[MAXM];

void spfa() {
int d[MAXV], vis[MAXV];
queue<int> q;
for(int i = 2; i <= n; i++) {
d[i] = INFTY;
vis[i] = 0;
}

d[1] = 0;
vis[1] = 1;
q.push(1);

while(!q.empty()) {
int v = q.front(); q.pop();
vis[v] = 0;

for(int i = headlist[v]; i != -1; i = edge[i].next)
if(d[v] + edge[i].w < d[edge[i].b]) {
d[edge[i].b] = d[v] + edge[i].w;
if(!vis[edge[i].b]) {
vis[edge[i].b] = 1;
q.push(edge[i].b);
}
}
}
cout << d[n] << endl;
}

int main() {
int a, b, c;
while(cin >> m >> n) {
for(int i = 1; i <= n; i++) headlist[i] = -1;
for(int i = 1; i <= 2 * m; i += 2) {
cin >> a >> b >> c;
edge[i].a = a;
edge[i].b = b;
edge[i].w = c;
edge[i+1].a = b;
edge[i+1].b = a;
edge[i+1].w = c;
}