0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:31:59 +00:00

P2984 [USACO10FEB]Chocolate Giving S

R53548464
This commit is contained in:
Baoshuo Ren 2021-07-19 19:30:02 +08:00 committed by Baoshuo Ren
parent b4bcc0d2ee
commit a4a1d02e43
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

View File

@ -0,0 +1,41 @@
#include <bits/stdc++.h>
using namespace std;
int n, m, b, r, s, l, p, q, dist[50005];
vector<pair<int, int>> g[50005];
bool st[50005];
void dijkstra() {
memset(dist, 0x3f, sizeof(dist));
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
dist[1] = 0;
q.push(make_pair(0, 1));
while (!q.empty()) {
auto t = q.top();
q.pop();
if (st[t.second]) continue;
for (auto i : g[t.second]) {
if (dist[i.first] > t.first + i.second) {
dist[i.first] = t.first + i.second;
q.push(make_pair(dist[i.first], i.first));
}
}
st[t.second] = true;
}
}
int main() {
cin >> n >> m >> b;
for (int i = 1; i <= m; i++) {
cin >> r >> s >> l;
g[r].push_back(make_pair(s, l));
g[s].push_back(make_pair(r, l));
}
dijkstra();
for (int i = 1; i <= b; i++) {
cin >> p >> q;
cout << dist[p] + dist[q] << endl;
}
return 0;
}