#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll _i=0;
#define ffn(x) _i=x
#define llll pair<ll , ll>
#define stitr set<llll>::iterator
#define fora(y,x) for(ll y=_i;x>y;y++)
#define pb push_back
#define pf push_front
#define debu cout << "hello\n"
#define fi first
#define sec second
#define all(a) a.begin() , a.end()
const ll limit = 1e13 + 7;
const ll ous=1e6 + 5;
const ll dx[4] = {1 , -1 , 0 , 0} , dy[4] = {0,0,-1,1};
void solve(){
ll n , m;cin >> n >> m;
map<string , vector<pair<ll , string>>> adj;
fora(i,m){
string h , y;ll x;cin >> h >> y >> x;
adj[h].pb({x , y});
}
ll q;cin >> q;
while(q--){
map<string , ll> vis;
string src , trg;
cin >> src >> trg;
priority_queue<pair<ll , string> , vector<pair<ll , string>> , greater<pair<ll , string>>> pq;
pq.push({0 , src});
ll kd = 0;
while(!pq.empty()){
ll a = pq.top().fi;
string sa = pq.top().sec;
pq.pop();
if(vis[sa] == 1){
continue;
}
vis[sa] = 1;
//cout << sa << "\n";
if(sa == trg){
cout << a << "\n";
kd = 1;
break;
}
for(auto j:adj[sa]){
if(vis[j.sec] == 0){
pq.push({j.fi + a , j.sec});
}
}
}
if(kd == 0){
cout << "Roger\n";
}
}
return;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
ll t=1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
124 ms |
460 KB |
Output is correct |
4 |
Correct |
149 ms |
576 KB |
Output is correct |
5 |
Correct |
6 ms |
460 KB |
Output is correct |
6 |
Correct |
7 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
460 KB |
Output is correct |