Submission #1097548

# Submission time Handle Problem Language Result Execution time Memory
1097548 2024-10-07T14:45:52 Z hiensumi Alias (COCI21_alias) C++14
70 / 70
33 ms 604 KB
#include <map>
#include <set>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
 
typedef long long ll;
typedef pair<ll, int> pli;
typedef pair<int, int> pii;
 
const int maxn = 1005;
 
int cnt;
map<string, int> ind_map;
 
int ind(string x) {
    if (ind_map.find(x) == ind_map.end())
        ind_map[x] = cnt++;
    return ind_map[x];
}
 
int n, m, q;
vector<pii> e[maxn];
 
ll dist[maxn];
 
int main() {
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int t;
        string x, y;
        cin >> x >> y >> t;
        e[ind(x)].push_back(pii(ind(y), t));
    }
    cin >> q;
    for (int 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);
            int curr_ind = curr.second;
            for (int j = 0; j < (int)e[curr_ind].size(); j++) {
                int nxt = e[curr_ind][j].first;
                int 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;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 21 ms 492 KB Output is correct
4 Correct 33 ms 604 KB Output is correct
5 Correct 4 ms 600 KB Output is correct
6 Correct 4 ms 600 KB Output is correct
7 Correct 4 ms 604 KB Output is correct