Problem p02703 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1e18;
const int MOD = 1000000007;
const int coin = 2501;

ll v = coin * 50;
vector<vector<pair<ll, ll>>> graph(v, vector<pair<ll, ll>>());
vector<ll> shortest(v);
vector<bool> visited(v, false);

void dijkstra(ll start) {
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
  pq.push(make_pair(0, start));
  while (!pq.empty()) {
    pair<ll, ll> now = pq.top();
    pq.pop();
    ll cost, node;
    tie(cost, node) = now;
    if (visited[node]) {
      continue;
    } else {
      shortest[node] = cost;
      visited[node] = true;
      for (pair<ll, ll> next : graph[node]) {
        ll next_cost, next_node;
        tie(next_cost, next_node) = next;
        pq.push(make_pair(cost + next_cost, next_node));
      }
    }
  }
}

int main() {
  ll n, m, s;
  cin >> n >> m >> s;
  rep(ii, m) {
    ll p, q, a, b;
    cin >> p >> q >> a >> b;
    --p;
    --q;
    p *= coin, q *= coin;
    for (int i = 0; i < coin - a; i++) {
      graph[p + a + i].push_back(P(b, q + i));
      graph[q + a + i].push_back(P(b, p + i));
    }
  }
  rep(i, n) {
    ll c, d;
    cin >> c >> d;
    rep(x, coin) {
      if (x + c >= coin)
        break;
      graph[i * coin + x].push_back(P(d, i * coin + x + c));
    }
  }

  s = min(s, 2500LL);
  dijkstra(0 + s);
  ll ans = INF;
  for (int i = 2501; i < n * 2501; i++) {
    ans = min(ans, shortest[i]);
    if (i % coin == 2500) {
      cout << ans << endl;
      ans = INF;
    }
  }

  /*
  rep(i,n*6000){
      for(P j:graph[i]){
          cout << j.first << " "<< j.second << "  ";
      }
      if(graph[i].size() != 0) cout << endl;
      if(i%6000 == 5999) cout << endl;
  }*/
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1e13;
const int MOD = 1000000007;

ll v = 350000;
vector<vector<pair<ll, ll>>> graph(v, vector<pair<ll, ll>>());
vector<ll> shortest(v);
vector<bool> visited(v, false);

void dijkstra(ll start) {
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
  pq.push(make_pair(0, start));
  while (!pq.empty()) {
    pair<ll, ll> now = pq.top();
    pq.pop();
    ll cost, node;
    tie(cost, node) = now;
    if (visited[node]) {
      continue;
    } else {
      shortest[node] = cost;
      visited[node] = true;
      for (pair<ll, ll> next : graph[node]) {
        ll next_cost, next_node;
        tie(next_cost, next_node) = next;
        pq.push(make_pair(cost + next_cost, next_node));
      }
    }
  }
}

int main() {
  ll n, m, s;
  cin >> n >> m >> s;
  rep(ii, m) {
    ll p, q, a, b;
    cin >> p >> q >> a >> b;
    --p;
    --q;
    p *= 6000, q *= 6000;
    for (int i = 0; i < 6000 - a; i++) {
      graph[p + a + i].push_back(P(b, q + i));
      graph[q + a + i].push_back(P(b, p + i));
    }
  }
  rep(i, n) {
    ll c, d;
    cin >> c >> d;
    rep(x, 6000) {
      ll k = 1;
      while (1) {
        graph[i * 6000 + x].push_back(P(k * d, i * 6000 + x + k * c));
        ++k;
        if (x + k * c >= 2000)
          break;
      }
    }
  }

  s = min(s, 5999LL);
  dijkstra(0 + s);
  ll ans = INF;
  rep(i, n * 6000) {
    ans = min(ans, shortest[i]);
    if (i % 6000 == 5999 && i != 5999) {
      cout << ans << endl;
      ans = INF;
    } else if (i == 5999) {
      ans = INF;
    }
  }

  /*
  rep(i,n*6000){
      for(P j:graph[i]){
          cout << j.first << " "<< j.second << "  ";
      }
      if(graph[i].size() != 0) cout << endl;
      if(i%6000 == 5999) cout << endl;
  }*/
  return 0;
}