#include<bits/stdc++.h>
#define ll long long
#define vl vector<ll>
#define sl set<ll>
#define all(v) v.begin(), v.end()
#define f first
#define s second
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define tests while(t--)
using namespace std;
const ll sz = 1010, inf = 1e15;
ll dist[sz];
vector<pair<ll, int>>adj[sz];
int n, m, i, j;
void dijkstra(int src)
{
for(i = 1; i <= n; i++) dist[i] = inf;
dist[src] = 0;
using T = pair<ll, int>;
priority_queue<T, vector<T>, greater<T>>q;
q.push({0ll, src});
while(!q.empty())
{
int node;
ll cdist;
tie(cdist, node) = q.top();
q.pop();
if(cdist != dist[node]) continue;
for(auto u : adj[node])
{
if(cdist + u.s < dist[u.f])
{
dist[u.f] = cdist + u.s;
q.push({dist[u.f], u.f});
}
}
}
}
int main()
{
ll c, l, x, y, q;
string a, b;
cin >> n >> m;
set<string>st;
map<string, ll>mp;
for(i = 0; i < m; i++)
{
cin >> a >> b >> c;
l = st.size();
st.insert(a);
if(st.size() != l)
mp[a] = st.size();
l = st.size();
st.insert(b);
if(st.size() != l)
mp[b] = st.size();
x = mp[a], y = mp[b];
adj[x].pb({y, c});
}
cin >> q;
while(q--)
{
cin >> a >> b;
x = mp[a], y = mp[b];
dijkstra(x);
if(dist[y] == inf) cout << "Roger\n";
else cout << dist[y] << "\n";
}
}
Compilation message
alias.cpp: In function 'int main()':
alias.cpp:53:22: warning: comparison of integer expressions of different signedness: 'std::set<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
53 | if(st.size() != l)
| ~~~~~~~~~~^~~~
alias.cpp:57:22: warning: comparison of integer expressions of different signedness: 'std::set<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
57 | if(st.size() != l)
| ~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
18 ms |
340 KB |
Output is correct |
4 |
Correct |
31 ms |
392 KB |
Output is correct |
5 |
Correct |
8 ms |
468 KB |
Output is correct |
6 |
Correct |
6 ms |
468 KB |
Output is correct |
7 |
Correct |
7 ms |
468 KB |
Output is correct |