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);
        }
        // ewÉeÔÌñÔÅ·éÅ¬ÌRXgðA©çAÙ©çA»ê¼êÉÎµÄßé
        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);
            // úÊuÅÌRXgÍ0,ÔÍ8:00ÒÁ½è
            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()){
                            // Å¬RXgðXVÂ\
                            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;
        // ewÅ ¤Æ¼èµÄA³çÉßéÜÅÌRXgàl¦ÄAÅ¬ÌRXgð­©·é
        for(int i = 0; i < idx; i++){
            // Ôð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;
                    // »Ýn_©çnextStartTimeÉX^[gµÄßêé©AßêéÈçÅ¬ÌRXgðÔ·
                    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;
}

// idxªsnodeÉsTimeÉ¢éóÔ©çÅ¬RXgÅidxÌússÉKêéÆ«ÌRXg
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);
    delete nxt;
    delete prv;
    return res;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:90:25: error: 'else' without a previous 'if'
   90 |                         else{
      |                         ^~~~
a.cc:91:41: error: 'ns' was not declared in this scope; did you mean 's'?
   91 |                             minCosts[i][ns.node][ns.cTime]=ns.cost;
      |                                         ^~
      |                                         s
a.cc:99:16: error: 'prv' was not declared in this scope
   99 |         delete prv;
      |                ^~~
a.cc:100:16: error: 'nxt' was not declared in this scope
  100 |         delete nxt;
      |                ^~~
a.cc:104:28: error: 'idx' was not declared in this scope
  104 |         for(int i = 0; i < idx; i++){
      |                            ^~~
a.cc:106:43: error: 'minCosts' was not declared in this scope; did you mean 'minCost'?
  106 |             for(map<int,int>::iterator it=minCosts[0][i].begin();it!=minCosts[0][i].end();it++){
      |                                           ^~~~~~~~
      |                                           minCost
a.cc: At global scope:
a.cc:123:5: error: expected unqualified-id before 'return'
  123 |     return 0;
      |     ^~~~~~
a.cc:124:1: error: expected declaration before '}' token
  124 | }
      | ^
a.cc: In function 'int backMinCost(int, int, int)':
a.cc:156:17: error: 'else' without a previous 'if'
  156 |                 else{
      |                 ^~~~
a.cc:157:23: error: 'ns' was not declared in this scope; did you mean 's'?
  157 |                     d[ns.node][ns.cTime]=ns.cost;
      |                       ^~
      |                       s
a.cc:163:5: warning: no return statement in function returning non-void [-Wreturn-type]
  163 |     }
      |     ^
a.cc: At global scope:
a.cc:165:5: error: expected unqualified-id before 'for'
  165 |     for(map<int,int>::iterator it=d[idx].begin();it!=d[idx].end();it++)
      |     ^~~
a.cc:165:50: error: 'it' does not name a type; did you mean 'int'?
  165 |     for(map<int,int>::iterator it=d[idx].begin();it!=d[idx].end();it++)
      |                                                  ^~
      |                                                  int
a.cc:165:67: error: 'it' does not name a type; did you mean 'int'?
  165 |     for(map<int,int>::iterator it=d[idx].begin();it!=d[idx].end();it++)
      |                                                                   ^~
      |                                                                   int
a.cc:167:5: error: expected unqualified-id before 'delete'
  167 |     delete nxt;
      |     ^~~~~~
a.cc:168:5: error: expected unqualified-id before 'delete'
  168 |     delete prv;
      |     ^~~~~~
a.cc:169:5: error: expected unqualified-id before 'return'
  169 |     return res;
      |     ^~~~~~
a.cc:170:1: error: expected declaration before '}' token
  170 | }
      | ^