#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define sqr(x) ((ll)(x))*(x)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
vector<pair<int, ll>> adj[1005];
ll dist[1005];
int N, M;
ll INF = LLONG_MAX;
void dijkstra(int start_node) {
for (int i = 0; i < N; i++) dist[i] = INF;
dist[start_node] = 0;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq{};
pq.push({0ll,start_node});
while (!pq.empty()) {
ll d; int node; tie(d, node) = pq.top();
pq.pop();
if (d != dist[node]) continue;
for (auto u : adj[node]) {
if (d+u.second < dist[u.first]) {
dist[u.first] = d+u.second;
pq.push({dist[u.first],u.first});
}
}
}
}
map<string, int> stringToNode;
int node = 0;
int main() {
cin >> N >> M;
FOR(i, 0, M) {
string a, b; cin >> a >> b;
if (stringToNode.count(a) == 0) stringToNode[a] = node++;
if (stringToNode.count(b) == 0) stringToNode[b] = node++;
int _a = stringToNode[a];
int _b = stringToNode[b];
ll _d; cin >> _d;
adj[_a].push_back({_b, _d});
}
int T; cin >> T;
FOR(i, 0, T) {
string a, b; cin >> a >> b;
int _a = stringToNode[a];
int _b = stringToNode[b];
dijkstra(_a);
if (dist[_b] == INF) cout << "Roger\n";
else cout << dist[_b] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
20 ms |
332 KB |
Output is correct |
4 |
Correct |
30 ms |
340 KB |
Output is correct |
5 |
Correct |
6 ms |
468 KB |
Output is correct |
6 |
Correct |
6 ms |
468 KB |
Output is correct |
7 |
Correct |
6 ms |
468 KB |
Output is correct |