Question analysis: the bare shortest path, but there are a lot of nodes. The adjacent matrix will be RE, and the graph will be stored in the adjacent linked list. Then BallmanFord went on.
Vomit: first, I didn't read the data and RESS all the time. Then, when I output the data, I reversed it with the number. I 've been WA, wow!
Code:
#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;const int INF = 2000000010;const int maxn = 2*50005;int u[maxn],v[maxn],w[maxn];int first[maxn],next[maxn];int n,m,s,t;queue<int> q;bool inq[maxn];int d[maxn];void Bellman_Ford(){ for(int i = 0; i < n; i++) d[i] = INF,inq[i] = false; d[s] = 0; q.push(s); inq[s] = true; while(!q.empty()) { int x = q.front(); q.pop(); inq[x] = false; for(int e = first[x]; e != -1; e = next[e]) { if(d[v[e]] > d[x] + w[e]) { d[v[e]] = d[x] + w[e]; if(!inq[v[e]]) { inq[v[e]] = true; q.push(v[e]); } } } }}int main(){ int N; scanf("%d",&N); for(int k = 1; k <= N; k++) { scanf("%d%d%d%d",&n,&m,&s,&t); for(int i = 0; i < n; i++) first[i] = -1; for(int e = 0,i = 0; i < m; e += 2,i++) { scanf("%d%d%d",&u[e],&v[e],&w[e]); next[e] = first[u[e]]; first[u[e]] = e; u[e+1] = v[e]; v[e+1] = u[e]; w[e+1] = w[e]; next[e+1] = first[v[e]]; first[v[e]] = e + 1; } Bellman_Ford(); printf("Case #%d: ",k); if(d[t] != INF) printf("%d\n",d[t]); else printf("unreachable\n"); } return 0;}