Submission #517581

# Submission time Handle Problem Language Result Execution time Memory
517581 2022-01-23T06:10:54 Z kartel Alias (COCI21_alias) C++14
70 / 70
19 ms 472 KB
#include <bits/stdc++.h>
#define pb push_back
#define el "\n"
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("-O3")
using namespace std;
typedef long long ll;

const int N = 2e3 + 500;

vector <pair <int, int> > g[N];
string x, y;
int t, n, m, a, b;
ll d[N];
map <string, int> num;
int ptr = 0;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> x >> y >> t;
        if (num.count(x)) {
            a = num[x];
        } else {
            num[x] = a = ptr++;
        }
        if (num.count(y)) {
            b = num[y];
        } else {
            num[y] = b = ptr++;
        }
        g[a].pb({b, t});
    }

    int q;
    cin >> q;
    while (q--) {
        cin >> x >> y;
        a = num[x]; b = num[y];
        for (int i = 0; i < ptr; i++) {
            d[i] = 1e18;
        }
        d[a] = 0;
        queue <int> q;
        q.push(a);
        while (q.size()) {
            int v = q.front(); q.pop();
            for (auto u : g[v]) {
                if (d[u.first] > d[v] + u.second) {
                    d[u.first] = d[v] + u.second;
                    q.push(u.first);
                }
            }
        }
        if (d[b] != (ll)1e18) {
            cout << d[b] << el;
        } else {
            cout << "Roger" << el;
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 472 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 14 ms 384 KB Output is correct
4 Correct 19 ms 468 KB Output is correct
5 Correct 3 ms 460 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 3 ms 460 KB Output is correct