Problem p01400 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

map<string, int> mp;
struct edge {
  int to, cost;
};
vector<edge> dis[600];
int d[600];

int main() {
  int n, m;
  for (;;) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    string s, a, g;
    rep(i, 600) dis[i].clear();
    cin >> s >> a >> g;
    mp.clear();
    mp[s] = 0;
    mp[a] = 1;
    mp[g] = 2;
    int count = 3;
    rep(i, m) {
      string sa, sb;
      int cost, pen;
      int aa, bb;
      cin >> sa >> sb >> cost >> pen;
      cost = cost / 40 + pen;
      if (mp.count(sa))
        aa = mp[sa];
      else
        mp[sa] = aa = count++;
      if (mp.count(sb))
        bb = mp[sb];
      else
        mp[sb] = bb = count++;
      edge e;
      e.to = bb;
      e.cost = cost;
      dis[aa].push_back(e);
      e.to = aa;
      dis[bb].push_back(e);
      // cout << sa << " " << aa << " " << sb <<" " << bb <<" " << cost << endl;
    }
    rep(i, 600) d[i] = 100000000;
    priority_queue<pair<int, int>> que;
    que.push(make_pair(0, 0));
    while (!que.empty()) {
      pair<int, int> now = que.top();
      // cout << now.first << " " << now.second << endl;
      que.pop();
      if (now.first >= d[now.second])
        continue;
      d[now.second] = now.first;
      rep(i, dis[now.second].size()) {
        que.push(make_pair(now.first + dis[now.second][i].cost,
                           dis[now.second][i].to));
      }
    }
    // cout << mp[a] << endl;
    int res = d[mp[a]];
    // cout << res << endl;
    rep(i, 600) d[i] = 100000000;
    que.push(make_pair(0, 1));
    while (!que.empty()) {
      pair<int, int> now = que.top();
      // cout << now.first << " " << now.second << endl;
      que.pop();
      if (now.first >= d[now.second])
        continue;
      d[now.second] = now.first;
      rep(i, dis[now.second].size()) {
        que.push(make_pair(now.first + dis[now.second][i].cost,
                           dis[now.second][i].to));
      }
    }
    res += d[mp[g]];
    cout << res << endl;
  }
}

Fixed submission:
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

map<string, int> mp;
struct edge {
  int to, cost;
};
vector<edge> dis[600];
int d[600];

int main() {
  int n, m;
  for (;;) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    string s, a, g;
    rep(i, 600) dis[i].clear();
    cin >> s >> a >> g;
    mp.clear();
    mp[s] = 0;
    mp[a] = 1;
    mp[g] = 2;
    int count = 3;
    rep(i, m) {
      string sa, sb;
      int cost, pen;
      int aa, bb;
      cin >> sa >> sb >> cost >> pen;
      cost = cost / 40 + pen;
      if (mp.count(sa))
        aa = mp[sa];
      else
        mp[sa] = aa = count++;
      if (mp.count(sb))
        bb = mp[sb];
      else
        mp[sb] = bb = count++;
      edge e;
      e.to = bb;
      e.cost = cost;
      dis[aa].push_back(e);
      e.to = aa;
      dis[bb].push_back(e);
      // cout << sa << " " << aa << " " << sb <<" " << bb <<" " << cost << endl;
    }
    rep(i, 600) d[i] = 100000000;
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        que;
    que.push(make_pair(0, 0));
    while (!que.empty()) {
      pair<int, int> now = que.top();
      // cout << now.first << " " << now.second << endl;
      que.pop();
      if (now.first >= d[now.second])
        continue;
      d[now.second] = now.first;
      rep(i, dis[now.second].size()) {
        que.push(make_pair(now.first + dis[now.second][i].cost,
                           dis[now.second][i].to));
      }
    }
    // cout << mp[a] << endl;
    int res = d[mp[a]];
    // cout << res << endl;
    rep(i, 600) d[i] = 100000000;
    que.push(make_pair(0, 1));
    while (!que.empty()) {
      pair<int, int> now = que.top();
      // cout << now.first << " " << now.second << endl;
      que.pop();
      if (now.first >= d[now.second])
        continue;
      d[now.second] = now.first;
      rep(i, dis[now.second].size()) {
        que.push(make_pair(now.first + dis[now.second][i].cost,
                           dis[now.second][i].to));
      }
    }
    res += d[mp[g]];
    cout << res << endl;
  }
}