Problem p02703 (C++) — buggy submission, verdict: Runtime Error


#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
#define int long long int
typedef complex<long double> cd;
const long double pi = acos(-1);
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<db, db> pdd;
typedef vector<int> vi;
typedef vector<vector<int>> matrix;

#define m1 make_pair
#define pb push_back
#define flush fflush(stdout)
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define reset(x, v) memset(x, v, sizeof(x))

#define fi first
#define se second
#define endl "\\n"
#define debug(x) (cerr << #x << ": " << x << "\\n")
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>

inline ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m) {
  if (n == 0)
    return 1;
  ll p = power(a, n / 2, m);
  p = (p % m * p % m) % m;
  if (n % 2)
    return (p % m * a % m) % m;
  else
    return p;
}
const double EPS = 1e-9;
const ll MOD = 998244353;
const ll hell = 1000000007;
const int INF = 1e15;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);

double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

const int N = 55;
int n, m, s;
vector<pair<int, pair<int, int>>> adj[N];

int solve(int t) {
  int MX = 2505;
  int dp[n + 1][MX + 1];
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= MX; j++)
      dp[i][j] = INF;

  dp[1][s] = 0;

  set<pair<int, pair<int, int>>> st;
  st.insert(m1(0, m1(1, s)));
  while (!st.empty()) {
    pair<int, int> pr = (*st.begin()).se;
    st.erase(st.begin());
    int u = pr.fi;
    if (u == t)
      break;
    int coins = pr.se;
    // cout<<pr.fi<<" "<<pr.se<<endl;
    for (int i = 0; i < adj[u].size(); i++) {
      int v = adj[u][i].fi;
      int cn = adj[u][i].se.fi;
      int w = adj[u][i].se.se;
      // cout<<u<<" "<<v<<" "<<w<<endl;
      if (v == u) {
        if (coins >= MX)
          continue;
        else {
          if (coins + cn >= MX) {
            cn = MX - coins;
          }
        }
      }
      if ((coins + cn) >= 0 && dp[v][coins + cn] > (w + dp[u][coins])) {
        // cout<<v<<endl;
        if (st.find(m1(dp[v][coins + cn], m1(v, coins + cn))) != st.end())
          st.erase(st.find(m1(dp[v][coins + cn], m1(v, coins + cn))));
        dp[v][coins + cn] = w + dp[u][coins];
        //  cout<<v<<" "<<dp[v][coins+cn]<<endl;
        st.insert(m1(dp[v][coins + cn], m1(v, coins + cn)));
      }
    }
  }
  int ans = INF;
  for (int j = 0; j <= MX; j++)
    ans = min(ans, dp[t][j]);
  return ans;
}
void solve() {
  cin >> n >> m >> s;
  int u, v, a, b;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> a >> b;
    adj[u].pb(m1(v, m1(-a, b)));
    adj[v].pb(m1(u, m1(-a, b)));
  }
  s = min(s, (int)2504);
  int c, d;
  for (int i = 1; i <= n; i++) {
    cin >> c >> d;
    adj[i].pb(m1(i, m1(c, d)));
  }
  int ans[n + 1];
  for (int i = 2; i <= n; i++)
    ans[i] = solve(i);
  for (int i = 2; i <= n; i++)
    cout << ans[i] << endl;
}

int32_t main() {
  IOS;
  // FILE *fin = freopen("in","r",stdin);
  // FILE *fout = freopen("out","w",stdout);
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:


#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
#define int long long int
typedef complex<long double> cd;
const long double pi = acos(-1);
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<db, db> pdd;
typedef vector<int> vi;
typedef vector<vector<int>> matrix;

#define m1 make_pair
#define pb push_back
#define flush fflush(stdout)
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define reset(x, v) memset(x, v, sizeof(x))

#define fi first
#define se second
#define endl "\\n"
#define debug(x) (cerr << #x << ": " << x << "\\n")
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>

inline ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m) {
  if (n == 0)
    return 1;
  ll p = power(a, n / 2, m);
  p = (p % m * p % m) % m;
  if (n % 2)
    return (p % m * a % m) % m;
  else
    return p;
}
const double EPS = 1e-9;
const ll MOD = 998244353;
const ll hell = 1000000007;
const int INF = 1e15;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);

double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

const int N = 51;
int n, m, s;
vector<pair<int, pair<int, int>>> adj[N];

int solve(int t) {
  int MX = 50 * n;
  int dp[n + 1][MX + 1];
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= MX; j++)
      dp[i][j] = INF;

  dp[1][s] = 0;

  set<pair<int, pair<int, int>>> st;
  st.insert(m1(0, m1(1, s)));
  while (!st.empty()) {
    pair<int, int> pr = (*st.begin()).se;
    st.erase(st.begin());
    int u = pr.fi;
    if (u == t)
      break;
    int coins = pr.se;
    // cout<<pr.fi<<" "<<pr.se<<endl;
    for (int i = 0; i < adj[u].size(); i++) {
      int v = adj[u][i].fi;
      int cn = adj[u][i].se.fi;
      int w = adj[u][i].se.se;
      // cout<<u<<" "<<v<<" "<<w<<endl;
      if (v == u) {
        if (coins >= MX)
          continue;
        else {
          if (coins + cn >= MX) {
            cn = MX - coins;
          }
        }
      }
      if ((coins + cn) >= 0 && dp[v][coins + cn] > (w + dp[u][coins])) {
        // cout<<v<<endl;
        if (st.find(m1(dp[v][coins + cn], m1(v, coins + cn))) != st.end())
          st.erase(st.find(m1(dp[v][coins + cn], m1(v, coins + cn))));
        dp[v][coins + cn] = w + dp[u][coins];
        //  cout<<v<<" "<<dp[v][coins+cn]<<endl;
        st.insert(m1(dp[v][coins + cn], m1(v, coins + cn)));
      }
    }
  }
  int ans = INF;
  for (int j = 0; j < MX; j++)
    ans = min(ans, dp[t][j]);
  return ans;
}
void solve() {
  cin >> n >> m >> s;
  int u, v, a, b;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> a >> b;
    adj[u].pb(m1(v, m1(-a, b)));
    adj[v].pb(m1(u, m1(-a, b)));
  }
  s = min(s, (int)2505);
  int c, d;
  for (int i = 1; i <= n; i++) {
    cin >> c >> d;
    adj[i].pb(m1(i, m1(c, d)));
  }
  int ans[n + 1];
  for (int i = 2; i <= n; i++)
    ans[i] = solve(i);
  for (int i = 2; i <= n; i++)
    cout << ans[i] << endl;
}

int32_t main() {
  IOS;
  // FILE *fin = freopen("in","r",stdin);
  // FILE *fout = freopen("out","w",stdout);
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}