/*
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef long double ld;
typedef pair<int , int> pii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1100;
const ll mod = 1e9+7;
const ld PI = acos((ld)-1);
#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}
map < string , int > mp;
vector < pii > adj[maxn];
int sz = 0;
int n , m , q;
int dist[maxn][maxn];
priority_queue < pii > pq;
bool mark[maxn];
void djk(int s){
ms(mark , 0);
dist[s][s] = 0;
pq.push({dist[s][s] , s});
while(pq.size()){
int v = pq.top().second;
int d = -pq.top().first;
pq.pop();
if(mark[v])continue;
mark[v] = 1;
for(auto u : adj[v]){
if(dist[s][u.first] == -1 or dist[s][u.first] > d + u.second)
dist[s][u.first] = d + u.second , pq.push({-dist[s][u.first] , u.first});
}
}
}
int32_t main(){
migmig;
cin >> n >> m;
for(int i = 1 ; i <= m ; i ++){
string a , b;
int w;
cin >> a >> b >> w;
if(!mp.count(a))mp[a] = ++sz;
if(!mp.count(b))mp[b] = ++sz;
adj[mp[a]].pb({mp[b] , w});
}
ms(dist , -1);
for(int i = 1 ; i <= n ; i ++)djk(i);
cin >> q;
while(q --){
string a , b;
cin >> a >> b;
if(dist[mp[a]][mp[b]] == -1)cout << "Roger" << endl;
else cout << dist[mp[a]][mp[b]] << endl;
}
return(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9804 KB |
Output is correct |
2 |
Correct |
5 ms |
9804 KB |
Output is correct |
3 |
Correct |
8 ms |
9844 KB |
Output is correct |
4 |
Correct |
10 ms |
9952 KB |
Output is correct |
5 |
Correct |
9 ms |
9932 KB |
Output is correct |
6 |
Correct |
9 ms |
9932 KB |
Output is correct |
7 |
Correct |
9 ms |
9932 KB |
Output is correct |