从点N到1的最短路
*记得无向图两个方向都要建边就好了……
以及多组数据= =
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; #define SZ 4005 #define INF 1e9+10 int head[SZ],nxt[SZ],tot = 0; struct edge { int t,d; }l[SZ]; void build(int f,int t,int d) { l[++tot] = (edge){t,d}; nxt[tot] = head[f]; head[f] = tot; } queue<int> q; bool use[SZ]; int dist[SZ]; int spfa(int s, int e) { memset(dist, 63, sizeof(dist)); memset(use, 0, sizeof(use)); use[s] = 1; dist[s] = 0; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); use[u] = 0; for(int i = head[u];i;i = nxt[i]) { int v = l[i].t; if(dist[v] > dist[u] + l[i].d) { dist[v] = dist[u] + l[i].d; if(!use[v]) use[v] = 1, q.push(v); } } } return dist[e]; } int main() { int T, N; while(scanf("%d %d", &T, &N) != EOF) { memset(head, 0, sizeof(head)); tot = 0; for(int i = 0; i < T; i++) { int x, y, z; scanf("%d %d %d", &x, &y, &z); build(x, y, z); build(y, x, z); } printf("%d ", spfa(N, 1)); } return 0; }