Code:
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+7;
const int M=1e3+7;
const int INF=0x3f3f3f3f;
struct edge{
int nxt,v,w;
}e[M];
int h[N],cnt;
string name[N];
map<string,int> where;
int tot;
void add_edge(int u,int v,int w){
e[++cnt].nxt=h[u],e[cnt].v=v,e[cnt].w=w;
h[u]=cnt;
}
int n,m,q;
struct node{
int dis,pos;
bool friend operator <(node x,node y){
return x.dis>y.dis;
}
}tmp;
int dis[N][N];
void Dijkstra(int s){
dis[s][s]=0;
priority_queue<node> q;
tmp.dis=0,tmp.pos=s;
q.push(tmp);
while(!q.empty()){
int u=q.top().pos,_dis=q.top().dis;q.pop();
if(dis[s][u]!=_dis) continue;
for(int i=h[u];i;i=e[i].nxt){
int v=e[i].v,w=e[i].w;
if(dis[s][v]>dis[s][u]+w){
dis[s][v]=dis[s][u]+w;
tmp.dis=dis[s][v],tmp.pos=v;
q.push(tmp);
}
}
}
}
int main(){
memset(dis,0x3f,sizeof dis);
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=m;i++){
string u,v;int w;
cin>>u>>v>>w;
if(where[u]==0) where[u]=++tot,name[tot]=u;
if(where[v]==0) where[v]=++tot,name[tot]=v;
add_edge(where[u],where[v],w);
}
for(int i=1;i<=n;i++)
Dijkstra(i);
cin>>q;
while(q--){
string x,y;
cin>>x>>y;
if(dis[where[x]][where[y]]==INF) cout<<"Roger"<<"\n";
else cout<<dis[where[x]][where[y]]<<"\n";
}
return 0;
}