Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <queue>

using namespace std;

int strToTime(string &s){
    return atoi(s.substr(0,2).c_str())*60 + atoi(s.substr(3,2).c_str());
}

struct edge{
    int cost;
    int start;
    int goal;
    int to;
};

vector<edge> G[101];

struct Sit{
    int cTime;
    int cost;
    int node;
};

const int INF=1000000000;

int backMinCost(int idx,int sTime,int snode);

int main(){
    int n;
    while(cin>>n&&n!=0){
        for(int i = 0; i < 101; i++)G[i].clear();
        map<string,vector<pair<int,int> > > startAndFinTimes;
        map<string,int> stationIdx;
        stationIdx["Tokyo"]=0;
        stationIdx["Hakodate"]=1;
        int idx=2;
        for(int i = 0; i < n; i++){
            string st,ft;
            string s1,s2;
            edge e;
            cin>>s1>>st>>s2>>ft>>e.cost;
            e.start=strToTime(st);
            e.goal=strToTime(ft);
            // Ü¾oÄ¢È¢wÅ êÎAm[hXgÉÇÁ
            if(stationIdx.find(s1)==stationIdx.end())
                stationIdx[s1]=idx++;
            if(stationIdx.find(s2)==stationIdx.end())
                stationIdx[s2]=idx++;
            e.to=stationIdx[s2];
            // oHðÇÁ
            G[stationIdx[s1]].push_back(e);
        }

        map<int,int> minCosts[2][101];
        queue<Sit> *prv=new queue<Sit>();
        queue<Sit> *nxt=new queue<Sit>();
        for(int i = 0; i < 2; i++){
            Sit ss;
            ss.cost=0;
            ss.cTime=480;
            ss.node=i;
            prv->push(ss);
            minCosts[i][ss.node][ss.cTime]=0;
            while(prv->size()){
                while(prv->size()){
                    Sit s=prv->front();prv->pop();
                    for(int ii = 0; ii < G[s.node].size(); ii++){
                        edge &e=G[s.node][ii];
                        if(e.start<s.cTime||e.start>60*18)
                            continue;
                        Sit ns=s;
                        ns.cost+=e.cost;
                        ns.cTime=e.goal;
                        ns.node=e.to;
                        if(minCosts[i][ns.node].find(ns.cTime)!=minCosts[i][ns.node].end()){
                            if(minCosts[i][ns.node][ns.cTime]>ns.cost){
                                minCosts[i][ns.node][ns.cTime]=ns.cost;
                                nxt->push(ns);
                            }
                        }
                        else{
                            minCosts[i][ns.node][ns.cTime]=ns.cost;
                            nxt->push(ns);
                        }
                    }
                }
                swap(prv,nxt);
            }
        }
        delete prv;
        delete nxt;

        int minCost=INF;
        for(int i = 0; i < idx; i++){
            for(map<int,int>::iterator it=minCosts[0][i].begin();it!=minCosts[0][i].end();it++){
                for(map<int,int>::iterator iit=minCosts[1][i].begin();iit!=minCosts[1][i].end();iit++){
                    int sum=it->second+iit->second;
                    int nextStartTime=max(it->first,iit->first)+30;
 
                    int res=backMinCost(0,nextStartTime,i);
                    int res2=backMinCost(1,nextStartTime,i);
                    if(res!=INF&&res2!=INF){
                        sum+=res+res2;
                        minCost=min(minCost,sum);
                    }
                }
            }
        }
        if(minCost==INF)cout<<0<<endl;
        else cout<<minCost<<endl;
    }
    return 0;
}

int backMinCost(int idx,int sTime,int snode){
    map<int,int> d[101];
    queue<Sit> *prv=new queue<Sit>();
    queue<Sit> *nxt=new queue<Sit>();
    Sit ss;
    ss.cost=0;
    ss.node=snode;
    ss.cTime=sTime;
    prv->push(ss);
    d[snode][ss.cTime]=ss.cost;
    while(prv->size()){
        while(prv->size()){
            Sit s=prv->front();prv->pop();
            for(int ii = 0; ii < G[s.node].size(); ii++){
                edge &e=G[s.node][ii];
                if(e.start<s.cTime||e.start>60*18)
                    continue;
                Sit ns=s;
                ns.cost+=e.cost;
                ns.cTime=e.goal;
                ns.node=e.to;
                // ·ÅÉ¶Ý
                if(d[ns.node].find(ns.cTime)!=d[ns.node].end()){
                    // Å¬RXgðXVÂ\
                    if(d[ns.node][ns.cTime]>ns.cost){
                        d[ns.node][ns.cTime]=ns.cost;
                        nxt->push(ns);
                    }
                }
                else{
                    d[ns.node][ns.cTime]=ns.cost;
                    nxt->push(ns);
                }
            }
        }
        swap(prv,nxt);
    }
    int res=INF;
    for(map<int,int>::iterator it=d[idx].begin();it!=d[idx].end();it++)
        res=min(res,it->second);
    return res;
}

Compiler output:
a.cc: In function 'int backMinCost(int, int, int)':
a.cc:150:17: error: 'else' without a previous 'if'
  150 |                 else{
      |                 ^~~~
a.cc:151:23: error: 'ns' was not declared in this scope; did you mean 's'?
  151 |                     d[ns.node][ns.cTime]=ns.cost;
      |                       ^~
      |                       s
a.cc:157:5: warning: no return statement in function returning non-void [-Wreturn-type]
  157 |     }
      |     ^
a.cc: At global scope:
a.cc:159:5: error: expected unqualified-id before 'for'
  159 |     for(map<int,int>::iterator it=d[idx].begin();it!=d[idx].end();it++)
      |     ^~~
a.cc:159:50: error: 'it' does not name a type; did you mean 'int'?
  159 |     for(map<int,int>::iterator it=d[idx].begin();it!=d[idx].end();it++)
      |                                                  ^~
      |                                                  int
a.cc:159:67: error: 'it' does not name a type; did you mean 'int'?
  159 |     for(map<int,int>::iterator it=d[idx].begin();it!=d[idx].end();it++)
      |                                                                   ^~
      |                                                                   int
a.cc:161:5: error: expected unqualified-id before 'return'
  161 |     return res;
      |     ^~~~~~
a.cc:162:1: error: expected declaration before '}' token
  162 | }
      | ^