1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include<bits/stdc++.h> using namespace std;
const int maxn = 50000+5; const int INF = 0x3f3f3f3f;
struct Edge{ int v, w; };
struct Item{ int u, dis;
bool operator < (const Item &rhs) const{ return dis > rhs.dis; } };
vector<Edge> G[maxn]; int dist[maxn];
void Dijkstra(int S){ memset(dist, INF, sizeof(dist)); dist[S] = 0;
priority_queue<Item> pq; pq.push({S, 0});
while(!pq.empty()){ Item now = pq.top(); pq.pop();
if(now.dis > dist[now.u]) continue;
for(Edge e : G[now.u]){ if(dist[e.v] > dist[now.u] + e.w){ dist[e.v] = dist[now.u] + e.w; pq.push({e.v, dist[e.v]}); } } } }
int main(){ int t, Case = 1; cin >> t;
while(t--){ int N, M, S, T; cin >> N >> M >> S >> T;
for(int i = 0; i <= N; ++i) G[i].clear();
int u, v, w; for(int i = 0; i < M; ++i){ cin >> u >> v >> w;
G[u].push_back({v, w}); G[v].push_back({u, w}); }
Dijkstra(S);
cout << "Case #" << Case++ << ": "; if(dist[T] == INF) cout << "unreachable" << endl; else cout << dist[T] << endl; } }
|