# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
851737 | fcmalkcin | Alias (COCI21_alias) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define endl "\n"
const ll maxn=1e5+54;
const ll mod =998244353 ;
const ll base=3e18;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
ll cnt;
map<string, ll> ind_map;
ll ind(string x) {
if (ind_map.find(x) == ind_map.end())
ind_map[x] = cnt++;
return ind_map[x];
}
ll n, m, q;
vector<pii> e[maxn];
ll dist[maxn];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("guard.in", "r"))
{
freopen("guard.in", "r", stdin);
freopen("guard.out", "w", stdout);
}
cin >> n >> m;
for (int i = 0; i < m; i++) {
ll t;
string x, y;
cin >> x >> y >> t;
e[ind(x)].push_back(pii(ind(y), t));
}
cin >> q;
for (ll i = 0; i < q; i++) {
string x, y;
cin >> x >> y;
set<pli> s;
memset(dist, -1, sizeof dist);
dist[ind(x)] = 0;
s.insert(pli(0, ind(x)));
while (!s.empty()) {
pli curr = *s.begin();
s.erase(curr);
ll curr_ind = curr.second;
for (ll j = 0; j < (ll)e[curr_ind].size(); j++) {
ll nxt = e[curr_ind][j].first;
ll d = e[curr_ind][j].second;
if (dist[nxt] == -1 || dist[nxt] > dist[curr_ind] + d) {
s.erase(pli(dist[nxt], nxt));
dist[nxt] = dist[curr_ind] + d;
s.insert(pli(dist[nxt], nxt));
}
}
}
if (dist[ind(y)] == -1)
cout << "Roger" << endl;
else
cout << dist[ind(y)] << endl;
}
return 0;
}
/*
5 1
1 2
4
+ 1
?
+ 1
?
*/