poj 2387 spfa板子
//可是spfa会被卡得很惨很惨wa
//待补充dij
#include <iostream> #include <math.h> #include <string.h> #include <vector> #include <map> #include <queue> #include <stdio.h> #include <algorithm> #include <cstdio> using namespace std; int t = 0, flag[10005], dis[10005], head[10005], book[10005]; struct info { int to, next, lenth; } edge[600000]; queue<int> q; void add(int from, int to, int dis) { edge[++t].next = head[from]; edge[t].to = to; edge[t].lenth = dis; head[from] = t; } int main() { int a, b, c, n, m, sta, end, min, u; //freopen("lys.in","r",stdin); cin >> m>> n; for (int i = 1; i <= m; i++) { scanf("%d %d %d", &a, &b, &c); add(a, b, c); add(b,a,c); } for (int i = 1; i <= n; i++) { dis[i] = 999999; } sta=n; dis[sta] = 0; book[sta] = 1; q.push(sta); while (!q.empty()) { u = q.front(); book[u] = 0; for (int k = head[u]; k != 0; k = edge[k].next) { if (dis[edge[k].to] > dis[u] + edge[k].lenth) { dis[edge[k].to] = dis[u] + edge[k].lenth; if (book[edge[k].to] == 0) { book[edge[k].to] = 1; q.push(edge[k].to); } } } q.pop(); } printf("%d ",dis[1]); }