#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const ll INF = 1e18;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
map<string, int> id;
set<string> st;
int cnt = 0;
vector<vector<pair<int, ll>>> g(n);
for (int i = 0; i < m; i++) {
string s1, s2;
cin >> s1 >> s2;
if (st.find(s1) == st.end()) {
st.insert(s1);
id[s1] = cnt++;
}
if (st.find(s2) == st.end()) {
st.insert(s2);
id[s2] = cnt++;
}
ll x;
cin >> x;
g[id[s1]].push_back({id[s2], x});
}
int q;
cin >> q;
for (int t = 0; t < q; t++) {
string s1, s2;
cin >> s1 >> s2;
int s = id[s1];
int f = id[s2];
vector<ll> d(n, INF);
d[s] = 0;
set<pair<ll, int>> queue;
queue.insert({0, s});
while (!queue.empty()) {
int v = queue.begin()->second;
queue.erase(queue.begin());
for (auto& to: g[v]) {
if (d[v] + to.second < d[to.first]) {
queue.erase({d[to.first], to.first});
d[to.first] = d[v] + to.second;
queue.insert({d[to.first], to.first});
}
}
}
if (d[f] == INF) {
cout << "Roger" << endl;
} else {
cout << d[f] << endl;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
25 ms |
324 KB |
Output is correct |
4 |
Correct |
40 ms |
460 KB |
Output is correct |
5 |
Correct |
5 ms |
588 KB |
Output is correct |
6 |
Correct |
4 ms |
620 KB |
Output is correct |
7 |
Correct |
5 ms |
580 KB |
Output is correct |