Problem p04003 (C++)

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

using namespace std;
int n, m;
int main() {
  cin >> n >> m;
  vector<int> p(m), q(m), c(m);
  vector<vector<pair<int, int>>> edges(n + 1);
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &p[i], &q[i], &c[i]);
    edges[p[i]].push_back(make_pair(q[i], c[i]));
    edges[q[i]].push_back(make_pair(p[i], c[i]));
  }

  vector<map<int, int>> cope; // company->penalty for each city
  cope.clear();
  cope.resize(n + 1);
  cope[1][-1] = 0;
  vector<int> lope(n + 1, 1000000000);
  lope[1] = 0;
  queue<int> cit;
  cit.push(1);
  vector<int> vis(n + 1, 0);
  vis[1] = 1;
  while (cit.size()) {
    int city = cit.front();
    cit.pop();
    vis[city] = 0;
    for (int i = 0; i < edges[city].size(); i++) {

      int ncit = edges[city][i].first;
      int ncom = edges[city][i].second;
      if (ncom < 0)
        continue;
      if (lope[ncit] < lope[city])
        continue;
      int res1 = 1000000000;
      if (cope[city].count(ncom))
        res1 = cope[city][ncom];
      int res2 = lope[city] + 1;
      if (!cope[ncit].count(ncom))
        cope[ncit][ncom] = 1000000000;
      int old = cope[ncit][ncom];
      cope[ncit][ncom] = min(min(res1, res2), cope[ncit][ncom]);

      lope[ncit] = min(lope[ncit], cope[ncit][ncom]);
      if (cope[ncit][ncom] - 1 <= lope[ncit]) {
        if (!vis[ncit])
          cit.push(ncit);
        vis[ncit] = 1;
      }
    }
    //	vis[city] = 0;
  }
  if (lope[n] == 1000000000)
    lope[n] = -1;
  cout << lope[n];
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;
int n, m;
int main() {
  cin >> n >> m;
  vector<int> p(m), q(m), c(m);
  vector<vector<pair<int, int>>> edges(n + 1);
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &p[i], &q[i], &c[i]);
    edges[p[i]].push_back(make_pair(q[i], c[i]));
    edges[q[i]].push_back(make_pair(p[i], c[i]));
  }

  vector<map<int, int>> cope; // company->penalty for each city
  cope.clear();
  cope.resize(n + 1);
  cope[1][-1] = 0;
  vector<int> lope(n + 1, 1000000000);
  lope[1] = 0;
  queue<int> cit;
  cit.push(1);
  vector<int> vis(n + 1, 0);
  vis[1] = 1;
  while (cit.size()) {
    int city = cit.front();
    cit.pop();
    vis[city] = 0;
    for (int i = 0; i < edges[city].size(); i++) {

      int ncit = edges[city][i].first;
      int ncom = edges[city][i].second;
      if (ncom < 0)
        continue;
      if (lope[ncit] < lope[city])
        continue;
      int res1 = 1000000000;
      if (cope[city].count(ncom))
        res1 = cope[city][ncom];
      int res2 = lope[city] + 1;
      if (!cope[ncit].count(ncom))
        cope[ncit][ncom] = 1000000000;
      int old = cope[ncit][ncom];
      cope[ncit][ncom] = min(min(res1, res2), cope[ncit][ncom]);

      lope[ncit] = min(lope[ncit], cope[ncit][ncom]);
      if (old > cope[ncit][ncom] && cope[ncit][ncom] - 1 <= lope[ncit]) {
        if (!vis[ncit])
          cit.push(ncit);
        vis[ncit] = 1;
      }
    }
    //	vis[city] = 0;
  }
  if (lope[n] == 1000000000)
    lope[n] = -1;
  cout << lope[n];
}