http://www.lydsy.com/JudgeOnline/problem.php?id=2015
裸最短路(' ' ) 不过我最初以为是mst (' ' )
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int maxn = 100010; const int maxe = 100010; const int INF = 0x3f3f3f3f; int n, m, Q; struct edge { int t, d; edge* next; }e[maxe * 2], *head[maxn]; int ne = 0; void addedge(int f, int t, int d) { e[ne].t = t, e[ne].d = d, e[ne].next = head[f], head[f] = e + ne ++; } struct pr { int dis, pos; pr(int a, int b) { dis = a, pos = b; } }; bool operator < (const pr &a, const pr &b) { return a.dis > b.dis; } priority_queue <pr> q; int dis[maxn]; void dijkstra(int s) { memset(dis, INF, sizeof(dis)); dis[s] = 0; for(int i = 1; i <= n; ++ i) q.push(pr(dis[i], i)); while(!q.empty()) { pr x = q.top(); q.pop(); if(x.dis != dis[x.pos]) continue; for(edge* p = head[x. pos]; p; p = p-> next) { if(dis[p-> t] > dis[x. pos] + p-> d) dis[p-> t] = dis[x. pos] + p-> d, q.push(pr(dis[p-> t], p-> t)); } } } int int_get() { int x = 0; char c = (char)getchar(); bool f = 0; while(!isdigit(c)) { if(c == '-') f = 1; c = (char)getchar(); } while(isdigit(c)) { x = x * 10 + (int)(c - '0'); c = (char)getchar(); } if(f) x = -x; return x; } void read() { n = int_get(), m = int_get(); Q = int_get(); for(int i = 1; i <= m; ++ i) { int u, v, w; u = int_get(), v = int_get(), w = int_get(); addedge(u, v, w); addedge(v, u, w); } } void sov() { dijkstra(1) ; while(Q --) { int a, b; a = int_get(), b = int_get(); printf("%d ", dis[a] + dis[b]); } } int main() { //freopen("test.in", "r", stdin); read(), sov(); return 0; }