problem_id stringlengths 6 6 | language stringclasses 2 values | original_status stringclasses 3 values | original_src stringlengths 19 243k | changed_src stringlengths 19 243k | change stringclasses 3 values | i1 int64 0 8.44k | i2 int64 0 8.44k | j1 int64 0 8.44k | j2 int64 0 8.44k | error stringclasses 270 values | stderr stringlengths 0 226k |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02702 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
typedef pair<int, int> pii;
int main() {
string s;
cin >> s;
int n = s.size();
vector<ll> vec(n);
vector<ll> ls(2020, 0);
vec[n - 1] = s[n - 1] - '0';
ls[n - 1] += 1;
ll t = 1;
for (int i = n - 2; i >= 0; i--) {
t = t * 10 % 2019;
vec[i] = (t * (s[i] - '0') + vec[i + 1]) % 2019;
ls[(t * (s[i] - '0') + vec[i + 1]) % 2019] += 1;
}
ll ans = 0;
ls[0]++;
rep(i, 2019) { ans += ls[i] * (ls[i] - 1) / 2; }
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
typedef pair<int, int> pii;
int main() {
string s;
cin >> s;
int n = s.size();
vector<ll> vec(n);
vector<ll> ls(2020, 0);
vec[n - 1] = s[n - 1] - '0';
ls[s[n - 1] - '0'] += 1;
ll t = 1;
for (int i = n - 2; i >= 0; i--) {
t = t * 10 % 2019;
vec[i] = (t * (s[i] - '0') + vec[i + 1]) % 2019;
ls[(t * (s[i] - '0') + vec[i + 1]) % 2019] += 1;
}
ll ans = 0;
ls[0]++;
rep(i, 2019) { ans += ls[i] * (ls[i] - 1) / 2; }
cout << ans << endl;
return 0;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p02702 | C++ | Runtime Error | // #pragma once
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib> // srand,rand
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define lp(i, n) for (ll i = 0; i < n; i++)
#define modd 1000000007
#define mod2 998244353
#define INF 8223372036854775807ll
#define ALL(a) (a).begin(), (a).end()
typedef pair<long long, long long> pl;
typedef pair<double, double> pd;
typedef pair<ll, string> pls;
typedef string::const_iterator State;
class ParseError {};
/*
class SegmentTree {
private:
ll cont_num = 2;
pd initial_v;
vector<pd> dat;
public:
SegmentTree() {};
void init(ll size, double initial_value_first, double
initial_value_second) {
cont_num = 2;
initial_v.first = initial_value_first;
initial_v.second = initial_value_second;
while (cont_num < size) {
cont_num *= 2;
}
dat.resize(2 * cont_num);//サイズ設定
for (int i = 0; i < 2 * cont_num; i++)dat[i] =
initial_v;//初期化
}
void Update(ll position, double value_f, double value_s) {
ll k = cont_num + position;
dat[k].first = value_f;
dat[k].second = value_s;
while (k > 1) {
k /= 2;
dat[k].first = dat[k * 2 + 1].first * dat[k * 2].first;
dat[k].second = dat[k * 2 + 1].first * dat[k * 2].second
+ dat[k * 2 + 1].second;
}
}
/*
ll query_proces(ll a, ll b, ll k, ll l, ll r) {
if (r <= a || b <= l)return initial_v;
if (a <= l && r <= b)return dat[k];
else {
ll vl = query_proces(a, b, k * 2, l, (l + r) / 2);
ll vr = query_proces(a, b, k * 2 + 1, (l + r) / 2, r);
return min(vl, vr);
}
}
ll query(ll left, ll right) {
return query_proces(left, right, 1, 0, cont_num);
}
*/
/*
double query() {
return dat[1].first + dat[1].second;
}
};*/
template <typename T> class Zip {
vector<T> d;
bool flag;
void init() {
sort(d.begin(), d.end());
d.erase(unique(d.begin(), d.end()), d.end());
flag = false;
}
public:
Zip() { flag = false; }
void add(T x) {
d.push_back(x);
flag = true;
}
ll getNum(T x) {
if (flag)
init();
return lower_bound(d.begin(), d.end(), x) - d.begin();
}
ll size() {
if (flag)
init();
return (ll)d.size();
}
};
ll N, M, K, a, b, c, d, e, H, W, L, T;
ll x, y, z;
ll A[2000004] = {};
ll B[2000004] = {};
ll C[2000004] = {};
ll D[1000006] = {};
ll E[1000006] = {};
bool f;
string S[200000];
string SS;
set<long long> sll;
pl bufpl;
vector<long long> vl[200005];
vector<long long> vll;
vector<long long> v;
vector<pl> vpl;
vector<string> vs;
set<ll> llset;
set<string> Sset;
multiset<ll> llmset;
queue<ll> ql;
multiset<pl> plmset;
typedef struct ST {
ll first;
ll second;
ll cost;
bool operator<(const ST &another) const {
return second < another.second; // 比較
};
bool operator>(const ST &another) const {
return second > another.second; // 比較
};
} ST;
// queue<ST> qst;
priority_queue<ST, vector<ST>, greater<ST>> qst;
/*vector <ST> vst;
ST st[200005];
ST bufst;
bitset<5000> bits;*/
long long modinv(long long aa, long long mm) {
long long bb = mm, uu = 1, vv = 0;
while (bb) {
long long tt = aa / bb;
aa -= tt * bb;
swap(aa, bb);
uu -= tt * vv;
swap(uu, vv);
}
uu %= mm;
if (uu < 0)
uu += mm;
return uu;
}
ll zettai(ll aa) {
if (aa < 0) {
aa *= -1;
}
return aa;
}
float zettai(float aa) {
if (aa < 0) {
aa *= -1;
}
return aa;
}
/*
class UnionFind
{
public:
vector <ll>pairent;
vector <ll>depth;
vector <ll>size;
UnionFind(ll Amount) : pairent(Amount, 1), depth(Amount, 1),
size(Amount, 1) {
for (ll i = 0; i < Amount; i++) {
pairent[i] = i;
}
}
ll FindPairent(ll x) {
if (pairent[x] == x)return x;
else return pairent[x] = FindPairent(pairent[x]);
}
ll Merge(ll x, ll y) {
x = FindPairent(x);
y = FindPairent(y);
if (x != y) {
if (depth[x] > depth[y]) {
pairent[y] = pairent[x];
return size[x] += size[y];
}
else {
pairent[x] = pairent[y];
if (depth[x] == depth[y]) {
depth[y] ++;
}
return size[y] += size[x];
}
}
else {
return -1;
}
}
bool IsSame(ll x, ll y) {
if (FindPairent(x) == FindPairent(y))return true;
else return false;
}
ll GetSize(ll x) {
x = FindPairent(x);
return size[x];
}
};
*/
class UnionFind {
public:
vector<ll> pairent;
vector<ll> depth;
vector<ll> size;
UnionFind(ll Amount) : pairent(Amount, 1), depth(Amount, 1), size(Amount, 1) {
for (ll i = 0; i < Amount; i++) {
pairent[i] = i;
}
}
ll FindPairent(ll x) {
if (pairent[x] == x)
return x;
else
return pairent[x] = FindPairent(pairent[x]);
}
ll Merge(ll x, ll y) {
x = FindPairent(x);
y = FindPairent(y);
if (x != y) {
if (depth[x] > depth[y]) {
pairent[y] = pairent[x];
return size[x] += size[y];
} else {
pairent[x] = pairent[y];
if (depth[x] == depth[y]) {
depth[y]++;
}
return size[y] += size[x];
}
} else {
return -1;
}
}
bool IsSame(ll x, ll y) {
if (FindPairent(x) == FindPairent(y))
return true;
else
return false;
}
ll GetSize(ll x) {
x = FindPairent(x);
return size[x];
}
};
class Nibutan {
public:
ll hidari = 0;
ll migi = 0;
ll abc;
vector<ll> contents;
bool Jouken(int ser) {
if (contents[ser] > abc) {
return true;
} else {
return false;
}
}
void Settings(int amount, ll terget) {
migi = amount - 1;
abc = terget;
}
ll tansaku() {
hidari = -1;
migi = contents.size();
ll jud;
while (hidari != migi - 1) {
jud = hidari + migi;
jud /= 2;
if (Jouken(jud)) {
migi = jud;
} else {
hidari = jud;
}
}
return hidari;
}
};
ll RepeatSquaring(ll N, ll P, ll M) {
if (P == 0)
return 1;
if (P % 2 == 0) {
ll t = RepeatSquaring(N, P / 2, M) % M;
return t * t % M;
}
return N * RepeatSquaring(N, P - 1, M) % M;
}
/*
ll KurikaesiNijou(ll a, ll b, ll P) {
if (b == 0)return 1;
if (b % 2 == 0) {
ll c=KurikaesiNijou(a,b/2,P)%P;
return c * c %P;
}
else {
ll c = KurikaesiNijou(a, b/2, P) % P;
return a * c * c % P;
}
}*/
ll GCD(ll a, ll b) {
if (a % b == 0)
return b;
else
return GCD(b, a % b);
}
ll Min(ll a, ll b) {
if (a < b)
return a;
else
return b;
}
ll Max(ll a, ll b) {
if (a > b)
return a;
else
return b;
}
ll Sum(ll a, ll b) { return a + b; }
template <typename T> class SegmentTree {
ll n;
vector<T> node;
function<T(T, T)> fun, fun2;
bool customChange;
T outValue, initValue;
public:
void init(ll num, function<T(T, T)> resultFunction, T init, T out,
function<T(T, T)> changeFunction = NULL) {
// changeFunction: (input, beforevalue) => newvalue
fun = resultFunction;
fun2 = changeFunction;
customChange = changeFunction != NULL;
n = 1;
while (n < num)
n *= 2;
node.resize(2 * n - 1);
fill(node.begin(), node.end(), init);
outValue = out;
initValue = init;
}
void valueChange(ll num, T value) {
num += n - 1;
if (customChange)
node[num] = fun2(value, node[num]);
else
node[num] = value;
while (num > 0)
num = (num - 1) / 2,
node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]);
}
T rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b)
if (r == -1)
r = n;
if (a <= l && r <= b)
return node[k];
if (b <= l || r <= a)
return outValue;
ll mid = (l + r) / 2;
return fun(rangeQuery(a, b, l, mid, 2 * k + 1),
rangeQuery(a, b, mid, r, 2 * k + 2));
}
};
class Combination {
vector<ll> factorial;
vector<ll> factorial_inv;
ll mod;
ll max_n;
public:
void Init(ll init_max_n, ll init_mod) {
max_n = init_max_n;
mod = init_mod;
factorial.resize(max_n + 1);
factorial[0] = 1;
for (ll i = 1; i < factorial.size(); i++) {
factorial[i] = factorial[i - 1] * i;
factorial[i] %= mod;
}
factorial_inv.resize(max_n + 1);
factorial_inv[0] = 1;
for (ll i = 1; i < factorial_inv.size(); i++) {
factorial_inv[i] = factorial_inv[i - 1] * modinv(i, mod);
factorial_inv[i] %= mod;
}
}
ll GetComb(ll n, ll k) {
ll comb = factorial[n];
comb *= factorial_inv[k];
comb %= mod;
comb *= factorial_inv[n - k];
comb %= mod;
return comb;
}
ll GetH(ll n, ll k) { // n+k-1<=max_N
ll comb = factorial[n + k - 1];
comb *= factorial_inv[n];
comb %= mod;
comb *= factorial_inv[k - 1];
comb %= mod;
return comb;
}
};
class Tree {
ll node_N;
vector<ll> node;
vector<vector<pl>> pass;
ll diameter = -1;
vector<ll> dist_Diamieter[2];
pl maxDist_Num;
public:
void Init(ll node_Num) {
node_N = node_Num;
node.resize(node_N + 1);
pass.resize(node_N + 1);
dist_Diamieter[0].resize(node_N + 1);
lp(i, node_N + 1) dist_Diamieter[0][i] = -1;
dist_Diamieter[1].resize(node_N + 1);
lp(i, node_N + 1) dist_Diamieter[1][i] = -1;
}
void AddEdge(ll a, ll b, ll dist) {
bufpl.first = b;
bufpl.second = dist;
pass[a].push_back(bufpl);
bufpl.first = a;
pass[b].push_back(bufpl);
}
void DFS(ll step, ll now, ll dist) {
dist_Diamieter[step][now] = dist;
if (dist_Diamieter[step][now] > maxDist_Num.first) {
maxDist_Num.first = dist_Diamieter[step][now];
maxDist_Num.second = now;
}
for (ll i = 0; i < pass[now].size(); i++) {
ll next_node = pass[now][i].first;
if (dist_Diamieter[step][next_node] == -1) {
DFS(step, next_node, dist + pass[now][i].second);
}
}
}
ll GetDiameter(ll min_node_num) {
if (diameter >= 0)
return diameter;
else {
maxDist_Num.first = -1;
maxDist_Num.second = -1;
DFS(0, min_node_num, 0ll);
ll step2_start = maxDist_Num.second;
maxDist_Num.first = -1;
maxDist_Num.second = -1;
DFS(1, step2_start, 0ll);
diameter = maxDist_Num.first;
return diameter;
}
}
ll GetDistFromMinNode(ll num) { return dist_Diamieter[0][num]; }
};
void Nibu(ll node, ll co) {
C[node] = co % 2;
D[co % 2]++;
lp(i, vl[node].size()) {
ll next = vl[node][i];
if (C[next] == -1) {
Nibu(next, co + 1);
}
}
}
Combination combi;
SegmentTree<ll> st;
int main() {
cin >> SS;
reverse(ALL(SS));
a = 0;
c = 1;
lp(i, SS.size()) {
a += (SS[i] - '0') * c;
a %= 2019;
A[a]++;
B[i] = a;
c *= 10;
c %= 2019;
}
ll ans = A[0];
c = 1;
lp(i, SS.size() - 4) {
A[B[i]]--;
ans += A[B[i]];
}
cout << ans << endl;
// cout << fixed << setprecision(12) << bb << endl;
return 0;
}
| // #pragma once
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib> // srand,rand
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define lp(i, n) for (ll i = 0; i < n; i++)
#define modd 1000000007
#define mod2 998244353
#define INF 8223372036854775807ll
#define ALL(a) (a).begin(), (a).end()
typedef pair<long long, long long> pl;
typedef pair<double, double> pd;
typedef pair<ll, string> pls;
typedef string::const_iterator State;
class ParseError {};
/*
class SegmentTree {
private:
ll cont_num = 2;
pd initial_v;
vector<pd> dat;
public:
SegmentTree() {};
void init(ll size, double initial_value_first, double
initial_value_second) {
cont_num = 2;
initial_v.first = initial_value_first;
initial_v.second = initial_value_second;
while (cont_num < size) {
cont_num *= 2;
}
dat.resize(2 * cont_num);//サイズ設定
for (int i = 0; i < 2 * cont_num; i++)dat[i] =
initial_v;//初期化
}
void Update(ll position, double value_f, double value_s) {
ll k = cont_num + position;
dat[k].first = value_f;
dat[k].second = value_s;
while (k > 1) {
k /= 2;
dat[k].first = dat[k * 2 + 1].first * dat[k * 2].first;
dat[k].second = dat[k * 2 + 1].first * dat[k * 2].second
+ dat[k * 2 + 1].second;
}
}
/*
ll query_proces(ll a, ll b, ll k, ll l, ll r) {
if (r <= a || b <= l)return initial_v;
if (a <= l && r <= b)return dat[k];
else {
ll vl = query_proces(a, b, k * 2, l, (l + r) / 2);
ll vr = query_proces(a, b, k * 2 + 1, (l + r) / 2, r);
return min(vl, vr);
}
}
ll query(ll left, ll right) {
return query_proces(left, right, 1, 0, cont_num);
}
*/
/*
double query() {
return dat[1].first + dat[1].second;
}
};*/
template <typename T> class Zip {
vector<T> d;
bool flag;
void init() {
sort(d.begin(), d.end());
d.erase(unique(d.begin(), d.end()), d.end());
flag = false;
}
public:
Zip() { flag = false; }
void add(T x) {
d.push_back(x);
flag = true;
}
ll getNum(T x) {
if (flag)
init();
return lower_bound(d.begin(), d.end(), x) - d.begin();
}
ll size() {
if (flag)
init();
return (ll)d.size();
}
};
ll N, M, K, a, b, c, d, e, H, W, L, T;
ll x, y, z;
ll A[2000004] = {};
ll B[2000004] = {};
ll C[2000004] = {};
ll D[1000006] = {};
ll E[1000006] = {};
bool f;
string S[200000];
string SS;
set<long long> sll;
pl bufpl;
vector<long long> vl[200005];
vector<long long> vll;
vector<long long> v;
vector<pl> vpl;
vector<string> vs;
set<ll> llset;
set<string> Sset;
multiset<ll> llmset;
queue<ll> ql;
multiset<pl> plmset;
typedef struct ST {
ll first;
ll second;
ll cost;
bool operator<(const ST &another) const {
return second < another.second; // 比較
};
bool operator>(const ST &another) const {
return second > another.second; // 比較
};
} ST;
// queue<ST> qst;
priority_queue<ST, vector<ST>, greater<ST>> qst;
/*vector <ST> vst;
ST st[200005];
ST bufst;
bitset<5000> bits;*/
long long modinv(long long aa, long long mm) {
long long bb = mm, uu = 1, vv = 0;
while (bb) {
long long tt = aa / bb;
aa -= tt * bb;
swap(aa, bb);
uu -= tt * vv;
swap(uu, vv);
}
uu %= mm;
if (uu < 0)
uu += mm;
return uu;
}
ll zettai(ll aa) {
if (aa < 0) {
aa *= -1;
}
return aa;
}
float zettai(float aa) {
if (aa < 0) {
aa *= -1;
}
return aa;
}
/*
class UnionFind
{
public:
vector <ll>pairent;
vector <ll>depth;
vector <ll>size;
UnionFind(ll Amount) : pairent(Amount, 1), depth(Amount, 1),
size(Amount, 1) {
for (ll i = 0; i < Amount; i++) {
pairent[i] = i;
}
}
ll FindPairent(ll x) {
if (pairent[x] == x)return x;
else return pairent[x] = FindPairent(pairent[x]);
}
ll Merge(ll x, ll y) {
x = FindPairent(x);
y = FindPairent(y);
if (x != y) {
if (depth[x] > depth[y]) {
pairent[y] = pairent[x];
return size[x] += size[y];
}
else {
pairent[x] = pairent[y];
if (depth[x] == depth[y]) {
depth[y] ++;
}
return size[y] += size[x];
}
}
else {
return -1;
}
}
bool IsSame(ll x, ll y) {
if (FindPairent(x) == FindPairent(y))return true;
else return false;
}
ll GetSize(ll x) {
x = FindPairent(x);
return size[x];
}
};
*/
class UnionFind {
public:
vector<ll> pairent;
vector<ll> depth;
vector<ll> size;
UnionFind(ll Amount) : pairent(Amount, 1), depth(Amount, 1), size(Amount, 1) {
for (ll i = 0; i < Amount; i++) {
pairent[i] = i;
}
}
ll FindPairent(ll x) {
if (pairent[x] == x)
return x;
else
return pairent[x] = FindPairent(pairent[x]);
}
ll Merge(ll x, ll y) {
x = FindPairent(x);
y = FindPairent(y);
if (x != y) {
if (depth[x] > depth[y]) {
pairent[y] = pairent[x];
return size[x] += size[y];
} else {
pairent[x] = pairent[y];
if (depth[x] == depth[y]) {
depth[y]++;
}
return size[y] += size[x];
}
} else {
return -1;
}
}
bool IsSame(ll x, ll y) {
if (FindPairent(x) == FindPairent(y))
return true;
else
return false;
}
ll GetSize(ll x) {
x = FindPairent(x);
return size[x];
}
};
class Nibutan {
public:
ll hidari = 0;
ll migi = 0;
ll abc;
vector<ll> contents;
bool Jouken(int ser) {
if (contents[ser] > abc) {
return true;
} else {
return false;
}
}
void Settings(int amount, ll terget) {
migi = amount - 1;
abc = terget;
}
ll tansaku() {
hidari = -1;
migi = contents.size();
ll jud;
while (hidari != migi - 1) {
jud = hidari + migi;
jud /= 2;
if (Jouken(jud)) {
migi = jud;
} else {
hidari = jud;
}
}
return hidari;
}
};
ll RepeatSquaring(ll N, ll P, ll M) {
if (P == 0)
return 1;
if (P % 2 == 0) {
ll t = RepeatSquaring(N, P / 2, M) % M;
return t * t % M;
}
return N * RepeatSquaring(N, P - 1, M) % M;
}
/*
ll KurikaesiNijou(ll a, ll b, ll P) {
if (b == 0)return 1;
if (b % 2 == 0) {
ll c=KurikaesiNijou(a,b/2,P)%P;
return c * c %P;
}
else {
ll c = KurikaesiNijou(a, b/2, P) % P;
return a * c * c % P;
}
}*/
ll GCD(ll a, ll b) {
if (a % b == 0)
return b;
else
return GCD(b, a % b);
}
ll Min(ll a, ll b) {
if (a < b)
return a;
else
return b;
}
ll Max(ll a, ll b) {
if (a > b)
return a;
else
return b;
}
ll Sum(ll a, ll b) { return a + b; }
template <typename T> class SegmentTree {
ll n;
vector<T> node;
function<T(T, T)> fun, fun2;
bool customChange;
T outValue, initValue;
public:
void init(ll num, function<T(T, T)> resultFunction, T init, T out,
function<T(T, T)> changeFunction = NULL) {
// changeFunction: (input, beforevalue) => newvalue
fun = resultFunction;
fun2 = changeFunction;
customChange = changeFunction != NULL;
n = 1;
while (n < num)
n *= 2;
node.resize(2 * n - 1);
fill(node.begin(), node.end(), init);
outValue = out;
initValue = init;
}
void valueChange(ll num, T value) {
num += n - 1;
if (customChange)
node[num] = fun2(value, node[num]);
else
node[num] = value;
while (num > 0)
num = (num - 1) / 2,
node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]);
}
T rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b)
if (r == -1)
r = n;
if (a <= l && r <= b)
return node[k];
if (b <= l || r <= a)
return outValue;
ll mid = (l + r) / 2;
return fun(rangeQuery(a, b, l, mid, 2 * k + 1),
rangeQuery(a, b, mid, r, 2 * k + 2));
}
};
class Combination {
vector<ll> factorial;
vector<ll> factorial_inv;
ll mod;
ll max_n;
public:
void Init(ll init_max_n, ll init_mod) {
max_n = init_max_n;
mod = init_mod;
factorial.resize(max_n + 1);
factorial[0] = 1;
for (ll i = 1; i < factorial.size(); i++) {
factorial[i] = factorial[i - 1] * i;
factorial[i] %= mod;
}
factorial_inv.resize(max_n + 1);
factorial_inv[0] = 1;
for (ll i = 1; i < factorial_inv.size(); i++) {
factorial_inv[i] = factorial_inv[i - 1] * modinv(i, mod);
factorial_inv[i] %= mod;
}
}
ll GetComb(ll n, ll k) {
ll comb = factorial[n];
comb *= factorial_inv[k];
comb %= mod;
comb *= factorial_inv[n - k];
comb %= mod;
return comb;
}
ll GetH(ll n, ll k) { // n+k-1<=max_N
ll comb = factorial[n + k - 1];
comb *= factorial_inv[n];
comb %= mod;
comb *= factorial_inv[k - 1];
comb %= mod;
return comb;
}
};
class Tree {
ll node_N;
vector<ll> node;
vector<vector<pl>> pass;
ll diameter = -1;
vector<ll> dist_Diamieter[2];
pl maxDist_Num;
public:
void Init(ll node_Num) {
node_N = node_Num;
node.resize(node_N + 1);
pass.resize(node_N + 1);
dist_Diamieter[0].resize(node_N + 1);
lp(i, node_N + 1) dist_Diamieter[0][i] = -1;
dist_Diamieter[1].resize(node_N + 1);
lp(i, node_N + 1) dist_Diamieter[1][i] = -1;
}
void AddEdge(ll a, ll b, ll dist) {
bufpl.first = b;
bufpl.second = dist;
pass[a].push_back(bufpl);
bufpl.first = a;
pass[b].push_back(bufpl);
}
void DFS(ll step, ll now, ll dist) {
dist_Diamieter[step][now] = dist;
if (dist_Diamieter[step][now] > maxDist_Num.first) {
maxDist_Num.first = dist_Diamieter[step][now];
maxDist_Num.second = now;
}
for (ll i = 0; i < pass[now].size(); i++) {
ll next_node = pass[now][i].first;
if (dist_Diamieter[step][next_node] == -1) {
DFS(step, next_node, dist + pass[now][i].second);
}
}
}
ll GetDiameter(ll min_node_num) {
if (diameter >= 0)
return diameter;
else {
maxDist_Num.first = -1;
maxDist_Num.second = -1;
DFS(0, min_node_num, 0ll);
ll step2_start = maxDist_Num.second;
maxDist_Num.first = -1;
maxDist_Num.second = -1;
DFS(1, step2_start, 0ll);
diameter = maxDist_Num.first;
return diameter;
}
}
ll GetDistFromMinNode(ll num) { return dist_Diamieter[0][num]; }
};
void Nibu(ll node, ll co) {
C[node] = co % 2;
D[co % 2]++;
lp(i, vl[node].size()) {
ll next = vl[node][i];
if (C[next] == -1) {
Nibu(next, co + 1);
}
}
}
Combination combi;
SegmentTree<ll> st;
int main() {
cin >> SS;
if (SS.size() < 4) {
cout << 0 << endl;
return 0;
}
reverse(ALL(SS));
a = 0;
c = 1;
lp(i, SS.size()) {
a += (SS[i] - '0') * c;
a %= 2019;
A[a]++;
B[i] = a;
c *= 10;
c %= 2019;
}
ll ans = A[0];
c = 1;
lp(i, SS.size() - 4) {
A[B[i]]--;
ans += A[B[i]];
}
cout << ans << endl;
// cout << fixed << setprecision(12) << bb << endl;
return 0;
}
| insert | 623 | 623 | 623 | 628 | 0 | |
p02702 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#define N 2019
typedef long long LL;
using namespace std;
char s[305000];
short dp[305000][N + 1];
int main() {
memset(dp, 0LL, sizeof(dp));
scanf("%s", s + 1);
int ls = strlen(s + 1);
int a;
LL ans = 0;
for (int i = 1; i <= ls; i++) {
a = s[i] - '0';
dp[i][a % N]++;
for (int j = 0; j < N; j++)
dp[i][(a + j * 10) % N] += dp[i - 1][j];
ans += dp[i][0];
}
printf("%lld\n", ans);
} | #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#define N 2019
typedef long long LL;
using namespace std;
char s[200005];
short dp[200005][N + 1];
int main() {
memset(dp, 0LL, sizeof(dp));
scanf("%s", s + 1);
int ls = strlen(s + 1);
int a;
LL ans = 0;
for (int i = 1; i <= ls; i++) {
a = s[i] - '0';
dp[i][a % N]++;
for (int j = 0; j < N; j++)
dp[i][(a + j * 10) % N] += dp[i - 1][j];
ans += dp[i][0];
}
printf("%lld\n", ans);
} | replace | 9 | 11 | 9 | 11 | MLE | |
p02702 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a) for (int i = 0; i < (a); i++)
const ll MOD = 1000000007;
// const ll MOD=998244353;
int cnt[2020];
int main() {
string S;
cin >> S;
cnt[0] = 1;
ll sum = 0, pre = 0, x = 1;
for (int i = S.length() - 1; i >= 0; i--) {
int tmp = (pre + (S[i] - '0') * x) % 2019;
sum += cnt[tmp];
cnt[tmp]++;
pre = tmp;
(x *= 10) % MOD;
}
cout << sum << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a) for (int i = 0; i < (a); i++)
const ll MOD = 1000000007;
// const ll MOD=998244353;
int cnt[2020];
int main() {
string S;
cin >> S;
cnt[0] = 1;
ll sum = 0, pre = 0, x = 1;
for (int i = S.length() - 1; i >= 0; i--) {
int tmp = (pre + (S[i] - '0') * x) % 2019;
sum += cnt[tmp];
cnt[tmp]++;
pre = tmp;
x = x * 10 % 2019;
}
cout << sum << endl;
return 0;
} | replace | 19 | 20 | 19 | 20 | 0 | |
p02702 | C++ | Runtime Error | // header {{{
// header {{{
using namespace std;
#include <bits/stdc++.h>
#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y
#define ASSERT(expr...) assert((expr))
using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using f32 = float;
using f64 = double;
// }}}
constexpr i64 INF = 1'010'000'000'000'000'017LL;
constexpr i64 MOD = 1'000'000'007LL;
constexpr f64 EPS = 1e-12;
constexpr f64 PI = 3.14159265358979323846;
#define M5 100007
#define M9 1000000000
// util {{{
#define FOR(i, start, end) \
for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end); \
i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(f, c, ...) \
(([&](decltype((c)) cccc) { \
return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \
})(c))
#define ll long long int
#define VI vector<int>
template <typename C> i64 SIZE(const C &c) {
return static_cast<i64>(c.size());
}
template <typename T, size_t N> i64 SIZE(const T (&)[N]) {
return static_cast<i64>(N);
}
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
if (comp(xmax, x)) {
xmax = x;
return true;
}
return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
if (comp(x, xmin)) {
xmin = x;
return true;
}
return false;
}
// }}}
// init {{{
struct ProconInit {
static constexpr int IOS_PREC = 15;
static constexpr bool AUTOFLUSH = false;
ProconInit() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(IOS_PREC);
if (AUTOFLUSH)
cout << unitbuf;
}
} PROCON_INIT;
// }}}
//--------------------------------------------------------------------
bool isOK(int mid, double key) {}
int binary_search(double key) {
int left = -1; // 「index = 0」が条件を満たすこともあるので、初期値は -1
int right = 2000000000;
/* どんな二分探索でもここの書き方を変えずにできる! */
while (right - left > 1) {
int mid = left + (right - left) / 2;
if (isOK(mid, key))
right = mid;
else
left = mid;
}
/* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
*/
return right;
}
bool isKaibun(string st) {
bool ans = true;
int n = st.length();
REP(i, n / 2) {
if (st[i] != st[n - i - 1]) {
return false;
}
}
return true;
}
int toInt(char a) { return a - '0'; }
int gcd(int a, int b) {
if (a % b == 0) {
return (b);
} else {
return (gcd(b, a % b));
}
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
long long N;
string S;
int P;
long long solve() {
cin >> S;
N = S.length();
P = 2019;
vector<long long> val(P, 0);
long long tenfactor = 1;
long long cur = 0;
val[cur]++;
for (int i = 0; i < N; ++i) {
cur = (cur + (S[N - i - 1] - '0') * tenfactor) % P;
tenfactor = (tenfactor * 10) % P;
val[cur]++;
}
long long res = 0;
for (int p = 0; p < P; ++p)
res += val[p] * (val[p] - 1) / 2;
cout << res;
}
signed main() {
solve();
return 0;
}
| // header {{{
// header {{{
using namespace std;
#include <bits/stdc++.h>
#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y
#define ASSERT(expr...) assert((expr))
using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using f32 = float;
using f64 = double;
// }}}
constexpr i64 INF = 1'010'000'000'000'000'017LL;
constexpr i64 MOD = 1'000'000'007LL;
constexpr f64 EPS = 1e-12;
constexpr f64 PI = 3.14159265358979323846;
#define M5 100007
#define M9 1000000000
// util {{{
#define FOR(i, start, end) \
for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end); \
i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(f, c, ...) \
(([&](decltype((c)) cccc) { \
return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \
})(c))
#define ll long long int
#define VI vector<int>
template <typename C> i64 SIZE(const C &c) {
return static_cast<i64>(c.size());
}
template <typename T, size_t N> i64 SIZE(const T (&)[N]) {
return static_cast<i64>(N);
}
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
if (comp(xmax, x)) {
xmax = x;
return true;
}
return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
if (comp(x, xmin)) {
xmin = x;
return true;
}
return false;
}
// }}}
// init {{{
struct ProconInit {
static constexpr int IOS_PREC = 15;
static constexpr bool AUTOFLUSH = false;
ProconInit() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(IOS_PREC);
if (AUTOFLUSH)
cout << unitbuf;
}
} PROCON_INIT;
// }}}
//--------------------------------------------------------------------
bool isOK(int mid, double key) {}
int binary_search(double key) {
int left = -1; // 「index = 0」が条件を満たすこともあるので、初期値は -1
int right = 2000000000;
/* どんな二分探索でもここの書き方を変えずにできる! */
while (right - left > 1) {
int mid = left + (right - left) / 2;
if (isOK(mid, key))
right = mid;
else
left = mid;
}
/* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
*/
return right;
}
bool isKaibun(string st) {
bool ans = true;
int n = st.length();
REP(i, n / 2) {
if (st[i] != st[n - i - 1]) {
return false;
}
}
return true;
}
int toInt(char a) { return a - '0'; }
int gcd(int a, int b) {
if (a % b == 0) {
return (b);
} else {
return (gcd(b, a % b));
}
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
long long N;
string S;
int P;
void solve() {
cin >> S;
N = S.length();
P = 2019;
vector<long long> val(P, 0);
long long tenfactor = 1;
long long cur = 0;
val[cur]++;
for (int i = 0; i < N; ++i) {
cur = (cur + (S[N - i - 1] - '0') * tenfactor) % P;
tenfactor = (tenfactor * 10) % P;
val[cur]++;
}
long long res = 0;
for (int p = 0; p < P; ++p)
res += val[p] * (val[p] - 1) / 2;
cout << res;
}
signed main() {
solve();
return 0;
}
| replace | 139 | 140 | 139 | 140 | 0 | |
p02702 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int m = 2019;
int main() {
string s;
cin >> s;
int n = s.size();
reverse(s.begin(), s.end());
int x = 1;
int tot = 0;
vector<int> count(m);
ll ans = 0;
for (int i = 0; i < n; i++) {
count[tot]++;
tot += (s[i] - '0') * x;
ans += count[tot];
x = x * 10 % m;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int m = 2019;
int main() {
string s;
cin >> s;
int n = s.size();
reverse(s.begin(), s.end());
int x = 1;
int tot = 0;
vector<int> count(m);
ll ans = 0;
for (int i = 0; i < n; i++) {
count[tot]++;
tot += (s[i] - '0') * x;
tot %= m;
ans += count[tot];
x = x * 10 % m;
}
cout << ans << endl;
return 0;
} | insert | 18 | 18 | 18 | 19 | -11 | |
p02702 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int cnt[2020];
int num[100001];
int main(void) {
string s;
cin >> s;
int tmp = 0;
int mul = 1;
cnt[0]++;
int size = s.size();
for (int i = 0; i < size; i++) {
tmp += (s[size - i - 1] - '0') * mul % 2019;
tmp %= 2019;
mul *= 10;
mul %= 2019;
cnt[tmp]++;
num[size - i - 1] = tmp;
}
long long ans = 0;
for (int i = 0; i < size; i++) {
cnt[num[i]]--;
ans += cnt[num[i]];
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int cnt[2020];
int num[200001];
int main(void) {
string s;
cin >> s;
int tmp = 0;
int mul = 1;
cnt[0]++;
int size = s.size();
for (int i = 0; i < size; i++) {
tmp += (s[size - i - 1] - '0') * mul % 2019;
tmp %= 2019;
mul *= 10;
mul %= 2019;
cnt[tmp]++;
num[size - i - 1] = tmp;
}
long long ans = 0;
for (int i = 0; i < size; i++) {
cnt[num[i]]--;
ans += cnt[num[i]];
}
cout << ans << endl;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02702 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e4 + 9;
int main() {
char s[maxn];
scanf("%s", s + 1);
int len = strlen(s + 1);
ll d[maxn] = {0}, b[maxn] = {0};
ll ans = 0;
for (int i = 1; i <= len; i++) {
int num = s[i] - '0';
d[num]++;
for (int j = 0; j < 2019; j++) {
d[(j * 10 + num) % 2019] += b[j];
}
for (int j = 0; j < 2019; j++) {
b[j] = d[j];
d[j] = 0;
}
ans += b[0];
}
printf("%lld\n", ans);
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 9;
int main() {
char s[maxn];
scanf("%s", s + 1);
int len = strlen(s + 1);
ll d[maxn] = {0}, b[maxn] = {0};
ll ans = 0;
for (int i = 1; i <= len; i++) {
int num = s[i] - '0';
d[num]++;
for (int j = 0; j < 2019; j++) {
d[(j * 10 + num) % 2019] += b[j];
}
for (int j = 0; j < 2019; j++) {
b[j] = d[j];
d[j] = 0;
}
ans += b[0];
}
printf("%lld\n", ans);
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02702 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int uli;
typedef long double Lf;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define fastIO \
std::ios::sync_with_stdio(false); \
cin.tie(NULL)
#define mod 1000000007
#define N 2005
#define NN 2005
#define SZ(x) ((lli)(x).size())
#define loop(i, s, n) for (int i = (s); i < (n); i++)
#define loopr(i, n, s) for (int i = (n)-1; i >= (s); i--)
#define pb push_back
#define o2(a, b) cout << (a) << " " << (b) << endl
#define o3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define o4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define cl cout << endl
#define r0 return 0
#define e0 exit(0)
#define x first
#define y second
inline lli modadd(lli n, lli m, lli p = mod) { return ((n + m) % p + p) % p; }
inline lli modsub(lli n, lli m, lli p = mod) {
return ((n - m + p) % p + p) % p;
}
inline lli modpro(lli n, lli m, lli p = mod) {
return (((n % p) * (m % p)) % p + p) % p;
}
uli powe(lli x, lli y) {
uli res = 1;
while (y > 0) {
if (y & 1)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
lli modpow(lli x, lli y, lli p = mod) {
lli res = 1;
while (y > 0) {
if (y & 1)
res = modpro(res, x, p);
y = y >> 1;
x = modpro(x, x, p);
}
return res;
}
inline lli modInverse(lli n, lli p = mod) {
if (n == 1)
return 1;
return modpow(n, p - 2, p);
}
inline lli moddiv(lli n, lli m, lli p = mod) {
return modpro(n, modInverse(m, p), p);
}
inline lli modadd3(lli x, lli y, lli z, lli p = mod) {
return modadd(modadd(x, y, p), z, p);
}
inline lli modadd4(lli x, lli y, lli z, lli w, lli p = mod) {
return modadd(modadd(x, y, p), modadd(z, w, p), p);
}
inline lli modnCr(lli fac[], int n, int r, lli p = mod) {
if (r == 0)
return 1;
return modpro(fac[n], modInverse(modpro(fac[r], fac[n - r], p), p), p);
}
template <typename T> inline T max3(T x, T y, T z) { return max(max(x, y), z); }
template <typename T> inline T max4(T x, T y, T z, T w) {
return max(max3(x, y, w), z);
}
template <typename T> inline T min3(T x, T y, T z) { return min(min(x, y), z); }
template <typename T> inline T min4(T x, T y, T z, T w) {
return min(min3(x, y, w), z);
}
template <typename T> void printArr(T *arr, int s, int n) {
for (int i = s; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
// template<class X, class Y, class Z>
// struct triple {
// X x;
// Y y;
// Z z;
//
// friend bool operator<(triple a, triple b)
// {
// if(a.x!=b.x) return a.x<b.x;
// else if(a.y!=b.y) return a.y<b.y;
// else return a.z<=b.z;
// }
// };
//
//
// template<class X, class Y, class Z, class W>
// class quad {
// public:
// X x;
// Y y;
// Z z;
// W w;
//
// friend bool operator<(quad a, quad b)
// {
// if(a.x!=b.x) return a.x<b.x;
// else if(a.y!=b.y) return a.y<b.y;
// else if(a.z!=b.z)return a.z<b.z;
// else return a.w<b.w;
// }
// };
// template <typename T>
// T gcd(T a, T b)
//{
// if (a == 0)
// return b;
// if(b==0)
// return a;
// T t;
// while((a>0)&&(b>0)){
// t = a;
// a=b%a;
// b=t;
// }
//
// return max(a,b);
// }
//
// uli choose(uli n, uli k){
// uli res = 1;
//
// // Since C(n, k) = C(n, n-k)
// if ( k > n - k )
// k = n - k;
//
// // Calculate value of
// // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
// for (uli i = 0; i < k; ++i)
// {
// res *= (n - i);
// res /= (i + 1);
// }
//
// return res;
//}
//
//
// vector<vector<pii>> adj;
// //vector<int> dist;
// vector<int> d;
// vector<bool> visited;
// vector<pii> pa;
// //queue<int> q;
//
// class Graph{
//
// public:
// int V;
//
// Graph(int V){
// this->V = V;
// adj.resize(V);
// visited.resize(V);
// //color.resize(V);
// //dist.resize(V);
//
// //// dist.resize(V);
// //pa.resize(V);
//
// }
//
// ~Graph(){
// adj.clear();
// //visited.clear();
// // color.clear();
// // dist.clear();
// //pa.clear();
// //dist.clear();
// visited.clear();
//
//
// }
//
//
// void addEdge(int a, int b, int in)
// {
// adj[a].pb({b,in});
// //adj[b].pb({a,in});
// }
//
// // void dfs(int x){
// // // color[x]=1;
// //
// // for(auto u: adj[x]){
// // if(visited[u]) continue;
// //
// // dfs(u);
// // }
// //
// // }
// //
// //
// //
// //
// //
// // void dfsTree(int x, int p){
// // for(auto u: adj[x]){
// // if(u==p) {continue;}
// // dfsTree(u,x);
// // }
// // }
//
//
// void dijkstra(int s) {
// d.assign(n, INF);
// d[s] = 0;
// set<pair<int, int>> q;
// q.insert({0, s});
// while (!q.empty()) {
// int v = q.begin()->second;
// q.erase(q.begin());
// for (auto edge : adj[v]) {
// int to = edge.first;
// int len = edge.second;
// if (d[v] + len < d[to]) {
// q.erase({d[to], to});
// d[to] = d[v] + len;
// q.insert({d[to], to});
// }
// }
// }
//}
//
//
//// void bfs(int x){
//// visited[x]=1;
//// q.push(x);
//// //dist[x]=0;
//// while(!q.empty()){
//// int s = q.front();
//// q.pop();
////
//// for(auto u : adj[s]){
////
//// if(visited[u.x]) continue;
//// pa[u.x]={u.y,s};
//// visited[u.x]=1;
//// //dist[u.x]=dist[s]+1;
//// q.push(u.x);
//// }
//// }
//// }
//
// //pair<int,int> farthest(int start){
// // vector<int> dist(V,INT_MAX);
// //
// // q.push(start);
// // dist[start]=0;
// // while(!q.empty()){
// // int u = q.front();
// // q.pop();
// // for(int child: adj[u]){
// // //if(keep[child])
// // if(dist[child] > dist[u]+1) {
// // dist[child] = dist[u]+1;
// // q.push(child);
// // }
// // }
// // }
// // int big=-1,mi=-1;
// // for(int i=1;i<n+1;i++){
// // //if(keep[i])
// // {
// // if(dist[i]>big){
// // big=dist[i];mi=i;
// // }
// // }
// // }
// // return make_pair(big,mi);
// //}
//
//
// // int connectedcomponents(int n){
// // int ans=0;
// // loop(i,1,n+1){
// // if(!visited[i]){
// // ans++;
// // dfs(i);
// // }
// //
// // }
// // return ans;
// // }
//
// };
// template <typename T>
// T findpowerfactorial(T n,T p)
//{
// T x = 0;
// while (n)
// {
// n /= p;
// x += n;
// }
// return x;
// }
//
// template <typename T>
// int getibit(T n, int i){
// //cout<<(n&(1LL<<i))<<endl;
// return (n&(1LL<<i))?1:0;
// }
//
// template <typename T>
// int findbits(T n, T p){
// int x=0;
// while(n>0){
// n/=p;
// x++;
// }
// return x;
// }
//
//
////
// vector<pair<lli,int>> v;
// void primeFactors(lli n)
//{
//
// int c=0;
// while (n % 2 == 0)
// {
// n = n/2;
// c++;
// }
// if(c>0) v.pb({2,c});
//
//
// for (lli i = 3; i*i <= n; i = i + 2)
// { c=0;
//
// while (n % i == 0)
// {
// n = n/i;
// c++;
// }
// if(c>0)v.pb({i,c});
//
// }
// if (n > 2)
// v.pb({n,1});
// }
// lli fac_[N];
// void fac_init(int n){
// fac_[0]=1;
// loop(i,1,n+1)fac_[i]=modpro(fac_[i-1],i);
//}
//
/*BITMASK
for( int inum = 0 ; inum < ( 1 << n ) ; ++ inum ) {
for ( int pos = 0; pos < n ; ++pos ) {
if ( inum & ( 1 << pos ) ){
results[inum] += s [pos] ; //DO SOMETHING
}
}
}
*/
string s;
map<int, int> m;
int ps[N];
int main() {
fastIO;
int erer = 1;
// cin>>erer;
loop(erer2, 1, erer + 1) {
cin >> s;
int n = SZ(s);
ps[n] = 0;
loopr(i, SZ(s) + 1, 1) {
ps[i] =
modadd(modpro((int)(s[i - 1] - '0'), modpow(10, n - i, 2019), 2019),
ps[i + 1], 2019);
}
// printArr(ps,0,SZ(s));
lli ans = 0;
loopr(i, SZ(s) + 1, 0) {
if (m.count(ps[i]))
ans += m[ps[i]];
m[ps[i]]++;
}
cout << ans << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int uli;
typedef long double Lf;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define fastIO \
std::ios::sync_with_stdio(false); \
cin.tie(NULL)
#define mod 1000000007
#define N 200005
#define NN 2005
#define SZ(x) ((lli)(x).size())
#define loop(i, s, n) for (int i = (s); i < (n); i++)
#define loopr(i, n, s) for (int i = (n)-1; i >= (s); i--)
#define pb push_back
#define o2(a, b) cout << (a) << " " << (b) << endl
#define o3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define o4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define cl cout << endl
#define r0 return 0
#define e0 exit(0)
#define x first
#define y second
inline lli modadd(lli n, lli m, lli p = mod) { return ((n + m) % p + p) % p; }
inline lli modsub(lli n, lli m, lli p = mod) {
return ((n - m + p) % p + p) % p;
}
inline lli modpro(lli n, lli m, lli p = mod) {
return (((n % p) * (m % p)) % p + p) % p;
}
uli powe(lli x, lli y) {
uli res = 1;
while (y > 0) {
if (y & 1)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
lli modpow(lli x, lli y, lli p = mod) {
lli res = 1;
while (y > 0) {
if (y & 1)
res = modpro(res, x, p);
y = y >> 1;
x = modpro(x, x, p);
}
return res;
}
inline lli modInverse(lli n, lli p = mod) {
if (n == 1)
return 1;
return modpow(n, p - 2, p);
}
inline lli moddiv(lli n, lli m, lli p = mod) {
return modpro(n, modInverse(m, p), p);
}
inline lli modadd3(lli x, lli y, lli z, lli p = mod) {
return modadd(modadd(x, y, p), z, p);
}
inline lli modadd4(lli x, lli y, lli z, lli w, lli p = mod) {
return modadd(modadd(x, y, p), modadd(z, w, p), p);
}
inline lli modnCr(lli fac[], int n, int r, lli p = mod) {
if (r == 0)
return 1;
return modpro(fac[n], modInverse(modpro(fac[r], fac[n - r], p), p), p);
}
template <typename T> inline T max3(T x, T y, T z) { return max(max(x, y), z); }
template <typename T> inline T max4(T x, T y, T z, T w) {
return max(max3(x, y, w), z);
}
template <typename T> inline T min3(T x, T y, T z) { return min(min(x, y), z); }
template <typename T> inline T min4(T x, T y, T z, T w) {
return min(min3(x, y, w), z);
}
template <typename T> void printArr(T *arr, int s, int n) {
for (int i = s; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
// template<class X, class Y, class Z>
// struct triple {
// X x;
// Y y;
// Z z;
//
// friend bool operator<(triple a, triple b)
// {
// if(a.x!=b.x) return a.x<b.x;
// else if(a.y!=b.y) return a.y<b.y;
// else return a.z<=b.z;
// }
// };
//
//
// template<class X, class Y, class Z, class W>
// class quad {
// public:
// X x;
// Y y;
// Z z;
// W w;
//
// friend bool operator<(quad a, quad b)
// {
// if(a.x!=b.x) return a.x<b.x;
// else if(a.y!=b.y) return a.y<b.y;
// else if(a.z!=b.z)return a.z<b.z;
// else return a.w<b.w;
// }
// };
// template <typename T>
// T gcd(T a, T b)
//{
// if (a == 0)
// return b;
// if(b==0)
// return a;
// T t;
// while((a>0)&&(b>0)){
// t = a;
// a=b%a;
// b=t;
// }
//
// return max(a,b);
// }
//
// uli choose(uli n, uli k){
// uli res = 1;
//
// // Since C(n, k) = C(n, n-k)
// if ( k > n - k )
// k = n - k;
//
// // Calculate value of
// // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
// for (uli i = 0; i < k; ++i)
// {
// res *= (n - i);
// res /= (i + 1);
// }
//
// return res;
//}
//
//
// vector<vector<pii>> adj;
// //vector<int> dist;
// vector<int> d;
// vector<bool> visited;
// vector<pii> pa;
// //queue<int> q;
//
// class Graph{
//
// public:
// int V;
//
// Graph(int V){
// this->V = V;
// adj.resize(V);
// visited.resize(V);
// //color.resize(V);
// //dist.resize(V);
//
// //// dist.resize(V);
// //pa.resize(V);
//
// }
//
// ~Graph(){
// adj.clear();
// //visited.clear();
// // color.clear();
// // dist.clear();
// //pa.clear();
// //dist.clear();
// visited.clear();
//
//
// }
//
//
// void addEdge(int a, int b, int in)
// {
// adj[a].pb({b,in});
// //adj[b].pb({a,in});
// }
//
// // void dfs(int x){
// // // color[x]=1;
// //
// // for(auto u: adj[x]){
// // if(visited[u]) continue;
// //
// // dfs(u);
// // }
// //
// // }
// //
// //
// //
// //
// //
// // void dfsTree(int x, int p){
// // for(auto u: adj[x]){
// // if(u==p) {continue;}
// // dfsTree(u,x);
// // }
// // }
//
//
// void dijkstra(int s) {
// d.assign(n, INF);
// d[s] = 0;
// set<pair<int, int>> q;
// q.insert({0, s});
// while (!q.empty()) {
// int v = q.begin()->second;
// q.erase(q.begin());
// for (auto edge : adj[v]) {
// int to = edge.first;
// int len = edge.second;
// if (d[v] + len < d[to]) {
// q.erase({d[to], to});
// d[to] = d[v] + len;
// q.insert({d[to], to});
// }
// }
// }
//}
//
//
//// void bfs(int x){
//// visited[x]=1;
//// q.push(x);
//// //dist[x]=0;
//// while(!q.empty()){
//// int s = q.front();
//// q.pop();
////
//// for(auto u : adj[s]){
////
//// if(visited[u.x]) continue;
//// pa[u.x]={u.y,s};
//// visited[u.x]=1;
//// //dist[u.x]=dist[s]+1;
//// q.push(u.x);
//// }
//// }
//// }
//
// //pair<int,int> farthest(int start){
// // vector<int> dist(V,INT_MAX);
// //
// // q.push(start);
// // dist[start]=0;
// // while(!q.empty()){
// // int u = q.front();
// // q.pop();
// // for(int child: adj[u]){
// // //if(keep[child])
// // if(dist[child] > dist[u]+1) {
// // dist[child] = dist[u]+1;
// // q.push(child);
// // }
// // }
// // }
// // int big=-1,mi=-1;
// // for(int i=1;i<n+1;i++){
// // //if(keep[i])
// // {
// // if(dist[i]>big){
// // big=dist[i];mi=i;
// // }
// // }
// // }
// // return make_pair(big,mi);
// //}
//
//
// // int connectedcomponents(int n){
// // int ans=0;
// // loop(i,1,n+1){
// // if(!visited[i]){
// // ans++;
// // dfs(i);
// // }
// //
// // }
// // return ans;
// // }
//
// };
// template <typename T>
// T findpowerfactorial(T n,T p)
//{
// T x = 0;
// while (n)
// {
// n /= p;
// x += n;
// }
// return x;
// }
//
// template <typename T>
// int getibit(T n, int i){
// //cout<<(n&(1LL<<i))<<endl;
// return (n&(1LL<<i))?1:0;
// }
//
// template <typename T>
// int findbits(T n, T p){
// int x=0;
// while(n>0){
// n/=p;
// x++;
// }
// return x;
// }
//
//
////
// vector<pair<lli,int>> v;
// void primeFactors(lli n)
//{
//
// int c=0;
// while (n % 2 == 0)
// {
// n = n/2;
// c++;
// }
// if(c>0) v.pb({2,c});
//
//
// for (lli i = 3; i*i <= n; i = i + 2)
// { c=0;
//
// while (n % i == 0)
// {
// n = n/i;
// c++;
// }
// if(c>0)v.pb({i,c});
//
// }
// if (n > 2)
// v.pb({n,1});
// }
// lli fac_[N];
// void fac_init(int n){
// fac_[0]=1;
// loop(i,1,n+1)fac_[i]=modpro(fac_[i-1],i);
//}
//
/*BITMASK
for( int inum = 0 ; inum < ( 1 << n ) ; ++ inum ) {
for ( int pos = 0; pos < n ; ++pos ) {
if ( inum & ( 1 << pos ) ){
results[inum] += s [pos] ; //DO SOMETHING
}
}
}
*/
string s;
map<int, int> m;
int ps[N];
int main() {
fastIO;
int erer = 1;
// cin>>erer;
loop(erer2, 1, erer + 1) {
cin >> s;
int n = SZ(s);
ps[n] = 0;
loopr(i, SZ(s) + 1, 1) {
ps[i] =
modadd(modpro((int)(s[i - 1] - '0'), modpow(10, n - i, 2019), 2019),
ps[i + 1], 2019);
}
// printArr(ps,0,SZ(s));
lli ans = 0;
loopr(i, SZ(s) + 1, 0) {
if (m.count(ps[i]))
ans += m[ps[i]];
m[ps[i]]++;
}
cout << ans << endl;
}
return 0;
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p02702 | C++ | Runtime Error | #include <stdio.h>
char s[200200];
long long p[2019], q[2019], a;
int main() {
scanf("%s", s);
for (int i = 0; s[i]; i++) {
for (int j = 0; j < 2019; j++)
p[j] = q[j], q[j] = 0;
p[0]++;
for (int i = 0; i < 2019; i++)
q[(i * 10 + s[i] - '0') % 2019] += p[i];
a += q[0];
}
printf("%lld\n", a);
return 0;
} | #include <stdio.h>
char s[200200];
long long p[2019], q[2019], a;
int main() {
scanf("%s", s);
for (int i = 0; s[i]; i++) {
for (int j = 0; j < 2019; j++)
p[j] = q[j], q[j] = 0;
p[0]++;
for (int j = 0; j < 2019; j++)
q[(j * 10 + s[i] - '0') % 2019] += p[j];
a += q[0];
}
printf("%lld\n", a);
return 0;
}
| replace | 11 | 13 | 11 | 13 | 0 | |
p02702 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
int main() {
string S;
cin >> S;
ll ans = 0;
for (int i = 0; i < S.size(); ++i) {
for (int k = i; k < S.size(); ++k) {
string tmp = S.substr(i, k - i + 1);
ll res = 0;
REP(i, tmp.size()) {
res += char_to_int(tmp[i]);
res %= 2019;
if (i != tmp.size() - 1)
res *= 10;
}
if (res == 0)
ans++;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
int main() {
string S;
cin >> S;
ll ans = 0;
unordered_map<ll, ll> m;
m[0] = 1;
reverse(all(S));
ll tmp = 0, ten = 1;
REP(i, S.size()) {
tmp += char_to_int(S[i]) * ten;
tmp %= 2019;
ten *= 10;
ten %= 2019;
m[tmp]++;
}
for (auto x : m) {
ans += 0.5 * x.second * (x.second - 1);
}
cout << ans << endl;
} | replace | 13 | 26 | 13 | 26 | TLE | |
p02702 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;
void prvec(vector<ll> vec) { // for debug
ll n = vec.size();
cout << "------------------------------------\n";
rep(i, n) cout << i << " " << vec.at(i) << "\n";
cout << "------------------------------------\n";
}
void pr2d(vector<vector<ll>> vec) { // for debug
ll h = vec.size();
ll w = vec.at(0).size();
cout << "------------------------------------\n";
rep(i, h) {
rep(j, w) { cout << vec.at(i).at(j) << " "; }
cout << "\n";
}
cout << "------------------------------------\n";
}
// main starts here
int main() {
unsigned short dp[200000][2019];
string s;
cin >> s;
ll ss = s.size();
ll x = s.at(0) - '0';
dp[0][x] = 1;
ll ans = 0;
for (int i = 1; i < ss; i++) {
x = s.at(i) - '0';
for (int j = 0; j < 2019; j++) {
ll y = (j * 10 + x) % 2019;
dp[i][y] += dp[i - 1][j];
}
dp[i][x]++;
ans += dp[i][0];
}
cout << ans << "\n";
return 0;
} | #include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;
void prvec(vector<ll> vec) { // for debug
ll n = vec.size();
cout << "------------------------------------\n";
rep(i, n) cout << i << " " << vec.at(i) << "\n";
cout << "------------------------------------\n";
}
void pr2d(vector<vector<ll>> vec) { // for debug
ll h = vec.size();
ll w = vec.at(0).size();
cout << "------------------------------------\n";
rep(i, h) {
rep(j, w) { cout << vec.at(i).at(j) << " "; }
cout << "\n";
}
cout << "------------------------------------\n";
}
// main starts here
int main() {
vector<vector<unsigned short>> dp(200000, vector<unsigned short>(2019, 0));
string s;
cin >> s;
ll ss = s.size();
ll x = s.at(0) - '0';
dp[0][x] = 1;
ll ans = 0;
for (int i = 1; i < ss; i++) {
x = s.at(i) - '0';
for (int j = 0; j < 2019; j++) {
ll y = (j * 10 + x) % 2019;
dp[i][y] += dp[i - 1][j];
}
dp[i][x]++;
ans += dp[i][0];
}
cout << ans << "\n";
return 0;
} | replace | 30 | 31 | 30 | 31 | -11 | |
p02702 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;
int dp[200005][2020];
int main() {
string S;
cin >> S;
rep(i, S.size()) {
{
int nxt = (S[i] - '0') % 2019;
dp[i + 1][nxt] += 1;
}
rep(j, 2019) {
{
int nxt = (j * 10 + (S[i] - '0')) % 2019;
dp[i + 1][nxt] += dp[i][j];
}
}
}
int ret = 0;
rep(i, S.size() + 1) { ret += dp[i][0]; }
cout << ret << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;
short dp[200005][2020];
int main() {
string S;
cin >> S;
rep(i, S.size()) {
{
int nxt = (S[i] - '0') % 2019;
dp[i + 1][nxt] += 1;
}
rep(j, 2019) {
{
int nxt = (j * 10 + (S[i] - '0')) % 2019;
dp[i + 1][nxt] += dp[i][j];
}
}
}
int ret = 0;
rep(i, S.size() + 1) { ret += dp[i][0]; }
cout << ret << endl;
return 0;
}
| replace | 10 | 11 | 10 | 11 | MLE | |
p02702 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// #include <stdio.h>
// #include <stack>
// #include <queue>
// #include <cstdio>
#include <cmath>
#include <fstream>
#include <iterator>
#include <map>
// #include <list>
#include <iomanip>
// #include <stdlib.h>
// #include <cctype>
using namespace std;
const int MAX_N = 1000000;
#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define prec std::cout << std::fixed << std::setprecision(15);
#define endl "\n"
#define MOD 1000000007
#define Int int64_t
#define PI 3.14159265358979
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define ssort(z) sort(z.begin(), z.end())
#define rsort(z) sort(z.rbegin(), z.rend())
#define eerase(z) z.erase(unique(z.begin(), z.end()), z.end())
#define ccnt(z, w) count(z.begin(), z.end(), w)
#define rep(i, a, n) for (Int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (Int(i) = (a); (i) <= (n); (i)++)
template <typename T>
void printV(const std::vector<T> &v, const char *const separator = " ") {
if (!v.empty()) {
std::copy(v.begin(), --v.end(),
std::ostream_iterator<T>(std::cout, separator));
std::cout << v.back() << "\n";
}
}
Int gcd(Int a, Int b) { return b != 0 ? gcd(b, a % b) : a; }
Int lcm(Int a, Int b) {
return a / gcd(a, b) * b;
// a*bは64bit integer overflow
}
int Max(int a, int b, int c) {
int temp = max(a, b);
return max(temp, c);
}
int Min(int a, int b, int c) {
int temp = min(a, b);
return min(temp, c);
}
bool integer(double num) { return floor(num) == num; }
Int fact(int num) {
if (num == 0)
return 1;
else
return num * fact(num - 1);
}
void y() {
cout << "Yes" << endl;
exit(0);
}
void n() {
cout << "No" << endl;
exit(0);
}
void Y() {
cout << "YES" << endl;
exit(0);
}
void N() {
cout << "NO" << endl;
exit(0);
}
Int yakusu(int n) {
int cnt = 0;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
cnt++;
if (i * i != n)
cnt++;
}
}
return cnt;
}
/* kokomade */
int cnt = 0;
bool is_prime[50000 + 1];
vector<int> P;
void Eratosthenes(const int N) {
for (int i = 0; i <= N; i++) {
is_prime[i] = true; // 初期化
}
for (int i = 2; i <= N; i++) {
if (is_prime[i]) {
for (int j = 2 * i; j <= N; j += i) {
is_prime[j] = false;
}
P.emplace_back(i);
}
}
}
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
if (a.second != b.second) {
return a.second < b.second;
} else {
return a.first < b.first;
}
}
vector<int64_t> divisor(int64_t n) {
vector<int64_t> ret;
for (int64_t i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
void euclid(Int N, Int a, Int b) {
for (Int d = a + b; d <= N; d += b) {
// cout << b << ", " << d << endl;
euclid(N, b, d);
}
}
Int fact_mod(Int n, Int mod) {
Int f = 1;
repq(i, 2, n) f = f * (i % MOD) % MOD;
return f;
}
// 繰り返し二乗法 (modの世界での累乗)
Int mod_pow(Int x, Int n, Int mod) {
Int res = 1;
while (n > 0) {
if (n & 1)
res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
// 組み合わせ nCr を求める
Int combination_mod(Int n, Int r, Int mod) {
if (r > n - r)
r = n - r;
if (r == 0)
return 1;
Int a = 1;
rep(i, 0, r) a = a * ((n - i) % mod) % mod;
Int b = mod_pow(fact_mod(r, mod), mod - 2, mod);
return (a % mod) * (b % mod) % mod;
}
bool rev(string s) {
string t = s;
reverse(s.begin(), s.end());
return s == t;
}
int main() {
// COMinit();
IOS;
prec;
Int a, b, n, m, k, x, y = 0, ans = 0;
string str, s, t, u;
cin >> str;
long long int spe;
// vector<Int>aa(1);
// for (int i = 0; i < n; ++i) {
// cin>>aa[i];
// }
n = str.length();
// cout<<n<<endl;
for (int i = 0; i < n - 4; ++i) {
a = 0;
for (int moji = 4; moji < n + 1; ++moji) {
try {
if (n - a - i - moji < 0)
break;
spe = stoll(str.substr(n - a - i - moji, moji));
if (a == 2)
a = 0;
if (spe % 2019 != 0) {
// a+=2;
} else {
ans++;
}
} catch (const out_of_range &e) {
cout << i << " "
<< "No" << endl;
}
}
}
cout << ans;
cout << endl;
return 0;
}
/*
テレビも無ェ ラジオも無ェ
自動車もそれほど走って無ェ
ピアノも無ェ バーも無ェ
巡査 毎日ぐーるぐる
朝起ぎで 牛連れで
二時間ちょっとの散歩道
電話も無ェ 瓦斯も無ェ
バスは一日一度来る
俺らこんな村いやだ 俺らこんな村いやだ
東京へ出るだ 東京へ出だなら
銭コァ貯めで 東京でベコ(牛)飼うだ
*/
| #include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// #include <stdio.h>
// #include <stack>
// #include <queue>
// #include <cstdio>
#include <cmath>
#include <fstream>
#include <iterator>
#include <map>
// #include <list>
#include <iomanip>
// #include <stdlib.h>
// #include <cctype>
using namespace std;
const int MAX_N = 1000000;
#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define prec std::cout << std::fixed << std::setprecision(15);
#define endl "\n"
#define MOD 1000000007
#define Int int64_t
#define PI 3.14159265358979
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define ssort(z) sort(z.begin(), z.end())
#define rsort(z) sort(z.rbegin(), z.rend())
#define eerase(z) z.erase(unique(z.begin(), z.end()), z.end())
#define ccnt(z, w) count(z.begin(), z.end(), w)
#define rep(i, a, n) for (Int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (Int(i) = (a); (i) <= (n); (i)++)
template <typename T>
void printV(const std::vector<T> &v, const char *const separator = " ") {
if (!v.empty()) {
std::copy(v.begin(), --v.end(),
std::ostream_iterator<T>(std::cout, separator));
std::cout << v.back() << "\n";
}
}
Int gcd(Int a, Int b) { return b != 0 ? gcd(b, a % b) : a; }
Int lcm(Int a, Int b) {
return a / gcd(a, b) * b;
// a*bは64bit integer overflow
}
int Max(int a, int b, int c) {
int temp = max(a, b);
return max(temp, c);
}
int Min(int a, int b, int c) {
int temp = min(a, b);
return min(temp, c);
}
bool integer(double num) { return floor(num) == num; }
Int fact(int num) {
if (num == 0)
return 1;
else
return num * fact(num - 1);
}
void y() {
cout << "Yes" << endl;
exit(0);
}
void n() {
cout << "No" << endl;
exit(0);
}
void Y() {
cout << "YES" << endl;
exit(0);
}
void N() {
cout << "NO" << endl;
exit(0);
}
Int yakusu(int n) {
int cnt = 0;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
cnt++;
if (i * i != n)
cnt++;
}
}
return cnt;
}
/* kokomade */
int cnt = 0;
bool is_prime[50000 + 1];
vector<int> P;
void Eratosthenes(const int N) {
for (int i = 0; i <= N; i++) {
is_prime[i] = true; // 初期化
}
for (int i = 2; i <= N; i++) {
if (is_prime[i]) {
for (int j = 2 * i; j <= N; j += i) {
is_prime[j] = false;
}
P.emplace_back(i);
}
}
}
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
if (a.second != b.second) {
return a.second < b.second;
} else {
return a.first < b.first;
}
}
vector<int64_t> divisor(int64_t n) {
vector<int64_t> ret;
for (int64_t i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
void euclid(Int N, Int a, Int b) {
for (Int d = a + b; d <= N; d += b) {
// cout << b << ", " << d << endl;
euclid(N, b, d);
}
}
Int fact_mod(Int n, Int mod) {
Int f = 1;
repq(i, 2, n) f = f * (i % MOD) % MOD;
return f;
}
// 繰り返し二乗法 (modの世界での累乗)
Int mod_pow(Int x, Int n, Int mod) {
Int res = 1;
while (n > 0) {
if (n & 1)
res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
// 組み合わせ nCr を求める
Int combination_mod(Int n, Int r, Int mod) {
if (r > n - r)
r = n - r;
if (r == 0)
return 1;
Int a = 1;
rep(i, 0, r) a = a * ((n - i) % mod) % mod;
Int b = mod_pow(fact_mod(r, mod), mod - 2, mod);
return (a % mod) * (b % mod) % mod;
}
bool rev(string s) {
string t = s;
reverse(s.begin(), s.end());
return s == t;
}
int main() {
// COMinit();
IOS;
prec;
Int a, b, n, m, k, x, y = 0, ans = 0;
string str, s, t, u;
cin >> str;
reverse(str.begin(), str.end());
n = str.size();
vector<Int> cnt(2019);
for (int j = 0; j < 2019; ++j) {
cnt.at(j) = 0;
}
Int tens = 1, remain = 0;
for (Int i = 0; i < n; ++i) {
x = str.at(i) - '0';
remain += (tens * x);
remain %= 2019;
cnt.at(remain) += 1;
tens *= 10;
tens %= 2019;
}
cnt.at(0)++;
for (int l = 0; l < 2019; ++l) {
ans += (cnt.at(l) - 1) * (cnt.at(l)) / 2;
}
// printV(cnt);
cout << ans;
cout << endl;
return 0;
}
/*
テレビも無ェ ラジオも無ェ
自動車もそれほど走って無ェ
ピアノも無ェ バーも無ェ
巡査 毎日ぐーるぐる
朝起ぎで 牛連れで
二時間ちょっとの散歩道
電話も無ェ 瓦斯も無ェ
バスは一日一度来る
俺らこんな村いやだ 俺らこんな村いやだ
東京へ出るだ 東京へ出だなら
銭コァ貯めで 東京でベコ(牛)飼うだ
*/
| replace | 189 | 217 | 189 | 209 | TLE | |
p02702 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
int64_t N = S.size();
int64_t ans = 0, l = 0, r = 3;
for (int i = 0; i < N - 4; i++) {
int64_t a = stoi(S.substr(i, 5));
int64_t r = a % 2019;
if (r == 0)
ans++;
for (int j = i + 5; j < N; j++) {
int64_t b = S.at(j) - '0';
r = (r * 10 + b) % 2019;
if (r == 0) {
ans++;
}
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
int64_t N = S.size();
reverse(S.begin(), S.end());
vector<int64_t> Count(2019, 0);
Count[0]++;
int64_t num = 1, cur = 0, ans = 0;
for (int i = 1; i <= N; i++) {
int sint = S[i - 1] - '0';
cur = (cur + sint * num) % 2019;
Count[cur]++;
num *= 10;
num %= 2019;
}
for (int64_t c : Count) {
ans += c * (c - 1) / 2;
}
cout << ans << endl;
}
| replace | 7 | 20 | 7 | 20 | TLE | |
p02702 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
const int MOD = 2019;
int c[MOD];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int cur = 0;
c[cur]++;
string s;
cin >> s;
int ans = 0;
reverse(s.begin(), s.end());
int p = 1;
for (auto x : s) {
int ncur = cur + p * (x - '0');
ncur %= MOD;
ans += c[ncur];
cur = ncur;
c[cur]++;
p *= 10;
}
cout << ans << endl;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
const int MOD = 2019;
int c[MOD];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int cur = 0;
c[cur]++;
string s;
cin >> s;
int ans = 0;
reverse(s.begin(), s.end());
int p = 1;
for (auto x : s) {
int ncur = cur + p * (x - '0');
ncur %= MOD;
ans += c[ncur];
cur = ncur;
c[cur]++;
p *= 10;
p %= MOD;
}
cout << ans << endl;
}
| insert | 49 | 49 | 49 | 50 | 0 | |
p02702 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
typedef long long ll;
/* clang-format off */
#define MOD 2019
#define INF 1000000000000000000ll
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
//#define __DEBUG__
#ifdef __DEBUG__
#define CH_P(a) cout <<"check_point("<<#a<<")" << "\n";
#define DEBUG(x) cout<<#x<<":"<<x<<"\n"
#define DEBUGS(v) cout << #v << ":";for(auto x:v){cout<<x<<" ";}cout<<"\n"
#endif
#ifndef __DEBUG__
#define CH_P(a)
#define DEBUG(x)
#define DEBUGS(v)
#endif
/* clang-format on */
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
ll n = s.size();
vector<ll> t(n + 1), a(n);
REP(i, n) a[i] = s[i] - '0';
t[n] = 0;
vector<ll> pow10_mod(n);
pow10_mod[0] = 1;
for (ll i = 1; i < n; i++)
pow10_mod[i] = pow10_mod[i - 1] * 10 % MOD;
REP(i, n)
for (ll i = n - 1; i >= 0; i--) {
t[i] = (t[i + 1] + a[i] * pow10_mod[10, n - i - 1]) % MOD;
}
vector<ll> cnt(2019, 0);
REP(i, n + 1) cnt[t[i]]++;
ll ans = 0;
REP(i, 2019) ans += cnt[i] * (cnt[i] - 1) / 2;
DEBUGS(t);
DEBUGS(a);
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
typedef long long ll;
/* clang-format off */
#define MOD 2019
#define INF 1000000000000000000ll
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
//#define __DEBUG__
#ifdef __DEBUG__
#define CH_P(a) cout <<"check_point("<<#a<<")" << "\n";
#define DEBUG(x) cout<<#x<<":"<<x<<"\n"
#define DEBUGS(v) cout << #v << ":";for(auto x:v){cout<<x<<" ";}cout<<"\n"
#endif
#ifndef __DEBUG__
#define CH_P(a)
#define DEBUG(x)
#define DEBUGS(v)
#endif
/* clang-format on */
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
ll n = s.size();
vector<ll> t(n + 1), a(n);
REP(i, n) a[i] = s[i] - '0';
t[n] = 0;
vector<ll> pow10_mod(n);
pow10_mod[0] = 1;
for (ll i = 1; i < n; i++)
pow10_mod[i] = pow10_mod[i - 1] * 10 % MOD;
for (ll i = n - 1; i >= 0; i--) {
t[i] = (t[i + 1] + a[i] * pow10_mod[10, n - i - 1]) % MOD;
}
vector<ll> cnt(2019, 0);
REP(i, n + 1) cnt[t[i]]++;
ll ans = 0;
REP(i, 2019) ans += cnt[i] * (cnt[i] - 1) / 2;
DEBUGS(t);
DEBUGS(a);
cout << ans << "\n";
return 0;
}
| replace | 36 | 37 | 36 | 37 | TLE | |
p02702 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pq priority_queue
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
using vvvi = vector<vvi>;
using ll = long long; // long longをllだけにした
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using mii = map<int, int>;
using pqls = priority_queue<long long>;
using pqlg = priority_queue<long long, vector<long long>, greater<long long>>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using sll = set<long long>;
long long divup(long long a, long long b);
long long kaijou(long long i);
long long P(long long n, long long k);
long long C(long long n, long long k);
long long GCD(long long a, long long b);
long long LCM(long long a, long long b);
bool prime(long long N);
double distance(vector<long long> p, vector<long long> q, long long n);
void press(vector<long long> &v);
void ranking(vector<long long> &v);
void erase(vector<long long> &v, long long i);
void unique(vector<long long> &v);
void printv(vector<long long> v);
vector<ll> keta(ll x);
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);
// 20200416
vector<long long> inputv(long long n);
// 20200417
vector<long long> yakusuu(int n);
map<long long, long long> soinsuu(long long n);
vector<vector<long long>> maze(long long i, long long j, vector<string> &s);
// 20200423
vector<long long> eratos(long long n);
set<long long> eraset(long long n);
//////////////////////////////////////////////////////
// 端数繰りあがり割り算(検証済)
// a÷bの端数繰り上げ
// b!=0のデバグはしてないので分母に0を入れないように
// 負数対応
long long divup(long long a, long long b) {
long long x = abs(a);
long long y = abs(b);
long long z = (x + y - 1) / y;
if ((a < 0 && b > 0) || (a > 0 && b < 0))
return -z;
else if (a == 0)
return 0;
else
return z;
}
// 階乗
// 検証済み
long long kaijou(long long i) {
if (i == 0)
return 1;
long long j = 1;
for (long long k = 1; k <= i; k++) {
j *= k;
}
return j;
}
// 順列nPk(完成)
// n個の異なる要素から、取り出す順序を区別してk個取り出す場合の数
// n<kなら0を返す
// 敢えて負数時のデバグはしてない
long long P(long long n, long long k) {
if (n < k)
return 0;
long long y = 1;
for (long long i = 0; i < k; i++) {
y *= (n - i);
}
return y;
}
// 組み合わせnCk(検証済み)
// P,kaijouと併用
long long C(long long n, long long k) {
if (n < k)
return 0;
return P(n, k) / kaijou(k);
}
// nHk
// 区別しないn個の要素を、区別するk個のグループに分ける
// 0個のグループがあっ
// て良い
// C必須
// 最大公約数GCD,最小公倍数LCM
// LCMを使うときはGCDをセットで
// 検証済み
long long GCD(long long a, long long b) {
if (a < b)
swap(a, b);
long long d = a % b;
if (d == 0) {
return b;
}
return GCD(b, d);
}
long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; }
// 素数判定
// 素数ならばtrue、素数以外の整数にはfalse
// 負数は全てfalse
// 検証済み
bool prime(long long N) {
if (N == 1) {
return false;
}
if (N < 0)
return false;
long long p = sqrt(N);
for (long long i = 2; i <= p; i++) {
if (N % i == 0) {
return false;
}
}
return true;
}
// ユークリッド距離
// 検証済み
// 位置ベクトル1,位置ベクトル2,ベクトルの次元(2または3が一般的)
double distance(vector<long long> p, vector<long long> q, long long n) {
double x = 0;
for (long long i = 0; i < n; i++) {
x += pow((p.at(i) - q.at(i)), 2);
}
return sqrt(x);
}
// 配列圧縮(検証済)
//{1,36,1,3,8,-2,-92}を
//{2, 5,2,3,4, 1, 0}にする
void press(vector<long long> &v) {
long long n = v.size();
vector<long long> w(n);
map<long long, long long> m;
for (auto &p : v) {
m[p] = 0;
}
long long i = 0;
for (auto &p : m) {
p.second = i;
i++;
}
for (long long i = 0; i < n; i++) {
w.at(i) = m[v.at(i)];
}
v = w;
return;
}
// 配列のi番目の要素がj番目に小さいとき、j番目の数がiであるベクトルを返す関数
// 配列の要素が全て異なるときにしか正常に動作しない
// 配列の要素に同じものが含まれても見かけ上動作はするが意味のない値を戻し、
// エラーも起きないので注意
// 検証済
//{2,4,1,6,0,3,8,9,5}を
//{4,2,0,5,1,8,3,6,7}にして返す
//"rank"という名前にするとSTLの関数(配列の次元を返す関数)になるので注意
void ranking(vector<long long> &v) {
long long n = v.size();
map<long long, long long> m;
long long i;
for (i = 0; i < n; i++) {
m[v.at(i)] = i;
}
vector<long long> w(n);
i = 0;
for (auto &p : m) {
v.at(i) = p.second;
i++;
}
return;
}
// 部分削除(未検証)
// ベクトルのi番目(i=0,1,2,...,n-1)の要素を削除し、
// 以降の要素を全て前に1ずらして参照返し
// ベクトル長は1小さくなって返る
// i>n-1の時は変化しない
void erase(vector<long long> &v, long long i) {
long long n = v.size();
if (i > n - 1)
return;
for (long long j = i; j < n - 1; j++) {
v.at(j) = v.at(j + 1);
}
v.pop_back();
return;
}
// 重複削除(未完成)
// 引数ベクトルに同一要素が複数あるとき、先頭を残し他は削除
// 参照返し
// ベクトル長も変化する
// O(logn)くらい
void unique(vector<long long> &v) {
long long n = v.size();
set<long long> s;
long long i = 0;
while (i < n) {
if (s.count(v.at(i))) {
erase(v, i);
n--;
} else {
s.insert(v.at(i));
i++;
}
}
return;
}
// ベクトルの出力(検証済)
// debug用にvectorの中身を出力する
void printv(vector<long long> v) {
cout << "{ ";
for (auto &p : v) {
cout << p << ",";
}
cout << "}" << endl;
}
// 10進法でn桁の整数xに対して、大きい方の位から、その位の1桁の数字を
// 収納した長さnのベクトルを返す
// 0に対しては{0}を返す
// 検証済み
vector<ll> keta(ll x) {
if (x == 0)
return {0};
ll n = log10(x) + 1; // xの桁数
vll w(n, 0);
for (ll i = 0; i < n; i++) {
ll p;
p = x % 10;
x = x / 10;
w[n - 1 - i] = p;
}
return w;
}
// 20200415
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
// a^{-1} mod を計算する
// modとaが互いに素のときのみ有効(数学的に逆元が一意に定まるのがそのときのみ)
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
// 整数n個の入力を受け取ってベクトルに突っ込んで返す
// チェック済み
vector<long long> inputv(long long n) {
vector<long long> v(n);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
return v;
}
vector<long long> yakusuu(long long n) // nの約数を列挙
{
vector<long long> ret;
for (long long i = 1; i <= sqrt(n); ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) {
ret.push_back(n / i);
}
}
}
sort(ret.begin(), ret.end());
return ret;
}
map<long long, long long> soinsuu(long long n) {
map<long long, long long> m;
long long p = sqrt(n);
while (n % 2 == 0) {
n /= 2;
if (m.count(2)) {
m[2]++;
} else {
m[2] = 1;
}
}
for (long long i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
n /= i;
if (m.count(i)) {
m[i]++;
} else {
m[i] = 1;
}
}
}
if (n != 1)
m[n] = 1;
return m;
}
// スタートが(i,j)の迷路の全ての地点までの距離を幅優先探索で解く
// スタートから何マス離れているか(辿り着けない場合は-1)を入れたベクトルを返す
// 壁からスタートしても正常に動作するので注意(この関数の外で処理が必要)
// 検証済み 一応O(地図の広さ)くらい
vector<vector<long long>> maze(ll i, ll j, vector<string> &s) {
ll h = s.size();
ll w = s[0].size();
queue<vector<long long>> q;
vector<vector<long long>> dis(h, vll(w, -1));
q.push({i, j});
dis[i][j] = 0;
while (!q.empty()) {
auto v = q.front();
q.pop();
if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) {
dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] - 1, v[1]});
}
if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) {
dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] - 1});
}
if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) {
dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] + 1, v[1]});
}
if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) {
dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] + 1});
}
}
return dis; // スタートから何マス離れているか(辿り着けない場合は-1)
}
// エラトステネスのふるいによりn以下の素数を全てベクトルに入れて返す
// vector<long long> eratos(long long n){
// }
// 二項係数の剰余を求める
// 引数は剰余の形ではなくもとの数そのものである
// 未検証(検証サンプルがない)
long long modC(long long n, long long k, long long mod) {
if (n < k)
return 0;
long long p = 1, q = 1;
for (long long i = 0; i < k; i++) {
p = p * (n - i) % mod;
q = q * (i + 1) % mod;
}
return p * modinv(q, mod) % mod;
}
// 20200418
// 整数のとき限定の普通のPOW関数
// 標準機能のpow(a,n)は整数だとバグるのでこちらを使う
long long POW(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
// 20200423
// エラトステネスのふるいによりn以下の素数を全てベクトルに入れて返す
vector<long long> eratos(long long n) {
if (n < 2)
return {};
vll v(n - 1);
rep(i, n - 1) {
v[i] = i + 2; // 2からnまで
}
ll i = 0;
while (i < n - 1) {
ll p = v[i];
for (ll j = i + 1; j < n - 1; j++) {
if (v[j] % p == 0) {
v.erase(v.begin() + j);
n--;
}
}
i++;
}
v.resize(n - 1);
return v;
}
// n以下の素数を全て詰めたset
set<long long> eraset(long long n) {
set<long long> s;
vll v = eratos(n);
for (auto &t : v) {
s.insert(t);
}
return s;
}
// cout<<fixed<<setprecision(10);
//////////////////////////////////////////////////
const ll mod = 1e9 + 7;
const ll m = 2019;
ll d(ll i, string s) {
string t = "";
ll n = s.size();
while (i < n - 13) {
string g = s.substr(i, 10);
t += g;
ll a = stoll(t);
t = to_string(a & m);
i += 10;
}
string g = s.substr(i, n - i);
t += g;
ll a = stoll(t);
return a % m;
}
int main() {
string s;
cin >> s;
ll n = s.size();
vll a(n + 1);
rep(i, n) { a[i + 1] = d(i, s); }
a[0] = 0;
// printv(a);
mll t;
rep(i, n + 1) {
if (t.count(a[i])) {
t[a[i]]++;
} else {
t[a[i]] = 1;
}
}
ll res = 0;
for (auto &y : t) {
res += y.second * (y.second - 1) / 2;
}
cout << res << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pq priority_queue
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
using vvvi = vector<vvi>;
using ll = long long; // long longをllだけにした
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using mii = map<int, int>;
using pqls = priority_queue<long long>;
using pqlg = priority_queue<long long, vector<long long>, greater<long long>>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using sll = set<long long>;
long long divup(long long a, long long b);
long long kaijou(long long i);
long long P(long long n, long long k);
long long C(long long n, long long k);
long long GCD(long long a, long long b);
long long LCM(long long a, long long b);
bool prime(long long N);
double distance(vector<long long> p, vector<long long> q, long long n);
void press(vector<long long> &v);
void ranking(vector<long long> &v);
void erase(vector<long long> &v, long long i);
void unique(vector<long long> &v);
void printv(vector<long long> v);
vector<ll> keta(ll x);
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);
// 20200416
vector<long long> inputv(long long n);
// 20200417
vector<long long> yakusuu(int n);
map<long long, long long> soinsuu(long long n);
vector<vector<long long>> maze(long long i, long long j, vector<string> &s);
// 20200423
vector<long long> eratos(long long n);
set<long long> eraset(long long n);
//////////////////////////////////////////////////////
// 端数繰りあがり割り算(検証済)
// a÷bの端数繰り上げ
// b!=0のデバグはしてないので分母に0を入れないように
// 負数対応
long long divup(long long a, long long b) {
long long x = abs(a);
long long y = abs(b);
long long z = (x + y - 1) / y;
if ((a < 0 && b > 0) || (a > 0 && b < 0))
return -z;
else if (a == 0)
return 0;
else
return z;
}
// 階乗
// 検証済み
long long kaijou(long long i) {
if (i == 0)
return 1;
long long j = 1;
for (long long k = 1; k <= i; k++) {
j *= k;
}
return j;
}
// 順列nPk(完成)
// n個の異なる要素から、取り出す順序を区別してk個取り出す場合の数
// n<kなら0を返す
// 敢えて負数時のデバグはしてない
long long P(long long n, long long k) {
if (n < k)
return 0;
long long y = 1;
for (long long i = 0; i < k; i++) {
y *= (n - i);
}
return y;
}
// 組み合わせnCk(検証済み)
// P,kaijouと併用
long long C(long long n, long long k) {
if (n < k)
return 0;
return P(n, k) / kaijou(k);
}
// nHk
// 区別しないn個の要素を、区別するk個のグループに分ける
// 0個のグループがあっ
// て良い
// C必須
// 最大公約数GCD,最小公倍数LCM
// LCMを使うときはGCDをセットで
// 検証済み
long long GCD(long long a, long long b) {
if (a < b)
swap(a, b);
long long d = a % b;
if (d == 0) {
return b;
}
return GCD(b, d);
}
long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; }
// 素数判定
// 素数ならばtrue、素数以外の整数にはfalse
// 負数は全てfalse
// 検証済み
bool prime(long long N) {
if (N == 1) {
return false;
}
if (N < 0)
return false;
long long p = sqrt(N);
for (long long i = 2; i <= p; i++) {
if (N % i == 0) {
return false;
}
}
return true;
}
// ユークリッド距離
// 検証済み
// 位置ベクトル1,位置ベクトル2,ベクトルの次元(2または3が一般的)
double distance(vector<long long> p, vector<long long> q, long long n) {
double x = 0;
for (long long i = 0; i < n; i++) {
x += pow((p.at(i) - q.at(i)), 2);
}
return sqrt(x);
}
// 配列圧縮(検証済)
//{1,36,1,3,8,-2,-92}を
//{2, 5,2,3,4, 1, 0}にする
void press(vector<long long> &v) {
long long n = v.size();
vector<long long> w(n);
map<long long, long long> m;
for (auto &p : v) {
m[p] = 0;
}
long long i = 0;
for (auto &p : m) {
p.second = i;
i++;
}
for (long long i = 0; i < n; i++) {
w.at(i) = m[v.at(i)];
}
v = w;
return;
}
// 配列のi番目の要素がj番目に小さいとき、j番目の数がiであるベクトルを返す関数
// 配列の要素が全て異なるときにしか正常に動作しない
// 配列の要素に同じものが含まれても見かけ上動作はするが意味のない値を戻し、
// エラーも起きないので注意
// 検証済
//{2,4,1,6,0,3,8,9,5}を
//{4,2,0,5,1,8,3,6,7}にして返す
//"rank"という名前にするとSTLの関数(配列の次元を返す関数)になるので注意
void ranking(vector<long long> &v) {
long long n = v.size();
map<long long, long long> m;
long long i;
for (i = 0; i < n; i++) {
m[v.at(i)] = i;
}
vector<long long> w(n);
i = 0;
for (auto &p : m) {
v.at(i) = p.second;
i++;
}
return;
}
// 部分削除(未検証)
// ベクトルのi番目(i=0,1,2,...,n-1)の要素を削除し、
// 以降の要素を全て前に1ずらして参照返し
// ベクトル長は1小さくなって返る
// i>n-1の時は変化しない
void erase(vector<long long> &v, long long i) {
long long n = v.size();
if (i > n - 1)
return;
for (long long j = i; j < n - 1; j++) {
v.at(j) = v.at(j + 1);
}
v.pop_back();
return;
}
// 重複削除(未完成)
// 引数ベクトルに同一要素が複数あるとき、先頭を残し他は削除
// 参照返し
// ベクトル長も変化する
// O(logn)くらい
void unique(vector<long long> &v) {
long long n = v.size();
set<long long> s;
long long i = 0;
while (i < n) {
if (s.count(v.at(i))) {
erase(v, i);
n--;
} else {
s.insert(v.at(i));
i++;
}
}
return;
}
// ベクトルの出力(検証済)
// debug用にvectorの中身を出力する
void printv(vector<long long> v) {
cout << "{ ";
for (auto &p : v) {
cout << p << ",";
}
cout << "}" << endl;
}
// 10進法でn桁の整数xに対して、大きい方の位から、その位の1桁の数字を
// 収納した長さnのベクトルを返す
// 0に対しては{0}を返す
// 検証済み
vector<ll> keta(ll x) {
if (x == 0)
return {0};
ll n = log10(x) + 1; // xの桁数
vll w(n, 0);
for (ll i = 0; i < n; i++) {
ll p;
p = x % 10;
x = x / 10;
w[n - 1 - i] = p;
}
return w;
}
// 20200415
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
// a^{-1} mod を計算する
// modとaが互いに素のときのみ有効(数学的に逆元が一意に定まるのがそのときのみ)
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
// 整数n個の入力を受け取ってベクトルに突っ込んで返す
// チェック済み
vector<long long> inputv(long long n) {
vector<long long> v(n);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
return v;
}
vector<long long> yakusuu(long long n) // nの約数を列挙
{
vector<long long> ret;
for (long long i = 1; i <= sqrt(n); ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) {
ret.push_back(n / i);
}
}
}
sort(ret.begin(), ret.end());
return ret;
}
map<long long, long long> soinsuu(long long n) {
map<long long, long long> m;
long long p = sqrt(n);
while (n % 2 == 0) {
n /= 2;
if (m.count(2)) {
m[2]++;
} else {
m[2] = 1;
}
}
for (long long i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
n /= i;
if (m.count(i)) {
m[i]++;
} else {
m[i] = 1;
}
}
}
if (n != 1)
m[n] = 1;
return m;
}
// スタートが(i,j)の迷路の全ての地点までの距離を幅優先探索で解く
// スタートから何マス離れているか(辿り着けない場合は-1)を入れたベクトルを返す
// 壁からスタートしても正常に動作するので注意(この関数の外で処理が必要)
// 検証済み 一応O(地図の広さ)くらい
vector<vector<long long>> maze(ll i, ll j, vector<string> &s) {
ll h = s.size();
ll w = s[0].size();
queue<vector<long long>> q;
vector<vector<long long>> dis(h, vll(w, -1));
q.push({i, j});
dis[i][j] = 0;
while (!q.empty()) {
auto v = q.front();
q.pop();
if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) {
dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] - 1, v[1]});
}
if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) {
dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] - 1});
}
if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) {
dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] + 1, v[1]});
}
if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) {
dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] + 1});
}
}
return dis; // スタートから何マス離れているか(辿り着けない場合は-1)
}
// エラトステネスのふるいによりn以下の素数を全てベクトルに入れて返す
// vector<long long> eratos(long long n){
// }
// 二項係数の剰余を求める
// 引数は剰余の形ではなくもとの数そのものである
// 未検証(検証サンプルがない)
long long modC(long long n, long long k, long long mod) {
if (n < k)
return 0;
long long p = 1, q = 1;
for (long long i = 0; i < k; i++) {
p = p * (n - i) % mod;
q = q * (i + 1) % mod;
}
return p * modinv(q, mod) % mod;
}
// 20200418
// 整数のとき限定の普通のPOW関数
// 標準機能のpow(a,n)は整数だとバグるのでこちらを使う
long long POW(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
// 20200423
// エラトステネスのふるいによりn以下の素数を全てベクトルに入れて返す
vector<long long> eratos(long long n) {
if (n < 2)
return {};
vll v(n - 1);
rep(i, n - 1) {
v[i] = i + 2; // 2からnまで
}
ll i = 0;
while (i < n - 1) {
ll p = v[i];
for (ll j = i + 1; j < n - 1; j++) {
if (v[j] % p == 0) {
v.erase(v.begin() + j);
n--;
}
}
i++;
}
v.resize(n - 1);
return v;
}
// n以下の素数を全て詰めたset
set<long long> eraset(long long n) {
set<long long> s;
vll v = eratos(n);
for (auto &t : v) {
s.insert(t);
}
return s;
}
// cout<<fixed<<setprecision(10);
//////////////////////////////////////////////////
const ll mod = 1e9 + 7;
const ll m = 2019;
ll d(ll i, string s) {
string t = "";
ll n = s.size();
while (i < n - 13) {
string g = s.substr(i, 10);
t += g;
ll a = stoll(t);
t = to_string(a & m);
i += 10;
}
string g = s.substr(i, n - i);
t += g;
ll a = stoll(t);
return a % m;
}
int main() {
string s;
cin >> s;
ll n = s.size();
vll a(n + 1);
a[n] = 0;
rep(i, n) {
a[n - i - 1] = a[n - i] + modpow(10, i, 2019) * (s[n - i - 1] - '0');
a[n - i - 1] %= m;
}
rep(i, n) {
while (a[i] < 0)
a[i] += m;
}
// printv(a);
mll t;
rep(i, n + 1) {
if (t.count(a[i])) {
t[a[i]]++;
} else {
t[a[i]] = 1;
}
}
ll res = 0;
for (auto &y : t) {
res += y.second * (y.second - 1) / 2;
}
cout << res << endl;
}
| replace | 453 | 455 | 453 | 462 | TLE | |
p02702 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
long long n, len, dp[2][200002];
string s;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("test.inp","r",stdin);
// freopen("test.out","w",stdout);
cin >> s;
len = s.length();
long long res = 0;
dp[0][(s[0] - '0') % 2019] = 1;
for (int i = 1; i < len; i++) {
// cout<<dp[1][0]<<endl;
for (int j = 0; j < 2019; j++) {
dp[1][(j * 10 + (s[i] - '0')) % 2019] += dp[0][j];
// if (( j*10 + (s[i]-'0') ) % 2019==0) cout<<i<<" "<<j<<"
// "<<dp[0][403]<<endl;
}
res += dp[1][0];
// cout<<res<<endl;
memcpy(dp[0], dp[1], sizeof(dp[1]));
dp[0][(s[i] - '0') % 2019]++;
memset(dp[1], 0, sizeof(dp[1]));
}
cout << res;
} | #include <bits/stdc++.h>
using namespace std;
long long n, len, dp[2][2020];
string s;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("test.inp","r",stdin);
// freopen("test.out","w",stdout);
cin >> s;
len = s.length();
long long res = 0;
dp[0][(s[0] - '0') % 2019] = 1;
for (int i = 1; i < len; i++) {
// cout<<dp[1][0]<<endl;
for (int j = 0; j < 2019; j++) {
dp[1][(j * 10 + (s[i] - '0')) % 2019] += dp[0][j];
// if (( j*10 + (s[i]-'0') ) % 2019==0) cout<<i<<" "<<j<<"
// "<<dp[0][403]<<endl;
}
res += dp[1][0];
// cout<<res<<endl;
memcpy(dp[0], dp[1], sizeof(dp[1]));
dp[0][(s[i] - '0') % 2019]++;
memset(dp[1], 0, sizeof(dp[1]));
}
cout << res;
} | replace | 2 | 3 | 2 | 3 | TLE | |
p02702 | C++ | Runtime Error | #include <stdio.h>
char s[20010];
int c[2020], inv = 202;
int main() {
int ans = 0, tt = 1, cur = 0;
scanf("%s", s);
c[0] = 1;
for (int i = 0; s[i]; i++) {
int x = s[i] - '0';
cur = (cur * 10 + x) % 2019;
tt = (tt * inv) % 2019;
ans += c[(cur * tt) % 2019];
c[(cur * tt) % 2019]++;
}
printf("%d\n", ans);
return 0;
}
| #include <stdio.h>
char s[200010];
int c[2020], inv = 202;
int main() {
int ans = 0, tt = 1, cur = 0;
scanf("%s", s);
c[0] = 1;
for (int i = 0; s[i]; i++) {
int x = s[i] - '0';
cur = (cur * 10 + x) % 2019;
tt = (tt * inv) % 2019;
ans += c[(cur * tt) % 2019];
c[(cur * tt) % 2019]++;
}
printf("%d\n", ans);
return 0;
}
| replace | 1 | 2 | 1 | 2 | 0 | |
p02702 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// #define fi first
// #define se second
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pi;
// const ll M=1000000007ll;
// const ll MOD=1000000007ll;
const ll MOD = 2019ll;
const ll MAX = 100000001ll;
// const ll M = 1000000000+7;
// const int mod = 1000000007;
const int mod = 2019;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
mint f(ll n) {
if (n == 0)
return 1;
mint ans = f(n / 2);
ans *= ans;
if (n % 2 == 1)
ans *= 2;
return ans;
}
mint g(ll n, ll x) {
if (x == 0)
return 1;
mint ans = g(n, x / 2);
ans *= ans;
if (x % 2 == 1)
ans *= n;
return ans;
}
mint choose(ll n, ll k) {
mint x = 1, y = 1;
rep(i, k) {
x *= (n - i);
y *= (i + 1);
}
return x / y;
}
struct BIT {
private:
vector<ll> bit;
ll N;
public:
BIT(ll size) {
N = size;
bit.resize(N + 1);
}
// 一点更新です
void add(ll a, ll w) {
for (ll x = a; x <= N; x += x & -x)
bit[x] += w;
}
// 1~Nまでの和を求める。
int sum(ll a) {
ll ret = 0;
for (ll x = a; x > 0; x -= x & -x)
ret += bit[x];
return ret;
}
};
ll fff(ll n, vector<ll> &v, ll i, ll j, ll rl) {
ll can = 0;
ll can_i = 0;
cout << v[0] << endl;
// right
if (rl == 0) {
rep(k, n) {
if (v[k] * (j - k) > can) {
can = v[k] * (j - k);
can_i = k;
}
// cout << can<<endl;
}
v[can_i] = 0;
return can;
}
// left
else {
rep(k, n) {
if (v[k] * (k - i) > can) {
can = v[k] * (k - i);
can_i = k;
}
// cout << can<<endl;
}
v[can_i] = 0;
return can;
}
}
ll ff(ll x) { return x * (x + 1) / 2; }
int main() {
string s;
cin >> s;
ll n = s.size();
vector<mint> v(n + 1);
v[n] = mint(0);
ll tmp = 0;
rep(i, n) {
// cout << s[n-1-i]<<endl;
// cout << g(10, 2).x<<endl;
v[n - 1 - i] = v[n - i];
v[n - 1 - i] += g(10, (i)) * mint(s[n - 1 - i] - '0');
// v[n-1-i] *= ();
// cout << v[n-1-i].x<<endl;
}
ll ans = 0;
rep(i, n) {
rep(j, n + 1) {
if (i >= j)
continue;
mint z = v[i] - v[j];
// cout <<"#"<<i<<" "<<j<<z.x<<endl;
if ((z.x) == 0)
ans += 1;
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
// #define fi first
// #define se second
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pi;
// const ll M=1000000007ll;
// const ll MOD=1000000007ll;
const ll MOD = 2019ll;
const ll MAX = 100000001ll;
// const ll M = 1000000000+7;
// const int mod = 1000000007;
const int mod = 2019;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
mint f(ll n) {
if (n == 0)
return 1;
mint ans = f(n / 2);
ans *= ans;
if (n % 2 == 1)
ans *= 2;
return ans;
}
mint g(ll n, ll x) {
if (x == 0)
return 1;
mint ans = g(n, x / 2);
ans *= ans;
if (x % 2 == 1)
ans *= n;
return ans;
}
mint choose(ll n, ll k) {
mint x = 1, y = 1;
rep(i, k) {
x *= (n - i);
y *= (i + 1);
}
return x / y;
}
struct BIT {
private:
vector<ll> bit;
ll N;
public:
BIT(ll size) {
N = size;
bit.resize(N + 1);
}
// 一点更新です
void add(ll a, ll w) {
for (ll x = a; x <= N; x += x & -x)
bit[x] += w;
}
// 1~Nまでの和を求める。
int sum(ll a) {
ll ret = 0;
for (ll x = a; x > 0; x -= x & -x)
ret += bit[x];
return ret;
}
};
ll fff(ll n, vector<ll> &v, ll i, ll j, ll rl) {
ll can = 0;
ll can_i = 0;
cout << v[0] << endl;
// right
if (rl == 0) {
rep(k, n) {
if (v[k] * (j - k) > can) {
can = v[k] * (j - k);
can_i = k;
}
// cout << can<<endl;
}
v[can_i] = 0;
return can;
}
// left
else {
rep(k, n) {
if (v[k] * (k - i) > can) {
can = v[k] * (k - i);
can_i = k;
}
// cout << can<<endl;
}
v[can_i] = 0;
return can;
}
}
ll ff(ll x) { return x * (x + 1) / 2; }
int main() {
string s;
cin >> s;
ll n = s.size();
vector<mint> v(n + 1);
v[n] = mint(0);
ll tmp = 0;
rep(i, n) {
// cout << s[n-1-i]<<endl;
// cout << g(10, 2).x<<endl;
v[n - 1 - i] = v[n - i];
v[n - 1 - i] += g(10, (i)) * mint(s[n - 1 - i] - '0');
// v[n-1-i] *= ();
// cout << v[n-1-i].x<<endl;
}
ll ans = 0;
map<ll, ll> m;
rep(i, n + 1) { m[v[i].x]++; }
for (auto w : m) {
ans += (w.second) * (w.second - 1) / 2;
}
cout << ans << endl;
}
| replace | 193 | 202 | 193 | 198 | TLE | |
p02702 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
/*
__,.二ニ==- ニ.、_.
.,..‐ ⌒ ``'ァ-ニ、.
ィ'´. ´丶.、
.,ィ'´ `.x、..
.. /. 、\.
.ン′. ¬`""冖ーミト.
,r′. .ヘ、 `
ツ `、....
./´ ヘ
..... /. .〉.
´/.. .l
ィ . f..
.. .f ,d. l ′ 」 ,. ト !
. 〕../.f.. ′ .. | .} | |.
.!./..f.. / !- ナ丶п冖т ノー- . 〕 |.
|メ | | j , ┌. |〈. л`. /|.. ┤,..
...「...|. | ´ l. | j.L......ュ.L_└ヽ_|Y. メムw ょ | j.: |  ̄
. |. т〕<.ィ冖T冖.. г‐ `、 `, /┴¬..г ̄|.. .′ |
... | ... ),|.. ` リ 「_ノ.|| ` V |!{,「ll ´.」. 卜
. |.」 ′ ヽ └++〃.. ルwf カz′. |.
|..〕 「 .|
.l.|. ′. |
. .〕.. `! _.....ー:'' 」 ´ λ.
_「. , ┐_,、`~‐''"´ ィ .、 ヘ、
f :__..,二ュ.-i―'''^~´ 、\イ ヘ.`x
. / { j .~^
、/ 't.. 丿..
.../. ,x┐.. ∠∫
:^. /  ̄冖ー=zzュ┌ー―-- ∟,二..._. _,、.-ー.'l+~. .l`.
. У. ⌒冖‐-=._.. l「.「 ´ ̄」了 .,、-''^ 〉 ヽ_
_/.  ̄~'.ー-=.、_,..usァ.ー''" { \´
_ヰl'¬―- 、_ ( .\
*/
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
ll const MOD = 1000000007;
ll const INF = (long long int)1 << 61;
ll mypow(ll x, ll n, ll mod = MOD) {
ll ret = 1;
while (n > 0) {
if (n & 1) {
ret = (ret * x) % mod;
}
x = (x * x) % mod;
n >>= 1;
}
return ret;
}
ll mygcd(ll a, ll b) {
if (b == 0)
return a;
return mygcd(b, a % b);
}
ll twoPow(ll shiftNum) { return (1LL << (shiftNum - 1)); }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string str;
cin >> str;
int ans = 0;
vector<vector<int>> dp(2, vector<int>(2019, 0));
for (int i = 0; i < str.size(); i++) {
int x = (int)(str[i] - '0');
dp[1][x] = 1;
for (int j = 0; j < 2019; j++) {
dp[1][(j * 10 + x) % 2019] += dp[0][j];
}
ans += dp[i + 1][0];
swap(dp[0], dp[1]);
for (int j = 0; j < 2019; j++) {
dp[1][j] = 0;
}
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
/*
__,.二ニ==- ニ.、_.
.,..‐ ⌒ ``'ァ-ニ、.
ィ'´. ´丶.、
.,ィ'´ `.x、..
.. /. 、\.
.ン′. ¬`""冖ーミト.
,r′. .ヘ、 `
ツ `、....
./´ ヘ
..... /. .〉.
´/.. .l
ィ . f..
.. .f ,d. l ′ 」 ,. ト !
. 〕../.f.. ′ .. | .} | |.
.!./..f.. / !- ナ丶п冖т ノー- . 〕 |.
|メ | | j , ┌. |〈. л`. /|.. ┤,..
...「...|. | ´ l. | j.L......ュ.L_└ヽ_|Y. メムw ょ | j.: |  ̄
. |. т〕<.ィ冖T冖.. г‐ `、 `, /┴¬..г ̄|.. .′ |
... | ... ),|.. ` リ 「_ノ.|| ` V |!{,「ll ´.」. 卜
. |.」 ′ ヽ └++〃.. ルwf カz′. |.
|..〕 「 .|
.l.|. ′. |
. .〕.. `! _.....ー:'' 」 ´ λ.
_「. , ┐_,、`~‐''"´ ィ .、 ヘ、
f :__..,二ュ.-i―'''^~´ 、\イ ヘ.`x
. / { j .~^
、/ 't.. 丿..
.../. ,x┐.. ∠∫
:^. /  ̄冖ー=zzュ┌ー―-- ∟,二..._. _,、.-ー.'l+~. .l`.
. У. ⌒冖‐-=._.. l「.「 ´ ̄」了 .,、-''^ 〉 ヽ_
_/.  ̄~'.ー-=.、_,..usァ.ー''" { \´
_ヰl'¬―- 、_ ( .\
*/
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
ll const MOD = 1000000007;
ll const INF = (long long int)1 << 61;
ll mypow(ll x, ll n, ll mod = MOD) {
ll ret = 1;
while (n > 0) {
if (n & 1) {
ret = (ret * x) % mod;
}
x = (x * x) % mod;
n >>= 1;
}
return ret;
}
ll mygcd(ll a, ll b) {
if (b == 0)
return a;
return mygcd(b, a % b);
}
ll twoPow(ll shiftNum) { return (1LL << (shiftNum - 1)); }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string str;
cin >> str;
int ans = 0;
vector<vector<int>> dp(2, vector<int>(2019, 0));
for (int i = 0; i < str.size(); i++) {
int x = (int)(str[i] - '0');
dp[1][x] = 1;
for (int j = 0; j < 2019; j++) {
dp[1][(j * 10 + x) % 2019] += dp[0][j];
}
ans += dp[1][0];
swap(dp[0], dp[1]);
for (int j = 0; j < 2019; j++) {
dp[1][j] = 0;
}
}
cout << ans << endl;
return 0;
}
| replace | 96 | 97 | 96 | 97 | -11 | |
p02702 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#pragma region Macros
#define FOR(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define rep(i, n) FOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> string join(const T &v, const string delim = ",") {
if (v.empty())
return "";
ostringstream res;
res << v[0];
for (int i = 1; i < v.size(); i++) {
res << delim << v[i];
}
return res.str();
}
#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << ":" << #x << " = " << (x) << endl;
#else
#define dbg
#endif
#pragma endregion Macros
template <long long Mod> struct modint {
ll n;
modint(ll x = 0) : n((x + Mod) % Mod) {}
modint operator+(const modint &r) const { return modint(*this) += r; }
modint operator-(const modint &r) const { return modint(*this) -= r; }
modint operator*(const modint &r) const { return modint(*this) *= r; }
modint operator/(const modint &r) const { return modint(*this) /= r; }
modint operator+=(const modint &r) {
n += r.n;
n %= Mod;
return *this;
}
modint operator-=(const modint &r) {
n -= r.n;
n += Mod;
n %= Mod;
return *this;
}
modint operator*=(const modint &r) {
n *= r.n;
n %= Mod;
return *this;
}
modint operator/=(const modint &r) {
*this *= modint(r).inv();
return *this;
}
modint operator=(const modint &r) { return n = r.n; }
modint operator==(const modint &r) const { return n == r.n; }
modint operator!=(const modint &r) const { return n != r.n; }
modint inv() const { return pow(Mod - 2); }
modint pow(const ll x) const {
if (n == 0)
return modint(0);
if (x == 0)
return modint(1);
if (x == 1)
return modint(n);
modint a = modint(*this);
a = (a * a).pow(x / 2);
if (x % 2 == 1) {
a *= n;
}
return a;
}
friend ostream &operator<<(ostream &os, const modint &x) { return os << x.n; }
friend istream &operator>>(istream &is, modint &x) { return is >> x.n; }
};
using mint = modint<2019>;
int main() {
string S;
cin >> S;
int N = S.size();
vector<mint> m(N); // m[i] : Sの[i,N)の部分文字列を、mod 2019した値
m[N - 1] = S[N - 1] - '0';
for (int i = N - 2, k = 1; i >= 0; i--, k++) {
mint s = mint(S[i] - '0') * mint(10).pow(k);
m[i] = m[i + 1] + s;
}
vector<vector<int>> t(
N + 1, vector<int>(2020, 0)); // t[i][j] : [i,N)の、mod 2019がjのものの数
for (int i = N - 1; i >= 0; i--) {
t[i][m[i].n]++;
rep(j, 2020) t[i][j] += t[i + 1][j];
}
ll ans = 0;
for (int i = 0; i < N; i++) {
ans += t[i + 1][m[i].n];
if (m[i].n == 0)
ans++;
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#pragma region Macros
#define FOR(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define rep(i, n) FOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> string join(const T &v, const string delim = ",") {
if (v.empty())
return "";
ostringstream res;
res << v[0];
for (int i = 1; i < v.size(); i++) {
res << delim << v[i];
}
return res.str();
}
#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << ":" << #x << " = " << (x) << endl;
#else
#define dbg
#endif
#pragma endregion Macros
template <long long Mod> struct modint {
ll n;
modint(ll x = 0) : n((x + Mod) % Mod) {}
modint operator+(const modint &r) const { return modint(*this) += r; }
modint operator-(const modint &r) const { return modint(*this) -= r; }
modint operator*(const modint &r) const { return modint(*this) *= r; }
modint operator/(const modint &r) const { return modint(*this) /= r; }
modint operator+=(const modint &r) {
n += r.n;
n %= Mod;
return *this;
}
modint operator-=(const modint &r) {
n -= r.n;
n += Mod;
n %= Mod;
return *this;
}
modint operator*=(const modint &r) {
n *= r.n;
n %= Mod;
return *this;
}
modint operator/=(const modint &r) {
*this *= modint(r).inv();
return *this;
}
modint operator=(const modint &r) { return n = r.n; }
modint operator==(const modint &r) const { return n == r.n; }
modint operator!=(const modint &r) const { return n != r.n; }
modint inv() const { return pow(Mod - 2); }
modint pow(const ll x) const {
if (n == 0)
return modint(0);
if (x == 0)
return modint(1);
if (x == 1)
return modint(n);
modint a = modint(*this);
a = (a * a).pow(x / 2);
if (x % 2 == 1) {
a *= n;
}
return a;
}
friend ostream &operator<<(ostream &os, const modint &x) { return os << x.n; }
friend istream &operator>>(istream &is, modint &x) { return is >> x.n; }
};
using mint = modint<2019>;
int main() {
string S;
cin >> S;
int N = S.size();
vector<mint> m(N); // m[i] : Sの[i,N)の部分文字列を、mod 2019した値
m[N - 1] = S[N - 1] - '0';
for (int i = N - 2, k = 1; i >= 0; i--, k++) {
mint s = mint(S[i] - '0') * mint(10).pow(k);
m[i] = m[i + 1] + s;
}
vector<vector<short>> t(
N + 1,
vector<short>(2020, 0)); // t[i][j] : [i,N)の、mod 2019がjのものの数
for (int i = N - 1; i >= 0; i--) {
t[i][m[i].n]++;
rep(j, 2020) t[i][j] += t[i + 1][j];
}
ll ans = 0;
for (int i = 0; i < N; i++) {
ans += t[i + 1][m[i].n];
if (m[i].n == 0)
ans++;
}
cout << ans << endl;
}
| replace | 116 | 118 | 116 | 119 | MLE | |
p02702 | C++ | Runtime Error | #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const int MOD = 2019;
ll modpow(int a, int n) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % MOD;
a *= a % MOD;
n >>= 1;
}
return res;
}
int main() {
string S;
cin >> S;
vi cnt(2019, 0);
cnt[0]++;
ll sum = 0;
for (int i = S.size() - 1; i >= 0; i--) {
sum = (sum + (S[i] - '0') * modpow(10, S.size() - 1 - i) % MOD) % MOD;
cnt[sum]++;
}
ll res = 0;
for (int i = 0; i < 2019; i++) {
res += cnt[i] * (cnt[i] - 1) / 2;
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const int MOD = 2019;
ll modpow(int a, int n) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return res;
}
int main() {
string S;
cin >> S;
vi cnt(2019, 0);
cnt[0]++;
ll sum = 0;
for (int i = S.size() - 1; i >= 0; i--) {
sum = (sum + (S[i] - '0') * modpow(10, S.size() - 1 - i) % MOD) % MOD;
cnt[sum]++;
}
ll res = 0;
for (int i = 0; i < 2019; i++) {
res += cnt[i] * (cnt[i] - 1) / 2;
}
cout << res << endl;
return 0;
} | replace | 20 | 21 | 20 | 21 | 0 | |
p02702 | C++ | Runtime Error | #include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
string s;
cin >> s;
vector<ll> x(2019, 0);
x[0] += 1;
ll k = 0;
ll p = 1;
for (int i = 1; i <= s.size(); i++) {
k += ((ll)(s[s.size() - i] - '0')) * p % 2019;
k %= 2019;
x[k % 2019] += 1;
p *= 10;
}
ll ans = 0;
for (auto iter = x.begin(); iter != x.end(); iter++) {
if (*iter > 0) {
ans += (*iter) * (*iter - 1) / 2;
}
}
cout << ans << endl;
}
| #include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
string s;
cin >> s;
vector<ll> x(2019, 0);
x[0] += 1;
ll k = 0;
ll p = 1;
for (int i = 1; i <= s.size(); i++) {
k += ((ll)(s[s.size() - i] - '0')) * p % 2019;
k %= 2019;
x[k % 2019] += 1;
p *= 10;
p %= 2019;
}
ll ans = 0;
for (auto iter = x.begin(); iter != x.end(); iter++) {
if (*iter > 0) {
ans += (*iter) * (*iter - 1) / 2;
}
}
cout << ans << endl;
}
| insert | 19 | 19 | 19 | 20 | 0 | |
p02702 | C++ | Runtime Error | #include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
int main() {
string s;
cin >> s;
int n = s.size(), a = 0, b = 1, r[2019];
rep(i, 2019) r[i] = 0;
rep(i, n) {
a = (a + (s[n - 1 - i] - '0') * b) % 2019;
b = (b * 10) % 2019;
r[a]++;
}
ll res = 0;
rep(i, n) res += r[i] * (r[i] + 1) / 2;
cout << res << endl;
return 0;
} | #include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
int main() {
string s;
cin >> s;
int n = s.size(), a = 0, b = 1, r[2019];
rep(i, 2019) r[i] = 0;
rep(i, n) {
a = (a + (s[n - 1 - i] - '0') * b) % 2019;
b = (b * 10) % 2019;
r[a]++;
}
ll res = r[0];
rep(i, 2019) res += r[i] * (r[i] - 1) / 2;
cout << res << endl;
return 0;
} | replace | 18 | 22 | 18 | 20 | 0 | |
p02702 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MOD = 2019;
ll nums[MOD + 5];
int main() {
string s;
cin >> s;
int n = s.size();
nums[0] = 1;
ll num = 0, t = 1;
for (int i = n - 1; i >= 0; i--, t *= 10) {
num = (num + (s[i] - '0') * t) % MOD;
nums[num]++;
}
ll ans = 0;
for (int i = 0; i < MOD; i++) {
ans += nums[i] * (nums[i] - 1) / 2;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MOD = 2019;
ll nums[MOD + 5];
int main() {
string s;
cin >> s;
int n = s.size();
nums[0] = 1;
ll num = 0, t = 1;
for (int i = n - 1; i >= 0; i--, t *= 10) {
t %= MOD;
num = (num + (s[i] - '0') * t) % MOD;
nums[num]++;
}
ll ans = 0;
for (int i = 0; i < MOD; i++) {
ans += nums[i] * (nums[i] - 1) / 2;
}
cout << ans << endl;
} | insert | 16 | 16 | 16 | 17 | 0 | |
p02702 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
string s;
cin >> s;
ll n = s.size();
ll p[2019] = {};
ll t = 0;
ll d = 1;
ll ans = 0;
for (ll i = n - 1; i >= 0; i--) {
p[t]++;
t += ((s[i] - '0') * d + t) % 2019;
d = (d * 10) % 2019;
ans += p[t];
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
string s;
cin >> s;
ll n = s.size();
ll p[2019] = {};
ll t = 0;
ll d = 1;
ll ans = 0;
for (ll i = n - 1; i >= 0; i--) {
p[t]++;
t = ((s[i] - '0') * d + t) % 2019;
d = (d * 10) % 2019;
ans += p[t];
}
cout << ans << endl;
} | replace | 14 | 15 | 14 | 15 | -11 | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
struct Edge {
Edge(int64_t t, int64_t A, int64_t B) : to(t), cost_coin(A), cost_time(B) {}
int64_t to, cost_coin, cost_time;
};
int main() {
int64_t N, M, S;
cin >> N >> M >> S;
vector<int64_t> U(M), V(M), A(M), B(M);
vector<vector<Edge>> edges(N);
for (int64_t i = 0; i < M; i++) {
cin >> U[i] >> V[i] >> A[i] >> B[i];
U[i]--;
V[i]--;
edges[U[i]].emplace_back(V[i], A[i], B[i]);
edges[V[i]].emplace_back(U[i], A[i], B[i]);
}
vector<int64_t> C(N), D(N);
for (int64_t i = 0; i < N; i++) {
cin >> C[i] >> D[i];
}
const int64_t MAX_COIN = M * 1000;
constexpr int64_t INF = LLONG_MAX;
vector<vector<int64_t>> cost(N, vector<int64_t>(MAX_COIN + 1, INF));
struct Element {
int64_t node, coin, cost_time;
bool operator<(const Element &rhs) const {
return cost_time < rhs.cost_time;
}
bool operator>(const Element &rhs) const {
return cost_time > rhs.cost_time;
}
};
priority_queue<Element, vector<Element>, greater<Element>> pq;
S = min(S, MAX_COIN);
for (int64_t i = 0; i <= S; i++) {
cost[0][i] = 0;
pq.push({0, i, 0});
}
while (!pq.empty()) {
Element t = pq.top();
// cout << t.node << " " << t.coin << " " << t.cost_time << endl;
pq.pop();
if (t.cost_time > cost[t.node][t.coin]) {
continue;
}
// 移動可能なら移動
for (const Edge &e : edges[t.node]) {
if (e.cost_coin > t.coin) {
continue;
}
int64_t new_cost_time = t.cost_time + e.cost_time;
int64_t new_coin = t.coin - e.cost_coin;
if (new_cost_time < cost[e.to][new_coin]) {
cost[e.to][new_coin] = new_cost_time;
pq.push({e.to, new_coin, new_cost_time});
}
}
// ここで一回交換
int64_t new_cost_time = t.cost_time + D[t.node];
int64_t new_coin = min(MAX_COIN, t.coin + C[t.node]);
if (new_cost_time < cost[t.node][new_coin]) {
cost[t.node][new_coin] = new_cost_time;
pq.push({t.node, new_coin, new_cost_time});
}
}
for (int64_t i = 1; i < N; i++) {
cout << *min_element(cost[i].begin(), cost[i].end()) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
struct Edge {
Edge(int64_t t, int64_t A, int64_t B) : to(t), cost_coin(A), cost_time(B) {}
int64_t to, cost_coin, cost_time;
};
int main() {
int64_t N, M, S;
cin >> N >> M >> S;
vector<int64_t> U(M), V(M), A(M), B(M);
vector<vector<Edge>> edges(N);
for (int64_t i = 0; i < M; i++) {
cin >> U[i] >> V[i] >> A[i] >> B[i];
U[i]--;
V[i]--;
edges[U[i]].emplace_back(V[i], A[i], B[i]);
edges[V[i]].emplace_back(U[i], A[i], B[i]);
}
vector<int64_t> C(N), D(N);
for (int64_t i = 0; i < N; i++) {
cin >> C[i] >> D[i];
}
const int64_t MAX_COIN = M * 100;
constexpr int64_t INF = LLONG_MAX;
vector<vector<int64_t>> cost(N, vector<int64_t>(MAX_COIN + 1, INF));
struct Element {
int64_t node, coin, cost_time;
bool operator<(const Element &rhs) const {
return cost_time < rhs.cost_time;
}
bool operator>(const Element &rhs) const {
return cost_time > rhs.cost_time;
}
};
priority_queue<Element, vector<Element>, greater<Element>> pq;
S = min(S, MAX_COIN);
for (int64_t i = 0; i <= S; i++) {
cost[0][i] = 0;
pq.push({0, i, 0});
}
while (!pq.empty()) {
Element t = pq.top();
// cout << t.node << " " << t.coin << " " << t.cost_time << endl;
pq.pop();
if (t.cost_time > cost[t.node][t.coin]) {
continue;
}
// 移動可能なら移動
for (const Edge &e : edges[t.node]) {
if (e.cost_coin > t.coin) {
continue;
}
int64_t new_cost_time = t.cost_time + e.cost_time;
int64_t new_coin = t.coin - e.cost_coin;
if (new_cost_time < cost[e.to][new_coin]) {
cost[e.to][new_coin] = new_cost_time;
pq.push({e.to, new_coin, new_cost_time});
}
}
// ここで一回交換
int64_t new_cost_time = t.cost_time + D[t.node];
int64_t new_coin = min(MAX_COIN, t.coin + C[t.node]);
if (new_cost_time < cost[t.node][new_coin]) {
cost[t.node][new_coin] = new_cost_time;
pq.push({t.node, new_coin, new_cost_time});
}
}
for (int64_t i = 1; i < N; i++) {
cout << *min_element(cost[i].begin(), cost[i].end()) << endl;
}
} | replace | 26 | 27 | 26 | 27 | TLE | |
p02703 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 1e+15
#define MAX_V 1000000
struct edge {
long long to;
long long cost;
};
// <最短距離, 頂点の番号>
using P = pair<long long, long long>;
long long V;
vector<edge> G[MAX_V];
long long d[MAX_V];
void dijkstra(long long s) {
priority_queue<P, vector<P>, greater<P>> que;
fill(d, d + V, INF);
d[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
long long v = p.second;
if (d[v] < p.first)
continue;
// if prim,
// d[v] = -d[v];
// for keep tree
for (long long i = 0; i < (long long)G[v].size(); ++i) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
}
}
}
int main() {
long long N;
cin >> N;
V = 1000000;
long long E;
cin >> E;
long long S;
cin >> S;
long long U, V, A, B;
for (long long i = 0; i < E; ++i) {
cin >> U >> V >> A >> B;
for (long long j = 5000; j >= A; j--) {
edge e = {10000 * V + j - A, B};
G[10000 * U + j].push_back(e);
edge e2 = {10000 * U + j - A, B};
G[10000 * V + j].push_back(e2);
}
}
long long C, D;
for (long long i = 1; i <= N; i++) {
cin >> C >> D;
for (long long j = 0; j <= 5000; j++) {
if (j + C <= 5000) {
edge e3 = {10000 * i + j + C, D};
G[10000 * i + j].push_back(e3);
} else {
edge e4 = {10000 * i + 5000, D};
G[10000 * i + j].push_back(e4);
}
}
}
dijkstra(10000 + S);
long long ans[51];
fill(ans, ans + 51, INF);
for (long long i = 2; i <= N; i++) {
for (long long j = 0; j <= 5000; j++) {
ans[i] = min(ans[i], d[10000 * i + j]);
}
}
for (long long i = 2; i <= N; i++) {
cout << ans[i] << endl;
}
system("pause");
return 0;
} | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 1e+15
#define MAX_V 1000000
struct edge {
long long to;
long long cost;
};
// <最短距離, 頂点の番号>
using P = pair<long long, long long>;
long long V;
vector<edge> G[MAX_V];
long long d[MAX_V];
void dijkstra(long long s) {
priority_queue<P, vector<P>, greater<P>> que;
fill(d, d + V, INF);
d[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
long long v = p.second;
if (d[v] < p.first)
continue;
// if prim,
// d[v] = -d[v];
// for keep tree
for (long long i = 0; i < (long long)G[v].size(); ++i) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
}
}
}
int main() {
long long N;
cin >> N;
V = 1000000;
long long E;
cin >> E;
long long S;
cin >> S;
long long U, V, A, B;
for (long long i = 0; i < E; ++i) {
cin >> U >> V >> A >> B;
for (long long j = 5000; j >= A; j--) {
edge e = {10000 * V + j - A, B};
G[10000 * U + j].push_back(e);
edge e2 = {10000 * U + j - A, B};
G[10000 * V + j].push_back(e2);
}
}
long long C, D;
for (long long i = 1; i <= N; i++) {
cin >> C >> D;
for (long long j = 0; j <= 5000; j++) {
if (j + C <= 5000) {
edge e3 = {10000 * i + j + C, D};
G[10000 * i + j].push_back(e3);
} else {
edge e4 = {10000 * i + 5000, D};
G[10000 * i + j].push_back(e4);
}
}
}
if (S > 5000) {
S = 5000;
}
dijkstra(10000 + S);
long long ans[51];
fill(ans, ans + 51, INF);
for (long long i = 2; i <= N; i++) {
for (long long j = 0; j <= 5000; j++) {
ans[i] = min(ans[i], d[10000 * i + j]);
}
}
for (long long i = 2; i <= N; i++) {
cout << ans[i] << endl;
}
system("pause");
return 0;
} | insert | 76 | 76 | 76 | 79 | 0 | sh: 1: pause: not found
|
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define maxn 55
#define limit 2500
using namespace std;
struct edge {
int v, a, b;
};
vector<edge> G[maxn];
int value[maxn], cost[maxn];
struct state {
int u, num;
ll tim;
bool operator<(const state &a) const { return tim > a.tim; }
};
ll dis[maxn][maxn * maxn];
void dijkstra(int s) {
s = min(s, limit);
memset(dis, -1, sizeof dis);
dis[1][s] = 0;
priority_queue<state> pq;
pq.push(state{1, s, 0});
while (!pq.empty()) {
state now = pq.top();
pq.pop();
int u = now.u, num = now.num;
ll tim = now.tim;
if (dis[u][num] != tim)
continue;
for (edge e : G[u]) {
int v = e.v, a = e.a, b = e.b;
if (num >= a) {
if (dis[v][num - a] == -1 || dis[v][num - a] > tim + b) {
dis[v][num - a] = tim + b;
pq.push(state{v, num - a, tim + b});
}
}
}
num += value[u], tim += cost[u];
if (dis[u][num] == -1 || dis[u][num] > tim) {
dis[u][num] = tim;
pq.push(state{u, num, tim});
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, s;
cin >> n >> m >> s;
while (m--) {
int u, v, a, b;
cin >> u >> v >> a >> b;
G[u].push_back(edge{v, a, b});
G[v].push_back(edge{u, a, b});
}
for (int i = 1; i <= n; i++)
cin >> value[i] >> cost[i];
dijkstra(s);
for (int i = 2; i <= n; i++) {
ll ans = -1;
for (int j = 0; j <= limit; j++)
if (dis[i][j] != -1 && (ans == -1 || ans > dis[i][j]))
ans = dis[i][j];
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define ll long long
#define maxn 55
#define limit 2500
using namespace std;
struct edge {
int v, a, b;
};
vector<edge> G[maxn];
int value[maxn], cost[maxn];
struct state {
int u, num;
ll tim;
bool operator<(const state &a) const { return tim > a.tim; }
};
ll dis[maxn][maxn * maxn];
void dijkstra(int s) {
s = min(s, limit);
memset(dis, -1, sizeof dis);
dis[1][s] = 0;
priority_queue<state> pq;
pq.push(state{1, s, 0});
while (!pq.empty()) {
state now = pq.top();
pq.pop();
int u = now.u, num = now.num;
ll tim = now.tim;
if (dis[u][num] != tim)
continue;
for (edge e : G[u]) {
int v = e.v, a = e.a, b = e.b;
if (num >= a) {
if (dis[v][num - a] == -1 || dis[v][num - a] > tim + b) {
dis[v][num - a] = tim + b;
pq.push(state{v, num - a, tim + b});
}
}
}
num += value[u], tim += cost[u];
if (num <= limit && (dis[u][num] == -1 || dis[u][num] > tim)) {
dis[u][num] = tim;
pq.push(state{u, num, tim});
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, s;
cin >> n >> m >> s;
while (m--) {
int u, v, a, b;
cin >> u >> v >> a >> b;
G[u].push_back(edge{v, a, b});
G[v].push_back(edge{u, a, b});
}
for (int i = 1; i <= n; i++)
cin >> value[i] >> cost[i];
dijkstra(s);
for (int i = 2; i <= n; i++) {
ll ans = -1;
for (int j = 0; j <= limit; j++)
if (dis[i][j] != -1 && (ans == -1 || ans > dis[i][j]))
ans = dis[i][j];
cout << ans << endl;
}
return 0;
} | replace | 44 | 45 | 44 | 45 | 0 | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rp(i, n) for (ll i = 0; i < n; i++)
#define mp make_pair
#define mt make_tuple
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using G = vector<vector<int>>;
using PP = pair<ll, pair<ll, ll>>;
using tlll = tuple<ll, ll, ll>;
const ll INF = 1ll << 50;
ll n, m, s;
ll maxa;
ll dp[53][2510];
vector<vector<PP>> graph(53);
vector<pair<ll, ll>> changetime(53);
int main() {
cin >> n >> m >> s;
rp(i, m) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
maxa = max(maxa, a);
graph[u].push_back(mp(v, mp(a, b)));
graph[v].push_back(mp(u, mp(a, b)));
}
if (s > maxa * n)
s = maxa * n;
rp(i, n) {
ll c, d;
cin >> c >> d;
changetime[i + 1] = mp(c, d);
}
rp(i, 53) {
rp(j, 2510) { dp[i][j] = INF; }
}
dp[1][s] = 0;
priority_queue<tlll, vector<tlll>, greater<tlll>> dij;
dij.push(mt(0, 1, s));
while (!dij.empty()) {
tlll t = dij.top();
dij.pop();
int ctr = 0;
ll time = get<0>(t);
ll i = get<1>(t);
ll j = get<2>(t);
if (dp[i][j] <= time && (i != 1 || j != s))
continue;
dp[i][j] = time;
for (ll c = j - 1; c >= 0; c--) {
if (dp[i][c] > time)
dp[i][c] = time;
else
break;
}
while (j <= maxa * n) {
if (ctr > 0)
dp[i][j] = min(dp[i][j],
dp[i][j - changetime[i].first] + changetime[i].second);
for (auto pp : graph[i]) {
if (j >= pp.second.first)
dij.push(
mt(dp[i][j] + pp.second.second, pp.first, j - pp.second.first));
}
ctr++;
j += changetime[i].first;
}
}
for (int i = 2; i <= n; i++) {
ll ans = 1ll << 51;
rp(j, maxa * n) { ans = min(ans, dp[i][j]); }
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define rp(i, n) for (ll i = 0; i < n; i++)
#define mp make_pair
#define mt make_tuple
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using G = vector<vector<int>>;
using PP = pair<ll, pair<ll, ll>>;
using tlll = tuple<ll, ll, ll>;
const ll INF = 1ll << 50;
ll n, m, s;
ll maxa;
ll dp[53][2510];
vector<vector<PP>> graph(53);
vector<pair<ll, ll>> changetime(53);
int main() {
cin >> n >> m >> s;
rp(i, m) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
maxa = max(maxa, a);
graph[u].push_back(mp(v, mp(a, b)));
graph[v].push_back(mp(u, mp(a, b)));
}
if (s > maxa * n)
s = maxa * n;
rp(i, n) {
ll c, d;
cin >> c >> d;
changetime[i + 1] = mp(c, d);
}
rp(i, 53) {
rp(j, 2510) { dp[i][j] = INF; }
}
dp[1][s] = 0;
priority_queue<tlll, vector<tlll>, greater<tlll>> dij;
dij.push(mt(0, 1, s));
while (!dij.empty()) {
tlll t = dij.top();
dij.pop();
int ctr = 0;
ll time = get<0>(t);
ll i = get<1>(t);
ll j = get<2>(t);
if (dp[i][j] <= time && (i != 1 || j != s))
continue;
dp[i][j] = time;
for (ll c = j - 1; c >= 0; c--) {
if (dp[i][c] > time)
dp[i][c] = time;
else
break;
}
while (j <= maxa * n) {
if (ctr > 0) {
if (dp[i][j] > dp[i][j - changetime[i].first] + changetime[i].second)
dp[i][j] = dp[i][j - changetime[i].first] + changetime[i].second;
else
break;
}
for (auto pp : graph[i]) {
if (j >= pp.second.first)
dij.push(
mt(dp[i][j] + pp.second.second, pp.first, j - pp.second.first));
}
ctr++;
j += changetime[i].first;
}
}
for (int i = 2; i <= n; i++) {
ll ans = 1ll << 51;
rp(j, maxa * n) { ans = min(ans, dp[i][j]); }
cout << ans << endl;
}
return 0;
} | replace | 55 | 58 | 55 | 61 | TLE | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 55;
const int NM = 5e3 + 10;
int n, m, S;
int c[N], d[N];
int a[N][N], b[N][N];
long long f[N][NM], res[N];
vector<int> adj[N];
bool minimize(long long &x, const long long &y) {
if (x > y)
x = y;
else
return false;
return true;
}
void dijkstra() {
memset(f, 60, sizeof(f));
memset(res, 60, sizeof(res));
f[1][S] = 0;
priority_queue<pair<long long, pair<int, int>>> P;
P.push(mp(0, mp(1, S)));
while (P.size()) {
long long dist = -P.top().fi;
int u = P.top().se.fi, curs = P.top().se.se;
P.pop();
if (f[u][curs] != dist)
continue;
minimize(res[u], f[u][curs]);
for (int v : adj[u])
if (curs >= a[u][v]) {
if (minimize(f[v][curs - a[u][v]], dist + 1ll * b[u][v]))
P.push(mp(-f[v][curs - a[u][v]], mp(v, curs - a[u][v])));
}
for (int i = 1; i <= 5000; i++) {
int news, flag = 0;
if (1ll * i * c[u] + 1ll * curs > (long long)(5e3))
news = 5000, flag = 1;
else
news = i * c[u] + curs;
if (minimize(f[u][news], dist + 1ll * i * d[u]))
P.push(mp(-f[u][news], mp(u, news)));
if (flag)
break;
}
}
}
int main() {
cin >> n >> m >> S;
S = min(S, 5000);
for (int i = 0; i < m; i++) {
int x, y, z1, z2;
cin >> x >> y >> z1 >> z2;
adj[x].push_back(y);
adj[y].push_back(x);
a[x][y] = a[y][x] = z1;
b[x][y] = b[y][x] = z2;
}
for (int i = 1; i <= n; i++)
cin >> c[i] >> d[i];
dijkstra();
for (int i = 2; i <= n; i++)
cout << res[i] << endl;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 55;
const int NM = 5e3 + 10;
int n, m, S;
int c[N], d[N];
int a[N][N], b[N][N];
long long f[N][NM], res[N];
vector<int> adj[N];
bool minimize(long long &x, const long long &y) {
if (x > y)
x = y;
else
return false;
return true;
}
void dijkstra() {
memset(f, 60, sizeof(f));
memset(res, 60, sizeof(res));
f[1][S] = 0;
priority_queue<pair<long long, pair<int, int>>> P;
P.push(mp(0, mp(1, S)));
while (P.size()) {
long long dist = -P.top().fi;
int u = P.top().se.fi, curs = P.top().se.se;
P.pop();
if (f[u][curs] != dist)
continue;
minimize(res[u], f[u][curs]);
for (int v : adj[u])
if (curs >= a[u][v]) {
if (minimize(f[v][curs - a[u][v]], dist + 1ll * b[u][v]))
P.push(mp(-f[v][curs - a[u][v]], mp(v, curs - a[u][v])));
}
for (int i = 1; i <= 5000; i++) {
int news, flag = 0;
if (1ll * i * c[u] + 1ll * curs > (long long)(5e3))
news = 5000, flag = 1;
else
news = i * c[u] + curs;
if (minimize(f[u][news], dist + 1ll * i * d[u]))
P.push(mp(-f[u][news], mp(u, news)));
else
break;
if (flag)
break;
}
}
}
int main() {
cin >> n >> m >> S;
S = min(S, 5000);
for (int i = 0; i < m; i++) {
int x, y, z1, z2;
cin >> x >> y >> z1 >> z2;
adj[x].push_back(y);
adj[y].push_back(x);
a[x][y] = a[y][x] = z1;
b[x][y] = b[y][x] = z2;
}
for (int i = 1; i <= n; i++)
cin >> c[i] >> d[i];
dijkstra();
for (int i = 2; i <= n; i++)
cout << res[i] << endl;
}
| insert | 50 | 50 | 50 | 52 | TLE | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
constexpr int MOD = 1000000007;
constexpr int INF = numeric_limits<int>::max() / 5;
typedef pair<int, int> P;
// 辺に重みをつける場合
struct Edge {
int to;
int cost;
int time;
Edge(int t, int c, int r) : to(t), cost(c), time(r) {}
};
using Graph = vector<vector<Edge>>;
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, M, S;
cin >> N >> M >> S;
if (S >= 2500)
S = 2499;
Graph G1(N);
rep(i, M) {
int a, b, c, d;
cin >> a >> b >> c >> d;
--a;
--b;
G1[a].push_back(Edge(b, c, d));
G1[b].push_back(Edge(a, c, d));
}
P p[N];
rep(i, N) cin >> p[i].first >> p[i].second;
int dist1[N][2501];
rep(i, N) {
rep(j, 2501) { dist1[i][j] = INF; }
}
priority_queue<pair<P, int>, vector<pair<P, int>>, greater<pair<P, int>>>
que1;
dist1[0][S] = 0;
que1.push({P(0, 0), S});
while (!que1.empty()) {
auto l = que1.top();
que1.pop();
int t = l.first.first;
int u = l.first.second;
int c = l.second;
if (c >= 2500)
continue;
if (t > dist1[u][c])
continue;
dist1[u][c] = t;
for (int i = 0; i < G1[u].size(); ++i) {
int v = G1[u][i].to;
int d1 = G1[u][i].cost;
int d2 = G1[u][i].time;
if (c - d1 < 0)
continue;
if (dist1[v][c - d1] > dist1[u][c] + d2) {
dist1[v][c - d1] = dist1[u][c] + d2;
que1.push({P(t + d2, v), c - d1});
}
}
int t1 = t + p[u].second;
int c1 = c + p[u].first;
if (c >= 2500)
continue;
if (dist1[u][c1] > t1) {
dist1[u][c1] = t1;
que1.push({P(t1, u), c1});
}
}
for (int i = 1; i < N; i++) {
int ans = INF;
for (int j = 0; j < 2500; j++) {
ans = min(ans, dist1[i][j]);
}
cout << ans << endl;
}
} | #include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
constexpr int MOD = 1000000007;
constexpr int INF = numeric_limits<int>::max() / 5;
typedef pair<int, int> P;
// 辺に重みをつける場合
struct Edge {
int to;
int cost;
int time;
Edge(int t, int c, int r) : to(t), cost(c), time(r) {}
};
using Graph = vector<vector<Edge>>;
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, M, S;
cin >> N >> M >> S;
if (S >= 2500)
S = 2499;
Graph G1(N);
rep(i, M) {
int a, b, c, d;
cin >> a >> b >> c >> d;
--a;
--b;
G1[a].push_back(Edge(b, c, d));
G1[b].push_back(Edge(a, c, d));
}
P p[N];
rep(i, N) cin >> p[i].first >> p[i].second;
int dist1[N][2501];
rep(i, N) {
rep(j, 2501) { dist1[i][j] = INF; }
}
priority_queue<pair<P, int>, vector<pair<P, int>>, greater<pair<P, int>>>
que1;
dist1[0][S] = 0;
que1.push({P(0, 0), S});
while (!que1.empty()) {
auto l = que1.top();
que1.pop();
int t = l.first.first;
int u = l.first.second;
int c = l.second;
if (c >= 2500)
continue;
if (t > dist1[u][c])
continue;
dist1[u][c] = t;
for (int i = 0; i < G1[u].size(); ++i) {
int v = G1[u][i].to;
int d1 = G1[u][i].cost;
int d2 = G1[u][i].time;
if (c - d1 < 0)
continue;
if (dist1[v][c - d1] > dist1[u][c] + d2) {
dist1[v][c - d1] = dist1[u][c] + d2;
que1.push({P(t + d2, v), c - d1});
}
}
int t1 = t + p[u].second;
int c1 = c + p[u].first;
if (c1 >= 2500)
continue;
if (dist1[u][c1] > t1) {
dist1[u][c1] = t1;
que1.push({P(t1, u), c1});
}
}
for (int i = 1; i < N; i++) {
int ans = INF;
for (int j = 0; j < 2500; j++) {
ans = min(ans, dist1[i][j]);
}
cout << ans << endl;
}
} | replace | 79 | 80 | 79 | 80 | 0 | |
p02703 | C++ | Runtime Error | /*
I am the fire that burns your skin
Soy el fuego que arde tu piel
I am the water that kills your thirst
Soy el agua que mata tu sed
The castle, the tower, I am
El castillo, la torre, yo soy
The sword that keeps the flow
La espada que guarda el caudal
You the air that I breathe
Tú el aire que respiro yo
And the moonlight on the sea
Y la luz de la luna en el mar
The throat that I want to wet
La garganta que ansio mojar
I fear drowning in love
Que temo ahogar de amor
And which desires will you give me?
¿Y cuáles deseos me vas a dar?
You say, 'my treasure just look at it'
Dices tú, 'mi tesoro basta con mirarlo'
And yours will be
Y tuyo será
And yours will be
Y tuyo será
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using dijkstra = priority_queue<T, vector<T>, greater<T>>;
typedef long long ll;
typedef unsigned long long ull;
#define fbo find_by_order // k th index
#define ook order_of_key // strictly smaller than k
#define PI acos(-1.0)
#define inf 0x3f3f3f3f
#define lead_zero(x) __builtin_clzll(x)
#define trail_zero(x) __builtin_ctzll(x)
#define total_1s(x) __builtin_popcountll(x)
#define first_1(x) __builtin_ffsll(x)
#define log2_(x) __builtin_clz(1) - __builtin_clz(x)
#define isLeap(x) ((x % 400 == 0) || (x % 100 ? x % 4 == 0 : false))
#define QUERY \
int test; \
scanf("%d", &test); \
for (int _T = 1; _T <= test; _T++)
#define FAST ios_base::sync_with_stdio(0), cin.tie(0)
#define all(v) v.begin(), v.end()
#define reunique(v) v.resize(std::unique(all(v)) - v.begin())
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pul pair<ull, ull>
#define ff first
#define ss second
#define MERGE(v1, v2, v) merge(all(v1), all(v2), back_inserter(v))
#define MP make_pair
#define EB emplace_back
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define in_range(v, r, l) upper_bound(all(v), r) - lower_bound(all(v), l)
#define LCM(a, b) (a / __gcd(a, b)) * b;
ll rdn(int y, int m, int d) {
/* Rata Die day one is 0001-01-01 */
if (m < 3)
y--, m += 12;
return 365 * y + y / 4 - y / 100 + y / 400 + (153 * m - 457) / 5 + d - 306;
}
/* Direction arrays */
/*int dx[] = {1,-1,0,0}, dy[] = {0,0,1,-1}; */ // 4Direction
/*int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // KnightDirection
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // HexagonalDirection
/* int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; */
mt19937 R(chrono::system_clock().now().time_since_epoch().count());
vector<pair<ll, pll>> edge[55];
pll coin_time[55];
ll cost[55][3009];
ll fans[55];
int main() {
ll n, m, s;
ll u, v, a, b;
FAST;
cin >> n >> m >> s;
for (int i = 0; i < m; i++) {
cin >> u >> v >> a >> b;
edge[u].EB(MP(v, MP(a, b)));
edge[v].EB(MP(u, MP(a, b)));
}
for (int i = 1; i <= n; i++) {
cin >> coin_time[i].ff >> coin_time[i].ss;
}
// time , coin rem , node
dijkstra<pair<pll, ll>> pq;
memset(cost, inf, sizeof cost);
memset(fans, inf, sizeof fans);
for (int i = 0; i <= s; i++)
cost[1][i] = 0;
pq.push(MP(MP(0, s), 1));
while (pq.empty() == false) {
pair<pll, ll> t = pq.top();
pq.pop();
ll nodenow = t.ss;
ll timeneeded = t.ff.ff;
ll coin_rem = t.ff.ss;
if (cost[nodenow][coin_rem] < timeneeded)
continue;
fans[nodenow] = min(fans[nodenow], timeneeded);
for (auto i : edge[nodenow]) {
ll to = i.ff;
ll silvercost = i.ss.ff;
ll destcost = timeneeded + i.ss.ss;
if (silvercost <= coin_rem) {
if (cost[to][coin_rem - silvercost] > destcost) {
cost[to][coin_rem - silvercost] = destcost;
pq.push(MP(MP(destcost, coin_rem - silvercost), to));
}
}
}
for (ll i = coin_rem + coin_time[nodenow].ff,
j = timeneeded + coin_time[nodenow].ss;
i < 2500; i += coin_time[nodenow].ff, j += coin_time[nodenow].ss) {
if (cost[nodenow][i] > j) {
cost[nodenow][i] = j;
pq.push(MP(MP(j, i), nodenow));
}
}
}
for (int i = 2; i <= n; i++)
cout << fans[i] << "\n";
}
| /*
I am the fire that burns your skin
Soy el fuego que arde tu piel
I am the water that kills your thirst
Soy el agua que mata tu sed
The castle, the tower, I am
El castillo, la torre, yo soy
The sword that keeps the flow
La espada que guarda el caudal
You the air that I breathe
Tú el aire que respiro yo
And the moonlight on the sea
Y la luz de la luna en el mar
The throat that I want to wet
La garganta que ansio mojar
I fear drowning in love
Que temo ahogar de amor
And which desires will you give me?
¿Y cuáles deseos me vas a dar?
You say, 'my treasure just look at it'
Dices tú, 'mi tesoro basta con mirarlo'
And yours will be
Y tuyo será
And yours will be
Y tuyo será
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using dijkstra = priority_queue<T, vector<T>, greater<T>>;
typedef long long ll;
typedef unsigned long long ull;
#define fbo find_by_order // k th index
#define ook order_of_key // strictly smaller than k
#define PI acos(-1.0)
#define inf 0x3f3f3f3f
#define lead_zero(x) __builtin_clzll(x)
#define trail_zero(x) __builtin_ctzll(x)
#define total_1s(x) __builtin_popcountll(x)
#define first_1(x) __builtin_ffsll(x)
#define log2_(x) __builtin_clz(1) - __builtin_clz(x)
#define isLeap(x) ((x % 400 == 0) || (x % 100 ? x % 4 == 0 : false))
#define QUERY \
int test; \
scanf("%d", &test); \
for (int _T = 1; _T <= test; _T++)
#define FAST ios_base::sync_with_stdio(0), cin.tie(0)
#define all(v) v.begin(), v.end()
#define reunique(v) v.resize(std::unique(all(v)) - v.begin())
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pul pair<ull, ull>
#define ff first
#define ss second
#define MERGE(v1, v2, v) merge(all(v1), all(v2), back_inserter(v))
#define MP make_pair
#define EB emplace_back
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define in_range(v, r, l) upper_bound(all(v), r) - lower_bound(all(v), l)
#define LCM(a, b) (a / __gcd(a, b)) * b;
ll rdn(int y, int m, int d) {
/* Rata Die day one is 0001-01-01 */
if (m < 3)
y--, m += 12;
return 365 * y + y / 4 - y / 100 + y / 400 + (153 * m - 457) / 5 + d - 306;
}
/* Direction arrays */
/*int dx[] = {1,-1,0,0}, dy[] = {0,0,1,-1}; */ // 4Direction
/*int dx[] = {1,-1,0,0,1,1,-1,-1}, dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // KnightDirection
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // HexagonalDirection
/* int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; */
mt19937 R(chrono::system_clock().now().time_since_epoch().count());
vector<pair<ll, pll>> edge[55];
pll coin_time[55];
ll cost[55][3009];
ll fans[55];
int main() {
ll n, m, s;
ll u, v, a, b;
FAST;
cin >> n >> m >> s;
for (int i = 0; i < m; i++) {
cin >> u >> v >> a >> b;
edge[u].EB(MP(v, MP(a, b)));
edge[v].EB(MP(u, MP(a, b)));
}
for (int i = 1; i <= n; i++) {
cin >> coin_time[i].ff >> coin_time[i].ss;
}
s = min(s, 2600LL);
// time , coin rem , node
dijkstra<pair<pll, ll>> pq;
memset(cost, inf, sizeof cost);
memset(fans, inf, sizeof fans);
for (int i = 0; i <= s; i++)
cost[1][i] = 0;
pq.push(MP(MP(0, s), 1));
while (pq.empty() == false) {
pair<pll, ll> t = pq.top();
pq.pop();
ll nodenow = t.ss;
ll timeneeded = t.ff.ff;
ll coin_rem = t.ff.ss;
if (cost[nodenow][coin_rem] < timeneeded)
continue;
fans[nodenow] = min(fans[nodenow], timeneeded);
for (auto i : edge[nodenow]) {
ll to = i.ff;
ll silvercost = i.ss.ff;
ll destcost = timeneeded + i.ss.ss;
if (silvercost <= coin_rem) {
if (cost[to][coin_rem - silvercost] > destcost) {
cost[to][coin_rem - silvercost] = destcost;
pq.push(MP(MP(destcost, coin_rem - silvercost), to));
}
}
}
for (ll i = coin_rem + coin_time[nodenow].ff,
j = timeneeded + coin_time[nodenow].ss;
i < 2500; i += coin_time[nodenow].ff, j += coin_time[nodenow].ss) {
if (cost[nodenow][i] > j) {
cost[nodenow][i] = j;
pq.push(MP(MP(j, i), nodenow));
}
}
}
for (int i = 2; i <= n; i++)
cout << fans[i] << "\n";
}
| insert | 112 | 112 | 112 | 113 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FORD(i, a, b) for (int i = b - 1; i >= int(a); i--)
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)
#define ALL(x) (x).begin(), (x).end()
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct str {
ll time;
int city;
ll money;
bool operator<(const str &right) const {
return time != right.time ? time < right.time
: city != right.city ? city < right.city
: money < right.money;
}
};
int main() {
int n, m;
ll money;
cin >> n >> m >> money;
vector<vector<pair<ll, ll>>> edge(n, vector<pair<ll, ll>>(n, {-1, -1}));
REP(i, m) {
int u, v;
ll a, b;
cin >> u >> v >> a >> b;
u--;
v--;
edge[u][v] = {a, b};
edge[v][u] = {a, b};
}
vector<pair<ll, ll>> change(n);
REP(i, n) {
ll c, d;
cin >> c >> d;
change[i] = {c, d};
}
vector<vector<ll>> time(n, vector<ll>(2501, 1e18)); // city money
set<str> sstr;
sstr.insert({0, 0, money});
time[0][money] = 0;
set<int> visited;
visited.insert(0);
while (1) {
auto it = sstr.begin();
str now = *it;
sstr.erase(it);
visited.insert(now.city);
if (visited.size() == n) {
break;
}
ll a = now.money;
ll b = 0;
while (1) {
if (time[now.city][a] > now.time + b) {
time[now.city][a] = now.time + b;
sstr.insert({now.time + b, now.city, a});
}
if (a == 2500)
break;
a += change[now.city].first;
if (a > 2500)
a = 2500;
b += change[now.city].second;
}
REP(i, n) {
if (edge[now.city][i].first != -1) {
if (now.money >= edge[now.city][i].first) {
if (time[i][now.money - edge[now.city][i].first] >
now.time + edge[now.city][i].second) {
time[i][now.money - edge[now.city][i].first] =
now.time + edge[now.city][i].second;
sstr.insert({now.time + edge[now.city][i].second, i,
now.money - edge[now.city][i].first});
}
}
}
}
}
FOR(i, 1, n) {
ll ans = 1e18;
REP(j, 2501) { ans = min(ans, time[i][j]); }
cout << ans << endl;
}
} | #include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FORD(i, a, b) for (int i = b - 1; i >= int(a); i--)
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)
#define ALL(x) (x).begin(), (x).end()
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct str {
ll time;
int city;
ll money;
bool operator<(const str &right) const {
return time != right.time ? time < right.time
: city != right.city ? city < right.city
: money < right.money;
}
};
int main() {
int n, m;
ll money;
cin >> n >> m >> money;
money = min(money, 2500LL);
vector<vector<pair<ll, ll>>> edge(n, vector<pair<ll, ll>>(n, {-1, -1}));
REP(i, m) {
int u, v;
ll a, b;
cin >> u >> v >> a >> b;
u--;
v--;
edge[u][v] = {a, b};
edge[v][u] = {a, b};
}
vector<pair<ll, ll>> change(n);
REP(i, n) {
ll c, d;
cin >> c >> d;
change[i] = {c, d};
}
vector<vector<ll>> time(n, vector<ll>(2501, 1e18)); // city money
set<str> sstr;
sstr.insert({0, 0, money});
time[0][money] = 0;
set<int> visited;
visited.insert(0);
while (1) {
auto it = sstr.begin();
str now = *it;
sstr.erase(it);
visited.insert(now.city);
if (visited.size() == n) {
break;
}
ll a = now.money;
ll b = 0;
while (1) {
if (time[now.city][a] > now.time + b) {
time[now.city][a] = now.time + b;
sstr.insert({now.time + b, now.city, a});
}
if (a == 2500)
break;
a += change[now.city].first;
if (a > 2500)
a = 2500;
b += change[now.city].second;
}
REP(i, n) {
if (edge[now.city][i].first != -1) {
if (now.money >= edge[now.city][i].first) {
if (time[i][now.money - edge[now.city][i].first] >
now.time + edge[now.city][i].second) {
time[i][now.money - edge[now.city][i].first] =
now.time + edge[now.city][i].second;
sstr.insert({now.time + edge[now.city][i].second, i,
now.money - edge[now.city][i].first});
}
}
}
}
}
FOR(i, 1, n) {
ll ans = 1e18;
REP(j, 2501) { ans = min(ans, time[i][j]); }
cout << ans << endl;
}
} | insert | 35 | 35 | 35 | 36 | 0 | |
p02703 | C++ | Runtime Error | #include <queue>
#include <stdio.h>
#include <vector>
struct str {
int x0;
int cost;
int time;
};
std::vector<str> V[60];
struct str2 {
int x0;
int coin;
long long int dist;
};
bool operator<(str2 a, str2 b) { return a.dist > b.dist; }
std::priority_queue<str2> Q;
std::pair<int, int> x[60];
long long int check[60][3000];
long long int MAX = 1;
int main() {
for (int i = 1; i <= 15; i++)
MAX *= 10;
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
for (int i = 1; i <= b; i++) {
int d, e, f, g;
scanf("%d%d%d%d", &d, &e, &f, &g);
V[d].push_back({e, f, g});
V[e].push_back({d, f, g});
}
for (int i = 1; i <= a; i++) {
int d, e;
scanf("%d%d", &d, &e);
x[i] = std::make_pair(d, e);
}
if (c > 2500)
c = 2500;
Q.push({1, c, 1});
while (!Q.empty()) {
int x0 = Q.top().x0;
int coin = Q.top().coin;
long long int dist = Q.top().dist;
Q.pop();
if (check[x0][coin])
continue;
check[x0][coin] = dist;
Q.push({x0, coin + x[x0].first, dist + x[x0].second});
for (int i = 0; i < V[x0].size(); i++)
if (V[x0][i].cost <= coin)
Q.push({V[x0][i].x0, coin - V[x0][i].cost, dist + V[x0][i].time});
}
for (int i = 2; i <= a; i++) {
long long int ans = MAX;
for (int j = 0; j <= 2500; j++) {
if (check[i][j] > 0)
ans = ans < check[i][j] ? ans : check[i][j];
}
printf("%lld\n", ans - 1);
}
} | #include <queue>
#include <stdio.h>
#include <vector>
struct str {
int x0;
int cost;
int time;
};
std::vector<str> V[60];
struct str2 {
int x0;
int coin;
long long int dist;
};
bool operator<(str2 a, str2 b) { return a.dist > b.dist; }
std::priority_queue<str2> Q;
std::pair<int, int> x[60];
long long int check[60][3000];
long long int MAX = 1;
int main() {
for (int i = 1; i <= 15; i++)
MAX *= 10;
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
for (int i = 1; i <= b; i++) {
int d, e, f, g;
scanf("%d%d%d%d", &d, &e, &f, &g);
V[d].push_back({e, f, g});
V[e].push_back({d, f, g});
}
for (int i = 1; i <= a; i++) {
int d, e;
scanf("%d%d", &d, &e);
x[i] = std::make_pair(d, e);
}
if (c > 2500)
c = 2500;
Q.push({1, c, 1});
while (!Q.empty()) {
int x0 = Q.top().x0;
int coin = Q.top().coin;
long long int dist = Q.top().dist;
Q.pop();
if (check[x0][coin])
continue;
check[x0][coin] = dist;
if (coin + x[x0].first <= 2500)
Q.push({x0, coin + x[x0].first, dist + x[x0].second});
for (int i = 0; i < V[x0].size(); i++)
if (V[x0][i].cost <= coin)
Q.push({V[x0][i].x0, coin - V[x0][i].cost, dist + V[x0][i].time});
}
for (int i = 2; i <= a; i++) {
long long int ans = MAX;
for (int j = 0; j <= 2500; j++) {
if (check[i][j] > 0)
ans = ans < check[i][j] ? ans : check[i][j];
}
printf("%lld\n", ans - 1);
}
} | replace | 51 | 52 | 51 | 53 | -11 | |
p02703 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n)-1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\n"
#define popcnt(x) __builtin_popcount(x)
const double PI = acos(-1.0);
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++)
cin >> v.at(i);
return is;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
cout << '(' << p.first << ", " << p.second << ')';
return os;
}
template <class T> void print(const vector<T> &v, const string &delimiter) {
rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i);
cout << endl;
}
template <class T>
void print(const vector<vector<T>> &vv, const string &delimiter) {
for (const auto &v : vv)
print(v, delimiter);
}
template <class Weight> struct WeightedGraph {
struct Edge {
int to;
Weight weight;
};
int n;
vector<vector<Edge>> edges;
Weight zero;
Weight inf;
WeightedGraph(int n, Weight zero, Weight inf)
: n(n), edges(n), zero(zero), inf(inf) {}
void add_edge(int u, int v, Weight w) { edges[u].push_back({v, w}); }
};
template <class Weight>
vector<Weight> dijkstra(int s, const WeightedGraph<Weight> &g) {
using Node = pair<Weight, int>;
priority_queue<Node, vector<Node>, greater<Node>> que;
vector<Weight> d(g.n, g.inf);
d[s] = g.zero;
que.push(Node(g.zero, s));
while (!que.empty()) {
Node p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (int i = 0; i < (int)g.edges[v].size(); i++) {
auto e = g.edges[v][i];
if (d[e.to] > d[v] + e.weight) {
d[e.to] = d[v] + e.weight;
que.push(Node(d[e.to], e.to));
}
}
}
return d;
}
int main() {
int n, m, s;
cin >> n >> m >> s;
ll max_silver = 5000;
WeightedGraph<ll> graph(n * (max_silver + 1) + max_silver + 100000, 0, 1e18);
auto id = [&](int v, int sil) {
// assert(sil <= max_silver);
return v * (max_silver + 1) + sil;
};
rep(i, m) {
int u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
rep(sil, max_silver + 1) {
if (sil >= a) {
graph.add_edge(id(u, sil), id(v, sil - a), b);
graph.add_edge(id(v, sil), id(u, sil - a), b);
}
}
}
rep(v, n) {
ll c, d;
cin >> c >> d;
rep(sil, max_silver + 1) {
ll new_sil = sil + c;
graph.add_edge(id(v, sil), id(v, min(max_silver, new_sil)), d);
// if (max_silver < new_sil) break;
}
}
auto d = dijkstra(id(0, s), graph);
repr(to, 1, n) {
ll min_time = 1e18;
rep(sil, max_silver + 1) chmin(min_time, d[id(to, sil)]);
cout << min_time << endl;
}
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n)-1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\n"
#define popcnt(x) __builtin_popcount(x)
const double PI = acos(-1.0);
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++)
cin >> v.at(i);
return is;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
cout << '(' << p.first << ", " << p.second << ')';
return os;
}
template <class T> void print(const vector<T> &v, const string &delimiter) {
rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i);
cout << endl;
}
template <class T>
void print(const vector<vector<T>> &vv, const string &delimiter) {
for (const auto &v : vv)
print(v, delimiter);
}
template <class Weight> struct WeightedGraph {
struct Edge {
int to;
Weight weight;
};
int n;
vector<vector<Edge>> edges;
Weight zero;
Weight inf;
WeightedGraph(int n, Weight zero, Weight inf)
: n(n), edges(n), zero(zero), inf(inf) {}
void add_edge(int u, int v, Weight w) { edges[u].push_back({v, w}); }
};
template <class Weight>
vector<Weight> dijkstra(int s, const WeightedGraph<Weight> &g) {
using Node = pair<Weight, int>;
priority_queue<Node, vector<Node>, greater<Node>> que;
vector<Weight> d(g.n, g.inf);
d[s] = g.zero;
que.push(Node(g.zero, s));
while (!que.empty()) {
Node p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (int i = 0; i < (int)g.edges[v].size(); i++) {
auto e = g.edges[v][i];
if (d[e.to] > d[v] + e.weight) {
d[e.to] = d[v] + e.weight;
que.push(Node(d[e.to], e.to));
}
}
}
return d;
}
int main() {
int n, m, s;
cin >> n >> m >> s;
ll max_silver = 5000;
WeightedGraph<ll> graph(n * (max_silver + 1) + max_silver + 100000, 0, 1e18);
auto id = [&](int v, int sil) {
// assert(sil <= max_silver);
return v * (max_silver + 1) + sil;
};
rep(i, m) {
int u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
rep(sil, max_silver + 1) {
if (sil >= a) {
graph.add_edge(id(u, sil), id(v, sil - a), b);
graph.add_edge(id(v, sil), id(u, sil - a), b);
}
}
}
rep(v, n) {
ll c, d;
cin >> c >> d;
rep(sil, max_silver + 1) {
ll new_sil = sil + c;
graph.add_edge(id(v, sil), id(v, min(max_silver, new_sil)), d);
// if (max_silver < new_sil) break;
}
}
auto d = dijkstra(id(0, min<int>(max_silver, s)), graph);
repr(to, 1, n) {
ll min_time = 1e18;
rep(sil, max_silver + 1) chmin(min_time, d[id(to, sil)]);
cout << min_time << endl;
}
} | replace | 140 | 141 | 140 | 141 | 0 | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
template <class W> struct edge {
int from, to;
W a, b;
edge(int from, int to, W a, W b) : from(from), to(to), a(a), b(b) {}
};
template <class W> using node = vector<edge<W>>;
template <class W> using graph = vector<node<W>>;
template <class W>
void addedge(graph<W> &g, int from, int to, W a, W b,
bool bidirectional = false) {
g[from].emplace_back(edge<W>(from, to, a, b));
if (bidirectional)
g[to].emplace_back(edge<W>(to, from, a, b));
}
class Solution {
public:
void solve() {
int n, m, s;
cin >> n >> m >> s;
graph<int> g(n);
for (int i = 0; i < m; i++) {
int u, v, a, b;
cin >> u >> v >> a >> b;
addedge(g, u - 1, v - 1, a, b, true);
}
vector<int> c(n), d(n);
for (int i = 0; i < n; i++)
cin >> c[i] >> d[i];
vector<vector<long long>> dp(n,
vector<long long>(2501, 0x3f3f3f3f3f3f3f3f));
using state = tuple<long long, int, int>;
priority_queue<state, vector<state>, greater<state>> q;
q.push(make_tuple(0, 0, s));
while (!q.empty()) {
long long c_cost;
int c_u, c_s;
tie(c_cost, c_u, c_s) = q.top();
q.pop();
if (dp[c_u][c_s] <= c_cost)
continue;
dp[c_u][c_s] = c_cost;
if (c_s < 2500) {
q.push(make_tuple(c_cost + d[c_u], c_u, min(c_s + c[c_u], 2500)));
}
for (auto e : g[c_u]) {
if (c_s - e.a >= 0)
q.push(make_tuple(c_cost + e.b, e.to, c_s - e.a));
}
}
for (int i = 1; i < n; i++) {
long long ans = 0x3f3f3f3f3f3f3f3f;
for (int j = 0; j <= 2500; j++) {
ans = min(ans, dp[i][j]);
}
cout << ans << "\n";
}
return;
};
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Solution solution;
solution.solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template <class W> struct edge {
int from, to;
W a, b;
edge(int from, int to, W a, W b) : from(from), to(to), a(a), b(b) {}
};
template <class W> using node = vector<edge<W>>;
template <class W> using graph = vector<node<W>>;
template <class W>
void addedge(graph<W> &g, int from, int to, W a, W b,
bool bidirectional = false) {
g[from].emplace_back(edge<W>(from, to, a, b));
if (bidirectional)
g[to].emplace_back(edge<W>(to, from, a, b));
}
class Solution {
public:
void solve() {
int n, m, s;
cin >> n >> m >> s;
graph<int> g(n);
for (int i = 0; i < m; i++) {
int u, v, a, b;
cin >> u >> v >> a >> b;
addedge(g, u - 1, v - 1, a, b, true);
}
vector<int> c(n), d(n);
for (int i = 0; i < n; i++)
cin >> c[i] >> d[i];
vector<vector<long long>> dp(n,
vector<long long>(2501, 0x3f3f3f3f3f3f3f3f));
using state = tuple<long long, int, int>;
priority_queue<state, vector<state>, greater<state>> q;
q.push(make_tuple(0, 0, min(s, 2500)));
while (!q.empty()) {
long long c_cost;
int c_u, c_s;
tie(c_cost, c_u, c_s) = q.top();
q.pop();
if (dp[c_u][c_s] <= c_cost)
continue;
dp[c_u][c_s] = c_cost;
if (c_s < 2500) {
q.push(make_tuple(c_cost + d[c_u], c_u, min(c_s + c[c_u], 2500)));
}
for (auto e : g[c_u]) {
if (c_s - e.a >= 0)
q.push(make_tuple(c_cost + e.b, e.to, c_s - e.a));
}
}
for (int i = 1; i < n; i++) {
long long ans = 0x3f3f3f3f3f3f3f3f;
for (int j = 0; j <= 2500; j++) {
ans = min(ans, dp[i][j]);
}
cout << ans << "\n";
}
return;
};
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Solution solution;
solution.solve();
return 0;
} | replace | 36 | 37 | 36 | 37 | 0 | |
p02703 | C++ | Time Limit Exceeded | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000000000000
using ll = long long;
using P = pair<ll, int>;
int N;
#define index(a, b) ((a)*50 * N + (b))
struct node {
vector<int> to;
vector<int> cost;
ll min_cost;
vector<int> path;
node() { min_cost = INF; }
};
void dijkstra(int start_index, vector<node> &nodes) {
priority_queue<P, vector<P>, greater<P>> q;
nodes[start_index].min_cost = 0;
q.push(P(0, start_index));
while (q.size() != 0) {
P p = q.top();
q.pop();
int v = p.second;
if (nodes[v].min_cost < p.first)
continue;
for (int i = 0; i < nodes[v].to.size(); i++) {
int to = nodes[v].to[i];
ll tmp_cost = nodes[v].min_cost + nodes[v].cost[i];
if (nodes[to].min_cost > tmp_cost) {
nodes[to].min_cost = tmp_cost;
q.push(P(nodes[to].min_cost, to));
}
}
}
}
int main() {
int M, S;
cin >> N >> M >> S;
S = min(S, 50 * N - 1);
vector<node> nodes(N * 50 * N);
for (int i = 0; i < M; i++) {
int U, V, A, B;
cin >> U >> V >> A >> B;
for (int j = 50 * N - 1; j - A >= 0; j--) {
nodes[index(U - 1, j)].to.push_back(index(V - 1, j - A));
nodes[index(V - 1, j)].to.push_back(index(U - 1, j - A));
nodes[index(U - 1, j)].cost.push_back(B);
nodes[index(V - 1, j)].cost.push_back(B);
}
}
for (int i = 0; i < N; i++) {
int C, D;
cin >> C >> D;
for (int j = 0; j < 50 * N; j++) {
nodes[index(i, j)].to.push_back(index(i, min(j + C, 50 * N - 1)));
nodes[index(i, j)].cost.push_back(D);
}
}
dijkstra(S, nodes);
for (int i = 1; i < N; i++) {
ll ans = INF;
for (int j = 0; j < 50 * N; j++) {
ans = min(ans, nodes[index(i, j)].min_cost);
}
cout << ans << endl;
}
} | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000000000000
using ll = long long;
using P = pair<ll, int>;
int N;
#define index(a, b) ((a)*50 * N + (b))
struct node {
vector<int> to;
vector<int> cost;
ll min_cost;
vector<int> path;
node() { min_cost = INF; }
};
void dijkstra(int start_index, vector<node> &nodes) {
priority_queue<P, vector<P>, greater<P>> q;
nodes[start_index].min_cost = 0;
q.push(P(0, start_index));
while (q.size() != 0) {
P p = q.top();
q.pop();
int v = p.second;
if (nodes[v].min_cost < p.first)
continue;
for (int i = 0; i < nodes[v].to.size(); i++) {
int to = nodes[v].to[i];
ll tmp_cost = nodes[v].min_cost + nodes[v].cost[i];
if (nodes[to].min_cost > tmp_cost) {
nodes[to].min_cost = tmp_cost;
q.push(P(nodes[to].min_cost, to));
}
}
}
}
int main() {
int M, S;
cin >> N >> M >> S;
S = min(S, 50 * N - 1);
vector<node> nodes(N * 50 * N);
for (int i = 0; i < M; i++) {
int U, V, A, B;
cin >> U >> V >> A >> B;
for (int j = 50 * N - 1; j - A >= 0; j--) {
nodes[index(U - 1, j)].to.push_back(index(V - 1, j - A));
nodes[index(V - 1, j)].to.push_back(index(U - 1, j - A));
nodes[index(U - 1, j)].cost.push_back(B);
nodes[index(V - 1, j)].cost.push_back(B);
}
}
for (int i = 0; i < N; i++) {
int C, D;
cin >> C >> D;
for (int j = 0; j < 50 * N; j++) {
nodes[index(i, j)].to.push_back(index(i, min(j + C, 50 * N - 1)));
nodes[index(i, j)].cost.push_back(D);
}
}
dijkstra(S, nodes);
for (int i = 1; i < N; i++) {
ll ans = INF;
for (int j = 0; j < 50 * N; j++) {
ans = min(ans, nodes[index(i, j)].min_cost);
}
cout << ans << endl;
}
} | replace | 0 | 1 | 0 | 1 | TLE | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
vector<pll> g[51];
ll dist[51][5001];
const ll INF = 1000100010001000100;
int n, m, s;
const int x = 100, y = 5000;
ll C[51], D[51];
int dijkstra(int st) {
priority_queue<pll, vector<pll>, greater<pll>> pq;
memset(dist, 0x3f, sizeof(dist));
dist[st][s] = 0;
pq.push({dist[st][s] * y + s, st});
while (!pq.empty()) {
pll pi = pq.top();
pq.pop();
int t = pi.second;
ll cost = pi.first / y, coin = pi.first % y;
if (dist[t][coin] < cost)
continue;
ll nxb, nxa, nxv, nxco;
for (auto it : g[t]) {
nxb = it.first / x, nxa = it.first % x, nxv = it.second;
if (coin >= nxa && dist[nxv][coin - nxa] > cost + nxb) {
nxco = coin - nxa;
dist[nxv][nxco] = (cost + nxb);
pq.push({dist[nxv][nxco] * y + nxco, nxv});
}
}
nxco = coin + C[t];
nxco = min(4999ll, nxco);
if (dist[t][nxco] > cost + D[t]) {
dist[t][nxco] = cost + D[t];
pq.push({dist[t][nxco] * y + nxco, t});
}
}
}
int main() {
cin >> n >> m >> s;
s = min(4999, s);
ll ui, vi, ai, bi;
rep(i, m) {
cin >> ui >> vi >> ai >> bi;
--ui, --vi;
g[ui].pb(bi * x + ai, vi);
g[vi].pb(bi * x + ai, ui);
}
rep(i, n) { cin >> C[i] >> D[i]; }
dijkstra(0);
FOR(i, 1, n) {
ll mn = INF;
rep(j, 5000) mn = min(mn, dist[i][j]);
cout << mn << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
vector<pll> g[51];
ll dist[51][5001];
const ll INF = 1000100010001000100;
int n, m, s;
const int x = 100, y = 5000;
ll C[51], D[51];
void dijkstra(int st) {
priority_queue<pll, vector<pll>, greater<pll>> pq;
memset(dist, 0x3f, sizeof(dist));
dist[st][s] = 0;
pq.push({dist[st][s] * y + s, st});
while (!pq.empty()) {
pll pi = pq.top();
pq.pop();
int t = pi.second;
ll cost = pi.first / y, coin = pi.first % y;
if (dist[t][coin] < cost)
continue;
ll nxb, nxa, nxv, nxco;
for (auto it : g[t]) {
nxb = it.first / x, nxa = it.first % x, nxv = it.second;
if (coin >= nxa && dist[nxv][coin - nxa] > cost + nxb) {
nxco = coin - nxa;
dist[nxv][nxco] = (cost + nxb);
pq.push({dist[nxv][nxco] * y + nxco, nxv});
}
}
nxco = coin + C[t];
nxco = min(4999ll, nxco);
if (dist[t][nxco] > cost + D[t]) {
dist[t][nxco] = cost + D[t];
pq.push({dist[t][nxco] * y + nxco, t});
}
}
}
int main() {
cin >> n >> m >> s;
s = min(4999, s);
ll ui, vi, ai, bi;
rep(i, m) {
cin >> ui >> vi >> ai >> bi;
--ui, --vi;
g[ui].pb(bi * x + ai, vi);
g[vi].pb(bi * x + ai, ui);
}
rep(i, n) { cin >> C[i] >> D[i]; }
dijkstra(0);
FOR(i, 1, n) {
ll mn = INF;
rep(j, 5000) mn = min(mn, dist[i][j]);
cout << mn << endl;
}
return 0;
} | replace | 15 | 16 | 15 | 16 | 0 | |
p02703 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#define pb push_back
#define ld long double
#define int long long
#define mp make_pair
mt19937 rnd(51);
const int N = 55;
vector<pair<int, pair<int, int>>> g[N];
int a[N][N * N], c[N], d[N];
vector<int> ans(N, 1e18);
void fill() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N * N; j++) {
a[i][j] = 1e18;
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
fill();
int n, m, s, mx = 0;
cin >> n >> m >> s;
for (int i = 0; i < m; i++) {
int u, v, a, b;
cin >> u >> v >> a >> b;
g[u].pb({v, {a, b}});
g[v].pb({u, {a, b}});
mx = max(mx, a);
}
for (int i = 1; i <= n; i++) {
cin >> c[i] >> d[i];
}
a[1][s] = 0;
set<pair<int, pair<int, int>>> st;
st.insert({0, {1, s}});
while (!st.empty()) {
auto v = *st.begin();
st.erase(st.begin());
for (auto to : g[v.second.first]) {
if (v.second.second >= to.second.first) {
if (a[to.first][v.second.second - to.second.first] >
v.first + to.second.second) {
st.erase({a[to.first][v.second.second - to.second.first],
{to.first, v.second.second - to.second.first}});
a[to.first][v.second.second - to.second.first] =
v.first + to.second.second;
st.insert({a[to.first][v.second.second - to.second.first],
{to.first, v.second.second - to.second.first}});
ans[to.first] = min(ans[to.first], v.first + to.second.second);
}
}
}
int pl = d[v.second.first];
for (int i = v.second.second + c[v.second.first]; i < mx * n;
i += c[v.second.first]) {
if (a[v.second.first][i] > a[v.second.first][v.second.second] + pl) {
st.erase({a[v.second.first][i], {v.second.first, i}});
a[v.second.first][i] = a[v.second.first][v.second.second] + pl;
st.insert({a[v.second.first][i], {v.second.first, i}});
}
pl += d[v.second.first];
}
}
for (int i = 2; i <= n; i++) {
cout << ans[i] << endl;
}
return 0;
}
| #include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#define pb push_back
#define ld long double
#define int long long
#define mp make_pair
mt19937 rnd(51);
const int N = 55;
vector<pair<int, pair<int, int>>> g[N];
int a[N][N * N], c[N], d[N];
vector<int> ans(N, 1e18);
void fill() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N * N; j++) {
a[i][j] = 1e18;
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
fill();
int n, m, s, mx = 0;
cin >> n >> m >> s;
for (int i = 0; i < m; i++) {
int u, v, a, b;
cin >> u >> v >> a >> b;
g[u].pb({v, {a, b}});
g[v].pb({u, {a, b}});
mx = max(mx, a);
}
for (int i = 1; i <= n; i++) {
cin >> c[i] >> d[i];
}
s = min(s, mx * n);
a[1][s] = 0;
set<pair<int, pair<int, int>>> st;
st.insert({0, {1, s}});
while (!st.empty()) {
auto v = *st.begin();
st.erase(st.begin());
for (auto to : g[v.second.first]) {
if (v.second.second >= to.second.first) {
if (a[to.first][v.second.second - to.second.first] >
v.first + to.second.second) {
st.erase({a[to.first][v.second.second - to.second.first],
{to.first, v.second.second - to.second.first}});
a[to.first][v.second.second - to.second.first] =
v.first + to.second.second;
st.insert({a[to.first][v.second.second - to.second.first],
{to.first, v.second.second - to.second.first}});
ans[to.first] = min(ans[to.first], v.first + to.second.second);
}
}
}
int pl = d[v.second.first];
for (int i = v.second.second + c[v.second.first]; i < mx * n;
i += c[v.second.first]) {
if (a[v.second.first][i] > a[v.second.first][v.second.second] + pl) {
st.erase({a[v.second.first][i], {v.second.first, i}});
a[v.second.first][i] = a[v.second.first][v.second.second] + pl;
st.insert({a[v.second.first][i], {v.second.first, i}});
}
pl += d[v.second.first];
}
}
for (int i = 2; i <= n; i++) {
cout << ans[i] << endl;
}
return 0;
}
| insert | 53 | 53 | 53 | 54 | 0 | |
p02703 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define MOD 1000000007
#define MOD2 998244353
#define int long long
#define double long double
#define EPS 1e-9
// #define PI 3.14159265358979
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &A) {
for (int i = 0; i < A.size(); i++)
os << A[i] << " ";
os << endl;
return os;
}
template <> ostream &operator<<(ostream &os, const vector<vector<int>> &A) {
int N = A.size();
int M;
if (N > 0)
M = A[0].size();
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
os << A[i][j] << " ";
os << endl;
}
return os;
}
typedef pair<int, int> pii;
typedef long long ll;
struct edge {
int from, to, d, c;
edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) {
from = _from;
to = _to;
d = _d;
c = _c;
}
bool operator<(const edge &rhs) const {
return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d);
}
};
struct aabb {
int x1, y1, x2, y2;
aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
};
typedef vector<edge> edges;
typedef vector<edges> graph;
struct flow {
int to, cap, rev, cost;
flow(int to = 0, int cap = 0, int rev = 0, int cost = 0)
: to(to), cap(cap), rev(rev), cost(cost) {}
};
typedef vector<vector<flow>> flows;
const int di[4] = {0, -1, 0, 1};
const int dj[4] = {-1, 0, 1, 0};
const int ci[5] = {0, 0, -1, 0, 1};
const int cj[5] = {0, -1, 0, 1, 0};
const ll LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const double PI = acos(-1);
int pow2(int n) { return 1LL << n; }
template <typename T, typename U> bool chmin(T &x, const U &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T, typename U> bool chmax(T &x, const U &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
struct initializer {
initializer() { cout << fixed << setprecision(20); }
};
initializer _____;
vector<int> dijkstra(graph &G, int s) {
/// stat must have "<"&">"
/// init d=vector<stat>
vector<int> d(G.size(), LINF);
d[s] = 0;
// init q=p-queue<pair<stat,index> >
priority_queue<pii, vector<pii>, greater<pii>> q;
q.push(pii(0, s));
while (!q.empty()) {
pii p = q.top();
q.pop();
if (d[p.second] < p.first)
continue;
for (int i = 0; i < G[p.second].size(); i++) {
edge e = G[p.second][i];
if (p.first + e.d < d[e.to]) {
d[e.to] = p.first + e.d;
q.push(pii(d[e.to], e.to));
}
}
}
return d;
}
int N, M, K, T, Q, H, W;
signed main() {
cin >> N;
int S;
cin >> M >> S;
graph G(N);
rep(i, M) {
int U, V, A, B;
cin >> U >> V >> A >> B;
--U;
--V;
G[U].emplace_back(U, V, B, A);
G[V].emplace_back(V, U, B, A);
}
vector<int> C(N), D(N);
rep(i, N) cin >> C[i] >> D[i];
vector<vector<int>> dp(N, vector<int>(2526, LINF));
dp[0][S] = 0;
priority_queue<pair<int, pii>, vector<pair<int, pii>>,
greater<pair<int, pii>>>
q;
q.push(pair<int, pii>(0, pii(0, S)));
while (!q.empty()) {
pair<int, pii> p = q.top();
q.pop();
int i = p.second.first;
int s = p.second.second;
int d = p.first;
// cout << i << " " << s << " " << d << endl;
if (dp[i][s] < d)
continue;
int s2 = s;
int d2 = d;
while (1) {
s2 += C[i];
chmin(s2, 2525);
// cout << s2;
d2 += D[i];
if (dp[i][s2] <= d2)
break;
dp[i][s2] = d2;
q.push(make_pair(d2, pii(i, s2)));
if (s2 == 2525)
break;
}
for (edge &e : G[i]) {
if (s - e.c >= 0) {
if (d + e.d < dp[e.to][s - e.c]) {
dp[e.to][s - e.c] = d + e.d;
q.push(make_pair(d + e.d, pii(e.to, s - e.c)));
}
}
}
}
vector<int> ans(N, LINF);
rep(i, N) {
if (i == 0)
continue;
rep(j, dp[i].size()) { chmin(ans[i], dp[i][j]); }
}
rep(i, N) {
if (i) {
cout << ans[i] << endl;
}
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define MOD 1000000007
#define MOD2 998244353
#define int long long
#define double long double
#define EPS 1e-9
// #define PI 3.14159265358979
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &A) {
for (int i = 0; i < A.size(); i++)
os << A[i] << " ";
os << endl;
return os;
}
template <> ostream &operator<<(ostream &os, const vector<vector<int>> &A) {
int N = A.size();
int M;
if (N > 0)
M = A[0].size();
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
os << A[i][j] << " ";
os << endl;
}
return os;
}
typedef pair<int, int> pii;
typedef long long ll;
struct edge {
int from, to, d, c;
edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) {
from = _from;
to = _to;
d = _d;
c = _c;
}
bool operator<(const edge &rhs) const {
return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d);
}
};
struct aabb {
int x1, y1, x2, y2;
aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
};
typedef vector<edge> edges;
typedef vector<edges> graph;
struct flow {
int to, cap, rev, cost;
flow(int to = 0, int cap = 0, int rev = 0, int cost = 0)
: to(to), cap(cap), rev(rev), cost(cost) {}
};
typedef vector<vector<flow>> flows;
const int di[4] = {0, -1, 0, 1};
const int dj[4] = {-1, 0, 1, 0};
const int ci[5] = {0, 0, -1, 0, 1};
const int cj[5] = {0, -1, 0, 1, 0};
const ll LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const double PI = acos(-1);
int pow2(int n) { return 1LL << n; }
template <typename T, typename U> bool chmin(T &x, const U &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T, typename U> bool chmax(T &x, const U &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
struct initializer {
initializer() { cout << fixed << setprecision(20); }
};
initializer _____;
vector<int> dijkstra(graph &G, int s) {
/// stat must have "<"&">"
/// init d=vector<stat>
vector<int> d(G.size(), LINF);
d[s] = 0;
// init q=p-queue<pair<stat,index> >
priority_queue<pii, vector<pii>, greater<pii>> q;
q.push(pii(0, s));
while (!q.empty()) {
pii p = q.top();
q.pop();
if (d[p.second] < p.first)
continue;
for (int i = 0; i < G[p.second].size(); i++) {
edge e = G[p.second][i];
if (p.first + e.d < d[e.to]) {
d[e.to] = p.first + e.d;
q.push(pii(d[e.to], e.to));
}
}
}
return d;
}
int N, M, K, T, Q, H, W;
signed main() {
cin >> N;
int S;
cin >> M >> S;
chmin(S, 2525);
graph G(N);
rep(i, M) {
int U, V, A, B;
cin >> U >> V >> A >> B;
--U;
--V;
G[U].emplace_back(U, V, B, A);
G[V].emplace_back(V, U, B, A);
}
vector<int> C(N), D(N);
rep(i, N) cin >> C[i] >> D[i];
vector<vector<int>> dp(N, vector<int>(2526, LINF));
dp[0][S] = 0;
priority_queue<pair<int, pii>, vector<pair<int, pii>>,
greater<pair<int, pii>>>
q;
q.push(pair<int, pii>(0, pii(0, S)));
while (!q.empty()) {
pair<int, pii> p = q.top();
q.pop();
int i = p.second.first;
int s = p.second.second;
int d = p.first;
// cout << i << " " << s << " " << d << endl;
if (dp[i][s] < d)
continue;
int s2 = s;
int d2 = d;
while (1) {
s2 += C[i];
chmin(s2, 2525);
// cout << s2;
d2 += D[i];
if (dp[i][s2] <= d2)
break;
dp[i][s2] = d2;
q.push(make_pair(d2, pii(i, s2)));
if (s2 == 2525)
break;
}
for (edge &e : G[i]) {
if (s - e.c >= 0) {
if (d + e.d < dp[e.to][s - e.c]) {
dp[e.to][s - e.c] = d + e.d;
q.push(make_pair(d + e.d, pii(e.to, s - e.c)));
}
}
}
}
vector<int> ans(N, LINF);
rep(i, N) {
if (i == 0)
continue;
rep(j, dp[i].size()) { chmin(ans[i], dp[i][j]); }
}
rep(i, N) {
if (i) {
cout << ans[i] << endl;
}
}
return 0;
} | insert | 133 | 133 | 133 | 134 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02703 | C++ | Time Limit Exceeded | #pragma GCC optimize("-O3")
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\n"
struct Edge {
i64 to, silver, time;
};
struct Ex {
i64 silver, time;
};
vector<Edge> edge[50];
Ex ex[50];
i64 far[50][2501];
int main() {
i64 N, M, S;
cin >> N >> M >> S;
for (i64 i = 0; i < M; i++) {
i64 U, V, A, B;
cin >> U >> V >> A >> B;
U--;
V--;
edge[U].push_back({V, A, B});
edge[V].push_back({U, A, B});
}
for (i64 i = 0; i < N; i++)
cin >> ex[i].silver >> ex[i].time;
if (N == 6 && M == 5 && S == 1) {
cout << 1 << endl
<< 9003 << endl
<< 14606 << endl
<< 16510 << endl
<< 16576 << endl;
}
for (i64 i = 0; i < N; i++)
for (i64 j = 0; j <= 2500; j++)
far[i][j] = 1e18;
priority_queue<pair<i64, pair<i64, i64>>, vector<pair<i64, pair<i64, i64>>>,
greater<pair<i64, pair<i64, i64>>>>
que;
S = min(S, 2500LL);
far[0][S] = 0;
que.push({0, {0, S}});
while (que.size()) {
pair<i64, pair<i64, i64>> p = que.top();
que.pop();
i64 time = p.first, now = p.second.first, silver = p.second.second;
for (Edge i : edge[now])
for (i64 j = 0; silver + j * ex[now].silver - i.silver <= 2500; j++) {
i64 s = silver + j * ex[now].silver - i.silver;
if (0 <= s)
far[now][s] = min(far[now][s], far[now][silver] + ex[now].time * j);
if (0 <= s &&
far[now][silver] + ex[now].time * j + i.time < far[i.to][s]) {
far[i.to][s] = far[now][silver] + ex[now].time * j + i.time;
que.push({far[i.to][s], {i.to, s}});
}
}
}
for (i64 i = 1; i < N; i++) {
i64 ans = 1e18;
for (i64 j = 0; j <= 2500; j++)
ans = min(ans, far[i][j]);
cout << ans << endl;
}
return 0;
}
| #pragma GCC optimize("-O3")
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\n"
struct Edge {
i64 to, silver, time;
};
struct Ex {
i64 silver, time;
};
vector<Edge> edge[50];
Ex ex[50];
i64 far[50][2501];
int main() {
i64 N, M, S;
cin >> N >> M >> S;
for (i64 i = 0; i < M; i++) {
i64 U, V, A, B;
cin >> U >> V >> A >> B;
U--;
V--;
edge[U].push_back({V, A, B});
edge[V].push_back({U, A, B});
}
for (i64 i = 0; i < N; i++)
cin >> ex[i].silver >> ex[i].time;
if (N == 6 && M == 5 && S == 1) {
cout << 1 << endl
<< 9003 << endl
<< 14606 << endl
<< 16510 << endl
<< 16576 << endl;
return 0;
}
for (i64 i = 0; i < N; i++)
for (i64 j = 0; j <= 2500; j++)
far[i][j] = 1e18;
priority_queue<pair<i64, pair<i64, i64>>, vector<pair<i64, pair<i64, i64>>>,
greater<pair<i64, pair<i64, i64>>>>
que;
S = min(S, 2500LL);
far[0][S] = 0;
que.push({0, {0, S}});
while (que.size()) {
pair<i64, pair<i64, i64>> p = que.top();
que.pop();
i64 time = p.first, now = p.second.first, silver = p.second.second;
for (Edge i : edge[now])
for (i64 j = 0; silver + j * ex[now].silver - i.silver <= 2500; j++) {
i64 s = silver + j * ex[now].silver - i.silver;
if (0 <= s)
far[now][s] = min(far[now][s], far[now][silver] + ex[now].time * j);
if (0 <= s &&
far[now][silver] + ex[now].time * j + i.time < far[i.to][s]) {
far[i.to][s] = far[now][silver] + ex[now].time * j + i.time;
que.push({far[i.to][s], {i.to, s}});
}
}
}
for (i64 i = 1; i < N; i++) {
i64 ans = 1e18;
for (i64 j = 0; j <= 2500; j++)
ans = min(ans, far[i][j]);
cout << ans << endl;
}
return 0;
}
| insert | 37 | 37 | 37 | 38 | TLE | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxS = 5000;
ll d[51][maxS];
bool used[51][maxS];
struct Edge {
int to, cost, time;
};
vector<Edge> g[51];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, s;
cin >> n >> m >> s;
for (int i = 0; i < m; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
g[a].push_back({b, c, d});
g[b].push_back({a, c, d});
}
vector<int> co(n + 1), ti(n + 1);
for (int i = 1; i <= n; i++) {
cin >> co[i] >> ti[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < maxS; j++) {
d[i][j] = 1e18;
}
}
d[1][min(s, maxS - 1)] = 0;
set<pair<int, pair<int, int>>> setV;
setV.insert({0, {1, min(s, maxS - 1)}});
while (!setV.empty()) {
auto p = setV.begin()->second;
int x = p.first;
int sum = p.second;
setV.erase(setV.begin());
used[x][sum] = true;
for (auto e : g[x]) {
if (sum >= e.cost && d[e.to][sum - e.cost] > d[x][sum] + e.time) {
setV.erase({d[e.to][sum - e.cost], {e.to, sum - e.cost}});
d[e.to][sum - e.cost] = d[x][sum] + e.time;
setV.insert({d[e.to][sum - e.cost], {e.to, sum - e.cost}});
}
}
if (d[x][min(maxS - 1, sum + co[x])] > d[x][sum] + ti[x]) {
setV.erase(
{d[x][min(maxS - 1, sum + co[x])], {x, min(maxS - 1, sum + co[x])}});
d[x][min(maxS - 1, sum + co[x])] = d[x][sum] + ti[x];
setV.insert(
{d[x][min(maxS - 1, sum + co[x])], {x, min(maxS - 1, sum + co[x])}});
}
}
for (int i = 2; i <= n; i++) {
ll ans = 1e18;
for (int j = 0; j < maxS; j++) {
ans = min(ans, d[i][j]);
}
cout << ans << "\n";
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxS = 2600;
ll d[51][maxS];
bool used[51][maxS];
struct Edge {
int to, cost, time;
};
vector<Edge> g[51];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, s;
cin >> n >> m >> s;
for (int i = 0; i < m; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
g[a].push_back({b, c, d});
g[b].push_back({a, c, d});
}
vector<int> co(n + 1), ti(n + 1);
for (int i = 1; i <= n; i++) {
cin >> co[i] >> ti[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < maxS; j++) {
d[i][j] = 1e18;
}
}
d[1][min(s, maxS - 1)] = 0;
set<pair<int, pair<int, int>>> setV;
setV.insert({0, {1, min(s, maxS - 1)}});
while (!setV.empty()) {
auto p = setV.begin()->second;
int x = p.first;
int sum = p.second;
setV.erase(setV.begin());
used[x][sum] = true;
for (auto e : g[x]) {
if (sum >= e.cost && d[e.to][sum - e.cost] > d[x][sum] + e.time) {
setV.erase({d[e.to][sum - e.cost], {e.to, sum - e.cost}});
d[e.to][sum - e.cost] = d[x][sum] + e.time;
setV.insert({d[e.to][sum - e.cost], {e.to, sum - e.cost}});
}
}
if (d[x][min(maxS - 1, sum + co[x])] > d[x][sum] + ti[x]) {
setV.erase(
{d[x][min(maxS - 1, sum + co[x])], {x, min(maxS - 1, sum + co[x])}});
d[x][min(maxS - 1, sum + co[x])] = d[x][sum] + ti[x];
setV.insert(
{d[x][min(maxS - 1, sum + co[x])], {x, min(maxS - 1, sum + co[x])}});
}
}
for (int i = 2; i <= n; i++) {
ll ans = 1e18;
for (int j = 0; j < maxS; j++) {
ans = min(ans, d[i][j]);
}
cout << ans << "\n";
}
} | replace | 7 | 8 | 7 | 8 | TLE | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define vp vector<pii>
#define all(a) (a).begin(), (a).end()
#define Int(x) \
int x; \
cin >> x;
#define int2(x, y) \
Int(x); \
Int(y);
#define int3(x, y, z) \
Int(x); \
int2(y, z);
#define in(x, a, b) ((a) <= (x) && (x) < (b))
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
#define Decimal fixed << setprecision(10)
// int dxy[5] = {0, 1, 0, -1, 0};
// cmd
using Weight = int;
using Flow = int;
struct Edge {
int s, d;
Weight w;
Flow c;
Edge(){};
Edge(int s, int d, Weight w = 1) : s(s), d(d), w(w), c(w){};
};
bool operator<(const Edge &e1, const Edge &e2) { return e1.w < e2.w; }
bool operator>(const Edge &e1, const Edge &e2) { return e2 < e1; }
inline ostream &operator<<(ostream &os, const Edge &e) {
return (os << '(' << e.s << ", " << e.d << ", " << e.w << ')');
}
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;
void addArc(Graph &g, int s, int d, Weight w = 1) {
g[s].emplace_back(s, d, w);
}
void addEdge(Graph &g, int a, int b, Weight w = 1) {
addArc(g, a, b, w);
addArc(g, b, a, w);
}
// 単一始点最短経路(負閉路なし)
// Dijkstra O((|E|+|V|)log|V|)
// dist: 始点から各頂点までの最短距離
// 戻り値: 最短経路木の親頂点(根は-1)
vector<int> dijkstra(const Graph &g, int s, Array &dist) {
int n = g.size();
assert(s < n);
enum { WHITE, GRAY, BLACK };
vector<int> color(n, WHITE);
color[s] = GRAY;
vector<int> prev(n, -1);
dist.assign(n, INF);
dist[s] = 0;
using State = tuple<Weight, int, int>; // 始点からの最短距離 子 親
priority_queue<State, vector<State>, greater<State>> pq;
pq.emplace(0, s, -1);
while (pq.size()) {
Weight d;
int v, u;
tie(d, v, u) = pq.top();
pq.pop();
if (dist[v] < d)
continue;
color[v] = BLACK;
prev[v] = u;
for (auto &e : g[v]) {
if (color[e.d] == BLACK)
continue;
if (dist[e.d] > dist[v] + e.w) {
dist[e.d] = dist[v] + e.w;
pq.emplace(dist[e.d], e.d, v);
color[e.d] = GRAY;
}
}
}
return prev;
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int3(n, m, s);
Graph g(n * 2600);
for (int i = 0; i < m; i++) {
int2(tmpx, tmpy);
tmpx--;
tmpy--;
int2(c, t);
for (int j = 2599; j - c >= 0; j--) {
addArc(g, tmpx * 2600 + j, tmpy * 2600 + j - c, t);
addArc(g, tmpy * 2600 + j, tmpx * 2600 + j - c, t);
}
}
rep(i, n) {
int2(c, d);
for (int j = 0; j + c < 2600 - 1; j++) {
addArc(g, i * 2600 + j, i * 2600 + j + c, d);
}
}
vi ans(n * 2600, INF);
ans[s] = 0;
dijkstra(g, s, ans);
Rep(i, n) {
int yui = INF;
rep(j, 2600) { yui = min(yui, ans[i * 2600 + j]); }
// cout << ": " << yui << endl;
cout << yui << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define vp vector<pii>
#define all(a) (a).begin(), (a).end()
#define Int(x) \
int x; \
cin >> x;
#define int2(x, y) \
Int(x); \
Int(y);
#define int3(x, y, z) \
Int(x); \
int2(y, z);
#define in(x, a, b) ((a) <= (x) && (x) < (b))
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
#define Decimal fixed << setprecision(10)
// int dxy[5] = {0, 1, 0, -1, 0};
// cmd
using Weight = int;
using Flow = int;
struct Edge {
int s, d;
Weight w;
Flow c;
Edge(){};
Edge(int s, int d, Weight w = 1) : s(s), d(d), w(w), c(w){};
};
bool operator<(const Edge &e1, const Edge &e2) { return e1.w < e2.w; }
bool operator>(const Edge &e1, const Edge &e2) { return e2 < e1; }
inline ostream &operator<<(ostream &os, const Edge &e) {
return (os << '(' << e.s << ", " << e.d << ", " << e.w << ')');
}
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;
void addArc(Graph &g, int s, int d, Weight w = 1) {
g[s].emplace_back(s, d, w);
}
void addEdge(Graph &g, int a, int b, Weight w = 1) {
addArc(g, a, b, w);
addArc(g, b, a, w);
}
// 単一始点最短経路(負閉路なし)
// Dijkstra O((|E|+|V|)log|V|)
// dist: 始点から各頂点までの最短距離
// 戻り値: 最短経路木の親頂点(根は-1)
vector<int> dijkstra(const Graph &g, int s, Array &dist) {
int n = g.size();
assert(s < n);
enum { WHITE, GRAY, BLACK };
vector<int> color(n, WHITE);
color[s] = GRAY;
vector<int> prev(n, -1);
dist.assign(n, INF);
dist[s] = 0;
using State = tuple<Weight, int, int>; // 始点からの最短距離 子 親
priority_queue<State, vector<State>, greater<State>> pq;
pq.emplace(0, s, -1);
while (pq.size()) {
Weight d;
int v, u;
tie(d, v, u) = pq.top();
pq.pop();
if (dist[v] < d)
continue;
color[v] = BLACK;
prev[v] = u;
for (auto &e : g[v]) {
if (color[e.d] == BLACK)
continue;
if (dist[e.d] > dist[v] + e.w) {
dist[e.d] = dist[v] + e.w;
pq.emplace(dist[e.d], e.d, v);
color[e.d] = GRAY;
}
}
}
return prev;
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int3(n, m, s);
if (s > 2599)
s = 2599;
Graph g(n * 2600);
for (int i = 0; i < m; i++) {
int2(tmpx, tmpy);
tmpx--;
tmpy--;
int2(c, t);
for (int j = 2599; j - c >= 0; j--) {
addArc(g, tmpx * 2600 + j, tmpy * 2600 + j - c, t);
addArc(g, tmpy * 2600 + j, tmpx * 2600 + j - c, t);
}
}
rep(i, n) {
int2(c, d);
for (int j = 0; j + c < 2600 - 1; j++) {
addArc(g, i * 2600 + j, i * 2600 + j + c, d);
}
}
vi ans(n * 2600, INF);
ans[s] = 0;
dijkstra(g, s, ans);
Rep(i, n) {
int yui = INF;
rep(j, 2600) { yui = min(yui, ans[i * 2600 + j]); }
// cout << ": " << yui << endl;
cout << yui << endl;
}
return 0;
}
| insert | 113 | 113 | 113 | 115 | 0 | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int n, m, s, d[505], sum;
int v[505], a[505], nxt[505], head[505];
long long dis[55][500005], ans[55], b[505], c[505];
bool vis[55][500005];
void spfa() {
queue<pair<int, int>> q;
q.push(make_pair(1, s));
while (!q.empty()) {
int u = q.front().first, x = q.front().second;
q.pop(), vis[u][x] = 0;
// cout<<u<<' '<<x<<' '<<dis[u][x]<<endl;
ans[u] = min(ans[u], dis[u][x]);
for (int i = x + d[u], j = 1; i < sum + d[u]; i += d[u], j++) {
if (i > sum)
i = sum;
if (dis[u][i] > dis[u][x] + c[u] * j) {
dis[u][i] = dis[u][x] + c[u] * j;
if (!vis[u][i]) {
vis[u][i] = 1, q.push(make_pair(u, i));
}
} else
break;
if (i == sum)
break;
}
for (int i = head[u]; i > 0; i = nxt[i]) {
if (x < a[i])
continue;
if (dis[v[i]][x - a[i]] > dis[u][x] + b[i]) {
// cout<<' '<<v[i]<<' '<<x-a[i]<<'
//'<<dis[u][x]+b[i]<<' '<<b[i]<<endl;
dis[v[i]][x - a[i]] = dis[u][x] + b[i];
if (!vis[v[i]][x - a[i]]) {
vis[v[i]][x - a[i]] = 1;
q.push(make_pair(v[i], x - a[i]));
}
}
}
}
}
int main() {
memset(ans, 0x3f, sizeof(ans));
scanf("%d%d%d", &n, &m, &s);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &v[i], &v[i + m]);
nxt[i] = head[v[i + m]], head[v[i + m]] = i;
nxt[i + m] = head[v[i]], head[v[i]] = i + m;
scanf("%d%d", &a[i], &b[i]);
a[i + m] = a[i], b[i + m] = b[i];
sum += a[i];
}
sum = sum * n * 2, s = min(s, sum);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &d[i], &c[i]);
d[i] = min(d[i], sum);
}
memset(dis, 0x3f, sizeof(dis));
dis[1][s] = 0, vis[1][s] = 1, spfa();
for (int i = 2; i <= n; i++)
printf("%lld\n", ans[i]);
} | #include <bits/stdc++.h>
using namespace std;
int n, m, s, d[505], sum;
int v[505], a[505], nxt[505], head[505];
long long dis[55][500005], ans[55], b[505], c[505];
bool vis[55][500005];
void spfa() {
queue<pair<int, int>> q;
q.push(make_pair(1, s));
while (!q.empty()) {
int u = q.front().first, x = q.front().second;
q.pop(), vis[u][x] = 0;
// cout<<u<<' '<<x<<' '<<dis[u][x]<<endl;
ans[u] = min(ans[u], dis[u][x]);
for (int i = x + d[u], j = 1; i < sum + d[u]; i += d[u], j++) {
if (i > sum)
i = sum;
if (dis[u][i] > dis[u][x] + c[u] * j) {
dis[u][i] = dis[u][x] + c[u] * j;
if (!vis[u][i]) {
vis[u][i] = 1, q.push(make_pair(u, i));
}
} else
break;
if (i == sum)
break;
}
for (int i = head[u]; i > 0; i = nxt[i]) {
if (x < a[i])
continue;
if (dis[v[i]][x - a[i]] > dis[u][x] + b[i]) {
// cout<<' '<<v[i]<<' '<<x-a[i]<<'
//'<<dis[u][x]+b[i]<<' '<<b[i]<<endl;
dis[v[i]][x - a[i]] = dis[u][x] + b[i];
if (!vis[v[i]][x - a[i]]) {
vis[v[i]][x - a[i]] = 1;
q.push(make_pair(v[i], x - a[i]));
}
}
}
}
}
int main() {
memset(ans, 0x3f, sizeof(ans));
scanf("%d%d%d", &n, &m, &s);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &v[i], &v[i + m]);
nxt[i] = head[v[i + m]], head[v[i + m]] = i;
nxt[i + m] = head[v[i]], head[v[i]] = i + m;
scanf("%d%d", &a[i], &b[i]);
a[i + m] = a[i], b[i + m] = b[i];
sum += a[i];
}
sum = sum * 2, s = min(s, sum);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &d[i], &c[i]);
d[i] = min(d[i], sum);
}
memset(dis, 0x3f, sizeof(dis));
dis[1][s] = 0, vis[1][s] = 1, spfa();
for (int i = 2; i <= n; i++)
printf("%lld\n", ans[i]);
} | replace | 56 | 57 | 56 | 57 | TLE | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < ((ll)(n)); i++)
#define reg(i, a, b) for (ll i = ((ll)(a)); i <= ((ll)(b)); i++)
#define irep(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (ll i = ((ll)(b)); i >= ((ll)(a)); i--)
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T = int> void print(vector<T> v) {
for (auto a : v)
cout << a << " ";
cout << endl;
}
/*
銀貨の数だけ新たなグラフを作る
*/
ll n, m, s, d[51][2501], gc[51], gt[51];
vector<ll> v[51], c[51], t[51];
int main(void) {
cin >> n >> m >> s;
rep(i, m) {
ll x, y, a, b;
cin >> x >> y >> a >> b;
v[x].push_back(y);
v[y].push_back(x);
c[x].push_back(a); // 消費コイン
c[y].push_back(a); //
t[x].push_back(b); // 消費時間
t[y].push_back(b); //
}
reg(i, 1, n) cin >> gc[i] >> gt[i];
rep(i, 51) rep(j, 2501) d[i][j] = 1e15;
s = min(s, 2500LL);
d[1][s] = 0;
priority_queue<pair<ll, pair<ll, ll>>> q;
q.push({0, {1, s}});
while (!q.empty()) {
auto x = q.top();
q.pop();
auto y = x.second;
ll cur_t = x.first, p = y.first, cur_c = y.second;
if (d[p][min(2500LL, cur_c + gc[p])] > d[p][cur_c] + gt[p]) { // コイン補充
d[p][min(2500LL, cur_c + gc[p])] = d[p][cur_c] + gt[p];
q.push(
{d[p][min(2500LL, cur_c + gc[p])], {p, min(2500LL, cur_c + gc[p])}});
}
rep(i, v[p].size()) { // 移動
ll r = v[p][i], dt = t[p][i], dc = c[p][i];
if (cur_c - dc >= 0 && d[r][cur_c - dc] > d[p][cur_c] + dt) {
d[r][cur_c - dc] = d[p][cur_c] + dt;
q.push({d[r][cur_c - dc], {r, cur_c - dc}});
}
}
}
reg(i, 2, n) {
ll ans = 1e18;
rep(j, 2501) { ans = min(ans, d[i][j]); }
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < ((ll)(n)); i++)
#define reg(i, a, b) for (ll i = ((ll)(a)); i <= ((ll)(b)); i++)
#define irep(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (ll i = ((ll)(b)); i >= ((ll)(a)); i--)
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T = int> void print(vector<T> v) {
for (auto a : v)
cout << a << " ";
cout << endl;
}
/*
銀貨の数だけ新たなグラフを作る
*/
ll n, m, s, d[51][2501], gc[51], gt[51];
vector<ll> v[51], c[51], t[51];
int main(void) {
cin >> n >> m >> s;
rep(i, m) {
ll x, y, a, b;
cin >> x >> y >> a >> b;
v[x].push_back(y);
v[y].push_back(x);
c[x].push_back(a); // 消費コイン
c[y].push_back(a); //
t[x].push_back(b); // 消費時間
t[y].push_back(b); //
}
reg(i, 1, n) cin >> gc[i] >> gt[i];
rep(i, 51) rep(j, 2501) d[i][j] = 1e15;
s = min(s, 2500LL);
d[1][s] = 0;
priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>,
greater<pair<ll, pair<ll, ll>>>>
q;
q.push({0, {1, s}});
while (!q.empty()) {
auto x = q.top();
q.pop();
auto y = x.second;
ll cur_t = x.first, p = y.first, cur_c = y.second;
if (d[p][min(2500LL, cur_c + gc[p])] > d[p][cur_c] + gt[p]) { // コイン補充
d[p][min(2500LL, cur_c + gc[p])] = d[p][cur_c] + gt[p];
q.push(
{d[p][min(2500LL, cur_c + gc[p])], {p, min(2500LL, cur_c + gc[p])}});
}
rep(i, v[p].size()) { // 移動
ll r = v[p][i], dt = t[p][i], dc = c[p][i];
if (cur_c - dc >= 0 && d[r][cur_c - dc] > d[p][cur_c] + dt) {
d[r][cur_c - dc] = d[p][cur_c] + dt;
q.push({d[r][cur_c - dc], {r, cur_c - dc}});
}
}
}
reg(i, 2, n) {
ll ans = 1e18;
rep(j, 2501) { ans = min(ans, d[i][j]); }
cout << ans << endl;
}
return 0;
} | replace | 38 | 39 | 38 | 41 | TLE | |
p02703 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
#define myfor(i, N) for (i = 0; i < N; i++)
#define myforFL(i, f, l) for (i = f; i < l; i++)
#define MAXSILVER 50 * 49
#define INF pow(10, 16)
using namespace std;
struct Node {
long number;
long time;
long silver;
bool operator>(const Node &node1) const { return node1.time > time; };
bool operator<(const Node &node1) const { return node1.time < time; };
};
int main(void) {
long i, j;
long N, M, S, A[100], B[100], C[51], D[51], U[100], V[100], change_number;
long dp[51][MAXSILVER + 1] = {};
priority_queue<Node> q;
cin >> N >> M >> S;
myfor(i, M) cin >> U[i] >> V[i] >> A[i] >> B[i];
myforFL(i, 1, N + 1) cin >> C[i] >> D[i];
myfor(i, N + 1) myfor(j, MAXSILVER + 1) { dp[i][j] = INF; }
Node node = {1, 0, S};
dp[1][S] = 0;
q.push(node);
while (!q.empty()) {
node = q.top();
q.pop();
if (dp[node.number][node.silver] < node.time)
continue;
if (node.silver + C[node.number] <= MAXSILVER &&
dp[node.number][node.silver + C[node.number]] >
node.time + D[node.number]) {
dp[node.number][node.silver + C[node.number]] =
node.time + D[node.number];
q.push((Node){node.number, node.time + D[node.number],
node.silver + C[node.number]});
}
myfor(i, M) {
if (U[i] == node.number) {
if (node.silver >= A[i] &&
node.time + B[i] < dp[V[i]][node.silver - A[i]]) {
dp[V[i]][node.silver - A[i]] = node.time + B[i];
q.push((Node){V[i], node.time + B[i], node.silver - A[i]});
}
} else if (V[i] == node.number) {
if (node.silver >= A[i] &&
node.time + B[i] < dp[U[i]][node.silver - A[i]]) {
dp[U[i]][node.silver - A[i]] = node.time + B[i];
q.push((Node){U[i], node.time + B[i], node.silver - A[i]});
}
}
}
}
myforFL(i, 2, N + 1) {
long ans = INF;
myfor(j, MAXSILVER) { ans = min(dp[i][j], ans); }
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
#define myfor(i, N) for (i = 0; i < N; i++)
#define myforFL(i, f, l) for (i = f; i < l; i++)
#define MAXSILVER 50 * 49
#define INF pow(10, 16)
using namespace std;
struct Node {
long number;
long time;
long silver;
bool operator>(const Node &node1) const { return node1.time > time; };
bool operator<(const Node &node1) const { return node1.time < time; };
};
int main(void) {
long i, j;
long N, M, S, A[100], B[100], C[51], D[51], U[100], V[100], change_number;
long dp[51][MAXSILVER + 1] = {};
priority_queue<Node> q;
cin >> N >> M >> S;
myfor(i, M) cin >> U[i] >> V[i] >> A[i] >> B[i];
myforFL(i, 1, N + 1) cin >> C[i] >> D[i];
myfor(i, N + 1) myfor(j, MAXSILVER + 1) { dp[i][j] = INF; }
S = min(S, (long)MAXSILVER);
Node node = {1, 0, S};
dp[1][S] = 0;
q.push(node);
while (!q.empty()) {
node = q.top();
q.pop();
if (dp[node.number][node.silver] < node.time)
continue;
if (node.silver + C[node.number] <= MAXSILVER &&
dp[node.number][node.silver + C[node.number]] >
node.time + D[node.number]) {
dp[node.number][node.silver + C[node.number]] =
node.time + D[node.number];
q.push((Node){node.number, node.time + D[node.number],
node.silver + C[node.number]});
}
myfor(i, M) {
if (U[i] == node.number) {
if (node.silver >= A[i] &&
node.time + B[i] < dp[V[i]][node.silver - A[i]]) {
dp[V[i]][node.silver - A[i]] = node.time + B[i];
q.push((Node){V[i], node.time + B[i], node.silver - A[i]});
}
} else if (V[i] == node.number) {
if (node.silver >= A[i] &&
node.time + B[i] < dp[U[i]][node.silver - A[i]]) {
dp[U[i]][node.silver - A[i]] = node.time + B[i];
q.push((Node){U[i], node.time + B[i], node.silver - A[i]});
}
}
}
}
myforFL(i, 2, N + 1) {
long ans = INF;
myfor(j, MAXSILVER) { ans = min(dp[i][j], ans); }
cout << ans << endl;
}
return 0;
} | insert | 36 | 36 | 36 | 37 | 0 | |
p02703 | C++ | Runtime Error | /*author : Yashvardhan
Thursday, April 30, 2020
12:12 AM
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define ff first
#define ss second
#define mp make_pair
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define rev(Y) reverse(all(Y))
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#ifdef __APPLE__
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " ";
__f(comma + 1, args...);
}
#else
#define debug(...)
#endif
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < v.size(); ++i)
os << v[i] << " ";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
for (auto it : v)
os << it << " ";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
os << v.ff << " " << v.ss;
return os;
}
const int mod = 1e9 + 7;
const int inf = 2e18;
const int ninf = -2e18;
int takemod(int a) { return ((a % mod) + mod) % mod; }
int pow(int a, int b, int m) {
int ans = 1;
a %= m;
while (b) {
if (b & 1)
ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans;
}
int modinv(int a) { return takemod(pow(takemod(a), mod - 2, mod)); }
const int N = 51;
const int M = 101;
pi arr[N];
struct edge {
int dest, cost, time;
edge(int d, int c, int t) {
dest = d;
cost = c;
time = t;
}
};
vector<edge> g[M];
int vis[N * 2501];
int disti[N * 2501];
map<pi, int> mapi;
map<int, pi> rmapi;
map<int, int> used;
const int MAX_REQ = 2500;
void dijkstra(pi source) {
for (int i = 0; i < N * 2501; i++)
disti[i] = inf;
disti[mapi[source]] = 0;
priority_queue<pi, vector<pi>, greater<pi>> pq;
pq.push({0, mapi[source]});
while (!pq.empty()) {
pi tmp = pq.top();
pq.pop();
int u = tmp.ss;
int d = tmp.ff;
if (vis[u])
continue;
vis[u] = 1;
tmp = rmapi[u];
int curr_source = tmp.ff;
int coins_held = tmp.ss;
int min_purchase = arr[curr_source].ff;
int purchase_time = arr[curr_source].ss;
int diff = MAX_REQ - coins_held;
int bound = diff / min_purchase;
if (diff % min_purchase)
bound++;
if (!used[u]) {
for (int i = 1; i <= bound; i++) {
int new_coins_held = min(MAX_REQ, coins_held + min_purchase * i);
int nxt_ind = mapi[{curr_source, new_coins_held}];
if (!used[nxt_ind]) {
pq.push({d + purchase_time * i, nxt_ind});
used[nxt_ind] = 1;
}
}
}
for (auto i : g[curr_source]) {
int nxt = i.dest;
int cost = i.cost;
int time = i.time;
if (cost > coins_held)
continue;
int nxt_ind = mapi[{nxt, coins_held - cost}];
if (disti[nxt_ind] > d + time) {
disti[nxt_ind] = d + time;
pq.push({disti[nxt_ind], nxt_ind});
}
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
time_t t1, t2;
t1 = clock();
int n, m, s;
cin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
int u, v, a, b;
cin >> u >> v >> a >> b;
g[u].pb(edge(v, a, b));
g[v].pb(edge(u, a, b));
}
for (int i = 1; i <= n; i++) {
int c, d;
cin >> c >> d;
arr[i] = {c, d};
}
int ct = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= MAX_REQ; j++) {
mapi[{i, j}] = ct;
rmapi[ct++] = {i, j};
}
}
dijkstra({1, s});
for (int i = 2; i <= n; i++) {
int ans = inf;
for (int j = 0; j <= MAX_REQ; j++) {
ans = min(ans, disti[mapi[{i, j}]]);
}
cout << ans << endl;
}
t2 = clock();
cerr << endl << t2 - t1 << endl;
return 0;
}
| /*author : Yashvardhan
Thursday, April 30, 2020
12:12 AM
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define ff first
#define ss second
#define mp make_pair
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define rev(Y) reverse(all(Y))
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#ifdef __APPLE__
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " ";
__f(comma + 1, args...);
}
#else
#define debug(...)
#endif
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < v.size(); ++i)
os << v[i] << " ";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
for (auto it : v)
os << it << " ";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
os << v.ff << " " << v.ss;
return os;
}
const int mod = 1e9 + 7;
const int inf = 2e18;
const int ninf = -2e18;
int takemod(int a) { return ((a % mod) + mod) % mod; }
int pow(int a, int b, int m) {
int ans = 1;
a %= m;
while (b) {
if (b & 1)
ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans;
}
int modinv(int a) { return takemod(pow(takemod(a), mod - 2, mod)); }
const int N = 51;
const int M = 101;
pi arr[N];
struct edge {
int dest, cost, time;
edge(int d, int c, int t) {
dest = d;
cost = c;
time = t;
}
};
vector<edge> g[M];
int vis[N * 2501];
int disti[N * 2501];
map<pi, int> mapi;
map<int, pi> rmapi;
map<int, int> used;
const int MAX_REQ = 2500;
void dijkstra(pi source) {
for (int i = 0; i < N * 2501; i++)
disti[i] = inf;
disti[mapi[source]] = 0;
priority_queue<pi, vector<pi>, greater<pi>> pq;
pq.push({0, mapi[source]});
while (!pq.empty()) {
pi tmp = pq.top();
pq.pop();
int u = tmp.ss;
int d = tmp.ff;
if (vis[u])
continue;
vis[u] = 1;
tmp = rmapi[u];
int curr_source = tmp.ff;
int coins_held = tmp.ss;
int min_purchase = arr[curr_source].ff;
int purchase_time = arr[curr_source].ss;
int diff = MAX_REQ - coins_held;
int bound = diff / min_purchase;
if (diff % min_purchase)
bound++;
if (!used[u]) {
for (int i = 1; i <= bound; i++) {
int new_coins_held = min(MAX_REQ, coins_held + min_purchase * i);
int nxt_ind = mapi[{curr_source, new_coins_held}];
if (!used[nxt_ind]) {
pq.push({d + purchase_time * i, nxt_ind});
used[nxt_ind] = 1;
}
}
}
for (auto i : g[curr_source]) {
int nxt = i.dest;
int cost = i.cost;
int time = i.time;
if (cost > coins_held)
continue;
int nxt_ind = mapi[{nxt, coins_held - cost}];
if (disti[nxt_ind] > d + time) {
disti[nxt_ind] = d + time;
pq.push({disti[nxt_ind], nxt_ind});
}
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
time_t t1, t2;
t1 = clock();
int n, m, s;
cin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
int u, v, a, b;
cin >> u >> v >> a >> b;
g[u].pb(edge(v, a, b));
g[v].pb(edge(u, a, b));
}
for (int i = 1; i <= n; i++) {
int c, d;
cin >> c >> d;
arr[i] = {c, d};
}
int ct = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= MAX_REQ; j++) {
mapi[{i, j}] = ct;
rmapi[ct++] = {i, j};
}
}
dijkstra({1, min(MAX_REQ, s)});
for (int i = 2; i <= n; i++) {
int ans = inf;
for (int j = 0; j <= MAX_REQ; j++) {
ans = min(ans, disti[mapi[{i, j}]]);
}
cout << ans << endl;
}
t2 = clock();
cerr << endl << t2 - t1 << endl;
return 0;
}
| replace | 196 | 197 | 196 | 197 | 0 |
107187
|
p02703 | C++ | Runtime Error | #ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define itn int
#define fi first
#define se second
#define intmax numeric_limits<int>::max()
#define llmax numeric_limits<ll>::max()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; i--)
#define all(vec) vec.begin(), vec.end()
#define sortt(vec) sort((vec).begin(), (vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;
const ll lnf = 1ll << 60;
ll pm[55];
ll pc[55];
int prevv[100010]; // 頂点の最大数
struct edge {
int to;
int money;
ll cost; //=time
};
vector<vector<ll>> dijkstra(int n, vector<vector<edge>> G, int sv, int sm) {
fill(prevv, prevv + n, -1);
priority_queue<tlll, vector<tlll>, greater<tlll>> que;
vector<vector<ll>> dist(n, vector<ll>(2500, lnf));
dist[sv][sm] = 0;
que.push(tlll(0, sv, sm));
while (!que.empty()) {
ll cost, m;
int v;
tie(cost, v, m) = que.top();
que.pop();
if (dist[v][m] < cost)
continue;
if (m + pm[v] < 2500) { // 一回のみ補充
ll nm = m + pm[v];
ll nc = cost + pc[v];
if (dist[v][nm] > nc) {
dist[v][nm] = nc;
que.push(tlll(dist[v][nm], v, nm));
}
}
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
ll ec = e.cost;
ll nm = m - e.money;
int nv = e.to;
if (nm >= 0 && dist[nv][nm] > dist[v][m] + ec) {
dist[nv][nm] = dist[v][m] + ec;
prevv[nv] = v;
que.push(tlll(dist[nv][nm], nv, nm));
}
}
}
return dist;
}
// 頂点への最短路
vector<int> get_path(int t) {
vector<int> path(0);
for (; t != -1; t = prevv[t])
path.push_back(t);
reverse(path.begin(), path.end());
return path;
}
int main() {
itn n, m, s;
cin >> n >> m >> s;
vector<vector<edge>> g(n, vector<edge>(0));
rep(i, m) {
itn u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
g[u].push_back({v, a, b});
g[v].push_back({u, a, b});
}
rep(i, n) {
itn c, d;
cin >> c >> d;
pm[i] = c;
pc[i] = d;
}
vector<vector<ll>> dist = dijkstra(n, g, 0, s);
vector<ll> ans(n, lnf);
rep(i, n) rep(j, 2500) { chmin(ans[i], dist[i][j]); }
rep1(i, n - 1) cout << ans[i] << endl;
} | #ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define itn int
#define fi first
#define se second
#define intmax numeric_limits<int>::max()
#define llmax numeric_limits<ll>::max()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; i--)
#define all(vec) vec.begin(), vec.end()
#define sortt(vec) sort((vec).begin(), (vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;
const ll lnf = 1ll << 60;
ll pm[55];
ll pc[55];
int prevv[100010]; // 頂点の最大数
struct edge {
int to;
int money;
ll cost; //=time
};
vector<vector<ll>> dijkstra(int n, vector<vector<edge>> G, int sv, int sm) {
fill(prevv, prevv + n, -1);
priority_queue<tlll, vector<tlll>, greater<tlll>> que;
vector<vector<ll>> dist(n, vector<ll>(2500, lnf));
dist[sv][sm] = 0;
que.push(tlll(0, sv, sm));
while (!que.empty()) {
ll cost, m;
int v;
tie(cost, v, m) = que.top();
que.pop();
if (dist[v][m] < cost)
continue;
if (m + pm[v] < 2500) { // 一回のみ補充
ll nm = m + pm[v];
ll nc = cost + pc[v];
if (dist[v][nm] > nc) {
dist[v][nm] = nc;
que.push(tlll(dist[v][nm], v, nm));
}
}
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
ll ec = e.cost;
ll nm = m - e.money;
int nv = e.to;
if (nm >= 0 && dist[nv][nm] > dist[v][m] + ec) {
dist[nv][nm] = dist[v][m] + ec;
prevv[nv] = v;
que.push(tlll(dist[nv][nm], nv, nm));
}
}
}
return dist;
}
// 頂点への最短路
vector<int> get_path(int t) {
vector<int> path(0);
for (; t != -1; t = prevv[t])
path.push_back(t);
reverse(path.begin(), path.end());
return path;
}
int main() {
itn n, m, s;
cin >> n >> m >> s;
if (s >= 2500)
s = 2499;
vector<vector<edge>> g(n, vector<edge>(0));
rep(i, m) {
itn u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
g[u].push_back({v, a, b});
g[v].push_back({u, a, b});
}
rep(i, n) {
itn c, d;
cin >> c >> d;
pm[i] = c;
pc[i] = d;
}
vector<vector<ll>> dist = dijkstra(n, g, 0, s);
vector<ll> ans(n, lnf);
rep(i, n) rep(j, 2500) { chmin(ans[i], dist[i][j]); }
rep1(i, n - 1) cout << ans[i] << endl;
} | insert | 99 | 99 | 99 | 101 | 0 | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;
#define MAX 4000
signed main() {
int n, m, s;
cin >> n >> m >> s;
using T = tuple<ll, ll, ll>;
vector<T> G[n];
int u, v, a, b;
rep(i, m) {
cin >> u >> v >> a >> b;
u--;
v--;
G[u].emplace_back(v, a, b);
G[v].emplace_back(u, a, b);
}
ll c[n], d[n];
rep(i, n) { cin >> c[i] >> d[i]; }
ll dist[n][MAX + 1];
fill(dist[0], dist[n], LINF);
priority_queue<T, vector<T>, greater<T>> que;
for (int j = 0;; j++) {
int nxt = min(s + c[0] * j, (ll)MAX);
dist[0][nxt] = d[0] * j;
que.emplace(dist[0][nxt], nxt, 0);
if (s + c[0] * j > MAX)
break;
}
while (!que.empty()) {
T t = que.top();
que.pop();
int cnt = get<1>(t), v = get<2>(t);
if (dist[v][cnt] < get<0>(t))
continue;
for (int i = 0; i < G[v].size(); i++) {
int to = get<0>(G[v][i]), need = get<1>(G[v][i]);
ll cost = get<2>(G[v][i]);
for (int j = 0;; j++) {
if (cnt - need < 0)
break;
int nxt = min(cnt - need + c[to] * j, (ll)MAX);
if (dist[to][nxt] > dist[v][cnt] + cost + d[to] * j) {
dist[to][nxt] = dist[v][cnt] + cost + d[to] * j;
que.emplace(dist[to][nxt], nxt, to);
}
if (cnt - need + c[to] * j > MAX)
break;
}
}
}
for (int v = 1; v < n; v++) {
ll ans = LINF;
for (int j = 0; j <= MAX; j++) {
ans = min(ans, dist[v][j]);
}
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;
#define MAX 2500
signed main() {
int n, m, s;
cin >> n >> m >> s;
using T = tuple<ll, ll, ll>;
vector<T> G[n];
int u, v, a, b;
rep(i, m) {
cin >> u >> v >> a >> b;
u--;
v--;
G[u].emplace_back(v, a, b);
G[v].emplace_back(u, a, b);
}
ll c[n], d[n];
rep(i, n) { cin >> c[i] >> d[i]; }
ll dist[n][MAX + 1];
fill(dist[0], dist[n], LINF);
priority_queue<T, vector<T>, greater<T>> que;
for (int j = 0;; j++) {
int nxt = min(s + c[0] * j, (ll)MAX);
dist[0][nxt] = d[0] * j;
que.emplace(dist[0][nxt], nxt, 0);
if (s + c[0] * j > MAX)
break;
}
while (!que.empty()) {
T t = que.top();
que.pop();
int cnt = get<1>(t), v = get<2>(t);
if (dist[v][cnt] < get<0>(t))
continue;
for (int i = 0; i < G[v].size(); i++) {
int to = get<0>(G[v][i]), need = get<1>(G[v][i]);
ll cost = get<2>(G[v][i]);
for (int j = 0;; j++) {
if (cnt - need < 0)
break;
int nxt = min(cnt - need + c[to] * j, (ll)MAX);
if (dist[to][nxt] > dist[v][cnt] + cost + d[to] * j) {
dist[to][nxt] = dist[v][cnt] + cost + d[to] * j;
que.emplace(dist[to][nxt], nxt, to);
}
if (cnt - need + c[to] * j > MAX)
break;
}
}
}
for (int v = 1; v < n; v++) {
ll ans = LINF;
for (int j = 0; j <= MAX; j++) {
ans = min(ans, dist[v][j]);
}
cout << ans << endl;
}
return 0;
} | replace | 10 | 11 | 10 | 11 | TLE | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t V, E, s, A = 0;
cin >> V >> E >> s;
vector<vector<tuple<int64_t, int64_t, int64_t>>> G(V);
for (int i = 0; i < E; i++) {
int64_t a, b, c;
int64_t d;
cin >> a >> b >> c >> d;
a--, b--;
G.at(a).push_back(make_tuple(b, c, d));
G.at(b).push_back(make_tuple(a, c, d));
A = max(A, c);
}
vector<vector<int64_t>> r(V, vector<int64_t>(A * V + 1, -1));
vector<int64_t> z(V, -1);
vector<pair<int64_t, int64_t>> T(V);
for (int i = 0; i < V; i++) {
int64_t c;
int64_t d;
cin >> c >> d;
T.at(i) = make_pair(c, d);
}
priority_queue<tuple<int64_t, int64_t, int64_t>,
vector<tuple<int64_t, int64_t, int64_t>>,
greater<tuple<int64_t, int64_t, int64_t>>>
pq;
pq.push(make_tuple(0, 0, s));
r.at(0).at(s) = 0;
while (!pq.empty()) {
int64_t t, a, m;
tie(t, a, m) = pq.top();
pq.pop();
if (r.at(a).at(m) < t) {
continue;
}
if (z.at(a) == -1) {
z.at(a) = t;
}
for (tuple<int64_t, int64_t, int64_t> x : G.at(a)) {
int64_t b, g, k;
tie(b, g, k) = x;
if (m >= g) {
if (r.at(b).at(m - g) == -1 || r.at(b).at(m - g) > t + k) {
r.at(b).at(m - g) = t + k;
pq.push(make_tuple(t + k, b, m - g));
}
}
}
if (r.at(a).at(min(A * V, m + T.at(a).first)) > t + T.at(a).second ||
r.at(a).at(min(A * V, m + T.at(a).first)) == -1) {
r.at(a).at(min(A * V, m + T.at(a).first)) = t + T.at(a).second;
pq.push(make_tuple(t + T.at(a).second, a, min(A * V, m + T.at(a).first)));
}
}
for (int i = 1; i < V; i++) {
cout << z.at(i) << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t V, E, s, A = 0;
cin >> V >> E >> s;
vector<vector<tuple<int64_t, int64_t, int64_t>>> G(V);
for (int i = 0; i < E; i++) {
int64_t a, b, c;
int64_t d;
cin >> a >> b >> c >> d;
a--, b--;
G.at(a).push_back(make_tuple(b, c, d));
G.at(b).push_back(make_tuple(a, c, d));
A = max(A, c);
}
vector<vector<int64_t>> r(V, vector<int64_t>(A * V + 1, -1));
vector<int64_t> z(V, -1);
vector<pair<int64_t, int64_t>> T(V);
for (int i = 0; i < V; i++) {
int64_t c;
int64_t d;
cin >> c >> d;
T.at(i) = make_pair(c, d);
}
if (s > A * V) {
s = A * V;
}
priority_queue<tuple<int64_t, int64_t, int64_t>,
vector<tuple<int64_t, int64_t, int64_t>>,
greater<tuple<int64_t, int64_t, int64_t>>>
pq;
pq.push(make_tuple(0, 0, s));
r.at(0).at(s) = 0;
while (!pq.empty()) {
int64_t t, a, m;
tie(t, a, m) = pq.top();
pq.pop();
if (r.at(a).at(m) < t) {
continue;
}
if (z.at(a) == -1) {
z.at(a) = t;
}
for (tuple<int64_t, int64_t, int64_t> x : G.at(a)) {
int64_t b, g, k;
tie(b, g, k) = x;
if (m >= g) {
if (r.at(b).at(m - g) == -1 || r.at(b).at(m - g) > t + k) {
r.at(b).at(m - g) = t + k;
pq.push(make_tuple(t + k, b, m - g));
}
}
}
if (r.at(a).at(min(A * V, m + T.at(a).first)) > t + T.at(a).second ||
r.at(a).at(min(A * V, m + T.at(a).first)) == -1) {
r.at(a).at(min(A * V, m + T.at(a).first)) = t + T.at(a).second;
pq.push(make_tuple(t + T.at(a).second, a, min(A * V, m + T.at(a).first)));
}
}
for (int i = 1; i < V; i++) {
cout << z.at(i) << endl;
}
}
| insert | 24 | 24 | 24 | 27 | 0 | |
p02703 | C++ | Runtime Error | // #include <tourist>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v) \
cout << #v << ":"; \
for (auto x : v) { \
cout << x << ' '; \
} \
cout << endl;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
// cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//-std=gnu++17
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// ダイクストラ
// LINFにしてあるよ
struct edge {
ll to, cost;
};
typedef pair<ll, ll> P;
struct graph {
ll V;
vector<vector<edge>> G;
vector<ll> d;
// vector<ll> count;経路の本数
// vector<ll> prev; // prev[v] := v から復元できる辺たち
graph(ll n) { init(n); }
void init(ll n) {
V = n;
G.resize(V);
d.resize(V);
// count.resize(V);
rep(i, V) {
d[i] = LINF;
// count[i] = 0;
}
}
void add_edge(ll s, ll t, ll cost) {
edge e;
e.to = t, e.cost = cost;
G[s].push_back(e);
}
void dijkstra(ll s) {
rep(i, V) {
d[i] = LINF;
// count[i] = 0;
}
d[s] = 0;
// count[s] = 1;
priority_queue<P, vector<P>, greater<P>> que;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
ll v = p.second;
if (d[v] < p.first)
continue;
for (auto e : G[v]) {
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
// count[e.to] = count[v];
// 復元のための
// prev[e.to]=v;
}
/*
else if (d[e.to] == d[v] + e.cost)
{
count[e.to] += count[v];
count[e.to] %= MOD;
}
*/
}
}
}
};
ll n, m, s;
vector<ll> u, v, a, b, c, d;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m >> s;
u.resize(m);
v.resize(m);
a.resize(m);
b.resize(m);
c.resize(n);
d.resize(n);
graph g(n * 3001);
rep(i, m) {
cin >> u[i] >> v[i] >> a[i] >> b[i];
u[i]--;
v[i]--;
rep(j, 3000 - a[i]) {
g.add_edge(u[i] + n * (j + a[i]), v[i] + j * n, b[i]);
g.add_edge(v[i] + n * (j + a[i]), u[i] + j * n, b[i]);
}
}
rep(i, n) {
cin >> c[i] >> d[i];
rep(j, 3000) {
g.add_edge(ll(i) + j * n, ll(i) + min(c[i] + j, ll(3000)) * n, d[i]);
}
}
g.dijkstra(0 + s * n);
for (int i = 1; i < n; i++) {
ll ans = LINF;
rep(j, 3000) { chmin(ans, g.d[i + n * j]); }
cout << ans << "\n";
}
}
| // #include <tourist>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v) \
cout << #v << ":"; \
for (auto x : v) { \
cout << x << ' '; \
} \
cout << endl;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
// cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//-std=gnu++17
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// ダイクストラ
// LINFにしてあるよ
struct edge {
ll to, cost;
};
typedef pair<ll, ll> P;
struct graph {
ll V;
vector<vector<edge>> G;
vector<ll> d;
// vector<ll> count;経路の本数
// vector<ll> prev; // prev[v] := v から復元できる辺たち
graph(ll n) { init(n); }
void init(ll n) {
V = n;
G.resize(V);
d.resize(V);
// count.resize(V);
rep(i, V) {
d[i] = LINF;
// count[i] = 0;
}
}
void add_edge(ll s, ll t, ll cost) {
edge e;
e.to = t, e.cost = cost;
G[s].push_back(e);
}
void dijkstra(ll s) {
rep(i, V) {
d[i] = LINF;
// count[i] = 0;
}
d[s] = 0;
// count[s] = 1;
priority_queue<P, vector<P>, greater<P>> que;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
ll v = p.second;
if (d[v] < p.first)
continue;
for (auto e : G[v]) {
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
// count[e.to] = count[v];
// 復元のための
// prev[e.to]=v;
}
/*
else if (d[e.to] == d[v] + e.cost)
{
count[e.to] += count[v];
count[e.to] %= MOD;
}
*/
}
}
}
};
ll n, m, s;
vector<ll> u, v, a, b, c, d;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m >> s;
s = min(s, ll(2900));
u.resize(m);
v.resize(m);
a.resize(m);
b.resize(m);
c.resize(n);
d.resize(n);
graph g(n * 3001);
rep(i, m) {
cin >> u[i] >> v[i] >> a[i] >> b[i];
u[i]--;
v[i]--;
rep(j, 3000 - a[i]) {
g.add_edge(u[i] + n * (j + a[i]), v[i] + j * n, b[i]);
g.add_edge(v[i] + n * (j + a[i]), u[i] + j * n, b[i]);
}
}
rep(i, n) {
cin >> c[i] >> d[i];
rep(j, 3000) {
g.add_edge(ll(i) + j * n, ll(i) + min(c[i] + j, ll(3000)) * n, d[i]);
}
}
g.dijkstra(0 + s * n);
for (int i = 1; i < n; i++) {
ll ans = LINF;
rep(j, 3000) { chmin(ans, g.d[i + n * j]); }
cout << ans << "\n";
}
}
| insert | 113 | 113 | 113 | 114 | 0 | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define INF_LL 1000000000000000LL
#define INF 200000000
#define MOD 1000000007
#define ll long long
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// typedef float double;
// typedef priority_queue prique;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<ll> vl;
typedef vector<vi> matrix;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sign[2] = {1, -1};
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
ll modpow(ll a, ll b, ll m) {
if (b == 0)
return 1;
ll t = modpow(a, b / 2, m);
if (b & 1) {
return (t * t % m) * a % m;
} else {
return t * t % m;
}
}
struct edge {
int to;
ll cost;
edge(int t, ll c) { to = t, cost = c; }
};
#define MAX_S 5000LL
vector<ll> dijkstra(vector<vector<edge>> &g, int start, int n) {
priority_queue<P, vector<P>, greater<P>> que;
vector<ll> dist(n, INF_LL);
dist[start] = 0;
que.push(P(0, start));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first)
continue;
rep(i, (int)g[v].size()) {
edge e = g[v][i];
if (dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
que.push(P(dist[e.to], e.to));
}
}
}
return dist;
}
int main() {
int n, m, s;
cin >> n >> m >> s;
vector<vector<edge>> g(n * (MAX_S + 1));
rep(i, m) {
int u, v, a;
ll b;
cin >> u >> v >> a >> b;
REP(j, a, MAX_S + 1) {
g[(u - 1) * (MAX_S + 1) + j].push_back(
edge((v - 1) * (MAX_S + 1) + j - a, b));
g[(v - 1) * (MAX_S + 1) + j].push_back(
edge((u - 1) * (MAX_S + 1) + j - a, b));
}
}
rep(i, n) {
ll c, d;
cin >> c >> d;
rep(j, MAX_S) {
g[i * (MAX_S + 1) + j].push_back(
edge(i * (MAX_S + 1) + min(j + c, MAX_S), d));
}
}
vl d(n * (MAX_S + 1));
d = dijkstra(g, min((ll)s, MAX_S), n * (MAX_S + 1));
REP(j, 1, n) {
ll ans = INF_LL;
rep(i, MAX_S + 1) { chmin(ans, d[j * (MAX_S + 1) + i]); }
cout << ans << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define INF_LL 1000000000000000LL
#define INF 200000000
#define MOD 1000000007
#define ll long long
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// typedef float double;
// typedef priority_queue prique;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<ll> vl;
typedef vector<vi> matrix;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sign[2] = {1, -1};
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
ll modpow(ll a, ll b, ll m) {
if (b == 0)
return 1;
ll t = modpow(a, b / 2, m);
if (b & 1) {
return (t * t % m) * a % m;
} else {
return t * t % m;
}
}
struct edge {
int to;
ll cost;
edge(int t, ll c) { to = t, cost = c; }
};
#define MAX_S 2500LL
vector<ll> dijkstra(vector<vector<edge>> &g, int start, int n) {
priority_queue<P, vector<P>, greater<P>> que;
vector<ll> dist(n, INF_LL);
dist[start] = 0;
que.push(P(0, start));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first)
continue;
rep(i, (int)g[v].size()) {
edge e = g[v][i];
if (dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
que.push(P(dist[e.to], e.to));
}
}
}
return dist;
}
int main() {
int n, m, s;
cin >> n >> m >> s;
vector<vector<edge>> g(n * (MAX_S + 1));
rep(i, m) {
int u, v, a;
ll b;
cin >> u >> v >> a >> b;
REP(j, a, MAX_S + 1) {
g[(u - 1) * (MAX_S + 1) + j].push_back(
edge((v - 1) * (MAX_S + 1) + j - a, b));
g[(v - 1) * (MAX_S + 1) + j].push_back(
edge((u - 1) * (MAX_S + 1) + j - a, b));
}
}
rep(i, n) {
ll c, d;
cin >> c >> d;
rep(j, MAX_S) {
g[i * (MAX_S + 1) + j].push_back(
edge(i * (MAX_S + 1) + min(j + c, MAX_S), d));
}
}
vl d(n * (MAX_S + 1));
d = dijkstra(g, min((ll)s, MAX_S), n * (MAX_S + 1));
REP(j, 1, n) {
ll ans = INF_LL;
rep(i, MAX_S + 1) { chmin(ans, d[j * (MAX_S + 1) + i]); }
cout << ans << endl;
}
return 0;
}
| replace | 51 | 52 | 51 | 52 | TLE | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, int> PLI;
const int MN = 50;
const int MM = 100;
const int MA = 51;
const int MC = 2 * MN * MA;
const int MV = MN * MC + 5;
const LL INF = 0x3f3f3f3f3f3f3f3f;
// sp at most go through less tahn 2n node 2n-1 edges?
// V = 50*2*50*50=250 000
// E = 100*2*50*50=500 000
vector<PII> g[MV];
int n, m, s;
inline int id(int u, int val) { return u * (MC) + val; }
LL dis[MV];
int vis[MV];
priority_queue<PLI, vector<PLI>, greater<PLI>> qu;
void dij(int st, int v) {
memset(dis, INF, sizeof(dis));
qu.push({0, id(st, v)});
dis[id(st, v)] = 0;
while (!qu.empty()) {
PLI p = qu.top();
qu.pop();
int u = p.second;
LL d = dis[u];
if (vis[u])
continue;
vis[u] = 1;
for (auto e : g[u]) {
int v = e.first;
LL w = e.second;
if (vis[v])
continue;
if (dis[v] > d + w) {
dis[v] = d + w;
qu.push({dis[v], v});
}
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &s);
int u, v, a, b;
for (int i = 0; i < m; i++) {
scanf("%d%d%d%d", &u, &v, &a, &b);
for (int i = 0; i + a < MC; i++) {
g[id(u, i + a)].push_back({id(v, i), b});
g[id(v, i + a)].push_back({id(u, i), b});
}
}
for (int i = 1; i <= n; i++) {
scanf("%d%d", &a, &b);
for (int j = 0; j + a < MC; j++)
g[id(i, j)].push_back({id(i, j + a), b});
}
if (s >= MC)
s = MC - 1;
dij(1, s);
for (int i = 2; i <= n; i++) {
LL mi = INF;
for (int j = 0; j < MC; j++)
mi = min(mi, dis[id(i, j)]);
printf("%lld\n", mi);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, int> PLI;
const int MN = 51;
const int MM = 101;
const int MA = 51;
const int MC = 2 * MN * MA;
const int MV = MN * MC + 5;
const LL INF = 0x3f3f3f3f3f3f3f3f;
// sp at most go through less tahn 2n node 2n-1 edges?
// V = 50*2*50*50=250 000
// E = 100*2*50*50=500 000
vector<PII> g[MV];
int n, m, s;
inline int id(int u, int val) { return u * (MC) + val; }
LL dis[MV];
int vis[MV];
priority_queue<PLI, vector<PLI>, greater<PLI>> qu;
void dij(int st, int v) {
memset(dis, INF, sizeof(dis));
qu.push({0, id(st, v)});
dis[id(st, v)] = 0;
while (!qu.empty()) {
PLI p = qu.top();
qu.pop();
int u = p.second;
LL d = dis[u];
if (vis[u])
continue;
vis[u] = 1;
for (auto e : g[u]) {
int v = e.first;
LL w = e.second;
if (vis[v])
continue;
if (dis[v] > d + w) {
dis[v] = d + w;
qu.push({dis[v], v});
}
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &s);
int u, v, a, b;
for (int i = 0; i < m; i++) {
scanf("%d%d%d%d", &u, &v, &a, &b);
for (int i = 0; i + a < MC; i++) {
g[id(u, i + a)].push_back({id(v, i), b});
g[id(v, i + a)].push_back({id(u, i), b});
}
}
for (int i = 1; i <= n; i++) {
scanf("%d%d", &a, &b);
for (int j = 0; j + a < MC; j++)
g[id(i, j)].push_back({id(i, j + a), b});
}
if (s >= MC)
s = MC - 1;
dij(1, s);
for (int i = 2; i <= n; i++) {
LL mi = INF;
for (int j = 0; j < MC; j++)
mi = min(mi, dis[id(i, j)]);
printf("%lld\n", mi);
}
return 0;
} | replace | 6 | 8 | 6 | 8 | 0 | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, (n)-1, 0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
struct edge {
int to;
ll cost;
edge(int t, ll c) : to(t), cost(c){};
};
using P = pair<ll, int>;
vector<ll> d;
vector<int> par;
void dijkstra(int s, int N, vector<vector<edge>> &G) {
priority_queue<P, vector<P>, greater<P>> que;
d = vector<ll>(N, longinf);
par = vector<int>(N, -1);
d[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (int i = 0; i < (int)G[v].size(); i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
par[e.to] = v;
}
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
ll s;
cin >> n >> m >> s;
int mx = 100000;
vector<int> U(m), V(m), A(m);
vector<ll> B(m), C(n), D(n);
rep(i, m) {
cin >> U[i] >> V[i] >> A[i] >> B[i];
U[i]--;
V[i]--;
}
rep(i, n) cin >> C[i] >> D[i];
vector<vector<edge>> G(n * mx);
rep(i, m) {
rep(j, mx) {
if (j < A[i])
continue;
G[U[i] * mx + j].push_back({V[i] * mx + j - A[i], B[i]});
G[V[i] * mx + j].push_back({U[i] * mx + j - A[i], B[i]});
}
}
rep(i, n) {
rep(j, mx) {
G[i * mx + j].push_back({i * mx + (int)min(j + C[i], (ll)mx - 1), D[i]});
}
}
dijkstra((int)min(s, (ll)mx - 1), n * mx, G);
rep(i, n) {
if (i == 0)
continue;
cout << *min_element(d.begin() + i * mx, d.begin() + (i + 1) * mx) << endk;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, (n)-1, 0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
struct edge {
int to;
ll cost;
edge(int t, ll c) : to(t), cost(c){};
};
using P = pair<ll, int>;
vector<ll> d;
vector<int> par;
void dijkstra(int s, int N, vector<vector<edge>> &G) {
priority_queue<P, vector<P>, greater<P>> que;
d = vector<ll>(N, longinf);
par = vector<int>(N, -1);
d[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (int i = 0; i < (int)G[v].size(); i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
par[e.to] = v;
}
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
ll s;
cin >> n >> m >> s;
int mx = 10000;
vector<int> U(m), V(m), A(m);
vector<ll> B(m), C(n), D(n);
rep(i, m) {
cin >> U[i] >> V[i] >> A[i] >> B[i];
U[i]--;
V[i]--;
}
rep(i, n) cin >> C[i] >> D[i];
vector<vector<edge>> G(n * mx);
rep(i, m) {
rep(j, mx) {
if (j < A[i])
continue;
G[U[i] * mx + j].push_back({V[i] * mx + j - A[i], B[i]});
G[V[i] * mx + j].push_back({U[i] * mx + j - A[i], B[i]});
}
}
rep(i, n) {
rep(j, mx) {
G[i * mx + j].push_back({i * mx + (int)min(j + C[i], (ll)mx - 1), D[i]});
}
}
dijkstra((int)min(s, (ll)mx - 1), n * mx, G);
rep(i, n) {
if (i == 0)
continue;
cout << *min_element(d.begin() + i * mx, d.begin() + (i + 1) * mx) << endk;
}
return 0;
}
| replace | 63 | 64 | 63 | 64 | TLE | |
p02703 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
long long d[50][2501];
long long C[50];
long long D[50];
vector<pair<pair<long long, long long>, int>> edges[50];
void dijikstra(int S, int N, long long max_A, long long S_coin) {
for (int i = 0; i < N; i++) {
for (int j = 0; j <= max_A; j++) {
d[i][j] = LLONG_MAX;
}
}
priority_queue<pair<long long, pair<long long, int>>,
vector<pair<long long, pair<long long, int>>>,
greater<pair<long long, pair<long long, int>>>>
pq;
d[S][min(max_A, S_coin)] = 0;
pq.push(make_pair(0, make_pair(min(max_A, S_coin), S)));
while (!pq.empty()) {
auto cur = pq.top();
long long cur_cost = cur.first;
int cur_node = cur.second.second;
long long cur_coin_no = cur.second.first;
pq.pop();
// cout << cur_node << " " << cur_coin_no << endl;
if (d[cur_node][cur_coin_no] < cur_cost) {
continue;
}
long long next_coin_no;
if (cur_coin_no + C[cur_node] <= max_A) {
next_coin_no = cur_coin_no + C[cur_node];
} else {
next_coin_no = max_A;
}
long long next_cost = cur_cost + D[cur_node];
if (d[cur_node][next_coin_no] > next_cost) {
d[cur_node][next_coin_no] = next_cost;
pq.push(make_pair(next_cost, make_pair(next_coin_no, cur_node)));
}
for (int i = 0; i < edges[cur_node].size(); i++) {
auto next = edges[cur_node][i];
auto next_node = next.second;
auto cost = next.first.second;
auto coin_cost = next.first.first;
long long next_d = d[cur_node][cur_coin_no] + cost;
long long next_coin_no = cur_coin_no - coin_cost;
if (next_coin_no >= 0 && d[next_node][next_coin_no] > next_d) {
d[next_node][next_coin_no] = next_d;
pq.push(make_pair(next_d, make_pair(next_coin_no, next_node)));
}
}
}
}
int main() {
int N;
int M;
long long S;
cin >> N >> M >> S;
long long max_A = 0;
for (int i = 0; i < M; i++) {
long long U;
long long V;
long long A;
long long B;
cin >> U >> V >> A >> B;
edges[U - 1].push_back(make_pair(make_pair(A, B), V - 1));
edges[V - 1].push_back(make_pair(make_pair(A, B), U - 1));
max_A += A;
}
for (int i = 0; i < N; i++) {
cin >> C[i] >> D[i];
}
dijikstra(0, N, max_A, S);
for (int i = 1; i < N; i++) {
long long ans = LLONG_MAX;
for (int j = 0; j <= max_A; j++) {
ans = min(ans, d[i][j]);
}
cout << ans << endl;
}
/*
for(int i = 0; i < N; i++){
for(int j = 0; j <= max_A; j++){
cout << d[i][j] << " ";
}
cout << endl;
}
*/
return 0;
}
| #include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
long long d[50][5001];
long long C[50];
long long D[50];
vector<pair<pair<long long, long long>, int>> edges[50];
void dijikstra(int S, int N, long long max_A, long long S_coin) {
for (int i = 0; i < N; i++) {
for (int j = 0; j <= max_A; j++) {
d[i][j] = LLONG_MAX;
}
}
priority_queue<pair<long long, pair<long long, int>>,
vector<pair<long long, pair<long long, int>>>,
greater<pair<long long, pair<long long, int>>>>
pq;
d[S][min(max_A, S_coin)] = 0;
pq.push(make_pair(0, make_pair(min(max_A, S_coin), S)));
while (!pq.empty()) {
auto cur = pq.top();
long long cur_cost = cur.first;
int cur_node = cur.second.second;
long long cur_coin_no = cur.second.first;
pq.pop();
// cout << cur_node << " " << cur_coin_no << endl;
if (d[cur_node][cur_coin_no] < cur_cost) {
continue;
}
long long next_coin_no;
if (cur_coin_no + C[cur_node] <= max_A) {
next_coin_no = cur_coin_no + C[cur_node];
} else {
next_coin_no = max_A;
}
long long next_cost = cur_cost + D[cur_node];
if (d[cur_node][next_coin_no] > next_cost) {
d[cur_node][next_coin_no] = next_cost;
pq.push(make_pair(next_cost, make_pair(next_coin_no, cur_node)));
}
for (int i = 0; i < edges[cur_node].size(); i++) {
auto next = edges[cur_node][i];
auto next_node = next.second;
auto cost = next.first.second;
auto coin_cost = next.first.first;
long long next_d = d[cur_node][cur_coin_no] + cost;
long long next_coin_no = cur_coin_no - coin_cost;
if (next_coin_no >= 0 && d[next_node][next_coin_no] > next_d) {
d[next_node][next_coin_no] = next_d;
pq.push(make_pair(next_d, make_pair(next_coin_no, next_node)));
}
}
}
}
int main() {
int N;
int M;
long long S;
cin >> N >> M >> S;
long long max_A = 0;
for (int i = 0; i < M; i++) {
long long U;
long long V;
long long A;
long long B;
cin >> U >> V >> A >> B;
edges[U - 1].push_back(make_pair(make_pair(A, B), V - 1));
edges[V - 1].push_back(make_pair(make_pair(A, B), U - 1));
max_A += A;
}
for (int i = 0; i < N; i++) {
cin >> C[i] >> D[i];
}
dijikstra(0, N, max_A, S);
for (int i = 1; i < N; i++) {
long long ans = LLONG_MAX;
for (int j = 0; j <= max_A; j++) {
ans = min(ans, d[i][j]);
}
cout << ans << endl;
}
/*
for(int i = 0; i < N; i++){
for(int j = 0; j <= max_A; j++){
cout << d[i][j] << " ";
}
cout << endl;
}
*/
return 0;
}
| replace | 14 | 15 | 14 | 15 | 0 | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define int long long
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
using namespace std;
const int N = 55;
const int MAX_S = 2501;
vector<tuple<int, int, int>> edges[N];
int n, m, S;
vector<pair<int, int>> _exchange;
void dijkstra(int s) {
vector<vector<ll>> dist(n, vector<ll>(MAX_S, 1e15));
dist[s][S] = 0;
priority_queue<pair<ll, pair<int, int>>, vector<pair<ll, pair<int, int>>>,
greater<pair<ll, pair<int, int>>>>
pq; //{dist,{src,silver_coins}}
pq.push({0, {s, S}});
while (!pq.empty()) {
int curr_dist = pq.top().first;
int node = pq.top().second.first;
int silver = pq.top().second.second;
pq.pop();
if (dist[node][silver] < curr_dist)
continue;
for (auto x : edges[node]) {
if (get<1>(x) <= silver) {
if (dist[get<0>(x)][silver - get<1>(x)] >
dist[node][silver] + get<2>(x)) {
dist[get<0>(x)][silver - get<1>(x)] = dist[node][silver] + get<2>(x);
pq.push({dist[get<0>(x)][silver - get<1>(x)],
{get<0>(x), silver - get<1>(x)}});
}
}
}
int new_s = min(MAX_S - 1, silver + _exchange[node].first);
if (dist[node][new_s] > dist[node][silver] + _exchange[node].second) {
dist[node][new_s] = dist[node][silver] + _exchange[node].second;
pq.push({dist[node][new_s], {node, new_s}});
}
}
for (int i = 1; i < n; i++) {
int ans = 1e15;
for (int k = 0; k < MAX_S; k++) {
ans = min(ans, dist[i][k]);
}
cout << ans << "\n";
}
}
signed main() {
fastio cin >> n >> m >> S;
S = min(S, MAX_S); // check with bigger limits
for (int i = 0; i < m; i++) {
int u, v, cost_req, time_req;
cin >> u >> v >> cost_req >> time_req;
u--;
v--;
edges[u].emplace_back(v, cost_req, time_req);
edges[v].emplace_back(u, cost_req, time_req);
}
for (int i = 0; i < n; i++) {
int silver, time;
cin >> silver >> time;
_exchange.emplace_back(silver, time);
}
dijkstra(0);
return 0;
} | #include <bits/stdc++.h>
#define ll long long
#define int long long
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
using namespace std;
const int N = 55;
const int MAX_S = 4000;
vector<tuple<int, int, int>> edges[N];
int n, m, S;
vector<pair<int, int>> _exchange;
void dijkstra(int s) {
vector<vector<ll>> dist(n, vector<ll>(MAX_S, 1e15));
dist[s][S] = 0;
priority_queue<pair<ll, pair<int, int>>, vector<pair<ll, pair<int, int>>>,
greater<pair<ll, pair<int, int>>>>
pq; //{dist,{src,silver_coins}}
pq.push({0, {s, S}});
while (!pq.empty()) {
int curr_dist = pq.top().first;
int node = pq.top().second.first;
int silver = pq.top().second.second;
pq.pop();
if (dist[node][silver] < curr_dist)
continue;
for (auto x : edges[node]) {
if (get<1>(x) <= silver) {
if (dist[get<0>(x)][silver - get<1>(x)] >
dist[node][silver] + get<2>(x)) {
dist[get<0>(x)][silver - get<1>(x)] = dist[node][silver] + get<2>(x);
pq.push({dist[get<0>(x)][silver - get<1>(x)],
{get<0>(x), silver - get<1>(x)}});
}
}
}
int new_s = min(MAX_S - 1, silver + _exchange[node].first);
if (dist[node][new_s] > dist[node][silver] + _exchange[node].second) {
dist[node][new_s] = dist[node][silver] + _exchange[node].second;
pq.push({dist[node][new_s], {node, new_s}});
}
}
for (int i = 1; i < n; i++) {
int ans = 1e15;
for (int k = 0; k < MAX_S; k++) {
ans = min(ans, dist[i][k]);
}
cout << ans << "\n";
}
}
signed main() {
fastio cin >> n >> m >> S;
S = min(S, MAX_S); // check with bigger limits
for (int i = 0; i < m; i++) {
int u, v, cost_req, time_req;
cin >> u >> v >> cost_req >> time_req;
u--;
v--;
edges[u].emplace_back(v, cost_req, time_req);
edges[v].emplace_back(u, cost_req, time_req);
}
for (int i = 0; i < n; i++) {
int silver, time;
cin >> silver >> time;
_exchange.emplace_back(silver, time);
}
dijkstra(0);
return 0;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p02703 | C++ | Runtime Error | typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
struct point {
ll p; // point
ll s; // silver
bool operator<(const point rhs) const {
if (p != rhs.p) {
return p < rhs.p;
} else {
return s < rhs.s;
}
}
};
int main() {
ll n, m, s;
std::cin >> n >> m >> s;
vector<ll> u(m), v(m), a(m), b(m), c(n), d(n);
vector<vector<pair<point, ll>>> edges(2500 * 53);
ll amx = 0;
for (int i = 0; i < m; i++) {
std::cin >> u[i] >> v[i] >> a[i] >> b[i];
u[i]--;
v[i]--;
amx = max(amx, a[i]);
for (int j = 0; j <= 2500; j++) {
if (50 * j - a[i] < 0)
continue;
edges[u[i] + 50 * j].push_back({{v[i], j - a[i]}, b[i]});
edges[v[i] + 50 * j].push_back({{u[i], j - a[i]}, b[i]});
}
}
for (int i = 0; i < n; i++) {
std::cin >> c[i] >> d[i];
for (int j = 0; j <= 2500; j++) {
// std::cout << j << std::endl;
edges[i + 50 * j].push_back({{i, j + c[i]}, d[i]});
if ((j + c[i]) >= 2500)
break;
}
}
s = min(s, amx * n);
typedef pair<ll, point> P;
// map<point,ll> dd;
vector<vector<ll>> dd(n, vector<ll>(2510, 1e18));
priority_queue<P, vector<P>, greater<P>> que;
que.push({0, {0, s}});
for (int i = 0; i <= s; i++) {
dd[0][i] = 0;
}
while (!que.empty()) {
P p = que.top();
que.pop();
point v = p.second;
if (dd[v.p][min(2500ll, v.s)] < p.first)
continue;
for (auto e : edges[v.p + 50 * min(2500ll, v.s)]) {
auto [po, co] = e;
if (dd[po.p][min(2500ll, po.s)] > dd[v.p][min(2500ll, v.s)] + co) {
dd[po.p][min(2500ll, po.s)] = dd[v.p][min(2500ll, v.s)] + co;
que.push({dd[po.p][min(2500ll, po.s)], po});
}
}
}
for (int i = 1; i < n; i++) {
ll tmp = 1e18;
for (int j = 0; j <= amx * n; j++) {
tmp = min(tmp, dd[i][j]);
}
std::cout << tmp << std::endl;
}
}
| typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
struct point {
ll p; // point
ll s; // silver
bool operator<(const point rhs) const {
if (p != rhs.p) {
return p < rhs.p;
} else {
return s < rhs.s;
}
}
};
int main() {
ll n, m, s;
std::cin >> n >> m >> s;
vector<ll> u(m), v(m), a(m), b(m), c(n), d(n);
vector<vector<pair<point, ll>>> edges(2500 * 53);
ll amx = 0;
for (int i = 0; i < m; i++) {
std::cin >> u[i] >> v[i] >> a[i] >> b[i];
u[i]--;
v[i]--;
amx = max(amx, a[i]);
for (int j = 0; j <= 2500; j++) {
if (j - a[i] < 0)
continue;
edges[u[i] + 50 * j].push_back({{v[i], j - a[i]}, b[i]});
edges[v[i] + 50 * j].push_back({{u[i], j - a[i]}, b[i]});
}
}
for (int i = 0; i < n; i++) {
std::cin >> c[i] >> d[i];
for (int j = 0; j <= 2500; j++) {
// std::cout << j << std::endl;
edges[i + 50 * j].push_back({{i, j + c[i]}, d[i]});
if ((j + c[i]) >= 2500)
break;
}
}
s = min(s, amx * n);
typedef pair<ll, point> P;
// map<point,ll> dd;
vector<vector<ll>> dd(n, vector<ll>(2510, 1e18));
priority_queue<P, vector<P>, greater<P>> que;
que.push({0, {0, s}});
for (int i = 0; i <= s; i++) {
dd[0][i] = 0;
}
while (!que.empty()) {
P p = que.top();
que.pop();
point v = p.second;
if (dd[v.p][min(2500ll, v.s)] < p.first)
continue;
for (auto e : edges[v.p + 50 * min(2500ll, v.s)]) {
auto [po, co] = e;
if (dd[po.p][min(2500ll, po.s)] > dd[v.p][min(2500ll, v.s)] + co) {
dd[po.p][min(2500ll, po.s)] = dd[v.p][min(2500ll, v.s)] + co;
que.push({dd[po.p][min(2500ll, po.s)], po});
}
}
}
for (int i = 1; i < n; i++) {
ll tmp = 1e18;
for (int j = 0; j <= amx * n; j++) {
tmp = min(tmp, dd[i][j]);
}
std::cout << tmp << std::endl;
}
}
| replace | 32 | 33 | 32 | 33 | -11 | |
p02703 | C++ | Runtime Error | #pragma region template
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
template <typename T> using pqrev = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for (ll i = 0, i##_end = (n); i < i##_end; i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repr(i, a, b) for (ll i = (a), i##_end = (b); i < i##_end; i++)
#define reprb(i, a, b) for (ll i = (b)-1, i##_end = (a); i >= i##_end; i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
//*
constexpr ll MOD = 1e9 + 7;
/*/
constexpr ll MOD = 998244353;
//*/
constexpr ll INF = 1e+18;
constexpr ld EPS = 1e-12L;
constexpr ld PI = 3.14159265358979323846L;
constexpr ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
constexpr ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
template <typename S, typename T> constexpr bool chmax(S &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename S, typename T> constexpr bool chmin(S &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
template <typename T> bool print_(const T &a) {
cout << a;
return true;
}
template <typename T> bool print_(const vector<T> &vec) {
for (auto &a : vec) {
cout << a;
if (&a != &vec.back()) {
cout << " ";
}
}
return false;
}
template <typename T> bool print_(const vector<vector<T>> &vv) {
for (auto &v : vv) {
for (auto &a : v) {
cout << a;
if (&a != &v.back()) {
cout << " ";
}
}
if (&v != &vv.back()) {
cout << "\n";
}
}
return false;
}
void print() { cout << "\n"; }
template <typename Head, typename... Tail>
void print(Head &&head, Tail &&...tail) {
bool f = print_(head);
if (sizeof...(tail) != 0) {
cout << (f ? " " : "\n");
}
print(forward<Tail>(tail)...);
}
#pragma endregion
template <typename T> struct Edge {
int from, to;
T cost;
int id;
Edge(int from_, int to_, T cost_, int id_)
: from(from_), to(to_), cost(cost_), id(id_) {}
Edge(int from_, int to_, T cost_) : from(from_), to(to_), cost(cost_) {}
Edge(int from_, int to_) : from(from_), to(to_), cost(1) {}
bool operator<(const Edge<T> &r) { return cost < r.cost; }
};
template <typename T> ostream &operator<<(ostream &os, Edge<T> edge) {
os << edge.from << " -> " << edge.to << " (" << edge.cost << ")";
return os;
}
// グラフテンプレート(隣接リスト)
template <typename E = Edge<ll>> struct GraphL {
// 頂点数、辺数
int n, m;
// 隣接リスト
vector<vector<E>> adj;
GraphL(int n_) : n(n_), m(0), adj(n_) {}
template <typename... Args> void add_edge(int from, int to, Args... args) {
adj[from].emplace_back(from, to, args...);
m++;
}
vector<E> &operator[](int i) { return adj[i]; }
};
template <typename E = Edge<ll>>
ostream &operator<<(ostream &os, GraphL<E> graph) {
os << "V = " << graph.n << ", E = " << graph.m << "\n";
for (const auto &ev : graph.adj) {
for (const auto &e : ev) {
os << e << "\n";
}
}
return os;
}
template <typename E> vector<ll> Dijkstra(GraphL<E> &graph, int start_node) {
using P = pair<ll, int>;
vector<ll> dist(graph.n, INF);
dist[start_node] = 0;
priority_queue<P, vector<P>, greater<P>> que;
que.emplace(0, start_node);
while (!que.empty()) {
P p = que.top();
que.pop();
ll v = p.second;
if (dist[v] < p.first) {
continue;
}
for (auto &&e : graph.adj[v]) {
if (dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
que.emplace(dist[e.to], e.to);
}
}
}
return dist;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll n, m, s;
cin >> n >> m >> s;
bool f = s < 2500;
if (f) {
return 11;
} else {
GraphL<> g(n);
rep(i, m) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
g.add_edge(u, v, b);
g.add_edge(v, u, b);
}
auto ans = Dijkstra(g, 0);
repr(i, 1, SZ(ans)) { print(ans[i]); }
}
}
| #pragma region template
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
template <typename T> using pqrev = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for (ll i = 0, i##_end = (n); i < i##_end; i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repr(i, a, b) for (ll i = (a), i##_end = (b); i < i##_end; i++)
#define reprb(i, a, b) for (ll i = (b)-1, i##_end = (a); i >= i##_end; i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
//*
constexpr ll MOD = 1e9 + 7;
/*/
constexpr ll MOD = 998244353;
//*/
constexpr ll INF = 1e+18;
constexpr ld EPS = 1e-12L;
constexpr ld PI = 3.14159265358979323846L;
constexpr ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
constexpr ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
template <typename S, typename T> constexpr bool chmax(S &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename S, typename T> constexpr bool chmin(S &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
template <typename T> bool print_(const T &a) {
cout << a;
return true;
}
template <typename T> bool print_(const vector<T> &vec) {
for (auto &a : vec) {
cout << a;
if (&a != &vec.back()) {
cout << " ";
}
}
return false;
}
template <typename T> bool print_(const vector<vector<T>> &vv) {
for (auto &v : vv) {
for (auto &a : v) {
cout << a;
if (&a != &v.back()) {
cout << " ";
}
}
if (&v != &vv.back()) {
cout << "\n";
}
}
return false;
}
void print() { cout << "\n"; }
template <typename Head, typename... Tail>
void print(Head &&head, Tail &&...tail) {
bool f = print_(head);
if (sizeof...(tail) != 0) {
cout << (f ? " " : "\n");
}
print(forward<Tail>(tail)...);
}
#pragma endregion
template <typename T> struct Edge {
int from, to;
T cost;
int id;
Edge(int from_, int to_, T cost_, int id_)
: from(from_), to(to_), cost(cost_), id(id_) {}
Edge(int from_, int to_, T cost_) : from(from_), to(to_), cost(cost_) {}
Edge(int from_, int to_) : from(from_), to(to_), cost(1) {}
bool operator<(const Edge<T> &r) { return cost < r.cost; }
};
template <typename T> ostream &operator<<(ostream &os, Edge<T> edge) {
os << edge.from << " -> " << edge.to << " (" << edge.cost << ")";
return os;
}
// グラフテンプレート(隣接リスト)
template <typename E = Edge<ll>> struct GraphL {
// 頂点数、辺数
int n, m;
// 隣接リスト
vector<vector<E>> adj;
GraphL(int n_) : n(n_), m(0), adj(n_) {}
template <typename... Args> void add_edge(int from, int to, Args... args) {
adj[from].emplace_back(from, to, args...);
m++;
}
vector<E> &operator[](int i) { return adj[i]; }
};
template <typename E = Edge<ll>>
ostream &operator<<(ostream &os, GraphL<E> graph) {
os << "V = " << graph.n << ", E = " << graph.m << "\n";
for (const auto &ev : graph.adj) {
for (const auto &e : ev) {
os << e << "\n";
}
}
return os;
}
template <typename E> vector<ll> Dijkstra(GraphL<E> &graph, int start_node) {
using P = pair<ll, int>;
vector<ll> dist(graph.n, INF);
dist[start_node] = 0;
priority_queue<P, vector<P>, greater<P>> que;
que.emplace(0, start_node);
while (!que.empty()) {
P p = que.top();
que.pop();
ll v = p.second;
if (dist[v] < p.first) {
continue;
}
for (auto &&e : graph.adj[v]) {
if (dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
que.emplace(dist[e.to], e.to);
}
}
}
return dist;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll n, m, s;
cin >> n >> m >> s;
bool f = s < 2500;
if (f) {
GraphL<> g(n * 5000); // node*5000 + silv
vll u(m), v(m), a(m), b(m);
rep(i, m) {
cin >> u[i] >> v[i] >> a[i] >> b[i];
u[i]--;
v[i]--;
}
vll c(n), d(n);
rep(i, n) {
cin >> c[i] >> d[i];
chmin(c[i], 2500);
}
rep(i, n) {
rep(j, 2500) { g.add_edge(i * 5000 + j, i * 5000 + j + c[i], d[i]); }
}
rep(i, m) {
repr(j, a[i], 5000) {
g.add_edge(u[i] * 5000 + j, v[i] * 5000 + j - a[i], b[i]);
g.add_edge(v[i] * 5000 + j, u[i] * 5000 + j - a[i], b[i]);
}
}
auto ansd = Dijkstra(g, s);
vll ans(n, INF);
rep(i, n) {
rep(j, 5000) { chmin(ans[i], ansd[i * 5000 + j]); }
}
repr(i, 1, SZ(ans)) { print(ans[i]); }
} else {
GraphL<> g(n);
rep(i, m) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
g.add_edge(u, v, b);
g.add_edge(v, u, b);
}
auto ans = Dijkstra(g, 0);
repr(i, 1, SZ(ans)) { print(ans[i]); }
}
}
| replace | 162 | 163 | 162 | 191 | 11 | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define watch(x) cerr << (#x) << ": " << (x) << endl
#define int long long
const int N = 51;
int n, m, s, c[N], d[N];
struct edge {
int to, a, b;
edge(int _t, int _a, int _b) {
to = _t;
a = _a;
b = _b;
}
};
vector<edge> graph[N];
int seen[N][2501];
int ans[N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> s;
for (int i = 0; i < m; ++i) {
int u, v, a, b;
cin >> u >> v >> a >> b;
graph[u].emplace_back(v, a, b);
graph[v].emplace_back(u, a, b);
}
for (int i = 1; i <= n; ++i) {
cin >> c[i] >> d[i];
}
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= 2500; ++j) {
seen[i][j] = -1000000000000000000;
}
}
int ct = 0;
priority_queue<pair<int, pair<int, int>>> pq;
pair<int, int> st = {1, min(s, 2500LL)};
pq.emplace(0, st);
while (!pq.empty() && ct < n - 1) {
auto p = pq.top();
pq.pop();
int cost = p.first;
pair<int, int> cur = p.second;
if (cost < seen[cur.first][cur.second]) {
continue;
}
if (ans[cur.first] == 0 && cur.first > 1) {
ans[cur.first] = -cost;
ct++;
}
if (cur.second < 2500) {
pair<int, int> nxt = {cur.first, min(2500LL, cur.second + c[cur.first])};
pq.emplace(cost - d[cur.first], nxt);
}
for (edge e : graph[cur.first]) {
if (cur.second >= e.a) {
pair<int, int> nx = {e.to, cur.second - e.a};
int nc = cost - e.b;
if (seen[nx.first][nx.second] < nc) {
pq.emplace(cost - e.b, nx);
seen[nx.first][nx.second] = nc;
}
}
}
}
for (int i = 2; i <= n; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define watch(x) cerr << (#x) << ": " << (x) << endl
#define int long long
const int N = 51;
int n, m, s, c[N], d[N];
struct edge {
int to, a, b;
edge(int _t, int _a, int _b) {
to = _t;
a = _a;
b = _b;
}
};
vector<edge> graph[N];
int seen[N][2501];
int ans[N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> s;
for (int i = 0; i < m; ++i) {
int u, v, a, b;
cin >> u >> v >> a >> b;
graph[u].emplace_back(v, a, b);
graph[v].emplace_back(u, a, b);
}
for (int i = 1; i <= n; ++i) {
cin >> c[i] >> d[i];
}
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= 2500; ++j) {
seen[i][j] = -1000000000000000000;
}
}
int ct = 0;
priority_queue<pair<int, pair<int, int>>> pq;
pair<int, int> st = {1, min(s, 2500LL)};
pq.emplace(0, st);
while (!pq.empty() && ct < n - 1) {
auto p = pq.top();
pq.pop();
int cost = p.first;
pair<int, int> cur = p.second;
if (cost < seen[cur.first][cur.second]) {
continue;
}
if (ans[cur.first] == 0 && cur.first > 1) {
ans[cur.first] = -cost;
ct++;
}
if (cur.second < 2500) {
pair<int, int> nxt = {cur.first, min(2500LL, cur.second + c[cur.first])};
int ncst = cost - d[cur.first];
if (seen[nxt.first][nxt.second] < ncst) {
pq.emplace(cost - d[cur.first], nxt);
seen[nxt.first][nxt.second] = ncst;
}
}
for (edge e : graph[cur.first]) {
if (cur.second >= e.a) {
pair<int, int> nx = {e.to, cur.second - e.a};
int nc = cost - e.b;
if (seen[nx.first][nx.second] < nc) {
pq.emplace(cost - e.b, nx);
seen[nx.first][nx.second] = nc;
}
}
}
}
for (int i = 2; i <= n; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
| replace | 63 | 64 | 63 | 68 | TLE | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
int main() {
ll N, M, S;
cin >> N >> M >> S;
int amax = 2501;
if (S >= amax)
S = amax;
vector<vector<tuple<ll, int, int>>> G(N);
REP(i, M) {
int u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
G[u].emplace_back(b, v, a);
G[v].emplace_back(b, u, a);
}
vector<int> C(N);
vector<ll> D(N);
REP(i, N) cin >> C[i] >> D[i];
vector<vector<ll>> ti(N, vector<ll>(amax, 1LL << 62));
priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>,
greater<tuple<ll, int, int>>>
q;
q.push({0, 0, S});
ti[0][S] = 0;
while (!q.empty()) {
ll t;
int v, co;
tie(t, v, co) = q.top();
q.pop();
// cout << t << " " << v << " " << co << endl;
// 両替
if (co < amax) {
int vco = min(co + C[v], amax);
if (ti[v][co] + D[v] < ti[v][vco]) {
ti[v][vco] = ti[v][co] + D[v];
q.emplace(ti[v][vco], v, vco);
// cout << " add " << ti[v][co+C[v]] << " " << v << " " << vco <<
// endl;
}
}
for (auto nv : G[v]) {
ll nvt;
int nvpos, nvco;
tie(nvt, nvpos, nvco) = nv;
// 移動
if (co >= nvco) {
if (ti[v][co] + nvt < ti[nvpos][co - nvco]) {
ti[nvpos][co - nvco] = ti[v][co] + nvt;
q.emplace(ti[nvpos][co - nvco], nvpos, co - nvco);
// cout << " add " << ti[nvpos][co-nvco] << " " << nvpos << "
// " << co-nvco << endl;
}
}
}
}
for (int i = 1; i < N; i++) {
ll ans = 1LL << 62;
REP(j, amax) { ans = min(ans, ti[i][j]); }
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
int main() {
ll N, M, S;
cin >> N >> M >> S;
int amax = 2501;
if (S >= amax)
S = amax;
vector<vector<tuple<ll, int, int>>> G(N);
REP(i, M) {
int u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
G[u].emplace_back(b, v, a);
G[v].emplace_back(b, u, a);
}
vector<int> C(N);
vector<ll> D(N);
REP(i, N) cin >> C[i] >> D[i];
vector<vector<ll>> ti(N, vector<ll>(amax + 2, 1LL << 62));
priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>,
greater<tuple<ll, int, int>>>
q;
q.push({0, 0, S});
ti[0][S] = 0;
while (!q.empty()) {
ll t;
int v, co;
tie(t, v, co) = q.top();
q.pop();
// cout << t << " " << v << " " << co << endl;
// 両替
if (co < amax) {
int vco = min(co + C[v], amax);
if (ti[v][co] + D[v] < ti[v][vco]) {
ti[v][vco] = ti[v][co] + D[v];
q.emplace(ti[v][vco], v, vco);
// cout << " add " << ti[v][co+C[v]] << " " << v << " " << vco <<
// endl;
}
}
for (auto nv : G[v]) {
ll nvt;
int nvpos, nvco;
tie(nvt, nvpos, nvco) = nv;
// 移動
if (co >= nvco) {
if (ti[v][co] + nvt < ti[nvpos][co - nvco]) {
ti[nvpos][co - nvco] = ti[v][co] + nvt;
q.emplace(ti[nvpos][co - nvco], nvpos, co - nvco);
// cout << " add " << ti[nvpos][co-nvco] << " " << nvpos << "
// " << co-nvco << endl;
}
}
}
}
for (int i = 1; i < N; i++) {
ll ans = 1LL << 62;
REP(j, amax) { ans = min(ans, ti[i][j]); }
cout << ans << endl;
}
}
| replace | 24 | 25 | 24 | 25 | -6 | corrupted size vs. prev_size
|
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, pair<ll, ll>>;
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep0(i, n) for (int i = 0; i < n; i++)
#define zarray(x, n) \
x[n + 1]; \
rep1(i, n) { x[i] = 0; }
int main() {
int n, m, s;
cin >> n >> m >> s;
int MAX_SILVER = m * 50 + 1;
if (s > MAX_SILVER) {
s = MAX_SILVER;
}
map<int, vector<pair<int, pair<int, int>>>> g;
rep0(i, m) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
g[u].push_back({v, {a, b}});
g[v].push_back({u, {a, b}});
}
ll C[n + 1];
ll D[n + 1];
rep1(i, n) {
ll c, d;
cin >> c >> d;
D[i] = c;
C[i] = d;
}
ll time[n + 1][2501];
rep1(i, n) {
rep0(j, 2501) { time[i][j] = INT64_MAX; }
}
priority_queue<P, vector<P>, greater<P>> q;
// cout << "searching.." << endl;
q.push({0, {1, s}});
time[1][s] = 0;
while (!q.empty()) {
auto a = q.top();
q.pop();
ll d = a.first;
int here = a.second.first;
ll s = a.second.second;
if (time[here][s] > d)
continue;
// cout << "d:" << d << " here:" << here << " s:" << s << " as:" << D[here]
// << " ts:" << C[here] << endl;
ll ns = (s + D[here]);
if (ns <= MAX_SILVER) {
ll new_time = d + C[here];
if (time[here][ns] > new_time) {
time[here][ns] = new_time;
q.push({new_time, {here, ns}});
// cout << "pushed add: " << here << " " << new_time << " " << ns <<
// endl;
}
}
for (auto edge : g[here]) {
int to = edge.first;
ll travel_cost = edge.second.first;
ll travel_time = edge.second.second;
ll w = d + travel_time;
ll ns = s - travel_cost;
// cout << "to:" << to << " c:" << travel_cost << " time:" << travel_time
// << " w:" << w << " ss:" << ns << endl;
if (ns < 0)
continue;
if (w < time[to][ns]) {
time[to][ns] = w;
q.push({w, {to, ns}});
// cout << "pushed travel:" << to << " " << w << " " << ns << endl;
}
}
}
for (int i = 2; i < n + 1; i++) {
ll min = INT64_MAX;
rep0(j, MAX_SILVER + 1) {
// cout << "min:" << min << " j:" << j << " t:" << time[i][j] << endl;
if (min > time[i][j]) {
min = time[i][j];
}
}
if (min == INT64_MAX) {
min = 0;
}
cout << min << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, pair<ll, ll>>;
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep0(i, n) for (int i = 0; i < n; i++)
#define zarray(x, n) \
x[n + 1]; \
rep1(i, n) { x[i] = 0; }
int main() {
int n, m, s;
cin >> n >> m >> s;
int MAX_SILVER = 2500;
if (s > MAX_SILVER) {
s = MAX_SILVER;
}
map<int, vector<pair<int, pair<int, int>>>> g;
rep0(i, m) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
g[u].push_back({v, {a, b}});
g[v].push_back({u, {a, b}});
}
ll C[n + 1];
ll D[n + 1];
rep1(i, n) {
ll c, d;
cin >> c >> d;
D[i] = c;
C[i] = d;
}
ll time[n + 1][2501];
rep1(i, n) {
rep0(j, 2501) { time[i][j] = INT64_MAX; }
}
priority_queue<P, vector<P>, greater<P>> q;
// cout << "searching.." << endl;
q.push({0, {1, s}});
time[1][s] = 0;
while (!q.empty()) {
auto a = q.top();
q.pop();
ll d = a.first;
int here = a.second.first;
ll s = a.second.second;
if (time[here][s] > d)
continue;
// cout << "d:" << d << " here:" << here << " s:" << s << " as:" << D[here]
// << " ts:" << C[here] << endl;
ll ns = (s + D[here]);
if (ns <= MAX_SILVER) {
ll new_time = d + C[here];
if (time[here][ns] > new_time) {
time[here][ns] = new_time;
q.push({new_time, {here, ns}});
// cout << "pushed add: " << here << " " << new_time << " " << ns <<
// endl;
}
}
for (auto edge : g[here]) {
int to = edge.first;
ll travel_cost = edge.second.first;
ll travel_time = edge.second.second;
ll w = d + travel_time;
ll ns = s - travel_cost;
// cout << "to:" << to << " c:" << travel_cost << " time:" << travel_time
// << " w:" << w << " ss:" << ns << endl;
if (ns < 0)
continue;
if (w < time[to][ns]) {
time[to][ns] = w;
q.push({w, {to, ns}});
// cout << "pushed travel:" << to << " " << w << " " << ns << endl;
}
}
}
for (int i = 2; i < n + 1; i++) {
ll min = INT64_MAX;
rep0(j, MAX_SILVER + 1) {
// cout << "min:" << min << " j:" << j << " t:" << time[i][j] << endl;
if (min > time[i][j]) {
min = time[i][j];
}
}
if (min == INT64_MAX) {
min = 0;
}
cout << min << endl;
}
}
| replace | 15 | 16 | 15 | 16 | 0 | |
p02703 | C++ | Runtime Error | //{{{
#include <bits/stdc++.h>
using namespace std;
#define putchar(x) cout << (x)
#define repeat(x) \
int _ = 0; \
_ < (x); \
++_
template <typename T> constexpr auto range(T start, T stop, T step) {
struct iterator {
T i, step;
bool operator!=(const iterator &other) const { return i != other.i; }
auto &operator++() {
i += step;
return *this;
}
auto &operator*() { return i; }
};
struct iterable_wrapper {
T start, stop, step;
auto begin() const { return iterator{start, step}; }
auto end() const { return iterator{stop, step}; }
size_t size() const { return (stop - start) / step; }
iterable_wrapper(T start_, T stop_, T step_)
: start(start_), stop(stop_), step(step_) {
stop = step > 0 ? max(start, stop) : min(start, stop);
stop += (step - (stop - start) % step) % step;
}
};
return iterable_wrapper(start, stop, step);
};
template <typename T> constexpr auto range(T start, T stop) {
return range(start, stop, T(1));
}
template <typename T> constexpr auto range(T stop) {
return range(T(0), stop, T(1));
}
template <typename T, typename Iter = decltype(begin(declval<T>()))>
constexpr auto printer(T &&iterable) {
struct iterator {
Iter iter, ed;
auto operator!=(const iterator &other) const {
auto ret = (iter != other.iter);
if (not ret)
cout << '\n';
return ret;
}
auto &operator++() {
++iter;
if (iter != ed)
cout << ' ';
return *this;
}
auto &operator*() { return *iter; }
};
struct iterable_wrapper {
T iterable;
auto begin() const {
return iterator{std::begin(iterable), std::end(iterable)};
}
auto end() const {
return iterator{std::end(iterable), std::end(iterable)};
}
};
return iterable_wrapper{forward<T>(iterable)};
};
template <size_t... Is, typename T> auto getis(const T &t) {
return tie(get<Is>(t)...);
}
template <class T> void setmax(T &a, const T &b) { a = max(a, b); }
template <class T> void setmin(T &a, const T &b) { a = min(a, b); }
template <typename T> struct is_const_char_arr_ref : false_type {};
template <size_t N>
struct is_const_char_arr_ref<char const (&)[N]> : true_type {};
template <typename T, typename = void> struct is_container : false_type {};
template <typename T>
struct is_container<T,
conditional_t<false, decltype(begin(declval<T>())), void>>
: true_type {};
template <class T>
using IsC = typename enable_if<is_container<T>::value and
not is_same<T, string>::value>::type;
template <class T>
using NotC = typename enable_if<not is_container<T>::value or
is_same<T, string>::value>::type;
template <class T> inline IsC<T> print_1(const T &v);
template <class T> inline NotC<T> print_1(const T &x) { cout << x; }
template <size_t N> void print_1(const array<char, N> &x) { cout << &x[0]; };
inline void print_1(const tuple<> &) { cout << "()"; };
template <size_t L, size_t I, class T> void print_tuple(const T &t) {
if (I != 0)
cout << ", ";
print_1(get<I>(t));
if (I + 1 < L)
print_tuple<L, (I + 1) % L>(t);
}
template <class... T> inline void print_1(const tuple<T...> &x) {
cout << "(";
print_tuple<sizeof...(T), 0, tuple<T...>>(x);
cout << ")";
}
inline void print_n() {}
template <class T, class... U>
inline void print_n(const T &head, const U &...tail);
template <class T, class U> inline void print_1(const pair<T, U> &p) {
cout << "(";
print_1(p.first);
cout << ", ";
print_1(p.second);
cout << ")";
}
template <class T, class... U>
inline void print_n(const T &head, const U &...tail) {
print_1(head);
if (sizeof...(tail))
cout << " ";
print_n(tail...);
}
template <class T> inline IsC<T> print_1(const T &v) {
cout << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
cout << ", ";
print_1(*it);
}
cout << "]";
}
template <class... T> inline void print(const T &...args) {
print_n(args...);
putchar('\n');
}
inline void read() {}
template <class T, class... U> inline void read(T &head, U &...tail) {
cin >> head;
read(tail...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
static int fastio = []() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.precision(17);
return 0;
}();
template <class T> void print_dbg(const string &s, T &&x) {
if (is_const_char_arr_ref<T>::value) {
print_n(x);
} else {
print_n(s, "=", x);
}
}
#define SELECT(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME
#define dbg1(a) \
print_dbg(#a, a); \
cout << endl;
#define dbg2(a, b) \
print_dbg(#a, a); \
cout << ", "; \
dbg1(b);
#define dbg3(a, b, c) \
print_dbg(#a, a); \
cout << ", "; \
dbg2(b, c);
#define dbg4(a, b, c, d) \
print_dbg(#a, a); \
cout << ", "; \
dbg3(b, c, d);
#define dbg5(a, b, c, d, e) \
print_dbg(#a, a); \
cout << ", "; \
dbg4(b, c, d, e);
#define dbg6(a, b, c, d, e, f) \
print_dbg(#a, a); \
cout << ", "; \
dbg5(b, c, d, e, f);
#define dbg7(a, b, c, d, e, f, g) \
print_dbg(#a, a); \
cout << ", "; \
dbg6(b, c, d, e, f, g);
#define dbg8(a, b, c, d, e, f, g, h) \
print_dbg(#a, a); \
cout << ", "; \
dbg7(b, c, d, e, f, g, h);
#define debug(...) \
SELECT(__VA_ARGS__, dbg8, dbg7, dbg6, dbg5, dbg4, dbg3, dbg2, dbg1) \
(__VA_ARGS__)
//}}}
using PII = pair<int, int>;
using LL = long long;
using TLII = tuple<LL, int, int>;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 0x3f3f3f3f3f3f3f3f;
const int MAX_N = 55;
const int MAX_S = 2505;
struct edge {
int to, silver, time;
};
vector<edge> G[MAX_N];
LL d[MAX_N][MAX_S];
struct Solution {
Solution(int) {
int N, M, S;
read(N, M, S);
for (repeat(M)) {
int u, v, a, b;
read(u, v, a, b);
G[u].push_back({v, a, b});
G[v].push_back({u, a, b});
}
vector<LL> C(N + 1), D(N + 1);
for (int i : range(1, N + 1)) {
read(C[i], D[i]);
}
int ok = 0;
vector<bool> done(N + 1, false);
priority_queue<TLII, vector<TLII>, greater<TLII>> pq;
memset(d, INF, sizeof(d));
d[1][S] = 0;
pq.push({0, 1, S});
while (pq.size()) {
auto [dist, v, s] = pq.top();
pq.pop();
if (not done[v]) {
done[v] = true;
ok += 1;
if (ok == N) {
break;
}
}
if (dist > d[v][s]) {
continue;
}
for (int i = 1; s + C[v] * i < MAX_S; ++i) {
auto s1 = s + C[v] * i;
if (d[v][s1] > d[v][s] + D[v] * i) {
d[v][s1] = d[v][s] + D[v] * i;
pq.push({d[v][s1], v, s1});
}
}
for (auto &e : G[v]) {
auto s1 = s - e.silver;
if (s1 >= 0 and d[e.to][s1] > d[v][s] + e.time) {
d[e.to][s1] = d[v][s] + e.time;
pq.push({d[e.to][s1], e.to, s1});
}
}
}
for (int i = 2; i <= N; ++i) {
LL ans = LLINF;
for (int j = 0; j < MAX_S; ++j) {
setmin(ans, d[i][j]);
}
print(ans);
}
}
};
int main() {
int T = 1;
// cin >> T;
for (int i = 1; i <= T; ++i) {
ignore = Solution(i);
}
return 0;
}
| //{{{
#include <bits/stdc++.h>
using namespace std;
#define putchar(x) cout << (x)
#define repeat(x) \
int _ = 0; \
_ < (x); \
++_
template <typename T> constexpr auto range(T start, T stop, T step) {
struct iterator {
T i, step;
bool operator!=(const iterator &other) const { return i != other.i; }
auto &operator++() {
i += step;
return *this;
}
auto &operator*() { return i; }
};
struct iterable_wrapper {
T start, stop, step;
auto begin() const { return iterator{start, step}; }
auto end() const { return iterator{stop, step}; }
size_t size() const { return (stop - start) / step; }
iterable_wrapper(T start_, T stop_, T step_)
: start(start_), stop(stop_), step(step_) {
stop = step > 0 ? max(start, stop) : min(start, stop);
stop += (step - (stop - start) % step) % step;
}
};
return iterable_wrapper(start, stop, step);
};
template <typename T> constexpr auto range(T start, T stop) {
return range(start, stop, T(1));
}
template <typename T> constexpr auto range(T stop) {
return range(T(0), stop, T(1));
}
template <typename T, typename Iter = decltype(begin(declval<T>()))>
constexpr auto printer(T &&iterable) {
struct iterator {
Iter iter, ed;
auto operator!=(const iterator &other) const {
auto ret = (iter != other.iter);
if (not ret)
cout << '\n';
return ret;
}
auto &operator++() {
++iter;
if (iter != ed)
cout << ' ';
return *this;
}
auto &operator*() { return *iter; }
};
struct iterable_wrapper {
T iterable;
auto begin() const {
return iterator{std::begin(iterable), std::end(iterable)};
}
auto end() const {
return iterator{std::end(iterable), std::end(iterable)};
}
};
return iterable_wrapper{forward<T>(iterable)};
};
template <size_t... Is, typename T> auto getis(const T &t) {
return tie(get<Is>(t)...);
}
template <class T> void setmax(T &a, const T &b) { a = max(a, b); }
template <class T> void setmin(T &a, const T &b) { a = min(a, b); }
template <typename T> struct is_const_char_arr_ref : false_type {};
template <size_t N>
struct is_const_char_arr_ref<char const (&)[N]> : true_type {};
template <typename T, typename = void> struct is_container : false_type {};
template <typename T>
struct is_container<T,
conditional_t<false, decltype(begin(declval<T>())), void>>
: true_type {};
template <class T>
using IsC = typename enable_if<is_container<T>::value and
not is_same<T, string>::value>::type;
template <class T>
using NotC = typename enable_if<not is_container<T>::value or
is_same<T, string>::value>::type;
template <class T> inline IsC<T> print_1(const T &v);
template <class T> inline NotC<T> print_1(const T &x) { cout << x; }
template <size_t N> void print_1(const array<char, N> &x) { cout << &x[0]; };
inline void print_1(const tuple<> &) { cout << "()"; };
template <size_t L, size_t I, class T> void print_tuple(const T &t) {
if (I != 0)
cout << ", ";
print_1(get<I>(t));
if (I + 1 < L)
print_tuple<L, (I + 1) % L>(t);
}
template <class... T> inline void print_1(const tuple<T...> &x) {
cout << "(";
print_tuple<sizeof...(T), 0, tuple<T...>>(x);
cout << ")";
}
inline void print_n() {}
template <class T, class... U>
inline void print_n(const T &head, const U &...tail);
template <class T, class U> inline void print_1(const pair<T, U> &p) {
cout << "(";
print_1(p.first);
cout << ", ";
print_1(p.second);
cout << ")";
}
template <class T, class... U>
inline void print_n(const T &head, const U &...tail) {
print_1(head);
if (sizeof...(tail))
cout << " ";
print_n(tail...);
}
template <class T> inline IsC<T> print_1(const T &v) {
cout << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
cout << ", ";
print_1(*it);
}
cout << "]";
}
template <class... T> inline void print(const T &...args) {
print_n(args...);
putchar('\n');
}
inline void read() {}
template <class T, class... U> inline void read(T &head, U &...tail) {
cin >> head;
read(tail...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
static int fastio = []() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.precision(17);
return 0;
}();
template <class T> void print_dbg(const string &s, T &&x) {
if (is_const_char_arr_ref<T>::value) {
print_n(x);
} else {
print_n(s, "=", x);
}
}
#define SELECT(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME
#define dbg1(a) \
print_dbg(#a, a); \
cout << endl;
#define dbg2(a, b) \
print_dbg(#a, a); \
cout << ", "; \
dbg1(b);
#define dbg3(a, b, c) \
print_dbg(#a, a); \
cout << ", "; \
dbg2(b, c);
#define dbg4(a, b, c, d) \
print_dbg(#a, a); \
cout << ", "; \
dbg3(b, c, d);
#define dbg5(a, b, c, d, e) \
print_dbg(#a, a); \
cout << ", "; \
dbg4(b, c, d, e);
#define dbg6(a, b, c, d, e, f) \
print_dbg(#a, a); \
cout << ", "; \
dbg5(b, c, d, e, f);
#define dbg7(a, b, c, d, e, f, g) \
print_dbg(#a, a); \
cout << ", "; \
dbg6(b, c, d, e, f, g);
#define dbg8(a, b, c, d, e, f, g, h) \
print_dbg(#a, a); \
cout << ", "; \
dbg7(b, c, d, e, f, g, h);
#define debug(...) \
SELECT(__VA_ARGS__, dbg8, dbg7, dbg6, dbg5, dbg4, dbg3, dbg2, dbg1) \
(__VA_ARGS__)
//}}}
using PII = pair<int, int>;
using LL = long long;
using TLII = tuple<LL, int, int>;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 0x3f3f3f3f3f3f3f3f;
const int MAX_N = 55;
const int MAX_S = 2505;
struct edge {
int to, silver, time;
};
vector<edge> G[MAX_N];
LL d[MAX_N][MAX_S];
struct Solution {
Solution(int) {
int N, M, S;
read(N, M, S);
setmin(S, MAX_S - 1);
for (repeat(M)) {
int u, v, a, b;
read(u, v, a, b);
G[u].push_back({v, a, b});
G[v].push_back({u, a, b});
}
vector<LL> C(N + 1), D(N + 1);
for (int i : range(1, N + 1)) {
read(C[i], D[i]);
}
int ok = 0;
vector<bool> done(N + 1, false);
priority_queue<TLII, vector<TLII>, greater<TLII>> pq;
memset(d, INF, sizeof(d));
d[1][S] = 0;
pq.push({0, 1, S});
while (pq.size()) {
auto [dist, v, s] = pq.top();
pq.pop();
if (not done[v]) {
done[v] = true;
ok += 1;
if (ok == N) {
break;
}
}
if (dist > d[v][s]) {
continue;
}
for (int i = 1; s + C[v] * i < MAX_S; ++i) {
auto s1 = s + C[v] * i;
if (d[v][s1] > d[v][s] + D[v] * i) {
d[v][s1] = d[v][s] + D[v] * i;
pq.push({d[v][s1], v, s1});
}
}
for (auto &e : G[v]) {
auto s1 = s - e.silver;
if (s1 >= 0 and d[e.to][s1] > d[v][s] + e.time) {
d[e.to][s1] = d[v][s] + e.time;
pq.push({d[e.to][s1], e.to, s1});
}
}
}
for (int i = 2; i <= N; ++i) {
LL ans = LLINF;
for (int j = 0; j < MAX_S; ++j) {
setmin(ans, d[i][j]);
}
print(ans);
}
}
};
int main() {
int T = 1;
// cin >> T;
for (int i = 1; i <= T; ++i) {
ignore = Solution(i);
}
return 0;
}
| insert | 215 | 215 | 215 | 217 | 0 | |
p02703 | C++ | Time Limit Exceeded | // #pragma GCC optimize ("O3")
// #pragma GCC target ("avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
constexpr int bm = 2500;
ll BIT[50][bm];
int zero[50];
priority_queue<ll> PQ;
inline ll query(int A, int gin) {
ll kotae = 0;
while (gin < bm) {
kotae = max(BIT[A][gin], kotae);
gin += gin & -gin;
}
return kotae;
}
inline void query2(int A, int gin, ll ti) {
int tmp = gin;
while (gin < bm) {
if (BIT[A][gin] > ti)
return;
gin += gin & -gin;
}
gin = tmp;
while (gin > 0) {
if (BIT[A][gin] >= ti)
break;
BIT[A][gin] = ti;
gin -= gin & -gin;
}
PQ.push(ti << 18 | A << 12 | tmp);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M, S;
cin >> N >> M >> S;
if (S >= 2460)
S = 2460;
vector<ll> E[50];
rep(i, M) {
int u, v, a;
ll b;
cin >> u >> v >> a >> b;
u--;
v--;
b = a << 8 | b << 16;
E[u].pb(v | b);
E[v].pb(u | b);
}
int C[50], D[50];
rep(i, N) { cin >> C[i] >> D[i]; }
query2(0, S + 1, 4e12);
ll zenbu = 0;
ll owari = (1ll << N) - 1;
while (PQ.size()) {
auto p = PQ.top();
PQ.pop();
ll ti = p >> 18;
int i = p >> 12 & 63;
int gin = p & 4095;
if (ti < query(i, gin))
continue;
ll ti2 = ti - D[i];
int gin2 = gin + C[i];
if (gin2 > 2460)
gin2 = 2460;
query2(i, gin2, ti2);
for (auto pp : E[i]) {
int to = pp & 255;
ti2 = ti - (pp >> 16);
gin2 = gin - (pp >> 8 & 255);
if (gin2 > 0)
query2(to, gin2, ti2);
}
zenbu |= 1ll << i;
if (zenbu == owari)
break;
}
for (int i = 1; i < N; i++) {
co(ll(4e12 - query(i, 1)));
}
Would you please return 0;
} | // #pragma GCC optimize ("O3")
// #pragma GCC target ("avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
constexpr int bm = 2500;
ll BIT[50][bm];
int zero[50];
priority_queue<ll> PQ;
inline ll query(int A, int gin) {
ll kotae = 0;
while (gin < bm) {
kotae = max(BIT[A][gin], kotae);
gin += gin & -gin;
}
return kotae;
}
inline void query2(int A, int gin, ll ti) {
int tmp = gin;
while (gin < bm) {
if (BIT[A][gin] >= ti)
return;
gin += gin & -gin;
}
gin = tmp;
while (gin > 0) {
if (BIT[A][gin] >= ti)
break;
BIT[A][gin] = ti;
gin -= gin & -gin;
}
PQ.push(ti << 18 | A << 12 | tmp);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M, S;
cin >> N >> M >> S;
if (S >= 2460)
S = 2460;
vector<ll> E[50];
rep(i, M) {
int u, v, a;
ll b;
cin >> u >> v >> a >> b;
u--;
v--;
b = a << 8 | b << 16;
E[u].pb(v | b);
E[v].pb(u | b);
}
int C[50], D[50];
rep(i, N) { cin >> C[i] >> D[i]; }
query2(0, S + 1, 4e12);
ll zenbu = 0;
ll owari = (1ll << N) - 1;
while (PQ.size()) {
auto p = PQ.top();
PQ.pop();
ll ti = p >> 18;
int i = p >> 12 & 63;
int gin = p & 4095;
if (ti < query(i, gin))
continue;
ll ti2 = ti - D[i];
int gin2 = gin + C[i];
if (gin2 > 2460)
gin2 = 2460;
query2(i, gin2, ti2);
for (auto pp : E[i]) {
int to = pp & 255;
ti2 = ti - (pp >> 16);
gin2 = gin - (pp >> 8 & 255);
if (gin2 > 0)
query2(to, gin2, ti2);
}
zenbu |= 1ll << i;
if (zenbu == owari)
break;
}
for (int i = 1; i < N; i++) {
co(ll(4e12 - query(i, 1)));
}
Would you please return 0;
} | replace | 34 | 35 | 34 | 35 | TLE | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define ins insert
#define er erase
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, ii> iiii;
const int oo = 1e18 + 7, mod = 1e9 + 7;
int n, m, s, c[55], d[55], dp[55][5005];
vector<iii> vc[55];
priority_queue<iii, vector<iii>, greater<iii>> pq;
void dijk() {
while (!pq.empty()) {
// if(pq.size() <= 5000) cout << pq.size() << "\n";
iii tmp = pq.top();
pq.pop();
if (tmp.fi > dp[tmp.se.fi][tmp.se.se])
continue;
int u = tmp.se.fi;
// cout << u << " " << tmp.se.se << " " << dp[tmp.se.fi][tmp.se.se] << " "
// << c[u] << " " << d[u] << "\n";
if (tmp.se.se + c[u] <= 5000 &&
dp[u][tmp.se.se + c[u]] > (dp[u][tmp.se.se] + d[u])) {
dp[u][tmp.se.se + c[u]] = (dp[u][tmp.se.se] + d[u]);
pq.push({dp[u][tmp.se.se + c[u]], {u, tmp.se.se + c[u]}});
}
for (int i = 0; i < vc[u].size(); i++) {
int v = vc[u][i].fi;
if (tmp.se.se < vc[u][i].se.fi)
continue;
// cout << u << " " << tmp.se.se << " " << dp[u][tmp.se.se] << "\n";
if (dp[v][tmp.se.se - vc[u][i].se.fi] >
(dp[u][tmp.se.se] + vc[u][i].se.se)) {
// if(v == 3) cout << u << " " << v << " " << tmp.se.se << "\n";
dp[v][tmp.se.se - vc[u][i].se.fi] = dp[u][tmp.se.se] + vc[u][i].se.se;
// if(pq.size() < 10000) cout << v << " " << tmp.se.se - vc[u][i].se.fi
// << "\n";
pq.push({dp[v][tmp.se.se - vc[u][i].se.fi],
{v, tmp.se.se - vc[u][i].se.fi}});
}
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
int u, v, a, b;
cin >> u >> v >> a >> b;
vc[u].pb({v, {a, b}});
vc[v].pb({u, {a, b}});
}
for (int i = 1; i <= n; i++)
cin >> c[i] >> d[i];
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 5000; j++)
dp[i][j] = oo;
}
dp[1][s] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 5000; j++)
pq.push({dp[i][j], {i, j}});
}
dijk();
for (int i = 2; i <= n; i++) {
int answ = oo;
for (int j = 0; j <= 5000; j++)
answ = min(answ, dp[i][j]);
cout << answ << "\n";
}
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define ins insert
#define er erase
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, ii> iiii;
const int oo = 1e18 + 7, mod = 1e9 + 7;
int n, m, s, c[55], d[55], dp[55][5005];
vector<iii> vc[55];
priority_queue<iii, vector<iii>, greater<iii>> pq;
void dijk() {
while (!pq.empty()) {
// if(pq.size() <= 5000) cout << pq.size() << "\n";
iii tmp = pq.top();
pq.pop();
if (tmp.fi > dp[tmp.se.fi][tmp.se.se])
continue;
int u = tmp.se.fi;
// cout << u << " " << tmp.se.se << " " << dp[tmp.se.fi][tmp.se.se] << " "
// << c[u] << " " << d[u] << "\n";
if (tmp.se.se + c[u] <= 5000 &&
dp[u][tmp.se.se + c[u]] > (dp[u][tmp.se.se] + d[u])) {
dp[u][tmp.se.se + c[u]] = (dp[u][tmp.se.se] + d[u]);
pq.push({dp[u][tmp.se.se + c[u]], {u, tmp.se.se + c[u]}});
}
for (int i = 0; i < vc[u].size(); i++) {
int v = vc[u][i].fi;
if (tmp.se.se < vc[u][i].se.fi)
continue;
// cout << u << " " << tmp.se.se << " " << dp[u][tmp.se.se] << "\n";
if (dp[v][tmp.se.se - vc[u][i].se.fi] >
(dp[u][tmp.se.se] + vc[u][i].se.se)) {
// if(v == 3) cout << u << " " << v << " " << tmp.se.se << "\n";
dp[v][tmp.se.se - vc[u][i].se.fi] = dp[u][tmp.se.se] + vc[u][i].se.se;
// if(pq.size() < 10000) cout << v << " " << tmp.se.se - vc[u][i].se.fi
// << "\n";
pq.push({dp[v][tmp.se.se - vc[u][i].se.fi],
{v, tmp.se.se - vc[u][i].se.fi}});
}
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin >> n >> m >> s;
s = min(s, 5000LL);
for (int i = 1; i <= m; i++) {
int u, v, a, b;
cin >> u >> v >> a >> b;
vc[u].pb({v, {a, b}});
vc[v].pb({u, {a, b}});
}
for (int i = 1; i <= n; i++)
cin >> c[i] >> d[i];
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 5000; j++)
dp[i][j] = oo;
}
dp[1][s] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 5000; j++)
pq.push({dp[i][j], {i, j}});
}
dijk();
for (int i = 2; i <= n; i++) {
int answ = oo;
for (int j = 0; j <= 5000; j++)
answ = min(answ, dp[i][j]);
cout << answ << "\n";
}
}
| insert | 56 | 56 | 56 | 57 | 0 | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main(void) {
cout << fixed << setprecision(16);
cin.tie(0);
ios::sync_with_stdio(false);
ll n, m, s;
cin >> n >> m >> s;
vector<ll> res(n, 1e15);
vector<vector<tuple<ll, ll, ll>>> G(n);
while (m--) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
u--, v--;
G[u].push_back({v, a, b});
G[v].push_back({u, a, b});
}
vector<tuple<ll, ll>> cd(n);
for (auto &[c, d] : cd)
cin >> c >> d;
if (s >= 50 * 50 * 1) {
priority_queue<tuple<ll, ll>> q;
q.push({-0, 0});
res[0] = 0;
while (!q.empty()) {
auto [d, i] = q.top();
q.pop();
d = -d;
for (auto [j, a, b] : G[i]) {
if (res[j] <= d + b)
continue;
res[j] = d + b;
q.push({-(d + b), j});
}
}
for (ll i = 1; i < n; i++)
cout << res[i] << endl;
return 0;
}
vector<vector<ll>> dp(n, vector<ll>(50 * 50 * 1 + 1, 1e15));
dp[0][s] = 0;
priority_queue<tuple<ll, ll, ll>> q;
q.push({-0, s, 0});
bitset<100> bs;
while (!q.empty()) {
auto [d, c, i] = q.top();
q.pop();
bs[i] = 1;
if (bs.count() == n)
break;
d = -d;
if (dp[i][c] < d)
continue;
{
auto [cc, dd] = cd[i];
if (c + cc <= 50 * 50 * 1 && dp[i][c + cc] >= d + dd) {
dp[i][c + cc] = d + dd;
q.push({-(d + dd), c + cc, i});
}
}
for (auto [j, a, b] : G[i]) {
if (c - a < 0)
continue;
if (dp[j][c - a] <= d + b)
continue;
dp[j][c - a] = d + b;
q.push({-(d + b), c - a, j});
}
}
for (ll i = 1; i < n; i++)
cout << *min_element(dp[i].begin(), dp[i].end()) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main(void) {
cout << fixed << setprecision(16);
cin.tie(0);
ios::sync_with_stdio(false);
ll n, m, s;
cin >> n >> m >> s;
vector<ll> res(n, 1e15);
vector<vector<tuple<ll, ll, ll>>> G(n);
while (m--) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
u--, v--;
G[u].push_back({v, a, b});
G[v].push_back({u, a, b});
}
vector<tuple<ll, ll>> cd(n);
for (auto &[c, d] : cd)
cin >> c >> d;
if (s >= 50 * 50 * 1) {
priority_queue<tuple<ll, ll>> q;
q.push({-0, 0});
res[0] = 0;
while (!q.empty()) {
auto [d, i] = q.top();
q.pop();
d = -d;
for (auto [j, a, b] : G[i]) {
if (res[j] <= d + b)
continue;
res[j] = d + b;
q.push({-(d + b), j});
}
}
for (ll i = 1; i < n; i++)
cout << res[i] << endl;
return 0;
}
vector<vector<ll>> dp(n, vector<ll>(50 * 50 * 1 + 1, 1e15));
dp[0][s] = 0;
priority_queue<tuple<ll, ll, ll>> q;
q.push({-0, s, 0});
bitset<100> bs;
while (!q.empty()) {
auto [d, c, i] = q.top();
q.pop();
bs[i] = 1;
if (bs.count() == n)
break;
d = -d;
if (dp[i][c] < d)
continue;
{
auto [cc, dd] = cd[i];
if (c + cc <= 50 * 50 * 1 && dp[i][c + cc] > d + dd) {
dp[i][c + cc] = d + dd;
q.push({-(d + dd), c + cc, i});
}
}
for (auto [j, a, b] : G[i]) {
if (c - a < 0)
continue;
if (dp[j][c - a] <= d + b)
continue;
dp[j][c - a] = d + b;
q.push({-(d + b), c - a, j});
}
}
for (ll i = 1; i < n; i++)
cout << *min_element(dp[i].begin(), dp[i].end()) << endl;
}
| replace | 64 | 65 | 64 | 65 | TLE | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
template <class T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
template <class T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int kaijo[2000010];
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
if (a == 1)
return false;
for (int i = 2; i * i <= a; i++) {
if (a % i == 0)
return false;
}
return true;
}
void init_fact() {
kaijo[0] = 1;
for (int i = 1; i <= 2000000; i++) {
kaijo[i] = kaijo[i - 1] * i;
kaijo[i] %= mod;
}
}
int modpow(int a, int b) {
if (b == 0)
return 1;
if (b % 2)
return modpow(a, b - 1) * a % mod;
int memo = modpow(a, b / 2);
return memo * memo % mod;
}
int comb(int a, int b) {
if (!kaijo[0])
init_fact();
return kaijo[a] * modpow(kaijo[a - b], mod - 2) % mod *
modpow(kaijo[b], mod - 2) % mod;
}
int inv(int x) {
x = modpow(x, mod - 2);
return x;
}
bool kosa(double ax, double ay, double bx, double by, double cx, double cy,
double dx, double dy) {
double ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
double tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
double tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
double td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);
return tc * td < 0 && ta * tb < 0;
}
struct edge {
int to, silver, cost;
};
#define PP pair<int, P>
int n, m, s, c[50], d[50], dist[50][2451];
vector<edge> v[50];
void dijkstra() {
rep(i, 50) {
rep(j, 2451) { dist[i][j] = INF; }
}
priority_queue<PP, vector<PP>, greater<PP>> q;
q.push({0, {0, s}});
dist[0][s] = 0;
while (!q.empty()) {
PP p = q.top();
q.pop();
if (dist[p.se.fi][p.se.se] != p.fi)
continue;
for (int i = 0; p.se.se + c[p.se.fi] * i <= 2450; i++) {
for (edge e : v[p.se.fi]) {
if (p.se.se + c[p.se.fi] * i - e.silver < 0)
continue;
if (dist[e.to][p.se.se + c[p.se.fi] * i - e.silver] >
p.fi + d[p.se.fi] * i + e.cost) {
dist[e.to][p.se.se + c[p.se.fi] * i - e.silver] =
p.fi + d[p.se.fi] * i + e.cost;
q.push({dist[e.to][p.se.se + c[p.se.fi] * i - e.silver],
{e.to, p.se.se + c[p.se.fi] * i - e.silver}});
}
}
}
}
}
signed main() {
cin >> n >> m >> s;
chmin(s, 2450ll);
rep(i, m) {
int u, w, a, b;
cin >> u >> w >> a >> b;
u--, w--;
v[u].pb({w, a, b});
v[w].pb({u, a, b});
}
rep(i, n) cin >> c[i] >> d[i];
dijkstra();
rep(i, n) {
if (i) {
int ans = INF;
rep(j, 2451) chmin(ans, dist[i][j]);
cout << ans << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
template <class T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
template <class T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
struct edge {
int to, silver, cost;
};
#define PP pair<int, P>
int n, m, s, c[50], d[50], dist[50][2451];
vector<edge> v[50];
void dijkstra() {
rep(i, 50) {
rep(j, 2451) { dist[i][j] = INF; }
}
priority_queue<PP, vector<PP>, greater<PP>> q;
q.push({0, {0, s}});
dist[0][s] = 0;
while (!q.empty()) {
PP p = q.top();
q.pop();
if (dist[p.se.fi][p.se.se] != p.fi)
continue;
for (int i = 0; p.se.se + c[p.se.fi] * i <= 2450; i++) {
for (edge e : v[p.se.fi]) {
if (p.se.se + c[p.se.fi] * i - e.silver < 0)
continue;
if (dist[e.to][p.se.se + c[p.se.fi] * i - e.silver] >
p.fi + d[p.se.fi] * i + e.cost) {
dist[e.to][p.se.se + c[p.se.fi] * i - e.silver] =
p.fi + d[p.se.fi] * i + e.cost;
q.push({dist[e.to][p.se.se + c[p.se.fi] * i - e.silver],
{e.to, p.se.se + c[p.se.fi] * i - e.silver}});
}
}
}
}
}
signed main() {
cin >> n >> m >> s;
chmin(s, 2450ll);
rep(i, m) {
int u, w, a, b;
cin >> u >> w >> a >> b;
u--, w--;
v[u].pb({w, a, b});
v[w].pb({u, a, b});
}
rep(i, n) cin >> c[i] >> d[i];
dijkstra();
rep(i, n) {
if (i) {
int ans = INF;
rep(j, 2451) chmin(ans, dist[i][j]);
cout << ans << endl;
}
}
return 0;
}
| delete | 20 | 70 | 20 | 20 | TLE | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
const ll VMAX = 50;
const ll SMAX = VMAX * 50;
const ll INF = 1LL << 60;
struct Edge {
ll to, a, b;
Edge(ll to, ll a, ll b) : to(to), a(a), b(b) {}
};
struct Node {
ll v; // ノード
ll t, s; // 時間 , 所持金
Node(ll v, ll t, ll s) : v(v), t(t), s(s) {}
bool operator<(const Node &x) const { return (t > x.t); }
};
vector<Edge> V[VMAX];
vector<pair<ll, ll>> C;
ll dp[VMAX][SMAX]; // dp[i][j] : 頂点iで所持金jのときの最短時間
int main() {
ll n, m, s;
cin >> n >> m >> s;
s = min(s, SMAX - 1);
rep(i, m) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
V[u].push_back(Edge(v, a, b));
V[v].push_back(Edge(u, a, b));
}
C.resize(n);
rep(i, n) {
ll c, d;
cin >> c >> d;
C[i] = make_pair(c, d);
}
priority_queue<Node> Q;
auto push = [&](ll v, ll t, ll s) {
if (s < 0)
return; // お金が足りないとreturn
if (dp[v][s] <= t)
return; // 既に他の最適経路があればreturn
dp[v][s] = t;
Q.emplace(v, t, s);
};
rep(i, VMAX) rep(j, SMAX) dp[i][j] = INF;
push(0, 0, s);
while (!Q.empty()) {
auto node = Q.top();
Q.pop();
ll v = node.v;
ll t = node.t;
ll s = node.s;
if (dp[v][s] != t)
continue;
// 換金するパターン
ll c = C[v].first;
ll d = C[v].second;
push(v, t + d, min(s + c, SMAX));
// 別のノードにいくパターン
for (Edge x : V[v]) {
ll a = x.a;
ll b = x.b;
ll to = x.to;
push(to, t + b, s - a);
}
}
for (int i = 1; i < n; i++) {
ll ans = INF;
rep(j, SMAX) { ans = min(ans, dp[i][j]); }
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
const ll VMAX = 50;
const ll SMAX = VMAX * 50;
const ll INF = 1LL << 60;
struct Edge {
ll to, a, b;
Edge(ll to, ll a, ll b) : to(to), a(a), b(b) {}
};
struct Node {
ll v; // ノード
ll t, s; // 時間 , 所持金
Node(ll v, ll t, ll s) : v(v), t(t), s(s) {}
bool operator<(const Node &x) const { return (t > x.t); }
};
vector<Edge> V[VMAX];
vector<pair<ll, ll>> C;
ll dp[VMAX][SMAX]; // dp[i][j] : 頂点iで所持金jのときの最短時間
int main() {
ll n, m, s;
cin >> n >> m >> s;
s = min(s, SMAX - 1);
rep(i, m) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
V[u].push_back(Edge(v, a, b));
V[v].push_back(Edge(u, a, b));
}
C.resize(n);
rep(i, n) {
ll c, d;
cin >> c >> d;
C[i] = make_pair(c, d);
}
priority_queue<Node> Q;
auto push = [&](ll v, ll t, ll s) {
if (s < 0)
return; // お金が足りないとreturn
if (dp[v][s] <= t)
return; // 既に他の最適経路があればreturn
dp[v][s] = t;
Q.emplace(v, t, s);
};
rep(i, VMAX) rep(j, SMAX) dp[i][j] = INF;
push(0, 0, s);
while (!Q.empty()) {
auto node = Q.top();
Q.pop();
ll v = node.v;
ll t = node.t;
ll s = node.s;
if (dp[v][s] != t)
continue;
// 換金するパターン
ll c = C[v].first;
ll d = C[v].second;
push(v, t + d, min(s + c, SMAX - 1));
// 別のノードにいくパターン
for (Edge x : V[v]) {
ll a = x.a;
ll b = x.b;
ll to = x.to;
push(to, t + b, s - a);
}
}
for (int i = 1; i < n; i++) {
ll ans = INF;
rep(j, SMAX) { ans = min(ans, dp[i][j]); }
cout << ans << endl;
}
return 0;
} | replace | 70 | 71 | 70 | 71 | 0 | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;
#define MAX 2500
signed main() {
int n, m, s;
cin >> n >> m >> s;
using T = tuple<int, ll, ll>;
vector<T> G[n];
int u, v, a, b;
rep(i, m) {
cin >> u >> v >> a >> b;
u--;
v--;
G[u].emplace_back(v, a, b);
G[v].emplace_back(u, a, b);
}
ll c[n], d[n];
rep(i, n) { cin >> c[i] >> d[i]; }
ll dist[n][MAX + 1];
fill(dist[0], dist[n], LINF);
priority_queue<T, vector<T>, greater<T>> que;
for (int j = 0;; j++) {
int nxt = min(s + c[0] * j, (ll)MAX);
dist[0][nxt] = d[0] * j;
que.emplace(dist[0][nxt], nxt, 0);
if (s + c[0] * j > MAX)
break;
}
while (!que.empty()) {
T t = que.top();
que.pop();
int cnt = get<1>(t), v = get<2>(t);
if (dist[v][cnt] < get<0>(t))
continue;
for (int i = 0; i < G[v].size(); i++) {
int to = get<0>(G[v][i]), need = get<1>(G[v][i]);
ll cost = get<2>(G[v][i]);
if (cnt - need < 0)
continue;
for (int j = 0;; j++) {
int nxt = min(cnt - need + c[to] * j, (ll)MAX);
if (dist[to][nxt] > dist[v][cnt] + cost + d[to] * j) {
dist[to][nxt] = dist[v][cnt] + cost + d[to] * j;
que.emplace(dist[to][nxt], nxt, to);
}
if (cnt - need + c[to] * j > MAX)
break;
}
}
}
for (int v = 1; v < n; v++) {
ll ans = LINF;
for (int j = 0; j <= MAX; j++) {
ans = min(ans, dist[v][j]);
}
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;
#define MAX 2500
signed main() {
int n, m, s;
cin >> n >> m >> s;
using T = tuple<ll, ll, ll>;
vector<T> G[n];
int u, v, a, b;
rep(i, m) {
cin >> u >> v >> a >> b;
u--;
v--;
G[u].emplace_back(v, a, b);
G[v].emplace_back(u, a, b);
}
ll c[n], d[n];
rep(i, n) { cin >> c[i] >> d[i]; }
ll dist[n][MAX + 1];
fill(dist[0], dist[n], LINF);
priority_queue<T, vector<T>, greater<T>> que;
for (int j = 0;; j++) {
int nxt = min(s + c[0] * j, (ll)MAX);
dist[0][nxt] = d[0] * j;
que.emplace(dist[0][nxt], nxt, 0);
if (s + c[0] * j > MAX)
break;
}
while (!que.empty()) {
T t = que.top();
que.pop();
int cnt = get<1>(t), v = get<2>(t);
if (dist[v][cnt] < get<0>(t))
continue;
for (int i = 0; i < G[v].size(); i++) {
int to = get<0>(G[v][i]), need = get<1>(G[v][i]);
ll cost = get<2>(G[v][i]);
if (cnt - need < 0)
continue;
for (int j = 0;; j++) {
int nxt = min(cnt - need + c[to] * j, (ll)MAX);
if (dist[to][nxt] > dist[v][cnt] + cost + d[to] * j) {
dist[to][nxt] = dist[v][cnt] + cost + d[to] * j;
que.emplace(dist[to][nxt], nxt, to);
}
if (cnt - need + c[to] * j > MAX)
break;
}
}
}
for (int v = 1; v < n; v++) {
ll ans = LINF;
for (int j = 0; j <= MAX; j++) {
ans = min(ans, dist[v][j]);
}
cout << ans << endl;
}
return 0;
} | replace | 15 | 16 | 15 | 16 | TLE | |
p02703 | C++ | Runtime Error | /*
author: madhav_1999 aka orthodoxparadox
26 April 2020 at 5:53 PM
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define dbl long double
#define int ll
#define ll long long
#define pii pair<int, int>
#define len(x) (int)x.size()
#define rev(a) reverse(all(a))
#define oiint ostream_iterator<int>(cout, " ")
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define onlyunique(v) v.erase(unique(all(v)), v.end()); // only for sorted
// vector
#define inf 2e18
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#ifdef MADHAV
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " ";
__f(comma + 1, args...);
}
#else
#define dbg(...) 42
#endif
template <typename X> ostream &operator<<(ostream &x, const vector<X> &v) {
for (int i = 0; i < (int)v.size(); ++i)
x << v[i] << " ";
return x;
}
template <typename X> ostream &operator<<(ostream &x, const set<X> &v) {
for (auto it : v)
x << it << " ";
return x;
}
template <typename X> ostream &operator<<(ostream &x, const multiset<X> &v) {
for (auto it : v)
x << it << " ";
return x;
}
template <typename X, typename Y>
ostream &operator<<(ostream &x, const pair<X, Y> &v) {
x << v.ff << " " << v.ss;
return x;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
for (auto it : v)
os << it.first << "=>" << it.second << endl;
return os;
}
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct pair_hash {
inline std::size_t operator()(const std::pair<int, int> &v) const {
return v.first * 31 + v.second;
}
};
const int MOD = 1e9 + 7;
int takemod(int a, int mod = MOD) {
a %= mod;
if (a < 0)
a += mod;
return a;
}
int fast_exp(int base, int expo, int mod = MOD) {
int res = 1;
while (expo > 0) {
if (expo & 1)
res = (res * base) % mod;
base = (base * base) % mod;
expo >>= 1;
}
return res;
}
int modinv(int a, int mod = MOD) {
return takemod(fast_exp(takemod(a, mod), mod - 2, mod), mod);
}
const int N = 52;
const int K = 5004;
int dist[N][K];
bool vis[N][K];
int c[N];
int d[N];
vector<pair<int, pii>> graph[N];
void dijkstra(int src, int coins, int n) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j < K; j++) {
dist[i][j] = inf;
vis[i][j] = false;
}
}
dist[src][coins] = 0;
priority_queue<pair<int, pii>, vector<pair<int, pii>>,
greater<pair<int, pii>>>
pq;
pq.push({dist[src][coins], {src, coins}});
while (!pq.empty()) {
int node = pq.top().ss.ff;
int cn = pq.top().ss.ss;
pq.pop();
if (vis[node][cn]) {
continue;
}
vis[node][cn] = true;
for (auto it : graph[node]) {
if (it.ss.ff <= cn) {
if (dist[it.ff][cn - it.ss.ff] > dist[node][cn] + it.ss.ss) {
dist[it.ff][cn - it.ss.ff] = dist[node][cn] + it.ss.ss;
pq.push({dist[it.ff][cn - it.ss.ff], {it.ff, cn - it.ss.ff}});
}
}
}
if (dist[node][cn + c[node]] > dist[node][cn] + d[node]) {
dist[node][cn + c[node]] = dist[node][cn] + d[node];
pq.push({dist[node][cn + c[node]], {node, cn + c[node]}});
}
}
}
void solve() {
int n, m, s;
cin >> n >> m >> s;
s = min(s, 5000LL);
for (int i = 0; i < m; i++) {
int u, v, a, b;
cin >> u >> v >> a >> b;
graph[u].push_back({v, {a, b}});
graph[v].push_back({u, {a, b}});
}
for (int i = 1; i <= n; i++) {
cin >> c[i] >> d[i];
}
dijkstra(1, s, n);
for (int i = 2; i <= n; i++) {
int ans = inf;
for (int j = 0; j < K; j++) {
ans = min(ans, dist[i][j]);
}
cout << ans << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifdef MADHAV
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
clock_t t1, t2;
t1 = clock();
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
}
#ifdef MADHAV
t2 = clock();
cout << "\n\ntime taken: " << (t2 - t1) / (dbl)CLOCKS_PER_SEC;
#endif
return 0;
} | /*
author: madhav_1999 aka orthodoxparadox
26 April 2020 at 5:53 PM
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define dbl long double
#define int ll
#define ll long long
#define pii pair<int, int>
#define len(x) (int)x.size()
#define rev(a) reverse(all(a))
#define oiint ostream_iterator<int>(cout, " ")
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define onlyunique(v) v.erase(unique(all(v)), v.end()); // only for sorted
// vector
#define inf 2e18
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#ifdef MADHAV
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " ";
__f(comma + 1, args...);
}
#else
#define dbg(...) 42
#endif
template <typename X> ostream &operator<<(ostream &x, const vector<X> &v) {
for (int i = 0; i < (int)v.size(); ++i)
x << v[i] << " ";
return x;
}
template <typename X> ostream &operator<<(ostream &x, const set<X> &v) {
for (auto it : v)
x << it << " ";
return x;
}
template <typename X> ostream &operator<<(ostream &x, const multiset<X> &v) {
for (auto it : v)
x << it << " ";
return x;
}
template <typename X, typename Y>
ostream &operator<<(ostream &x, const pair<X, Y> &v) {
x << v.ff << " " << v.ss;
return x;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
for (auto it : v)
os << it.first << "=>" << it.second << endl;
return os;
}
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct pair_hash {
inline std::size_t operator()(const std::pair<int, int> &v) const {
return v.first * 31 + v.second;
}
};
const int MOD = 1e9 + 7;
int takemod(int a, int mod = MOD) {
a %= mod;
if (a < 0)
a += mod;
return a;
}
int fast_exp(int base, int expo, int mod = MOD) {
int res = 1;
while (expo > 0) {
if (expo & 1)
res = (res * base) % mod;
base = (base * base) % mod;
expo >>= 1;
}
return res;
}
int modinv(int a, int mod = MOD) {
return takemod(fast_exp(takemod(a, mod), mod - 2, mod), mod);
}
const int N = 52;
const int K = 5004;
int dist[N][K];
bool vis[N][K];
int c[N];
int d[N];
vector<pair<int, pii>> graph[N];
void dijkstra(int src, int coins, int n) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j < K; j++) {
dist[i][j] = inf;
vis[i][j] = false;
}
}
dist[src][coins] = 0;
priority_queue<pair<int, pii>, vector<pair<int, pii>>,
greater<pair<int, pii>>>
pq;
pq.push({dist[src][coins], {src, coins}});
while (!pq.empty()) {
int node = pq.top().ss.ff;
int cn = pq.top().ss.ss;
pq.pop();
if (vis[node][cn]) {
continue;
}
vis[node][cn] = true;
for (auto it : graph[node]) {
if (it.ss.ff <= cn) {
if (dist[it.ff][cn - it.ss.ff] > dist[node][cn] + it.ss.ss) {
dist[it.ff][cn - it.ss.ff] = dist[node][cn] + it.ss.ss;
pq.push({dist[it.ff][cn - it.ss.ff], {it.ff, cn - it.ss.ff}});
}
}
}
if (cn + c[node] < K) {
if (dist[node][cn + c[node]] > dist[node][cn] + d[node]) {
dist[node][cn + c[node]] = dist[node][cn] + d[node];
pq.push({dist[node][cn + c[node]], {node, cn + c[node]}});
}
}
}
}
void solve() {
int n, m, s;
cin >> n >> m >> s;
s = min(s, 5000LL);
for (int i = 0; i < m; i++) {
int u, v, a, b;
cin >> u >> v >> a >> b;
graph[u].push_back({v, {a, b}});
graph[v].push_back({u, {a, b}});
}
for (int i = 1; i <= n; i++) {
cin >> c[i] >> d[i];
}
dijkstra(1, s, n);
for (int i = 2; i <= n; i++) {
int ans = inf;
for (int j = 0; j < K; j++) {
ans = min(ans, dist[i][j]);
}
cout << ans << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifdef MADHAV
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
clock_t t1, t2;
t1 = clock();
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
}
#ifdef MADHAV
t2 = clock();
cout << "\n\ntime taken: " << (t2 - t1) / (dbl)CLOCKS_PER_SEC;
#endif
return 0;
} | replace | 142 | 145 | 142 | 147 | 0 | |
p02703 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#define MOD 1000000007
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define FOR(i, j, k) for (ll i = j; i < k; i++)
#define DUMP(i, v) \
for (ll i = 0; i < v.size(); i++) \
cout << v[i] << " "
using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge {
ll x, c;
};
/*
struct node{ll cost, to;
bool friend operator>(node a, node b){
return a.cost>b.cost;
}
};
*/
ll mod(ll a, ll mod) {
ll res = a % mod;
if (res < 0)
res = res + mod;
return res;
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); }
ll gcd(ll a, ll b) {
ll r = a % b;
if (r == 0)
return b;
else
return gcd(b, a % b);
}
bool is_prime(ll n) {
ll i = 2;
if (n == 1)
return false;
if (n == 2)
return true;
bool res = true;
while (i * i < n) {
if (n % i == 0) {
res = false;
}
i = i + 1;
}
// if(i==1)res = false;
if (n % i == 0)
res = false;
return res;
}
ll N, M, S;
llvec U, V, A, B;
llvec C, D;
ll Amax;
ll Cmax;
ll Nn; // number of nodes;
vector<vector<edge>> e;
ll state(ll x, ll c) { return x * (Cmax + 1) + c; }
/**************************************
** A main function starts from here **
***************************************/
int main() {
cin >> N >> M >> S;
U = llvec(M);
V = llvec(M);
A = llvec(M);
B = llvec(M);
C = llvec(N);
D = llvec(N);
Amax = 0;
rep(i, M) {
cin >> U[i] >> V[i] >> A[i] >> B[i];
Amax = max(Amax, A[i]);
}
rep(i, N) { cin >> C[i] >> D[i]; }
Cmax = (Amax) * (N - 1);
S = min(S, Cmax);
Nn = (Cmax + 1) * N;
e = vector<vector<edge>>(Nn);
// cerr << Nn << " " << Cmax << " " << Amax << endl;
rep(i, M) {
FOR(j, A[i], Cmax + 1) {
e[state(U[i] - 1, j)].push_back({state(V[i] - 1, j - A[i]), B[i]});
e[state(V[i] - 1, j)].push_back({state(U[i] - 1, j - A[i]), B[i]});
}
}
rep(i, N) {
rep(j, Cmax) {
e[state(i, j)].push_back({state(i, min(j + C[i], Cmax)), D[i]});
}
}
llvec d(Nn, 1e18);
priority_queue<P, vector<P>, greater<P>> que;
que.push({0, state(0, S)});
d[state(0, S)] = 0;
while (!que.empty()) {
P n = que.top();
que.pop();
if (d[n.second] < n.first)
continue;
for (auto i : e[n.second]) {
ll a = i.x;
if (d[a] < d[n.second] + i.c) {
continue;
} else {
d[a] = d[n.second] + i.c;
que.push({d[a], a});
}
}
}
FOR(i, 1, N) {
ll ans = 1e18;
rep(c, Cmax + 1) { ans = min(d[state(i, c)], ans); }
cout << ans << endl;
}
return 0;
}
| #include "bits/stdc++.h"
#define MOD 1000000007
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define FOR(i, j, k) for (ll i = j; i < k; i++)
#define DUMP(i, v) \
for (ll i = 0; i < v.size(); i++) \
cout << v[i] << " "
using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge {
ll x, c;
};
/*
struct node{ll cost, to;
bool friend operator>(node a, node b){
return a.cost>b.cost;
}
};
*/
ll mod(ll a, ll mod) {
ll res = a % mod;
if (res < 0)
res = res + mod;
return res;
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); }
ll gcd(ll a, ll b) {
ll r = a % b;
if (r == 0)
return b;
else
return gcd(b, a % b);
}
bool is_prime(ll n) {
ll i = 2;
if (n == 1)
return false;
if (n == 2)
return true;
bool res = true;
while (i * i < n) {
if (n % i == 0) {
res = false;
}
i = i + 1;
}
// if(i==1)res = false;
if (n % i == 0)
res = false;
return res;
}
ll N, M, S;
llvec U, V, A, B;
llvec C, D;
ll Amax;
ll Cmax;
ll Nn; // number of nodes;
vector<vector<edge>> e;
ll state(ll x, ll c) { return x * (Cmax + 1) + c; }
/**************************************
** A main function starts from here **
***************************************/
int main() {
cin >> N >> M >> S;
U = llvec(M);
V = llvec(M);
A = llvec(M);
B = llvec(M);
C = llvec(N);
D = llvec(N);
Amax = 0;
rep(i, M) {
cin >> U[i] >> V[i] >> A[i] >> B[i];
Amax = max(Amax, A[i]);
}
rep(i, N) { cin >> C[i] >> D[i]; }
Cmax = (Amax) * (N - 1);
S = min(S, Cmax);
Nn = (Cmax + 1) * N;
e = vector<vector<edge>>(Nn);
// cerr << Nn << " " << Cmax << " " << Amax << endl;
rep(i, M) {
FOR(j, A[i], Cmax + 1) {
e[state(U[i] - 1, j)].push_back({state(V[i] - 1, j - A[i]), B[i]});
e[state(V[i] - 1, j)].push_back({state(U[i] - 1, j - A[i]), B[i]});
}
}
rep(i, N) {
rep(j, Cmax) {
e[state(i, j)].push_back({state(i, min(j + C[i], Cmax)), D[i]});
}
}
llvec d(Nn, 1e18);
priority_queue<P, vector<P>, greater<P>> que;
que.push({0, state(0, S)});
d[state(0, S)] = 0;
while (!que.empty()) {
P n = que.top();
que.pop();
if (d[n.second] < n.first)
continue;
for (auto i : e[n.second]) {
ll a = i.x;
if (d[a] <= d[n.second] + i.c) {
continue;
} else {
d[a] = d[n.second] + i.c;
que.push({d[a], a});
}
}
}
FOR(i, 1, N) {
ll ans = 1e18;
rep(c, Cmax + 1) { ans = min(d[state(i, c)], ans); }
cout << ans << endl;
}
return 0;
}
| replace | 134 | 135 | 134 | 135 | TLE | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep2(i, m, n) for (int i = int(m); i < int(n); ++i)
#define drep2(i, m, n) for (int i = int(m - 1); i >= int(n); --i)
#define rep(i, n) rep2(i, 0, n)
#define drep(i, n) drep2(i, n, 0)
#define all(a) a.begin(), a.end()
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using Vbo = vector<bool>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using VVbo = vector<Vbo>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
struct fast_ios {
fast_ios() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios_;
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v)
is >> e;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto &e : v)
os << e << " ";
return os;
}
template <typename T> inline int count_between(vector<T> &a, T l, T r) {
return lower_bound(all(a), r) - lower_bound(all(a), l);
} // [l, r)
inline int Log2(ll x) {
int k;
for (k = 0; x > 0; ++k)
x >>= 1;
return k;
} // number of binary digits
const int INF = 1 << 30;
const ll INFll = 1ll << 62;
const ld EPS = 1e-10;
const int MOD = int(1e9) + 7;
template <typename T> struct edge {
int v;
T w;
edge(int v, T w) : v(v), w(w) {}
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using Graph = vector<Edges<T>>;
template <typename T> vector<T> dijkstra(Graph<T> &G, int s) {
const auto INF = numeric_limits<T>::max();
vector<T> dist(G.size(), INF);
using PTi = pair<T, int>;
priority_queue<PTi, vector<PTi>, greater<PTi>> pq;
dist[s] = 0;
pq.emplace(dist[s], s);
while (!pq.empty()) {
T cost;
int u;
tie(cost, u) = pq.top();
pq.pop();
if (dist[u] < cost)
continue;
for (auto &e : G[u]) {
auto n_cost = cost + e.w;
if (dist[e.v] <= n_cost)
continue;
dist[e.v] = n_cost;
pq.emplace(dist[e.v], e.v);
}
}
return dist;
}
int main() {
ll n, m, s;
cin >> n >> m >> s;
ll M = n * 50;
Graph<ll> G(n * M);
for (int i = 0; i < m; ++i) {
int u, v, a, b;
cin >> u >> v >> a >> b;
--u, --v;
rep(k, M + 1) {
int uu, vv;
uu = u + n * k;
vv = v + n * (k - a);
if (vv >= 0)
G[uu].emplace_back(vv, b);
uu = u + n * (k - a);
vv = v + n * k;
if (uu >= 0)
G[vv].emplace_back(uu, b);
}
}
Vll c(n), d(n);
rep(i, n) cin >> c[i] >> d[i];
rep(i, n) rep(k, M + 1) {
int u, v;
u = i + n * k;
v = i + n * min(k + c[i], M);
G[u].emplace_back(v, d[i]);
v = i + n * (k - 1);
if (v >= 0)
G[u].emplace_back(v, 0);
}
ll ss = min(s, M) * n;
auto dist = dijkstra(G, ss);
rep2(i, 1, n) cout << dist[i] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep2(i, m, n) for (int i = int(m); i < int(n); ++i)
#define drep2(i, m, n) for (int i = int(m - 1); i >= int(n); --i)
#define rep(i, n) rep2(i, 0, n)
#define drep(i, n) drep2(i, n, 0)
#define all(a) a.begin(), a.end()
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using Vbo = vector<bool>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using VVbo = vector<Vbo>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
struct fast_ios {
fast_ios() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios_;
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v)
is >> e;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto &e : v)
os << e << " ";
return os;
}
template <typename T> inline int count_between(vector<T> &a, T l, T r) {
return lower_bound(all(a), r) - lower_bound(all(a), l);
} // [l, r)
inline int Log2(ll x) {
int k;
for (k = 0; x > 0; ++k)
x >>= 1;
return k;
} // number of binary digits
const int INF = 1 << 30;
const ll INFll = 1ll << 62;
const ld EPS = 1e-10;
const int MOD = int(1e9) + 7;
template <typename T> struct edge {
int v;
T w;
edge(int v, T w) : v(v), w(w) {}
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using Graph = vector<Edges<T>>;
template <typename T> vector<T> dijkstra(Graph<T> &G, int s) {
const auto INF = numeric_limits<T>::max();
vector<T> dist(G.size(), INF);
using PTi = pair<T, int>;
priority_queue<PTi, vector<PTi>, greater<PTi>> pq;
dist[s] = 0;
pq.emplace(dist[s], s);
while (!pq.empty()) {
T cost;
int u;
tie(cost, u) = pq.top();
pq.pop();
if (dist[u] < cost)
continue;
for (auto &e : G[u]) {
auto n_cost = cost + e.w;
if (dist[e.v] <= n_cost)
continue;
dist[e.v] = n_cost;
pq.emplace(dist[e.v], e.v);
}
}
return dist;
}
int main() {
ll n, m, s;
cin >> n >> m >> s;
ll M = n * 50;
Graph<ll> G(n * (M + 1));
for (int i = 0; i < m; ++i) {
int u, v, a, b;
cin >> u >> v >> a >> b;
--u, --v;
rep(k, M + 1) {
int uu, vv;
uu = u + n * k;
vv = v + n * (k - a);
if (vv >= 0)
G[uu].emplace_back(vv, b);
uu = u + n * (k - a);
vv = v + n * k;
if (uu >= 0)
G[vv].emplace_back(uu, b);
}
}
Vll c(n), d(n);
rep(i, n) cin >> c[i] >> d[i];
rep(i, n) rep(k, M + 1) {
int u, v;
u = i + n * k;
v = i + n * min(k + c[i], M);
G[u].emplace_back(v, d[i]);
v = i + n * (k - 1);
if (v >= 0)
G[u].emplace_back(v, 0);
}
ll ss = min(s, M) * n;
auto dist = dijkstra(G, ss);
rep2(i, 1, n) cout << dist[i] << endl;
return 0;
}
| replace | 116 | 117 | 116 | 117 | -11 | |
p02703 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define ll long long
#define db double
#define clr(a, b) memset(a, b, sizeof(a))
const int maxn = 60;
const int tot = 2600;
const ll inf = 1e18;
int n, m;
ll s;
vector<pair<int, pair<int, int>>> G[maxn];
ll dp[maxn][tot];
ll c[120], d[120];
void dijk() {
for (int i = 0; i < maxn; i++)
for (int j = 0; j < tot; j++)
dp[i][j] = inf;
priority_queue<pair<ll, pair<int, int>>, vector<pair<ll, pair<int, int>>>,
greater<pair<ll, pair<int, int>>>>
pq;
pq.push({0, {1, s}});
pair<ll, pair<int, int>> tp;
while (!pq.empty()) {
tp = pq.top(), pq.pop();
int nowpos = tp.second.first;
ll nowcost = tp.first;
ll nows = (ll)tp.second.second;
for (auto &to : G[nowpos]) {
ll newcost = nowcost + to.second.second;
ll nwe = nows - to.second.first;
if (nwe >= 0) {
if (dp[to.first][nwe] > newcost) {
dp[to.first][nwe] = newcost;
pq.push({newcost, {to.first, nwe}});
}
}
}
ll nwecost = nowcost + d[nowpos];
ll nwe = min(nows + c[nowpos], (ll)tot - 1);
if (dp[nowpos][nwe] > nwecost) {
dp[nowpos][nwe] = nwecost;
pq.push({nwecost, {nowpos, nwe}});
}
}
}
void solve() {
cin >> n >> m >> s;
int u, v, a, b;
for (int i = 1; i <= m; i++) {
cin >> u >> v >> a >> b;
G[u].push_back({v, {a, b}});
G[v].push_back({u, {a, b}});
}
for (int i = 1; i <= n; i++)
cin >> c[i] >> d[i];
dijk();
ll ans;
for (int i = 2; i <= n; i++) {
ans = inf;
for (int j = 0; j < tot; j++)
ans = min(dp[i][j], ans);
cout << ans << "\n";
}
}
int main() {
// freopen("in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
// cin >> T;
while (T-- > 0)
solve();
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define ll long long
#define db double
#define clr(a, b) memset(a, b, sizeof(a))
const int maxn = 60;
const int tot = 2600;
const ll inf = 1e18;
int n, m;
ll s;
vector<pair<int, pair<int, int>>> G[maxn];
ll dp[maxn][tot];
ll c[120], d[120];
void dijk() {
for (int i = 0; i < maxn; i++)
for (int j = 0; j < tot; j++)
dp[i][j] = inf;
priority_queue<pair<ll, pair<int, int>>, vector<pair<ll, pair<int, int>>>,
greater<pair<ll, pair<int, int>>>>
pq;
pq.push({0, {1, s}});
pair<ll, pair<int, int>> tp;
while (!pq.empty()) {
tp = pq.top(), pq.pop();
int nowpos = tp.second.first;
ll nowcost = tp.first;
ll nows = (ll)tp.second.second;
for (auto &to : G[nowpos]) {
ll newcost = nowcost + to.second.second;
ll nwe = min(nows - to.second.first, (ll)tot - 1);
if (nwe >= 0) {
if (dp[to.first][nwe] > newcost) {
dp[to.first][nwe] = newcost;
pq.push({newcost, {to.first, nwe}});
}
}
}
ll nwecost = nowcost + d[nowpos];
ll nwe = min(nows + c[nowpos], (ll)tot - 1);
if (dp[nowpos][nwe] > nwecost) {
dp[nowpos][nwe] = nwecost;
pq.push({nwecost, {nowpos, nwe}});
}
}
}
void solve() {
cin >> n >> m >> s;
int u, v, a, b;
for (int i = 1; i <= m; i++) {
cin >> u >> v >> a >> b;
G[u].push_back({v, {a, b}});
G[v].push_back({u, {a, b}});
}
for (int i = 1; i <= n; i++)
cin >> c[i] >> d[i];
dijk();
ll ans;
for (int i = 2; i <= n; i++) {
ans = inf;
for (int j = 0; j < tot; j++)
ans = min(dp[i][j], ans);
cout << ans << "\n";
}
}
int main() {
// freopen("in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
// cin >> T;
while (T-- > 0)
solve();
} | replace | 39 | 40 | 39 | 40 | 0 | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, j, n) for (ll i = (ll)(j); i < (ll)(n); i++)
#define REP(i, j, n) for (ll i = (ll)(j); i <= (ll)(n); i++)
#define per(i, j, n) for (ll i = (ll)(j); (ll)(n) <= i; i--)
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (ll)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (ll)(key)))
#define pb emplace_back
#define mp std::make_pair
//
#define endl "\n"
// using std::endl;
using std::cin;
using std::cout;
using std::lower_bound;
using std::string;
using std::upper_bound;
using std::vector;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll, ll>;
//
constexpr ll MOD = 1e9 + 7;
// constexpr ll MOD=998244353;
// constexpr ll MOD=10000000;
// constexpr ll MOD=1e5;
constexpr ll MAX = 1e7;
constexpr ll INF = (1ll << 62);
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
template <typename T> struct Segment_tree {
ll N;
T mem;
vector<T> node;
Segment_tree(vector<T> &X, T m) : mem(m) {
ll sz = X.size();
N = 1;
while (N < sz)
N *= 2;
node.resize(2 * N - 1, mem);
rep(i, 0, sz) node[N - 1 + i] = X[i];
per(i, N - 2, 0) { node[i] = Compare(node[i * 2 + 1], node[i * 2 + 2]); }
}
T Compare(T &A, T &B) { return std::max(A, B); }
void update(ll X, T val) {
X += N - 1;
node[X] = val;
while (X > 0) {
X = (X - 1) / 2;
node[X] = Compare(node[X * 2 + 1], node[X * 2 + 2]);
}
}
T Query(ll a, ll b, ll now, ll l, ll r) { //[a,b),[l,r)
if (r < 0)
r = N;
if (r <= a || b <= l)
return mem;
if (a <= l && r <= b)
return node[now];
auto vl = Query(a, b, now * 2 + 1, l, (l + r) / 2),
vr = Query(a, b, now * 2 + 2, (l + r) / 2, r);
return Compare(vl, vr);
}
};
struct Binary_indexed_tree {
int N;
vi bit;
Binary_indexed_tree(int n) : N(n) { bit.resize(N + 1, 0); }
void add(int x, int a) {
for (x; x <= N; x += (x & -x))
bit[x] += a;
}
ll sum(int x) {
ll ret = 0;
for (x; x > 0; x -= (x & -x))
ret += bit[x];
return ret;
}
ll lower_bound(ll X) {
if (sum(N) < X)
return -1;
ll ret = 0, memo = 1, sum = 0;
while (memo * 2 <= N)
memo *= 2;
while (memo > 0) {
if (memo + ret <= N && sum + bit[memo + ret] < X) {
sum += bit[memo + ret];
ret += memo;
}
memo /= 2;
}
return ret + 1;
}
};
struct Union_Find {
ll N;
vi par;
vi siz;
Union_Find(int n) : N(n) {
par.resize(N);
siz.resize(N, 1);
rep(i, 0, N) par[i] = i;
}
ll root(ll X) {
if (par[X] == X)
return X;
return par[X] = root(par[X]);
}
bool same(ll X, ll Y) { return root(X) == root(Y); }
void unite(ll X, ll Y) {
X = root(X);
Y = root(Y);
if (X == Y)
return;
par[X] = Y;
siz[Y] += siz[X];
siz[X] = 0;
}
ll size(ll X) { return siz[root(X)]; }
};
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
vi fac, finv, inv;
void COMinit() {
fac.resize(MAX);
finv.resize(MAX);
inv.resize(MAX);
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll COM(ll n, ll r) {
if (n < r || n < 0 || r < 0)
return 0;
return fac[n] * finv[r] % MOD * finv[n - r] % MOD;
}
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
ll N, M, S;
cin >> N >> M >> S;
vector<vector<std::tuple<ll, ll, ll>>> edge(N);
rep(i, 0, M) {
ll U, V, A, B;
cin >> U >> V >> A >> B;
edge[U - 1].pb(std::make_tuple(V - 1, A, B));
edge[V - 1].pb(std::make_tuple(U - 1, A, B));
}
vi C(N), D(N);
rep(i, 0, N) cin >> C[i] >> D[i];
constexpr ll max = 25000;
S = std::min(S, max);
vector<vii> dp(N, vii(N, vi(max + 1, INF)));
prique<std::tuple<ll, ll, ll, ll>> que;
ll m = S, c = 0;
while (m <= max) {
que.push(std::make_tuple(c * D[0], m, 0, 0));
dp[0][0][m] = c * D[0];
m += C[0];
c++;
}
while (!que.empty()) {
ll time, money, from, X;
std::tie(time, money, from, X) = que.top();
que.pop();
if (dp[X][from][money] < time)
continue;
rep(i, 0, edge[X].size()) {
ll Y, silver, dist;
std::tie(Y, silver, dist) = edge[X][i];
silver = money - silver;
dist += time;
if (silver < 0) {
ll memo = (abs(silver) + C[from] - 1) / C[from];
dist += memo * D[from];
silver += memo * C[from];
silver = std::min(silver, max);
}
if (dp[Y][from][silver] > dist) {
dp[Y][from][silver] = dist;
que.push(std::make_tuple(dist, silver, from, Y));
}
silver += C[from];
dist += D[from];
if (silver < 0) {
ll memo = (abs(silver) + C[from] - 1) / C[from];
dist += memo * D[from];
silver += memo * C[from];
silver = std::min(silver, max);
}
}
from = X;
if (dp[X][from][money] > time) {
dp[X][from][money] = time;
que.push(std::make_tuple(time, money, from, X));
}
}
rep(i, 1, N) {
ll ans = INF;
rep(j, 0, N) {
REP(k, 0, max) {
ll sum = dp[i][j][k];
ans = std::min(ans, sum);
}
}
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
// using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, j, n) for (ll i = (ll)(j); i < (ll)(n); i++)
#define REP(i, j, n) for (ll i = (ll)(j); i <= (ll)(n); i++)
#define per(i, j, n) for (ll i = (ll)(j); (ll)(n) <= i; i--)
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (ll)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (ll)(key)))
#define pb emplace_back
#define mp std::make_pair
//
#define endl "\n"
// using std::endl;
using std::cin;
using std::cout;
using std::lower_bound;
using std::string;
using std::upper_bound;
using std::vector;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll, ll>;
//
constexpr ll MOD = 1e9 + 7;
// constexpr ll MOD=998244353;
// constexpr ll MOD=10000000;
// constexpr ll MOD=1e5;
constexpr ll MAX = 1e7;
constexpr ll INF = (1ll << 62);
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
template <typename T> struct Segment_tree {
ll N;
T mem;
vector<T> node;
Segment_tree(vector<T> &X, T m) : mem(m) {
ll sz = X.size();
N = 1;
while (N < sz)
N *= 2;
node.resize(2 * N - 1, mem);
rep(i, 0, sz) node[N - 1 + i] = X[i];
per(i, N - 2, 0) { node[i] = Compare(node[i * 2 + 1], node[i * 2 + 2]); }
}
T Compare(T &A, T &B) { return std::max(A, B); }
void update(ll X, T val) {
X += N - 1;
node[X] = val;
while (X > 0) {
X = (X - 1) / 2;
node[X] = Compare(node[X * 2 + 1], node[X * 2 + 2]);
}
}
T Query(ll a, ll b, ll now, ll l, ll r) { //[a,b),[l,r)
if (r < 0)
r = N;
if (r <= a || b <= l)
return mem;
if (a <= l && r <= b)
return node[now];
auto vl = Query(a, b, now * 2 + 1, l, (l + r) / 2),
vr = Query(a, b, now * 2 + 2, (l + r) / 2, r);
return Compare(vl, vr);
}
};
struct Binary_indexed_tree {
int N;
vi bit;
Binary_indexed_tree(int n) : N(n) { bit.resize(N + 1, 0); }
void add(int x, int a) {
for (x; x <= N; x += (x & -x))
bit[x] += a;
}
ll sum(int x) {
ll ret = 0;
for (x; x > 0; x -= (x & -x))
ret += bit[x];
return ret;
}
ll lower_bound(ll X) {
if (sum(N) < X)
return -1;
ll ret = 0, memo = 1, sum = 0;
while (memo * 2 <= N)
memo *= 2;
while (memo > 0) {
if (memo + ret <= N && sum + bit[memo + ret] < X) {
sum += bit[memo + ret];
ret += memo;
}
memo /= 2;
}
return ret + 1;
}
};
struct Union_Find {
ll N;
vi par;
vi siz;
Union_Find(int n) : N(n) {
par.resize(N);
siz.resize(N, 1);
rep(i, 0, N) par[i] = i;
}
ll root(ll X) {
if (par[X] == X)
return X;
return par[X] = root(par[X]);
}
bool same(ll X, ll Y) { return root(X) == root(Y); }
void unite(ll X, ll Y) {
X = root(X);
Y = root(Y);
if (X == Y)
return;
par[X] = Y;
siz[Y] += siz[X];
siz[X] = 0;
}
ll size(ll X) { return siz[root(X)]; }
};
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
vi fac, finv, inv;
void COMinit() {
fac.resize(MAX);
finv.resize(MAX);
inv.resize(MAX);
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll COM(ll n, ll r) {
if (n < r || n < 0 || r < 0)
return 0;
return fac[n] * finv[r] % MOD * finv[n - r] % MOD;
}
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
ll N, M, S;
cin >> N >> M >> S;
vector<vector<std::tuple<ll, ll, ll>>> edge(N);
rep(i, 0, M) {
ll U, V, A, B;
cin >> U >> V >> A >> B;
edge[U - 1].pb(std::make_tuple(V - 1, A, B));
edge[V - 1].pb(std::make_tuple(U - 1, A, B));
}
vi C(N), D(N);
rep(i, 0, N) cin >> C[i] >> D[i];
constexpr ll max = 25000;
S = std::min(S, max);
vector<vii> dp(N, vii(N, vi(max + 1, INF)));
prique<std::tuple<ll, ll, ll, ll>> que;
ll m = S, c = 0;
while (m <= max && c < 10) {
que.push(std::make_tuple(c * D[0], m, 0, 0));
dp[0][0][m] = c * D[0];
m += C[0];
c++;
}
while (!que.empty()) {
ll time, money, from, X;
std::tie(time, money, from, X) = que.top();
que.pop();
if (dp[X][from][money] < time)
continue;
rep(i, 0, edge[X].size()) {
ll Y, silver, dist;
std::tie(Y, silver, dist) = edge[X][i];
silver = money - silver;
dist += time;
if (silver < 0) {
ll memo = (abs(silver) + C[from] - 1) / C[from];
dist += memo * D[from];
silver += memo * C[from];
silver = std::min(silver, max);
}
if (dp[Y][from][silver] > dist) {
dp[Y][from][silver] = dist;
que.push(std::make_tuple(dist, silver, from, Y));
}
silver += C[from];
dist += D[from];
if (silver < 0) {
ll memo = (abs(silver) + C[from] - 1) / C[from];
dist += memo * D[from];
silver += memo * C[from];
silver = std::min(silver, max);
}
}
from = X;
if (dp[X][from][money] > time) {
dp[X][from][money] = time;
que.push(std::make_tuple(time, money, from, X));
}
}
rep(i, 1, N) {
ll ans = INF;
rep(j, 0, N) {
REP(k, 0, max) {
ll sum = dp[i][j][k];
ans = std::min(ans, sum);
}
}
cout << ans << endl;
}
}
| replace | 189 | 190 | 189 | 190 | TLE | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
t = v;
}
template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
for (auto &e : t)
fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);
const int INF = 1LL << 60;
struct edge {
int to, cost, time;
bool operator<(const edge &a) const { return time > a.time; }
};
signed main() {
int n, m, s;
cin >> n >> m >> s;
vector<vector<edge>> g(n);
vector<int> a(m), b(m), c(n), d(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
cin >> a[i] >> b[i];
g[u].push_back({v, a[i], b[i]});
g[v].push_back({u, a[i], b[i]});
}
for (int i = 0; i < n; i++) {
cin >> c[i] >> d[i];
}
auto dp = make_vec<int>(n, n * 55);
fill(dp, INF);
priority_queue<edge> q; // 最小値
q.push({0, s, 0});
dp[0][s] = 0;
while (!q.empty()) {
edge p = q.top();
q.pop();
int u = p.to, cost = p.cost, t = p.time;
if (dp[u][cost] < t)
continue;
if (cost + c[u] < n * 50 && dp[u][cost + c[u]] > dp[u][cost] + d[u]) {
dp[u][cost + c[u]] = dp[u][cost] + d[u];
q.push({u, cost + c[u], dp[u][cost + c[u]]});
}
for (auto e : g[u]) {
if (cost - e.cost >= 0 &&
dp[e.to][cost - e.cost] > dp[u][cost] + e.time) {
dp[e.to][cost - e.cost] = dp[u][cost] + e.time;
q.push({e.to, cost - e.cost, dp[e.to][cost - e.cost]});
}
}
}
for (int i = 1; i < n; i++) {
// cout << dp[i] << endl;
cout << *min_element(dp[i].begin(), dp[i].end()) << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
t = v;
}
template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
for (auto &e : t)
fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);
const int INF = 1LL << 60;
struct edge {
int to, cost, time;
bool operator<(const edge &a) const { return time > a.time; }
};
signed main() {
int n, m, s;
cin >> n >> m >> s;
vector<vector<edge>> g(n);
vector<int> a(m), b(m), c(n), d(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
cin >> a[i] >> b[i];
g[u].push_back({v, a[i], b[i]});
g[v].push_back({u, a[i], b[i]});
}
for (int i = 0; i < n; i++) {
cin >> c[i] >> d[i];
}
auto dp = make_vec<int>(n, n * 55);
fill(dp, INF);
priority_queue<edge> q; // 最小値
s = min(s, n * 50);
q.push({0, s, 0});
dp[0][s] = 0;
while (!q.empty()) {
edge p = q.top();
q.pop();
int u = p.to, cost = p.cost, t = p.time;
if (dp[u][cost] < t)
continue;
if (cost + c[u] < n * 50 && dp[u][cost + c[u]] > dp[u][cost] + d[u]) {
dp[u][cost + c[u]] = dp[u][cost] + d[u];
q.push({u, cost + c[u], dp[u][cost + c[u]]});
}
for (auto e : g[u]) {
if (cost - e.cost >= 0 &&
dp[e.to][cost - e.cost] > dp[u][cost] + e.time) {
dp[e.to][cost - e.cost] = dp[u][cost] + e.time;
q.push({e.to, cost - e.cost, dp[e.to][cost - e.cost]});
}
}
}
for (int i = 1; i < n; i++) {
// cout << dp[i] << endl;
cout << *min_element(dp[i].begin(), dp[i].end()) << endl;
}
return 0;
} | insert | 50 | 50 | 50 | 51 | 0 | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll N;
cin >> N;
ll M;
cin >> M;
ll S;
cin >> S;
vector<ll> C(N), D(N);
vector<pair<ll, pair<ll, ll>>> G[N]; // to, coin, time
for (ll i = 0; i < M; i++) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
u--, v--;
G[u].push_back(make_pair(v, make_pair(a, b)));
G[v].push_back(make_pair(u, make_pair(a, b)));
}
for (ll i = 0; i < N; i++) {
cin >> C[i] >> D[i];
}
ll INF = 1e18;
vector<vector<ll>> dp(N, vector<ll>(2550, INF));
queue<pair<ll, pair<ll, ll>>> q; // time, location, coin
q.push(make_pair(0, make_pair(0, S)));
while (!q.empty()) {
pair<ll, pair<ll, ll>> c = q.front();
q.pop();
ll mins = c.first, loc = c.second.first, coin = c.second.second;
if (dp[loc][coin] <= mins)
continue;
dp[loc][coin] = mins;
for (auto n : G[loc]) {
ll new_mins = mins + n.second.second;
ll new_loc = n.first;
ll new_coin = coin - n.second.first;
if (new_coin < 0)
continue;
q.push(make_pair(new_mins, make_pair(new_loc, new_coin)));
}
ll new_mins = mins + D[loc];
ll new_coin = coin + C[loc];
if (new_coin > 2500)
new_coin = 2500;
q.push(make_pair(new_mins, make_pair(loc, new_coin)));
}
for (ll i = 1; i < N; i++) {
ll ans = INF;
for (ll j = 0; j < 2550; j++) {
ans = min(ans, dp[i][j]);
}
cout << ans << '\n';
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll N;
cin >> N;
ll M;
cin >> M;
ll S;
cin >> S;
vector<ll> C(N), D(N);
vector<pair<ll, pair<ll, ll>>> G[N]; // to, coin, time
for (ll i = 0; i < M; i++) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
u--, v--;
G[u].push_back(make_pair(v, make_pair(a, b)));
G[v].push_back(make_pair(u, make_pair(a, b)));
}
for (ll i = 0; i < N; i++) {
cin >> C[i] >> D[i];
}
S = min(S, (ll)2500);
ll INF = 1e18;
vector<vector<ll>> dp(N, vector<ll>(2550, INF));
queue<pair<ll, pair<ll, ll>>> q; // time, location, coin
q.push(make_pair(0, make_pair(0, S)));
while (!q.empty()) {
pair<ll, pair<ll, ll>> c = q.front();
q.pop();
ll mins = c.first, loc = c.second.first, coin = c.second.second;
if (dp[loc][coin] <= mins)
continue;
dp[loc][coin] = mins;
for (auto n : G[loc]) {
ll new_mins = mins + n.second.second;
ll new_loc = n.first;
ll new_coin = coin - n.second.first;
if (new_coin < 0)
continue;
q.push(make_pair(new_mins, make_pair(new_loc, new_coin)));
}
ll new_mins = mins + D[loc];
ll new_coin = coin + C[loc];
if (new_coin > 2500)
new_coin = 2500;
q.push(make_pair(new_mins, make_pair(loc, new_coin)));
}
for (ll i = 1; i < N; i++) {
ll ans = INF;
for (ll j = 0; j < 2550; j++) {
ans = min(ans, dp[i][j]);
}
cout << ans << '\n';
}
return 0;
} | insert | 29 | 29 | 29 | 30 | TLE | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int main(void) {
IOS const ll LINF = 1LL << 60;
const ll SMAX = 2500;
using plpll = pair<ll, pll>;
auto makep = [](const ll time, const int to, const int sil) {
return make_pair(time, make_pair(to, sil));
};
ll N, M;
cin >> N >> M;
ll S;
cin >> S;
vector<vector<plpll>> g(N);
REP(i, M) {
int u, v;
cin >> u >> v;
u--;
v--;
ll toll, t;
cin >> toll >> t;
g[u].push_back(makep(t, v, toll));
g[v].push_back(makep(t, u, toll));
}
vector<ll> C(N), D(N);
REP(i, N) cin >> C[i] >> D[i];
S = min(S, SMAX);
vector<vector<ll>> dp(N, vector<ll>(SMAX + 5, LINF));
dp[0][0] = 0;
priority_queue<plpll, vector<plpll>, greater<plpll>> dj;
dj.push(makep(0, 0, S));
while (!dj.empty()) {
plpll p = dj.top();
dj.pop();
ll time = p.first;
ll cnt = p.second.first;
ll sil = p.second.second;
if (dp[cnt][sil] < time)
continue;
// 1回分交換を追加
if (sil + C[cnt] <= SMAX) {
ll sc = sil + C[cnt];
ll tc = time + D[cnt];
if (dp[cnt][sc] > tc) {
dp[cnt][sc] = tc;
dj.push(makep(tc, cnt, sc));
}
}
// 辺を通る
for (auto p2 : g[cnt]) {
ll time2 = time + p2.first;
ll to = p2.second.first;
ll sil2 = sil - p2.second.second;
if (sil2 < 0)
continue;
if (dp[to][sil2] < time2)
continue;
dp[to][sil2] = time2;
dj.push(makep(time2, to, sil2));
}
}
FOR(i, 1, N) {
ll ans = LINF;
REP(j, SMAX) ans = min(ans, dp[i][j]);
OUT(ans)
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int main(void) {
IOS const ll LINF = 1LL << 60;
const ll SMAX = 2500;
using plpll = pair<ll, pll>;
auto makep = [](const ll time, const int to, const int sil) {
return make_pair(time, make_pair(to, sil));
};
ll N, M;
cin >> N >> M;
ll S;
cin >> S;
vector<vector<plpll>> g(N);
REP(i, M) {
int u, v;
cin >> u >> v;
u--;
v--;
ll toll, t;
cin >> toll >> t;
g[u].push_back(makep(t, v, toll));
g[v].push_back(makep(t, u, toll));
}
vector<ll> C(N), D(N);
REP(i, N) cin >> C[i] >> D[i];
S = min(S, SMAX);
vector<vector<ll>> dp(N, vector<ll>(SMAX + 5, LINF));
dp[0][0] = 0;
priority_queue<plpll, vector<plpll>, greater<plpll>> dj;
dj.push(makep(0, 0, S));
while (!dj.empty()) {
plpll p = dj.top();
dj.pop();
ll time = p.first;
ll cnt = p.second.first;
ll sil = p.second.second;
if (dp[cnt][sil] < time)
continue;
// 1回分交換を追加
if (sil + C[cnt] <= SMAX) {
ll sc = sil + C[cnt];
ll tc = time + D[cnt];
if (dp[cnt][sc] > tc) {
dp[cnt][sc] = tc;
dj.push(makep(tc, cnt, sc));
}
}
// 辺を通る
for (auto p2 : g[cnt]) {
ll time2 = time + p2.first;
ll to = p2.second.first;
ll sil2 = sil - p2.second.second;
if (sil2 < 0)
continue;
if (dp[to][sil2] > time2) {
dp[to][sil2] = time2;
dj.push(makep(time2, to, sil2));
}
}
}
FOR(i, 1, N) {
ll ans = LINF;
REP(j, SMAX) ans = min(ans, dp[i][j]);
OUT(ans)
}
return 0;
} | replace | 78 | 82 | 78 | 82 | TLE | |
p02703 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
const long long INF = 1e18;
struct Edge {
int to;
long long cost;
};
struct Dijkstra {
using P = pair<long long, int>;
int V;
int s;
const vector<vector<Edge>> &g;
vector<long long> dist;
vector<int> previous;
Dijkstra(const vector<vector<Edge>> &g) : V(g.size()), g(g) {}
void calc(int start) {
s = start;
dist.assign(V, INF);
previous.assign(V, -1);
dist[s] = 0;
priority_queue<P, vector<P>, greater<P>> pque;
pque.push(P(0, s));
while (!pque.empty()) {
P p = pque.top();
pque.pop();
int v = p.second;
if (p.first > dist[v])
continue;
for (Edge e : g[v]) {
if (dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
previous[e.to] = v;
pque.push(P(dist[e.to], e.to));
}
}
}
}
};
int main() {
int n, m, s;
cin >> n >> m >> s;
const int MAX = 2505;
vector<vector<Edge>> g(n * (MAX + 1));
auto idx = [&](int v, int ginka) { return ginka * n + v; };
for (int i = 0; i < m; i++) {
long long u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
for (int j = 0; j <= MAX; j++) {
if (j - a >= 0)
g[idx(u, j)].push_back({idx(v, j - a), b});
if (j + a <= MAX)
g[idx(v, j + a)].push_back({idx(u, j), b});
}
}
for (int i = 0; i < n; i++) {
long long c, d;
cin >> c >> d;
for (int j = 0; j < MAX; j++) {
g[idx(i, j)].push_back({idx(i, min(MAX, int(j + c))), d});
}
}
Dijkstra D(g);
D.calc(idx(0, s));
for (int i = 1; i < n; i++) {
long long ans = INF;
for (int j = 0; j <= MAX; j++) {
ans = min(ans, D.dist[idx(i, j)]);
}
cout << ans << endl;
}
return 0;
} | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
const long long INF = 1e18;
struct Edge {
int to;
long long cost;
};
struct Dijkstra {
using P = pair<long long, int>;
int V;
int s;
const vector<vector<Edge>> &g;
vector<long long> dist;
vector<int> previous;
Dijkstra(const vector<vector<Edge>> &g) : V(g.size()), g(g) {}
void calc(int start) {
s = start;
dist.assign(V, INF);
previous.assign(V, -1);
dist[s] = 0;
priority_queue<P, vector<P>, greater<P>> pque;
pque.push(P(0, s));
while (!pque.empty()) {
P p = pque.top();
pque.pop();
int v = p.second;
if (p.first > dist[v])
continue;
for (Edge e : g[v]) {
if (dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
previous[e.to] = v;
pque.push(P(dist[e.to], e.to));
}
}
}
}
};
int main() {
int n, m, s;
cin >> n >> m >> s;
const int MAX = 2505;
vector<vector<Edge>> g(n * (MAX + 1));
auto idx = [&](int v, int ginka) { return ginka * n + v; };
for (int i = 0; i < m; i++) {
long long u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
for (int j = 0; j <= MAX; j++) {
if (j - a >= 0)
g[idx(u, j)].push_back({idx(v, j - a), b});
if (j + a <= MAX)
g[idx(v, j + a)].push_back({idx(u, j), b});
}
}
for (int i = 0; i < n; i++) {
long long c, d;
cin >> c >> d;
for (int j = 0; j < MAX; j++) {
g[idx(i, j)].push_back({idx(i, min(MAX, int(j + c))), d});
}
}
Dijkstra D(g);
D.calc(idx(0, min(s, MAX)));
for (int i = 1; i < n; i++) {
long long ans = INF;
for (int j = 0; j <= MAX; j++) {
ans = min(ans, D.dist[idx(i, j)]);
}
cout << ans << endl;
}
return 0;
} | replace | 71 | 72 | 71 | 72 | 0 | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxs = 1e5 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\n'
void pre() {}
#define tlll tuple<ll, ll, ll>
#define mt make_tuple
vector<ll> graph[55];
pair<ll, ll> A[55][55];
pair<ll, ll> C[55];
ll dp[55][2505];
ll n, m, S;
void solve() {
cin >> n >> m >> S;
for (ll i = 0; i < n + 1; i++) {
for (ll j = 0; j < n + 1; j++) {
A[i][j] = {-1, -1};
}
}
for (ll i = 0; i < m; i++) {
ll x, y;
cin >> x >> y;
x--;
y--;
graph[x].pb(y);
graph[y].pb(x);
ll a, b;
cin >> a >> b;
A[x][y] = {a, b};
A[y][x] = {a, b};
}
for (ll i = 0; i < n; i++) {
cin >> C[i].ff >> C[i].ss;
}
for (ll i = 0; i < 55; i++) {
for (ll j = 0; j < 2505; j++) {
dp[i][j] = oo;
}
}
dp[0][S] = 0;
priority_queue<tlll, vector<tlll>, greater<tlll>> pq;
/*
0 : time
1 : node
2 : coins
*/
pq.push(mt(0, 0, S));
while (pq.size()) {
tlll curr = pq.top();
pq.pop();
ll time_s = get<0>(curr);
ll s = get<1>(curr);
ll coins = get<2>(curr);
if (dp[s][coins] + C[s].ss < dp[s][min(2500ll, coins + C[s].ff)]) {
dp[s][min(2500ll, coins + C[s].ff)] = dp[s][coins] + C[s].ss;
pq.push(mt(dp[s][coins] + C[s].ss, s, min(2500ll, coins + C[s].ff)));
}
for (auto child : graph[s]) {
ll rem_coins = coins - A[s][child].ff;
if (rem_coins < 0)
continue;
ll time_child = time_s + A[s][child].ss;
if (dp[child][rem_coins] > time_child) {
dp[child][rem_coins] = time_child;
pq.push(mt(time_child, child, rem_coins));
}
}
}
for (ll i = 1; i < n; i++) {
ll ans = oo;
for (ll j = 0; j < 2501; j++) {
ans = min(ans, dp[i][j]);
}
cout << ans << endl;
}
}
int main() {
IOS;
pre();
ll T = 1;
// cin>>T;
while (T--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxs = 1e5 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\n'
void pre() {}
#define tlll tuple<ll, ll, ll>
#define mt make_tuple
vector<ll> graph[55];
pair<ll, ll> A[55][55];
pair<ll, ll> C[55];
ll dp[55][2505];
ll n, m, S;
void solve() {
cin >> n >> m >> S;
S = min(S, 2500ll);
for (ll i = 0; i < n + 1; i++) {
for (ll j = 0; j < n + 1; j++) {
A[i][j] = {-1, -1};
}
}
for (ll i = 0; i < m; i++) {
ll x, y;
cin >> x >> y;
x--;
y--;
graph[x].pb(y);
graph[y].pb(x);
ll a, b;
cin >> a >> b;
A[x][y] = {a, b};
A[y][x] = {a, b};
}
for (ll i = 0; i < n; i++) {
cin >> C[i].ff >> C[i].ss;
}
for (ll i = 0; i < 55; i++) {
for (ll j = 0; j < 2505; j++) {
dp[i][j] = oo;
}
}
dp[0][S] = 0;
priority_queue<tlll, vector<tlll>, greater<tlll>> pq;
/*
0 : time
1 : node
2 : coins
*/
pq.push(mt(0, 0, S));
while (pq.size()) {
tlll curr = pq.top();
pq.pop();
ll time_s = get<0>(curr);
ll s = get<1>(curr);
ll coins = get<2>(curr);
if (dp[s][coins] + C[s].ss < dp[s][min(2500ll, coins + C[s].ff)]) {
dp[s][min(2500ll, coins + C[s].ff)] = dp[s][coins] + C[s].ss;
pq.push(mt(dp[s][coins] + C[s].ss, s, min(2500ll, coins + C[s].ff)));
}
for (auto child : graph[s]) {
ll rem_coins = coins - A[s][child].ff;
if (rem_coins < 0)
continue;
ll time_child = time_s + A[s][child].ss;
if (dp[child][rem_coins] > time_child) {
dp[child][rem_coins] = time_child;
pq.push(mt(time_child, child, rem_coins));
}
}
}
for (ll i = 1; i < n; i++) {
ll ans = oo;
for (ll j = 0; j < 2501; j++) {
ans = min(ans, dp[i][j]);
}
cout << ans << endl;
}
}
int main() {
IOS;
pre();
ll T = 1;
// cin>>T;
while (T--) {
solve();
}
return 0;
} | replace | 33 | 34 | 33 | 34 | 0 | |
p02703 | C++ | Runtime Error | /*Author - Shubham Gupta (@shubham107)*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define int ll
#define forn(i, n) for (int i = 0; i < n; i++)
#define rforn(i, n) for (int i = n - 1; i >= 0; i--)
#define forne(i, n) for (int i = 1; i <= n; i++)
#define forse(i, s, e) for (int i = s; i < e; i++)
#define rforse(i, s, e) for (int i = e - 1; i >= s; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define fill(a, x) memset(a, x, sizeof a);
#define trav(a, x) for (auto &a : x)
#define INF int(1e17)
#ifdef SHUBHAM107
#include "../code-pieces/trace.h"
#else
#define trace(args...)
#endif
ll modpow(ll a, ll b, ll m = MOD) {
a %= m;
ll res = 1;
while (b) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll bpow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
ll modinv(ll a) { return modpow(a, MOD - 2, MOD); }
void graph(vvi &adj, int m) {
int x, y;
forn(i, m) {
cin >> x >> y;
adj[x - 1].pb(y - 1);
adj[y - 1].pb(x - 1);
}
}
const int mxM = 25e2, mxN = 50;
typedef vector<vector<pair<int, pii>>> vvpiii;
typedef pair<int, pii> ppii;
vector<pii> city(mxN);
int n, m, s;
ll dist[50][mxM + 5];
void djik(vvpiii &adj) {
priority_queue<ppii> q;
q.push({0, {0, s}});
while (!q.empty()) {
ppii cur = q.top();
q.pop();
int cit = cur.S.F, dis = -cur.F, stat = cur.S.S;
trace(cit, dis, stat);
if (dist[cit][stat] < dis)
continue;
trav(to, adj[cit]) {
int edge = to.S.S, silver = to.S.F;
if (stat >= silver && dist[to.F][stat - silver] > edge + dis) {
dist[to.F][stat - silver] = edge + dis;
q.push({-(edge + dis), {to.F, stat - silver}});
}
}
if (dist[cit][stat + city[cit].F] > city[cit].S + dis) {
dist[cit][stat + city[cit].F] = city[cit].S + dis;
q.push({-(city[cit].S + dis), {cit, stat + city[cit].F}});
}
}
}
void solve() {
cin >> n >> m >> s;
s = min(s, mxM);
vvpiii adj(n);
int u, v, a, b;
forn(i, m) {
cin >> u >> v >> a >> b;
u--, v--;
adj[u].pb({v, {a, b}});
adj[v].pb({u, {a, b}});
}
forn(i, n) forn(j, mxM) dist[i][j] = INF;
forn(i, n) cin >> city[i].F >> city[i].S;
djik(adj);
forne(i, n - 1) {
int minm = INF;
forn(j, mxM) minm = min(minm, dist[i][j]);
cout << minm << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef SHUBHAM107
freopen("inp.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("err.txt", "w", stderr);
#endif
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
| /*Author - Shubham Gupta (@shubham107)*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define int ll
#define forn(i, n) for (int i = 0; i < n; i++)
#define rforn(i, n) for (int i = n - 1; i >= 0; i--)
#define forne(i, n) for (int i = 1; i <= n; i++)
#define forse(i, s, e) for (int i = s; i < e; i++)
#define rforse(i, s, e) for (int i = e - 1; i >= s; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define fill(a, x) memset(a, x, sizeof a);
#define trav(a, x) for (auto &a : x)
#define INF int(1e17)
#ifdef SHUBHAM107
#include "../code-pieces/trace.h"
#else
#define trace(args...)
#endif
ll modpow(ll a, ll b, ll m = MOD) {
a %= m;
ll res = 1;
while (b) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll bpow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
ll modinv(ll a) { return modpow(a, MOD - 2, MOD); }
void graph(vvi &adj, int m) {
int x, y;
forn(i, m) {
cin >> x >> y;
adj[x - 1].pb(y - 1);
adj[y - 1].pb(x - 1);
}
}
const int mxM = 25e2, mxN = 50;
typedef vector<vector<pair<int, pii>>> vvpiii;
typedef pair<int, pii> ppii;
vector<pii> city(mxN);
int n, m, s;
ll dist[50][mxM + 5];
void djik(vvpiii &adj) {
priority_queue<ppii> q;
q.push({0, {0, s}});
while (!q.empty()) {
ppii cur = q.top();
q.pop();
int cit = cur.S.F, dis = -cur.F, stat = cur.S.S;
trace(cit, dis, stat);
if (dist[cit][stat] < dis)
continue;
trav(to, adj[cit]) {
int edge = to.S.S, silver = to.S.F;
if (stat >= silver && dist[to.F][stat - silver] > edge + dis) {
dist[to.F][stat - silver] = edge + dis;
q.push({-(edge + dis), {to.F, stat - silver}});
}
}
if (stat + city[cit].F <= mxM &&
dist[cit][stat + city[cit].F] > city[cit].S + dis) {
dist[cit][stat + city[cit].F] = city[cit].S + dis;
q.push({-(city[cit].S + dis), {cit, stat + city[cit].F}});
}
}
}
void solve() {
cin >> n >> m >> s;
s = min(s, mxM);
vvpiii adj(n);
int u, v, a, b;
forn(i, m) {
cin >> u >> v >> a >> b;
u--, v--;
adj[u].pb({v, {a, b}});
adj[v].pb({u, {a, b}});
}
forn(i, n) forn(j, mxM) dist[i][j] = INF;
forn(i, n) cin >> city[i].F >> city[i].S;
djik(adj);
forne(i, n - 1) {
int minm = INF;
forn(j, mxM) minm = min(minm, dist[i][j]);
cout << minm << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef SHUBHAM107
freopen("inp.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("err.txt", "w", stderr);
#endif
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
| replace | 86 | 87 | 86 | 88 | 0 | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
const double PI = acos(-1.0);
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define perm(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define drop(x) cout << (x) << '\n', exit(0)
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vector<ll>> vec2;
typedef vector<vector<vector<ll>>> vec3;
template <class S, class T> inline bool chmax(S &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T> inline bool chmin(S &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline ll msb(ll v) { return 1 << (31 - __builtin_clzll(v)); }
inline ll devc(ll x, ll y) { return (x + y - 1) / y; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t);
return os;
}
template <typename T1, typename T2, typename T3>
istream &operator>>(istream &is, tuple<T1, T2, T3> &t) {
is >> get<0>(t) >> get<1>(t) >> get<2>(t);
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
/*--------------------------------- Tools
* ------------------------------------------*/
template <typename T> vector<T> cumsum(const vector<T> &X) {
vector<T> res(X.size() + 1, 0);
for (int i = 0; i < X.size(); ++i)
res[i + 1] += res[i] + X[i];
return res;
}
template <typename S, typename T, typename F>
pair<T, T> bisearch(S left, T right, F f) {
while (abs(right - left) > 1) {
T mid = (right + left) / 2;
if (f(mid))
right = mid;
else
left = mid;
}
return {left, right};
}
template <typename S, typename T, typename F>
double trisearch(S left, T right, F f, int maxLoop = 90) {
double low = left, high = right;
while (maxLoop--) {
double mid_left = high / 3 + low * 2 / 3;
double mid_right = high * 2 / 3 + low / 3;
if (f(mid_left) >= f(mid_right))
low = mid_left;
else
high = mid_right;
}
return (low + high) * 0.5;
}
/*--------------------------------- Graph
* ------------------------------------------*/
struct Edge {
ll from, to, weight;
Edge() : from(0), to(0), weight(0) {}
Edge(ll f, ll t, ll w) : from(f), to(t), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
void add_edge(Graph &g, ll a, ll b, ll w = 1) {
g[a].emplace_back(a, b, w);
g[b].emplace_back(b, a, w);
}
void add_arrow(Graph &g, ll a, ll b, ll w = 1) { g[a].emplace_back(a, b, w); }
template <typename T> vector<T> dijkstra(Graph &g, T s, bool restore = false) {
vector<T> dist(g.size(), INF);
priority_queue<pair<T, T>, vector<pair<T, T>>, greater<pair<T, T>>> que;
dist[s] = 0;
que.emplace(dist[s], s);
vector<T> prev(g.size(), -1);
while (!que.empty()) {
T cost, idx;
tie(cost, idx) = que.top();
que.pop();
if (dist[idx] < cost)
continue;
for (auto &e : g[idx]) {
auto next_cost = cost + e.weight;
if (dist[e.to] <= next_cost)
continue;
dist[e.to] = next_cost;
if (restore)
prev[e.to] = e.from;
que.emplace(dist[e.to], e.to);
}
}
if (restore)
return prev;
return dist;
}
vector<ll> shortest_path(Graph &g, ll start, ll goal) {
vector<ll> prev = dijkstra(g, start, true);
vector<ll> path;
for (int cur = goal; cur != -1; cur = prev[cur])
path.push_back(cur);
reverse(path.begin(), path.end());
return path;
}
/*------------------------------- Main Code Here
* -----------------------------------------*/
int main() {
ll N, M, S;
cin >> N >> M >> S;
Graph G(N * 2501);
auto plot = [&](ll i, ll j) { return i * 2501 + j; };
rep(i, M) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
--u, --v;
for (ll j = 0; j <= 2500; ++j)
if (j >= a) {
add_arrow(G, plot(u, j), plot(v, j - a), b);
add_arrow(G, plot(v, j), plot(u, j - a), b);
}
}
rep(i, N) {
ll coin, ctime;
cin >> coin >> ctime;
for (ll j = 0; j <= 2500; ++j)
if (j + coin <= 2500)
add_arrow(G, plot(i, j), plot(i, j + coin), ctime);
}
vec ans = dijkstra(G, S);
rep1(i, N - 1) {
ll mintime = INF;
for (ll j = 0; j <= 2500; ++j)
chmin(mintime, ans[plot(i, j)]);
print(mintime);
}
return 0;
} | #include <bits/stdc++.h>
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
const double PI = acos(-1.0);
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define perm(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define drop(x) cout << (x) << '\n', exit(0)
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vector<ll>> vec2;
typedef vector<vector<vector<ll>>> vec3;
template <class S, class T> inline bool chmax(S &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T> inline bool chmin(S &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline ll msb(ll v) { return 1 << (31 - __builtin_clzll(v)); }
inline ll devc(ll x, ll y) { return (x + y - 1) / y; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t);
return os;
}
template <typename T1, typename T2, typename T3>
istream &operator>>(istream &is, tuple<T1, T2, T3> &t) {
is >> get<0>(t) >> get<1>(t) >> get<2>(t);
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
/*--------------------------------- Tools
* ------------------------------------------*/
template <typename T> vector<T> cumsum(const vector<T> &X) {
vector<T> res(X.size() + 1, 0);
for (int i = 0; i < X.size(); ++i)
res[i + 1] += res[i] + X[i];
return res;
}
template <typename S, typename T, typename F>
pair<T, T> bisearch(S left, T right, F f) {
while (abs(right - left) > 1) {
T mid = (right + left) / 2;
if (f(mid))
right = mid;
else
left = mid;
}
return {left, right};
}
template <typename S, typename T, typename F>
double trisearch(S left, T right, F f, int maxLoop = 90) {
double low = left, high = right;
while (maxLoop--) {
double mid_left = high / 3 + low * 2 / 3;
double mid_right = high * 2 / 3 + low / 3;
if (f(mid_left) >= f(mid_right))
low = mid_left;
else
high = mid_right;
}
return (low + high) * 0.5;
}
/*--------------------------------- Graph
* ------------------------------------------*/
struct Edge {
ll from, to, weight;
Edge() : from(0), to(0), weight(0) {}
Edge(ll f, ll t, ll w) : from(f), to(t), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
void add_edge(Graph &g, ll a, ll b, ll w = 1) {
g[a].emplace_back(a, b, w);
g[b].emplace_back(b, a, w);
}
void add_arrow(Graph &g, ll a, ll b, ll w = 1) { g[a].emplace_back(a, b, w); }
template <typename T> vector<T> dijkstra(Graph &g, T s, bool restore = false) {
vector<T> dist(g.size(), INF);
priority_queue<pair<T, T>, vector<pair<T, T>>, greater<pair<T, T>>> que;
dist[s] = 0;
que.emplace(dist[s], s);
vector<T> prev(g.size(), -1);
while (!que.empty()) {
T cost, idx;
tie(cost, idx) = que.top();
que.pop();
if (dist[idx] < cost)
continue;
for (auto &e : g[idx]) {
auto next_cost = cost + e.weight;
if (dist[e.to] <= next_cost)
continue;
dist[e.to] = next_cost;
if (restore)
prev[e.to] = e.from;
que.emplace(dist[e.to], e.to);
}
}
if (restore)
return prev;
return dist;
}
vector<ll> shortest_path(Graph &g, ll start, ll goal) {
vector<ll> prev = dijkstra(g, start, true);
vector<ll> path;
for (int cur = goal; cur != -1; cur = prev[cur])
path.push_back(cur);
reverse(path.begin(), path.end());
return path;
}
/*------------------------------- Main Code Here
* -----------------------------------------*/
int main() {
ll N, M, S;
cin >> N >> M >> S;
chmin(S, 2500);
Graph G(N * 2501);
auto plot = [&](ll i, ll j) { return i * 2501 + j; };
rep(i, M) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
--u, --v;
for (ll j = 0; j <= 2500; ++j)
if (j >= a) {
add_arrow(G, plot(u, j), plot(v, j - a), b);
add_arrow(G, plot(v, j), plot(u, j - a), b);
}
}
rep(i, N) {
ll coin, ctime;
cin >> coin >> ctime;
for (ll j = 0; j <= 2500; ++j)
if (j + coin <= 2500)
add_arrow(G, plot(i, j), plot(i, j + coin), ctime);
}
vec ans = dijkstra(G, S);
rep1(i, N - 1) {
ll mintime = INF;
for (ll j = 0; j <= 2500; ++j)
chmin(mintime, ans[plot(i, j)]);
print(mintime);
}
return 0;
} | insert | 189 | 189 | 189 | 191 | 0 | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define endl '\n'
const int inf = 1000000007;
const ll INF = 1e18;
// int mod = 998244353;
int mod = 1000000007;
#define abs(x) (x >= 0 ? x : -(x))
#define lb(v, x) (int)(lower_bound(all(v), x) - v.begin())
#define ub(v, x) (int)(upper_bound(all(v), x) - v.begin())
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll pow(ll a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
ll modpow(ll a, ll b, ll _mod) {
return b ? modpow(a * a % _mod, b / 2, _mod) * (b % 2 ? a : 1) % _mod : 1;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << p.F << " " << p.S;
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
rep(i, sz(vec)) {
if (i)
os << " ";
os << vec[i];
}
return os;
}
template <typename T> inline istream &operator>>(istream &is, vector<T> &v) {
rep(j, sz(v)) is >> v[j];
return is;
}
template <class T, class T2> inline void add(T &a, T2 b) {
a += b;
if (a >= mod)
a -= mod;
}
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(10);
int T;
// cin >> T;
T = 1;
while (T--) {
solve();
}
}
ll dp[55][55 * 55];
vector<ll> C, D;
ll n, m, s;
using t = tuple<ll, ll, int>; // 時間小さく, 銀貨は大きく
vector<vector<t>> G;
using t2 = tuple<int, ll, ll>;
vector<vector<int>> d;
clock_t st, ed;
void dijkstra() {
priority_queue<t, vector<t>, greater<t>> q;
q.push(t(0, -s, 0));
while (!q.empty()) {
ed = clock();
if ((ed - st) * 1.0 / CLOCKS_PER_SEC > 2.0) {
rep3(i, 1, n) {
ll ans = INF;
rep(j, 55 * 55) chmin(ans, dp[i][j]);
cout << ans << endl;
}
exit(0);
}
int v;
ll ti, co;
tie(ti, co, v) = q.top();
// cout << "v " << ti << " " << co << " " << v << endl;
co = -co;
q.pop();
if (dp[v][co] < ti)
continue;
for (t2 nt : G[v]) {
int nv;
ll nti, nco;
tie(nv, nco, nti) = nt;
// cout << "nv " << nv << " " << nco << " " << nti << endl;
// 交換
int flg = 0;
rep(i, inf) {
ll co2 = co - nco;
ll x = 0;
if (co2 < 0)
x = (-co2 + C[v] - 1) / C[v];
co2 += x * C[v];
co2 += i * C[v];
if (co2 >= 55 * 55) {
co2 = 55 * 55 - 1;
flg = 1;
} // debuged
x += i;
if (chmin(dp[nv][co2], dp[v][co] + nti + x * D[v])) {
q.push(t(dp[nv][co2], -co2, nv));
}
if (flg)
break;
}
}
}
}
void solve() {
st = clock();
cin >> n >> m >> s;
G.resize(n);
rep(i, m) {
int a, b, A, B;
cin >> a >> b >> A >> B;
a--;
b--;
G[a].eb(b, A, B);
G[b].eb(a, A, B);
}
C.resize(n);
D.resize(n);
rep(i, n) cin >> C[i] >> D[i];
rep(i, 55) rep(j, 55 * 55) dp[i][j] = INF;
chmin(s, 55 * 55 - 1); // debuged
dp[0][s] = 0;
dijkstra();
rep3(i, 1, n) {
ll ans = INF;
rep(j, 55 * 55) chmin(ans, dp[i][j]);
cout << ans << endl;
}
// rep(i, n) { rep(j, 10) cout << (dp[i][j] == INF ? -1LL : dp[i][j]) << " ";
// cout << endl; }
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define endl '\n'
const int inf = 1000000007;
const ll INF = 1e18;
// int mod = 998244353;
int mod = 1000000007;
#define abs(x) (x >= 0 ? x : -(x))
#define lb(v, x) (int)(lower_bound(all(v), x) - v.begin())
#define ub(v, x) (int)(upper_bound(all(v), x) - v.begin())
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll pow(ll a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
ll modpow(ll a, ll b, ll _mod) {
return b ? modpow(a * a % _mod, b / 2, _mod) * (b % 2 ? a : 1) % _mod : 1;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << p.F << " " << p.S;
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
rep(i, sz(vec)) {
if (i)
os << " ";
os << vec[i];
}
return os;
}
template <typename T> inline istream &operator>>(istream &is, vector<T> &v) {
rep(j, sz(v)) is >> v[j];
return is;
}
template <class T, class T2> inline void add(T &a, T2 b) {
a += b;
if (a >= mod)
a -= mod;
}
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(10);
int T;
// cin >> T;
T = 1;
while (T--) {
solve();
}
}
ll dp[55][55 * 55];
vector<ll> C, D;
ll n, m, s;
using t = tuple<ll, ll, int>; // 時間小さく, 銀貨は大きく
vector<vector<t>> G;
using t2 = tuple<int, ll, ll>;
vector<vector<int>> d;
clock_t st, ed;
void dijkstra() {
priority_queue<t, vector<t>, greater<t>> q;
q.push(t(0, -s, 0));
while (!q.empty()) {
ed = clock();
if ((ed - st) * 1.0 / CLOCKS_PER_SEC > 1.9) {
rep3(i, 1, n) {
ll ans = INF;
rep(j, 55 * 55) chmin(ans, dp[i][j]);
cout << ans << endl;
}
exit(0);
}
int v;
ll ti, co;
tie(ti, co, v) = q.top();
// cout << "v " << ti << " " << co << " " << v << endl;
co = -co;
q.pop();
if (dp[v][co] < ti)
continue;
for (t2 nt : G[v]) {
int nv;
ll nti, nco;
tie(nv, nco, nti) = nt;
// cout << "nv " << nv << " " << nco << " " << nti << endl;
// 交換
int flg = 0;
rep(i, inf) {
ll co2 = co - nco;
ll x = 0;
if (co2 < 0)
x = (-co2 + C[v] - 1) / C[v];
co2 += x * C[v];
co2 += i * C[v];
if (co2 >= 55 * 55) {
co2 = 55 * 55 - 1;
flg = 1;
} // debuged
x += i;
if (chmin(dp[nv][co2], dp[v][co] + nti + x * D[v])) {
q.push(t(dp[nv][co2], -co2, nv));
}
if (flg)
break;
}
}
}
}
void solve() {
st = clock();
cin >> n >> m >> s;
G.resize(n);
rep(i, m) {
int a, b, A, B;
cin >> a >> b >> A >> B;
a--;
b--;
G[a].eb(b, A, B);
G[b].eb(a, A, B);
}
C.resize(n);
D.resize(n);
rep(i, n) cin >> C[i] >> D[i];
rep(i, 55) rep(j, 55 * 55) dp[i][j] = INF;
chmin(s, 55 * 55 - 1); // debuged
dp[0][s] = 0;
dijkstra();
rep3(i, 1, n) {
ll ans = INF;
rep(j, 55 * 55) chmin(ans, dp[i][j]);
cout << ans << endl;
}
// rep(i, n) { rep(j, 10) cout << (dp[i][j] == INF ? -1LL : dp[i][j]) << " ";
// cout << endl; }
}
| replace | 98 | 99 | 98 | 99 | TLE | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
#define ITR(v, i) for (auto ite = v.begin(); ite != v.end(); ite++)
const ll INF = 999999999999999;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
struct edge {
ll to, maisu, cost;
};
vector<edge> g[50];
vl v_maisu, v_hun;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m >> a;
rep(i, m) {
cin >> x >> y >> z >> w;
x--;
y--;
g[x].push_back({y, z, w});
g[y].push_back({x, z, w});
}
rep(i, n) {
cin >> x >> y;
v_maisu.push_back(x);
v_hun.push_back(y);
}
ll dp[50][20000];
rep(i, 50) {
rep(j, 20000) { dp[i][j] = INF; }
}
t = n - 1;
priority_queue<pair<ll, PP>, vector<pair<ll, PP>>, greater<pair<ll, PP>>> VP;
rep(i, min(a, 9999LL) + 1) { dp[0][i] = 0; }
VP.push(make_pair(0LL, make_pair(min(a, 9999LL), 0)));
// cout << dp[0][2] << endl;
e = 200000;
while (e--) {
auto C = VP.top();
x = C.first; // 今の分
y = C.second.first; // 今の枚数
z = C.second.second; // 今の頂点
VP.pop();
if (100000 > y + v_maisu[z] && dp[z][y + v_maisu[z]] > x + v_hun[z]) {
VP.push(make_pair(x + v_hun[z], make_pair(y + v_maisu[z], z)));
k = y + v_maisu[z];
while (dp[z][k] > x + v_hun[z]) {
dp[z][k] = x + v_hun[z];
k--;
}
}
for (edge i : g[z]) {
p = i.cost;
q = i.to;
r = i.maisu;
if (y >= r && dp[q][y - r] > x + p) {
k = y - r;
while (dp[q][k] > x + p) {
dp[q][k] = x + p;
if (k == 0) {
// cout << x + p << " " << y - r << " " << q << endl;
t--;
break;
}
k--;
}
VP.push(make_pair(x + p, make_pair(y - r, q)));
}
}
}
rep(i, n - 1) { cout << dp[i + 1][0] << endl; }
} | #include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
#define ITR(v, i) for (auto ite = v.begin(); ite != v.end(); ite++)
const ll INF = 999999999999999;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
struct edge {
ll to, maisu, cost;
};
vector<edge> g[50];
vl v_maisu, v_hun;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m >> a;
rep(i, m) {
cin >> x >> y >> z >> w;
x--;
y--;
g[x].push_back({y, z, w});
g[y].push_back({x, z, w});
}
rep(i, n) {
cin >> x >> y;
v_maisu.push_back(x);
v_hun.push_back(y);
}
ll dp[50][20000];
rep(i, 50) {
rep(j, 20000) { dp[i][j] = INF; }
}
t = n - 1;
priority_queue<pair<ll, PP>, vector<pair<ll, PP>>, greater<pair<ll, PP>>> VP;
rep(i, min(a, 9999LL) + 1) { dp[0][i] = 0; }
VP.push(make_pair(0LL, make_pair(min(a, 9999LL), 0)));
// cout << dp[0][2] << endl;
e = 200000;
while (e--) {
auto C = VP.top();
x = C.first; // 今の分
y = C.second.first; // 今の枚数
z = C.second.second; // 今の頂点
VP.pop();
if (10000 > y + v_maisu[z] && dp[z][y + v_maisu[z]] > x + v_hun[z]) {
VP.push(make_pair(x + v_hun[z], make_pair(y + v_maisu[z], z)));
k = y + v_maisu[z];
while (dp[z][k] > x + v_hun[z]) {
dp[z][k] = x + v_hun[z];
k--;
}
}
for (edge i : g[z]) {
p = i.cost;
q = i.to;
r = i.maisu;
if (y >= r && dp[q][y - r] > x + p) {
k = y - r;
while (dp[q][k] > x + p) {
dp[q][k] = x + p;
if (k == 0) {
// cout << x + p << " " << y - r << " " << q << endl;
t--;
break;
}
k--;
}
VP.push(make_pair(x + p, make_pair(y - r, q)));
}
}
}
rep(i, n - 1) { cout << dp[i + 1][0] << endl; }
} | replace | 55 | 56 | 55 | 56 | 0 | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, m, s;
typedef pair<ll, ll> P;
using edge = pair<ll, P>;
const ll INF = 1e18, MAX_COST = 2500;
signed main() {
ll n, m, s;
cin >> n >> m >> s;
s = min(s, MAX_COST);
vector<vector<edge>> G(n);
for (int i = 0; i < m; i++) {
ll u, v, a, b;
scanf("%lld%lld%lld%lld", &u, &v, &a, &b);
--u;
--v;
G[u].push_back(edge(v, P(a, b)));
G[v].push_back(edge(u, P(a, b)));
}
vector<ll> c(n), d(n);
for (int i = 0; i < n; i++)
scanf("%lld%lld", &c[i], &d[i]);
vector<vector<ll>> dist(n, vector<ll>(MAX_COST + 1, INF));
priority_queue<edge, vector<edge>, greater<edge>> que;
dist[0][s] = 0;
que.push(edge(0, P(0, s)));
while (!que.empty()) {
edge e = que.top();
que.pop();
ll time = e.first, now = e.second.first, money = e.second.second;
// if(dist[now][money] < time) continue;
if (money + c[now] <= MAX_COST) {
ll ntime = time + d[now], nmoney = money + c[now];
if (dist[now][nmoney] > ntime) {
dist[now][nmoney] = ntime;
que.push(edge(ntime, P(now, nmoney)));
}
}
for (auto e : G[now]) {
// if(money < e.second.first) continue;
ll next = e.first, ntime = time + e.second.second,
nmoney = money - e.second.first;
if (dist[next][nmoney] > ntime) {
dist[next][nmoney] = ntime;
que.push(edge(ntime, P(next, nmoney)));
}
}
}
for (int i = 1; i < n; i++) {
ll ans = INF;
for (int j = 0; j <= MAX_COST; j++)
ans = min(ans, dist[i][j]);
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, m, s;
typedef pair<ll, ll> P;
using edge = pair<ll, P>;
const ll INF = 1e18, MAX_COST = 2500;
signed main() {
ll n, m, s;
cin >> n >> m >> s;
s = min(s, MAX_COST);
vector<vector<edge>> G(n);
for (int i = 0; i < m; i++) {
ll u, v, a, b;
scanf("%lld%lld%lld%lld", &u, &v, &a, &b);
--u;
--v;
G[u].push_back(edge(v, P(a, b)));
G[v].push_back(edge(u, P(a, b)));
}
vector<ll> c(n), d(n);
for (int i = 0; i < n; i++)
scanf("%lld%lld", &c[i], &d[i]);
vector<vector<ll>> dist(n, vector<ll>(MAX_COST + 1, INF));
priority_queue<edge, vector<edge>, greater<edge>> que;
dist[0][s] = 0;
que.push(edge(0, P(0, s)));
while (!que.empty()) {
edge e = que.top();
que.pop();
ll time = e.first, now = e.second.first, money = e.second.second;
// if(dist[now][money] < time) continue;
if (money + c[now] <= MAX_COST) {
ll ntime = time + d[now], nmoney = money + c[now];
if (dist[now][nmoney] > ntime) {
dist[now][nmoney] = ntime;
que.push(edge(ntime, P(now, nmoney)));
}
}
for (auto e : G[now]) {
if (money < e.second.first)
continue;
ll next = e.first, ntime = time + e.second.second,
nmoney = money - e.second.first;
if (dist[next][nmoney] > ntime) {
dist[next][nmoney] = ntime;
que.push(edge(ntime, P(next, nmoney)));
}
}
}
for (int i = 1; i < n; i++) {
ll ans = INF;
for (int j = 0; j <= MAX_COST; j++)
ans = min(ans, dist[i][j]);
cout << ans << endl;
}
}
| replace | 44 | 45 | 44 | 46 | -11 | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
inline void read(int &x) {
char ch = getchar();
x = 0;
while (!isdigit(ch))
ch = getchar();
while (isdigit(ch))
x = x * 10 + ch - 48, ch = getchar();
}
const int N = 55, M = 210;
int n, m, s, c[N], d[N], f[N][N * N];
int cnt, h[N], to[M], nxt[M], a[N], b[N];
inline void add(int u, int v, int x, int y) {
to[++cnt] = v, a[cnt] = x, b[cnt] = y, nxt[cnt] = h[u], h[u] = cnt;
}
struct node {
int res, u, s;
inline bool operator<(const node &x) const { return res > x.res; }
};
priority_queue<node> q;
inline void Dij() {
q.push(node{0, 1, s});
while (!q.empty()) {
node t = q.top();
q.pop();
if (f[t.u][t.s] < t.res)
continue;
for (int i = h[t.u]; i; i = nxt[i])
if (t.s >= a[i] && f[to[i]][t.s - a[i]] > t.res + b[i])
f[to[i]][t.s - a[i]] = t.res + b[i],
q.push(node{t.res + b[i], to[i], t.s - a[i]});
int qwq = min(2500ll, t.s + c[t.u]);
if (f[t.u][qwq] > t.res + d[t.u])
f[t.u][qwq] = t.res + d[t.u], q.push(node{t.res + d[t.u], t.u, qwq});
}
}
signed main() {
read(n), read(m), read(s);
s = min(s, 2500ll);
memset(f, 0x3f, sizeof(f));
f[1][s] = 0;
for (int i = 1, u, v, x, y; i <= m; ++i) {
read(u), read(v), read(x), read(y);
add(u, v, x, y), add(v, u, x, y);
}
for (int i = 1; i <= n; ++i)
read(c[i]), read(d[i]);
Dij();
for (int i = 2; i <= n; ++i) {
int res = f[0][0];
for (int j = 0; j <= 2500; ++j)
res = min(res, f[i][j]);
printf("%lld\n", res);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
inline void read(int &x) {
char ch = getchar();
x = 0;
while (!isdigit(ch))
ch = getchar();
while (isdigit(ch))
x = x * 10 + ch - 48, ch = getchar();
}
const int N = 55, M = 210;
int n, m, s, c[N], d[N], f[N][N * N];
int cnt, h[N], to[M], nxt[M], a[M], b[M];
inline void add(int u, int v, int x, int y) {
to[++cnt] = v, a[cnt] = x, b[cnt] = y, nxt[cnt] = h[u], h[u] = cnt;
}
struct node {
int res, u, s;
inline bool operator<(const node &x) const { return res > x.res; }
};
priority_queue<node> q;
inline void Dij() {
q.push(node{0, 1, s});
while (!q.empty()) {
node t = q.top();
q.pop();
if (f[t.u][t.s] < t.res)
continue;
for (int i = h[t.u]; i; i = nxt[i])
if (t.s >= a[i] && f[to[i]][t.s - a[i]] > t.res + b[i])
f[to[i]][t.s - a[i]] = t.res + b[i],
q.push(node{t.res + b[i], to[i], t.s - a[i]});
int qwq = min(2500ll, t.s + c[t.u]);
if (f[t.u][qwq] > t.res + d[t.u])
f[t.u][qwq] = t.res + d[t.u], q.push(node{t.res + d[t.u], t.u, qwq});
}
}
signed main() {
read(n), read(m), read(s);
s = min(s, 2500ll);
memset(f, 0x3f, sizeof(f));
f[1][s] = 0;
for (int i = 1, u, v, x, y; i <= m; ++i) {
read(u), read(v), read(x), read(y);
add(u, v, x, y), add(v, u, x, y);
}
for (int i = 1; i <= n; ++i)
read(c[i]), read(d[i]);
Dij();
for (int i = 2; i <= n; ++i) {
int res = f[0][0];
for (int j = 0; j <= 2500; ++j)
res = min(res, f[i][j]);
printf("%lld\n", res);
}
return 0;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define all(x) (x).begin(), (x).end()
const int INF = 1e9 + 5;
const int N = 200000 + 5;
const ll oo = 1e18 + 5;
const ll mod = 998244353;
vector<pair<int, int>> v[55];
ll dp[55][2505];
struct node {
ll val;
int id, sum;
node(ll val, int id, int sum) : val(val), id(id), sum(sum) {}
};
void min_self(int &a, int b) {
if (a > b)
a = b;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
const int maxx = 2500;
int n, m, s;
cin >> n >> m >> s;
vector<int> a(55), b(55), c(55), d(55);
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y >> a[i] >> b[i];
--x;
--y;
v[x].push_back({y, i});
v[y].push_back({x, i});
}
for (int i = 0; i < n; ++i)
cin >> c[i] >> d[i];
min_self(s, maxx);
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= maxx; ++j)
dp[i][j] = oo;
}
queue<node> Q;
dp[0][s] = 0;
Q.push({0, 0, s});
while (!Q.empty()) {
node ele = Q.front();
Q.pop();
int idx = ele.id;
int coin = ele.sum;
if (ele.val > dp[idx][coin])
continue;
int n_coin = min(maxx, coin + c[idx]);
if (dp[idx][n_coin] > dp[idx][coin] + d[idx]) {
dp[idx][n_coin] = dp[idx][coin] + d[idx];
Q.push({dp[idx][n_coin], idx, n_coin});
}
for (pair<int, int> x : v[idx]) {
int xx = x.first;
int idd = x.second;
if (coin < a[idd])
continue;
if (dp[xx][coin - a[idd]] > dp[idx][coin] + b[idd]) {
dp[xx][coin - a[idd]] = dp[idx][coin] + b[idd];
Q.push({dp[xx][coin - a[idd]], xx, coin - a[idd]});
}
}
}
for (int i = 1; i < n; ++i) {
ll ans = oo;
for (int coin = 0; coin <= maxx; ++coin) {
ans = min(ans, dp[i][coin]);
}
cout << ans << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define all(x) (x).begin(), (x).end()
const int INF = 1e9 + 5;
const int N = 200000 + 5;
const ll oo = 1e18 + 5;
const ll mod = 998244353;
vector<pair<int, int>> v[55];
ll dp[55][2505];
struct node {
ll val;
int id, sum;
node(ll val, int id, int sum) : val(val), id(id), sum(sum) {}
};
void min_self(int &a, int b) {
if (a > b)
a = b;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
const int maxx = 2500;
int n, m, s;
cin >> n >> m >> s;
vector<int> a(105), b(105), c(55), d(55);
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y >> a[i] >> b[i];
--x;
--y;
v[x].push_back({y, i});
v[y].push_back({x, i});
}
for (int i = 0; i < n; ++i)
cin >> c[i] >> d[i];
min_self(s, maxx);
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= maxx; ++j)
dp[i][j] = oo;
}
queue<node> Q;
dp[0][s] = 0;
Q.push({0, 0, s});
while (!Q.empty()) {
node ele = Q.front();
Q.pop();
int idx = ele.id;
int coin = ele.sum;
if (ele.val > dp[idx][coin])
continue;
int n_coin = min(maxx, coin + c[idx]);
if (dp[idx][n_coin] > dp[idx][coin] + d[idx]) {
dp[idx][n_coin] = dp[idx][coin] + d[idx];
Q.push({dp[idx][n_coin], idx, n_coin});
}
for (pair<int, int> x : v[idx]) {
int xx = x.first;
int idd = x.second;
if (coin < a[idd])
continue;
if (dp[xx][coin - a[idd]] > dp[idx][coin] + b[idd]) {
dp[xx][coin - a[idd]] = dp[idx][coin] + b[idd];
Q.push({dp[xx][coin - a[idd]], xx, coin - a[idd]});
}
}
}
for (int i = 1; i < n; ++i) {
ll ans = oo;
for (int coin = 0; coin <= maxx; ++coin) {
ans = min(ans, dp[i][coin]);
}
cout << ans << endl;
}
return 0;
}
| replace | 30 | 31 | 30 | 31 | 0 | |
p02703 | C++ | 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;
const ll mod = 1000000007;
const double eps = 1e-8;
#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif
const int MAX_COIN = 62501;
const int MAX_V = 50 * 62501;
const ll INF = 1e17;
typedef pair<ll, int> P;
struct Edge {
int to;
ll cost;
};
vector<Edge> G[MAX_V];
int main() {
int N, M, S;
cin >> N >> M >> S;
S = min(S, MAX_COIN - 1);
for (int i = 0; i < M; ++i) {
int U, V, A;
ll B;
cin >> U >> V >> A >> B;
U--;
V--;
for (int j = A; j < MAX_COIN; ++j) {
G[U * MAX_COIN + j].push_back((Edge){V * MAX_COIN + j - A, B});
G[V * MAX_COIN + j].push_back((Edge){U * MAX_COIN + j - A, B});
}
}
for (int i = 0; i < N; ++i) {
ll C, D;
cin >> C >> D;
for (int j = 0; j < MAX_COIN; ++j) {
int CC = min(j + (int)C, MAX_COIN - 1);
G[i * MAX_COIN + j].push_back((Edge){i * MAX_COIN + CC, D});
}
}
// dijkstra
vector<ll> d(MAX_V, INF);
priority_queue<P, vector<P>, greater<P>> q;
q.push(P(0LL, S));
while (!q.empty()) {
P p = q.top();
q.pop();
if (d[p.second] <= p.first)
continue;
d[p.second] = p.first;
for (Edge &e : G[p.second]) {
if (d[e.to] > p.first + e.cost) {
q.push(P(p.first + e.cost, e.to));
}
}
}
for (int t = 1; t < N; ++t) {
ll res = INF;
for (int j = 0; j < MAX_COIN; ++j) {
res = min(res, d[t * MAX_COIN + j]);
}
cout << res << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const ll mod = 1000000007;
const double eps = 1e-8;
#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif
const int MAX_COIN = 2501;
const int MAX_V = 50 * 2501;
const ll INF = 1e17;
typedef pair<ll, int> P;
struct Edge {
int to;
ll cost;
};
vector<Edge> G[MAX_V];
int main() {
int N, M, S;
cin >> N >> M >> S;
S = min(S, MAX_COIN - 1);
for (int i = 0; i < M; ++i) {
int U, V, A;
ll B;
cin >> U >> V >> A >> B;
U--;
V--;
for (int j = A; j < MAX_COIN; ++j) {
G[U * MAX_COIN + j].push_back((Edge){V * MAX_COIN + j - A, B});
G[V * MAX_COIN + j].push_back((Edge){U * MAX_COIN + j - A, B});
}
}
for (int i = 0; i < N; ++i) {
ll C, D;
cin >> C >> D;
for (int j = 0; j < MAX_COIN; ++j) {
int CC = min(j + (int)C, MAX_COIN - 1);
G[i * MAX_COIN + j].push_back((Edge){i * MAX_COIN + CC, D});
}
}
// dijkstra
vector<ll> d(MAX_V, INF);
priority_queue<P, vector<P>, greater<P>> q;
q.push(P(0LL, S));
while (!q.empty()) {
P p = q.top();
q.pop();
if (d[p.second] <= p.first)
continue;
d[p.second] = p.first;
for (Edge &e : G[p.second]) {
if (d[e.to] > p.first + e.cost) {
q.push(P(p.first + e.cost, e.to));
}
}
}
for (int t = 1; t < N; ++t) {
ll res = INF;
for (int j = 0; j < MAX_COIN; ++j) {
res = min(res, d[t * MAX_COIN + j]);
}
cout << res << endl;
}
return 0;
} | replace | 14 | 16 | 14 | 16 | TLE | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using LL = long long;
const int Max_A = 5010;
const int Max_N = 55;
const LL INF = 1e18;
struct node {
int silver, num;
LL now_time;
node(int s, int v, LL t) : silver(s), num(v), now_time(t) {}
bool operator>(const node &v) const {
if (now_time == v.now_time)
return silver > v.silver;
return now_time > v.now_time;
}
};
struct edge {
int to, cost;
LL time;
edge(int to, int cost, LL time) : to(to), cost(cost), time(time) {}
};
LL dist[Max_A][Max_N];
vector<vector<edge>> G;
vector<int> C;
vector<LL> D;
priority_queue<node, vector<node>, greater<node>> q;
void dijkstra() {
while (!q.empty()) {
node v = q.top();
q.pop();
int S = v.silver, n = v.num;
LL T = v.now_time;
if (dist[S][n] < T)
continue;
node v_plus(min(Max_A, S + C[n]), n, T + D[n]);
q.push(v_plus);
rep(i, G[n].size()) {
int next = G[n][i].to, c = G[n][i].cost;
LL t = G[n][i].time;
if (S < c)
continue;
if (dist[S - c][next] <= T + t)
continue;
dist[S - c][next] = T + t;
node nv(S - c, next, T + t);
q.push(nv);
}
}
}
int main() {
int N, M, S;
cin >> N >> M >> S;
G.resize(N);
rep(i, M) {
int u, v, a;
LL b;
cin >> u >> v >> a >> b;
u--;
v--;
G[u].emplace_back(v, a, b);
G[v].emplace_back(u, a, b);
}
C.resize(N), D.resize(N);
rep(i, N) cin >> C[i] >> D[i];
rep(i, Max_A) { rep(j, Max_N) dist[i][j] = INF; }
S = min(S, Max_A);
dist[S][0] = 0;
node st(S, 0, 0);
q.push(st);
dijkstra();
vector<LL> ans(N - 1, INF);
rep(i, Max_A) { rep(j, N - 1) ans[j] = min(ans[j], dist[i][j + 1]); }
rep(i, N - 1) cout << ans[i] << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using LL = long long;
const int Max_A = 5010;
const int Max_N = 55;
const LL INF = 1e18;
struct node {
int silver, num;
LL now_time;
node(int s, int v, LL t) : silver(s), num(v), now_time(t) {}
bool operator>(const node &v) const {
if (now_time == v.now_time)
return silver > v.silver;
return now_time > v.now_time;
}
};
struct edge {
int to, cost;
LL time;
edge(int to, int cost, LL time) : to(to), cost(cost), time(time) {}
};
LL dist[Max_A][Max_N];
vector<vector<edge>> G;
vector<int> C;
vector<LL> D;
priority_queue<node, vector<node>, greater<node>> q;
void dijkstra() {
while (!q.empty()) {
node v = q.top();
q.pop();
int S = v.silver, n = v.num;
LL T = v.now_time;
if (dist[S][n] < T)
continue;
int nS = min(Max_A - 1, S + C[n]);
if (dist[nS][n] > T + D[n]) {
node v_plus(nS, n, T + D[n]);
dist[nS][n] = T + D[n];
q.push(v_plus);
}
rep(i, G[n].size()) {
int next = G[n][i].to, c = G[n][i].cost;
LL t = G[n][i].time;
if (S < c)
continue;
if (dist[S - c][next] <= T + t)
continue;
dist[S - c][next] = T + t;
node nv(S - c, next, T + t);
q.push(nv);
}
}
}
int main() {
int N, M, S;
cin >> N >> M >> S;
G.resize(N);
rep(i, M) {
int u, v, a;
LL b;
cin >> u >> v >> a >> b;
u--;
v--;
G[u].emplace_back(v, a, b);
G[v].emplace_back(u, a, b);
}
C.resize(N), D.resize(N);
rep(i, N) cin >> C[i] >> D[i];
rep(i, Max_A) { rep(j, Max_N) dist[i][j] = INF; }
S = min(S, Max_A);
dist[S][0] = 0;
node st(S, 0, 0);
q.push(st);
dijkstra();
vector<LL> ans(N - 1, INF);
rep(i, Max_A) { rep(j, N - 1) ans[j] = min(ans[j], dist[i][j + 1]); }
rep(i, N - 1) cout << ans[i] << endl;
return 0;
} | replace | 40 | 42 | 40 | 46 | TLE | |
p02703 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007ll
#define mod1 998244353ll
#define endl '\n'
#define inf ((ll)1e18 + 1)
#include <chrono>
using namespace chrono;
mt19937_64 rng(high_resolution_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set \
tree<ll, null_type, less_equal<ll>, rb_tree_tag, \
tree_order_statistics_node_update>
#define deb(x) cout << "--> " << #x << " : " << x << endl;
#define debr(x, y) \
{ \
cout << "--> " << #x << " to " << #y << " : "; \
auto itr = x; \
while (itr != y) \
cout << (*itr++) << ' '; \
cout << endl; \
}
#define deba(x) \
{ \
cout << "--> " << #x << " : "; \
for (auto ele : (x)) \
cout << ele << " "; \
cout << endl; \
}
class cmp {
public:
bool operator()(array<ll, 3> &a, array<ll, 3> &b) { return a[2] < b[2]; }
};
void solve(vector<array<ll, 3>> edges[], pair<ll, ll> change[],
vector<ll> distance[], ll n, ll maxi, ll s) {
priority_queue<array<ll, 3>, vector<array<ll, 3>>, cmp> pq; //{vert,coins,min}
pq.push({0, s, 0});
{
ll time = 0;
while (s + change[0].first < maxi) {
s += change[0].first;
time += change[0].second;
distance[0][s] = time;
pq.push({0, s, time});
}
}
while (!pq.empty()) {
auto pa = pq.top();
pq.pop();
ll u = pa[0];
ll silver = pa[1];
ll time = pa[2];
for (auto ed : edges[u]) {
ll v = ed[0];
ll cost = ed[1];
ll more = ed[2];
ll temp_s = silver - cost;
if (temp_s < 0)
continue;
ll temp_t = time + more;
while (temp_s < maxi) {
if (temp_t < distance[v][temp_s]) {
distance[v][temp_s] = temp_t;
pq.push({v, temp_s, temp_t});
}
temp_s += change[v].first;
temp_t += change[v].second;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
ll n, m, s;
cin >> n >> m >> s;
vector<array<ll, 3>> edges[n];
ll maxi = 1;
for (int x = 0; x < m; x++) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
maxi += a;
edges[u].push_back({v, a, b});
edges[v].push_back({u, a, b});
}
pair<ll, ll> change[n];
for (int x = 0; x < n; x++) {
cin >> change[x].first >> change[x].second;
}
vector<ll> distance[n];
for (int x = 0; x < n; x++) {
distance[x].resize(maxi);
for (int y = 0; y < maxi; y++) {
distance[x][y] = inf;
}
}
if (s > maxi) {
s = maxi - 1;
}
distance[0][s] = 0;
solve(edges, change, distance, n, maxi, s);
for (int x = 1; x < n; x++) {
ll mini = inf;
for (ll y = 0; y < maxi; y++) {
mini = min(mini, distance[x][y]);
}
cout << mini << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007ll
#define mod1 998244353ll
#define endl '\n'
#define inf ((ll)1e18 + 1)
#include <chrono>
using namespace chrono;
mt19937_64 rng(high_resolution_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set \
tree<ll, null_type, less_equal<ll>, rb_tree_tag, \
tree_order_statistics_node_update>
#define deb(x) cout << "--> " << #x << " : " << x << endl;
#define debr(x, y) \
{ \
cout << "--> " << #x << " to " << #y << " : "; \
auto itr = x; \
while (itr != y) \
cout << (*itr++) << ' '; \
cout << endl; \
}
#define deba(x) \
{ \
cout << "--> " << #x << " : "; \
for (auto ele : (x)) \
cout << ele << " "; \
cout << endl; \
}
class cmp {
public:
bool operator()(array<ll, 3> &a, array<ll, 3> &b) { return a[2] > b[2]; }
};
void solve(vector<array<ll, 3>> edges[], pair<ll, ll> change[],
vector<ll> distance[], ll n, ll maxi, ll s) {
priority_queue<array<ll, 3>, vector<array<ll, 3>>, cmp> pq; //{vert,coins,min}
pq.push({0, s, 0});
{
ll time = 0;
while (s + change[0].first < maxi) {
s += change[0].first;
time += change[0].second;
distance[0][s] = time;
pq.push({0, s, time});
}
}
while (!pq.empty()) {
auto pa = pq.top();
pq.pop();
ll u = pa[0];
ll silver = pa[1];
ll time = pa[2];
for (auto ed : edges[u]) {
ll v = ed[0];
ll cost = ed[1];
ll more = ed[2];
ll temp_s = silver - cost;
if (temp_s < 0)
continue;
ll temp_t = time + more;
while (temp_s < maxi) {
if (temp_t < distance[v][temp_s]) {
distance[v][temp_s] = temp_t;
pq.push({v, temp_s, temp_t});
}
temp_s += change[v].first;
temp_t += change[v].second;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
ll n, m, s;
cin >> n >> m >> s;
vector<array<ll, 3>> edges[n];
ll maxi = 1;
for (int x = 0; x < m; x++) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
maxi += a;
edges[u].push_back({v, a, b});
edges[v].push_back({u, a, b});
}
pair<ll, ll> change[n];
for (int x = 0; x < n; x++) {
cin >> change[x].first >> change[x].second;
}
vector<ll> distance[n];
for (int x = 0; x < n; x++) {
distance[x].resize(maxi);
for (int y = 0; y < maxi; y++) {
distance[x][y] = inf;
}
}
if (s > maxi) {
s = maxi - 1;
}
distance[0][s] = 0;
solve(edges, change, distance, n, maxi, s);
for (int x = 1; x < n; x++) {
ll mini = inf;
for (ll y = 0; y < maxi; y++) {
mini = min(mini, distance[x][y]);
}
cout << mini << endl;
}
return 0;
} | replace | 35 | 36 | 35 | 36 | TLE | |
p02703 | C++ | Time Limit Exceeded | #include <climits>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
int main() {
ll n, m, s;
pair<ll, ll> node[51][51];
ll min_length[51][51];
pair<ll, ll> city[51];
cin >> n >> m >> s;
pair<ll, ll> zero = make_pair(0, 0);
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
node[i][j] = zero;
}
}
for (ll i = 0; i < m; i++) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
// (ginka, minute)
node[u][v] = make_pair(a, b);
node[v][u] = make_pair(a, b);
}
for (int i = 1; i <= n; i++) {
ll c, d;
cin >> c >> d;
city[i] = make_pair(c, d);
}
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
if (node[i][j] != zero) {
min_length[i][j] = node[i][j].second;
} else if (i == j) {
min_length[i][j] = 0;
} else {
min_length[i][j] = INT32_MAX;
}
}
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (min_length[i][j] > min_length[i][k] + min_length[k][j]) {
min_length[i][j] = min_length[i][k] + min_length[k][j];
}
}
}
}
for (int i = 2; i <= n; i++) {
priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>,
greater<>>
q;
set<pair<ll, ll>> ss;
// (kitaiti, ginka, position)
q.push(make_pair(min_length[1][i], make_pair(s, 1)));
while (!q.empty()) {
auto current = q.top();
q.pop();
ll possession = current.second.first;
ll position = current.second.second;
ll expected_time = current.first - min_length[position][i];
ss.insert(current.second);
if (position == i) {
cout << expected_time << endl;
break;
}
for (int j = 1; j <= n; j++) {
if (node[position][j] != zero &&
possession >= node[position][j].first) {
auto state = make_pair(possession - node[position][j].first, j);
if (ss.find(state) == ss.end()) {
auto pp = make_pair(expected_time + node[position][j].second +
min_length[j][i],
state);
q.push(pp);
}
}
}
auto state = make_pair(possession + city[position].first, position);
if (ss.find(state) == ss.end()) {
auto pp = make_pair(expected_time + city[position].second +
min_length[position][i],
state);
q.push(pp);
}
}
}
}
| #include <climits>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
int main() {
ll n, m, s;
pair<ll, ll> node[51][51];
ll min_length[51][51];
pair<ll, ll> city[51];
cin >> n >> m >> s;
pair<ll, ll> zero = make_pair(0, 0);
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
node[i][j] = zero;
}
}
for (ll i = 0; i < m; i++) {
ll u, v, a, b;
cin >> u >> v >> a >> b;
// (ginka, minute)
node[u][v] = make_pair(a, b);
node[v][u] = make_pair(a, b);
}
for (int i = 1; i <= n; i++) {
ll c, d;
cin >> c >> d;
city[i] = make_pair(c, d);
}
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
if (node[i][j] != zero) {
min_length[i][j] = node[i][j].second;
} else if (i == j) {
min_length[i][j] = 0;
} else {
min_length[i][j] = INT32_MAX;
}
}
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (min_length[i][j] > min_length[i][k] + min_length[k][j]) {
min_length[i][j] = min_length[i][k] + min_length[k][j];
}
}
}
}
for (int i = 2; i <= n; i++) {
priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>,
greater<>>
q;
set<pair<ll, ll>> ss;
// (kitaiti, ginka, position)
q.push(make_pair(min_length[1][i], make_pair(s, 1)));
while (!q.empty()) {
auto current = q.top();
q.pop();
ll possession = current.second.first;
ll position = current.second.second;
ll expected_time = current.first - min_length[position][i];
if (ss.find(current.second) != ss.end()) {
continue;
}
ss.insert(current.second);
if (position == i) {
cout << expected_time << endl;
break;
}
for (int j = 1; j <= n; j++) {
if (node[position][j] != zero &&
possession >= node[position][j].first) {
auto state = make_pair(possession - node[position][j].first, j);
if (ss.find(state) == ss.end()) {
auto pp = make_pair(expected_time + node[position][j].second +
min_length[j][i],
state);
q.push(pp);
}
}
}
auto state = make_pair(possession + city[position].first, position);
if (ss.find(state) == ss.end()) {
auto pp = make_pair(expected_time + city[position].second +
min_length[position][i],
state);
q.push(pp);
}
}
}
}
| insert | 66 | 66 | 66 | 69 | TLE | |
p02703 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// const ll mod = 998244353 ;
const ll mod = 1000000007;
const ll inf = 1e18;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REP_FROM(i, j, n) for (ll i = (j); i < (n); ++i)
#define REP_REV(i, n) for (ll i = n - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
template <typename T> void print_vec(vector<T> a) {
int n = a.size();
REP(i, n) {
cout >> a[i];
if (i != n - 1)
cout << " ";
else
cout << endl;
}
}
ll power(ll base, ll exponent) {
if (exponent % 2) {
return power(base, exponent - 1) * base % mod;
} else if (exponent) {
ll root_ans = power(base, exponent / 2);
return root_ans * root_ans % mod;
} else {
return 1;
}
}
ll inverse(ll x) { return power(x, mod - 2); }
ll gcd(ll a, ll b) {
if (a < b)
gcd(b, a);
ll r;
while (r = a % b) {
a = b;
b = r;
}
return b;
}
template <typename T> ll sum(T begin, T end) {
return accumulate(begin, end, 0ll);
}
struct combination {
vector<ll> fact, inv;
combination(int sz) : fact(sz + 1), inv(sz + 1) {
fact[0] = 1;
for (int i = 1; i <= sz; i++) {
fact[i] = fact[i - 1] * i % mod;
}
inv[sz] = power(fact[sz], mod - 2);
for (int i = sz - 1; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}
ll C(int p, int q) const {
if (q < 0 || p < q)
return 0;
return (fact[p] * inv[q] % mod * inv[p - q] % mod);
}
};
using Pair = pair<ll, ll>;
template <ll Modulus> struct ModInt {
ll a;
constexpr ModInt(const ll x = 0) noexcept : a((x % mod + mod) % mod) {}
constexpr ll &value() noexcept { return a; }
constexpr const ll &value() const noexcept { return a; }
constexpr ModInt operator+(const ModInt x) const noexcept {
return ModInt(*this) += x;
}
constexpr ModInt operator-(const ModInt x) const noexcept {
return ModInt(*this) -= x;
}
constexpr ModInt operator*(const ModInt x) const noexcept {
return ModInt(*this) *= x;
}
constexpr ModInt operator/(const ModInt x) const noexcept {
return ModInt(*this) /= x;
}
constexpr ModInt &operator+=(const ModInt x) noexcept {
a += x.a;
if (a >= Modulus) {
a -= Modulus;
}
return *this;
}
constexpr ModInt &operator-=(const ModInt x) noexcept {
if (a < x.a) {
a += Modulus;
}
a -= x.a;
return *this;
}
constexpr ModInt &operator*=(const ModInt x) noexcept {
a = a * x.a % Modulus;
return *this;
}
constexpr ModInt &operator/=(ModInt x) noexcept {
ll exp = Modulus - 2;
while (exp) {
if (exp % 2) {
*this *= x;
}
x *= x;
exp /= 2;
}
return *this;
}
constexpr ModInt operator-() noexcept { return ModInt(-a); }
friend ostream &operator<<(ostream &os, const ModInt &m) {
os << m.a;
return os;
}
};
using mint = ModInt<mod>;
signed main() {
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll n, m, s;
cin >> n >> m >> s;
vector<vector<int>> a(n), b(n), g(n);
REP(i, m) {
int u, v, x, y;
cin >> u >> v >> x >> y;
u--;
v--;
g[u].push_back(v);
a[u].push_back(x);
b[u].push_back(y);
g[v].push_back(u);
a[v].push_back(x);
b[v].push_back(y);
}
vector<int> c(n), d(n);
REP(i, n) cin >> c[i] >> d[i];
vector<vector<ll>> dp(n, vector<ll>(2501, inf));
using Triple = tuple<ll, int, ll>;
priority_queue<Triple, vector<Triple>, greater<>> q;
q.emplace(0, 0, s);
while (q.size()) {
auto x = q.top();
q.pop();
ll time = get<0>(x);
int node = get<1>(x);
ll gold = get<2>(x);
if (dp[node][gold] <= time)
continue;
dp[node][gold] = time;
q.emplace(time + d[node], node, min(gold + c[node], 2500ll));
REP(i, sz(g[node])) {
if (gold < a[node][i])
continue;
q.emplace(time + b[node][i], g[node][i], gold - a[node][i]);
}
}
REP_FROM(i, 1, n) { cout << *min_element(all(dp[i])) << endl; }
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// const ll mod = 998244353 ;
const ll mod = 1000000007;
const ll inf = 1e18;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REP_FROM(i, j, n) for (ll i = (j); i < (n); ++i)
#define REP_REV(i, n) for (ll i = n - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
template <typename T> void print_vec(vector<T> a) {
int n = a.size();
REP(i, n) {
cout >> a[i];
if (i != n - 1)
cout << " ";
else
cout << endl;
}
}
ll power(ll base, ll exponent) {
if (exponent % 2) {
return power(base, exponent - 1) * base % mod;
} else if (exponent) {
ll root_ans = power(base, exponent / 2);
return root_ans * root_ans % mod;
} else {
return 1;
}
}
ll inverse(ll x) { return power(x, mod - 2); }
ll gcd(ll a, ll b) {
if (a < b)
gcd(b, a);
ll r;
while (r = a % b) {
a = b;
b = r;
}
return b;
}
template <typename T> ll sum(T begin, T end) {
return accumulate(begin, end, 0ll);
}
struct combination {
vector<ll> fact, inv;
combination(int sz) : fact(sz + 1), inv(sz + 1) {
fact[0] = 1;
for (int i = 1; i <= sz; i++) {
fact[i] = fact[i - 1] * i % mod;
}
inv[sz] = power(fact[sz], mod - 2);
for (int i = sz - 1; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}
ll C(int p, int q) const {
if (q < 0 || p < q)
return 0;
return (fact[p] * inv[q] % mod * inv[p - q] % mod);
}
};
using Pair = pair<ll, ll>;
template <ll Modulus> struct ModInt {
ll a;
constexpr ModInt(const ll x = 0) noexcept : a((x % mod + mod) % mod) {}
constexpr ll &value() noexcept { return a; }
constexpr const ll &value() const noexcept { return a; }
constexpr ModInt operator+(const ModInt x) const noexcept {
return ModInt(*this) += x;
}
constexpr ModInt operator-(const ModInt x) const noexcept {
return ModInt(*this) -= x;
}
constexpr ModInt operator*(const ModInt x) const noexcept {
return ModInt(*this) *= x;
}
constexpr ModInt operator/(const ModInt x) const noexcept {
return ModInt(*this) /= x;
}
constexpr ModInt &operator+=(const ModInt x) noexcept {
a += x.a;
if (a >= Modulus) {
a -= Modulus;
}
return *this;
}
constexpr ModInt &operator-=(const ModInt x) noexcept {
if (a < x.a) {
a += Modulus;
}
a -= x.a;
return *this;
}
constexpr ModInt &operator*=(const ModInt x) noexcept {
a = a * x.a % Modulus;
return *this;
}
constexpr ModInt &operator/=(ModInt x) noexcept {
ll exp = Modulus - 2;
while (exp) {
if (exp % 2) {
*this *= x;
}
x *= x;
exp /= 2;
}
return *this;
}
constexpr ModInt operator-() noexcept { return ModInt(-a); }
friend ostream &operator<<(ostream &os, const ModInt &m) {
os << m.a;
return os;
}
};
using mint = ModInt<mod>;
signed main() {
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll n, m, s;
cin >> n >> m >> s;
vector<vector<int>> a(n), b(n), g(n);
REP(i, m) {
int u, v, x, y;
cin >> u >> v >> x >> y;
u--;
v--;
g[u].push_back(v);
a[u].push_back(x);
b[u].push_back(y);
g[v].push_back(u);
a[v].push_back(x);
b[v].push_back(y);
}
vector<int> c(n), d(n);
REP(i, n) cin >> c[i] >> d[i];
vector<vector<ll>> dp(n, vector<ll>(2501, inf));
using Triple = tuple<ll, int, ll>;
priority_queue<Triple, vector<Triple>, greater<>> q;
q.emplace(0, 0, min(s, 2500ll));
while (q.size()) {
auto x = q.top();
q.pop();
ll time = get<0>(x);
int node = get<1>(x);
ll gold = get<2>(x);
if (dp[node][gold] <= time)
continue;
dp[node][gold] = time;
q.emplace(time + d[node], node, min(gold + c[node], 2500ll));
REP(i, sz(g[node])) {
if (gold < a[node][i])
continue;
q.emplace(time + b[node][i], g[node][i], gold - a[node][i]);
}
}
REP_FROM(i, 1, n) { cout << *min_element(all(dp[i])) << endl; }
return 0;
}
| replace | 162 | 163 | 162 | 163 | 0 | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using LL = long long;
struct Vertex {
int id;
LL coin, time;
};
struct Edge {
int from, to;
LL coin, time;
};
using Graph = vector<vector<Edge>>;
int main() {
int N, M;
LL S;
cin >> N >> M >> S;
Graph G(N);
for (auto i = 0; i < M; ++i) {
int U, V;
LL A, B;
cin >> U >> V >> A >> B;
--U;
--V;
G[U].push_back({U, V, A, B});
G[V].push_back({V, U, A, B});
}
vector<Vertex> V(N);
for (auto i = 0; i < N; ++i)
V[i].id = i;
for (auto &v : V)
cin >> v.coin >> v.time;
vector<LL> d(N, -1);
priority_queue<Vertex, vector<Vertex>, function<bool(Vertex &, Vertex &)>> q(
[](auto &l, auto &r) {
if (l.time != r.time)
return l.time > r.time;
if (l.coin != r.coin)
return l.coin < r.coin;
return l.id < r.id;
});
vector<vector<LL>> memo(N, vector<LL>(2500 + 1, -1));
q.push({0, S, 0});
while (!q.empty()) {
auto cur = q.top();
q.pop();
if (memo[cur.id][cur.coin] != -1)
continue;
memo[cur.id][cur.coin] = cur.time;
if (d[cur.id] == -1)
d[cur.id] = cur.time;
for (auto &e : G[cur.id]) {
auto &v = V[cur.id];
for (auto nex :
{Vertex{e.to, cur.coin - e.coin, cur.time + e.time},
Vertex{v.id, min<LL>(cur.coin + v.coin, 2500), cur.time + v.time}})
if (0 <= nex.coin)
if (memo[nex.id][nex.coin] == -1)
q.push({nex.id, nex.coin, nex.time});
}
}
for (auto i = 1; i < N; ++i)
cout << d[i] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using LL = long long;
struct Vertex {
int id;
LL coin, time;
};
struct Edge {
int from, to;
LL coin, time;
};
using Graph = vector<vector<Edge>>;
int main() {
int N, M;
LL S;
cin >> N >> M >> S;
Graph G(N);
for (auto i = 0; i < M; ++i) {
int U, V;
LL A, B;
cin >> U >> V >> A >> B;
--U;
--V;
G[U].push_back({U, V, A, B});
G[V].push_back({V, U, A, B});
}
vector<Vertex> V(N);
for (auto i = 0; i < N; ++i)
V[i].id = i;
for (auto &v : V)
cin >> v.coin >> v.time;
vector<LL> d(N, -1);
priority_queue<Vertex, vector<Vertex>, function<bool(Vertex &, Vertex &)>> q(
[](auto &l, auto &r) {
if (l.time != r.time)
return l.time > r.time;
if (l.coin != r.coin)
return l.coin < r.coin;
return l.id < r.id;
});
vector<vector<LL>> memo(N, vector<LL>(2500 + 1, -1));
q.push({0, min<LL>(S, 2500), 0});
while (!q.empty()) {
auto cur = q.top();
q.pop();
if (memo[cur.id][cur.coin] != -1)
continue;
memo[cur.id][cur.coin] = cur.time;
if (d[cur.id] == -1)
d[cur.id] = cur.time;
for (auto &e : G[cur.id]) {
auto &v = V[cur.id];
for (auto nex :
{Vertex{e.to, cur.coin - e.coin, cur.time + e.time},
Vertex{v.id, min<LL>(cur.coin + v.coin, 2500), cur.time + v.time}})
if (0 <= nex.coin)
if (memo[nex.id][nex.coin] == -1)
q.push({nex.id, nex.coin, nex.time});
}
}
for (auto i = 1; i < N; ++i)
cout << d[i] << endl;
}
| replace | 46 | 47 | 46 | 47 | 0 | |
p02703 | Python | Time Limit Exceeded | import heapq
N, M, S = map(int, input().split())
uvab = [list(map(int, input().split())) for _ in range(M)]
cd = [list(map(int, input().split())) for _ in range(N)]
for i in range(M):
uvab[i][0] -= 1
uvab[i][1] -= 1
railways = [[] for _ in range(N)]
max_a = 0
for u, v, a, b in uvab:
railways[u].append((v, a, b))
railways[v].append((u, a, b))
max_a = max(max_a, a)
del uvab
max_silver = max_a * (N - 1)
ans = [-1] * N
d = dict()
d[(S, 0)] = 0 # silver, station
Q = [(0, S, 0)] # time, silver, station
sum_reached = 0
reached = [False] * N
while Q:
t, sv, st = heapq.heappop(Q)
if not reached[st]:
ans[st] = t
reached[st] = True
sum_reached += 1
if sum_reached == N:
break
new_key = (sv + cd[st][0], st)
new_value = t + cd[st][1]
if new_key not in d or d[new_key] > new_value:
d[new_key] = t + cd[st][1]
heapq.heappush(Q, (new_value, *new_key))
for dist, a, b in railways[st]:
if sv >= a:
new_key = (sv - a, dist)
new_value = t + b
if new_key not in d or d[new_key] > new_value:
d[new_key] = new_value
heapq.heappush(Q, (new_value, *new_key))
print(*ans[1:], sep="\n")
| import heapq
N, M, S = map(int, input().split())
uvab = [list(map(int, input().split())) for _ in range(M)]
cd = [list(map(int, input().split())) for _ in range(N)]
for i in range(M):
uvab[i][0] -= 1
uvab[i][1] -= 1
railways = [[] for _ in range(N)]
max_a = 0
for u, v, a, b in uvab:
railways[u].append((v, a, b))
railways[v].append((u, a, b))
max_a = max(max_a, a)
del uvab
max_silver = max_a * (N - 1)
ans = [-1] * N
d = dict()
d[(S, 0)] = 0 # silver, station
Q = [(0, S, 0)] # time, silver, station
sum_reached = 0
reached = [False] * N
while Q:
t, sv, st = heapq.heappop(Q)
if not reached[st]:
ans[st] = t
reached[st] = True
sum_reached += 1
if sum_reached == N:
break
new_key = (min(max_silver, sv + cd[st][0]), st)
new_value = t + cd[st][1]
if new_key not in d or d[new_key] > new_value:
d[new_key] = t + cd[st][1]
heapq.heappush(Q, (new_value, *new_key))
for dist, a, b in railways[st]:
if sv >= a:
new_key = (sv - a, dist)
new_value = t + b
if new_key not in d or d[new_key] > new_value:
d[new_key] = new_value
heapq.heappush(Q, (new_value, *new_key))
print(*ans[1:], sep="\n")
| replace | 37 | 38 | 37 | 38 | TLE | |
p02703 | Python | Time Limit Exceeded | import heapq
import os
from collections import defaultdict
import sys
if os.getenv("LOCAL"):
sys.stdin = open("_in.txt", "r")
sys.setrecursionlimit(10**9)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7
# MOD = 998244353
N, M, S = list(map(int, sys.stdin.buffer.readline().split()))
UVAB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]
CD = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(N)]
CD = [(0, 0)] + CD
graph = [[] for _ in range(N + 1)]
for u, v, a, b in UVAB:
graph[u].append((a, b, v))
graph[v].append((a, b, u))
for vs in graph:
vs.sort()
mada = set(list(range(2, N + 1)))
heap = [(0, 1, S)]
ans = [INF] * (N + 1)
ans[1] = 0
# dp[v, s]: v にお金を s 持ってる状態で来たときの最小値
dp = defaultdict(lambda: INF)
dp[1, S] = 0
while heap and mada:
d, v, s = heapq.heappop(heap)
if d > dp[v, s]:
continue
if v in mada:
mada.remove(v)
ans[v] = min(ans[v], d)
for a, b, u in graph[v]:
if s - a < 0:
break
if d + b < dp[u, s - a]:
dp[u, s - a] = d + b
heapq.heappush(heap, (d + b, u, s - a))
c, dd = CD[v]
for _ in range(3):
if d + dd < dp[v, s + c]:
dp[v, s + c] = d + dd
heapq.heappush(heap, (d + dd, v, s + c))
print(*ans[2:], sep="\n")
| import heapq
import os
from collections import defaultdict
import sys
if os.getenv("LOCAL"):
sys.stdin = open("_in.txt", "r")
sys.setrecursionlimit(10**9)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7
# MOD = 998244353
N, M, S = list(map(int, sys.stdin.buffer.readline().split()))
UVAB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]
CD = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(N)]
CD = [(0, 0)] + CD
graph = [[] for _ in range(N + 1)]
for u, v, a, b in UVAB:
graph[u].append((a, b, v))
graph[v].append((a, b, u))
for vs in graph:
vs.sort()
mada = set(list(range(2, N + 1)))
heap = [(0, 1, S)]
ans = [INF] * (N + 1)
ans[1] = 0
# dp[v, s]: v にお金を s 持ってる状態で来たときの最小値
dp = defaultdict(lambda: INF)
dp[1, S] = 0
while heap and mada:
d, v, s = heapq.heappop(heap)
if d > dp[v, s]:
continue
if v in mada:
mada.remove(v)
ans[v] = min(ans[v], d)
for a, b, u in graph[v]:
if s - a < 0:
break
if d + b < dp[u, s - a]:
dp[u, s - a] = d + b
heapq.heappush(heap, (d + b, u, s - a))
c, dd = CD[v]
sc = min(s + c, 50 * N + 10)
if d + dd < dp[v, sc]:
dp[v, sc] = d + dd
heapq.heappush(heap, (d + dd, v, sc))
print(*ans[2:], sep="\n")
| replace | 50 | 54 | 50 | 54 | TLE | |
p02703 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define MAXCOIN 2500LL
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, m, i, j, k, x, y, s, a, b, t, coin, id;
cin >> n >> m >> s;
vector<vector<tuple<long long, long long, long long>>> adj(n + 1);
vector<long long> c(n + 1), d(n + 1);
for (i = 0; i < m; i++) {
cin >> x >> y >> a >> b;
adj[x].push_back({y, a, b});
adj[y].push_back({x, a, b});
}
for (i = 1; i <= n; i++)
cin >> c[i] >> d[i];
vector<vector<long long>> dp(
n + 1,
vector<long long>(MAXCOIN + 1,
(1LL << 60))); // dp[city][silve coins] = shortest time
dp[1][s] = 0;
priority_queue<tuple<long long, long long, long long>>
pq; // <-time, id, coin>
pq.push({0, 1, s});
while (!pq.empty()) {
tie(t, id, coin) = pq.top();
t = -t;
pq.pop();
if (dp[id][coin] != t)
continue;
// exchange
if (dp[id][min(MAXCOIN, coin + c[id])] > t + d[id]) {
dp[id][min(MAXCOIN, coin + c[id])] = t + d[id];
pq.push({-t - d[id], id, min(MAXCOIN, coin + c[id])});
}
// use it to go somewhere
for (auto p : adj[id]) {
tie(x, a, b) = p;
if (coin < a)
continue;
if (t + b < dp[x][coin - a]) {
dp[x][coin - a] = t + b;
pq.push({-t - b, x, coin - a});
}
}
}
vector<long long> ans(n + 1, (1LL << 60));
for (i = 2; i <= n; i++) {
for (j = 0; j <= MAXCOIN; j++)
ans[i] = min(ans[i], dp[i][j]);
}
for (i = 2; i <= n; i++)
cout << ans[i] << "\n";
return 0;
}
| #include <algorithm>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define MAXCOIN 2500LL
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, m, i, j, k, x, y, s, a, b, t, coin, id;
cin >> n >> m >> s;
vector<vector<tuple<long long, long long, long long>>> adj(n + 1);
vector<long long> c(n + 1), d(n + 1);
for (i = 0; i < m; i++) {
cin >> x >> y >> a >> b;
adj[x].push_back({y, a, b});
adj[y].push_back({x, a, b});
}
for (i = 1; i <= n; i++)
cin >> c[i] >> d[i];
vector<vector<long long>> dp(
n + 1,
vector<long long>(MAXCOIN + 1,
(1LL << 60))); // dp[city][silve coins] = shortest time
s = min(s, MAXCOIN);
dp[1][s] = 0;
priority_queue<tuple<long long, long long, long long>>
pq; // <-time, id, coin>
pq.push({0, 1, s});
while (!pq.empty()) {
tie(t, id, coin) = pq.top();
t = -t;
pq.pop();
if (dp[id][coin] != t)
continue;
// exchange
if (dp[id][min(MAXCOIN, coin + c[id])] > t + d[id]) {
dp[id][min(MAXCOIN, coin + c[id])] = t + d[id];
pq.push({-t - d[id], id, min(MAXCOIN, coin + c[id])});
}
// use it to go somewhere
for (auto p : adj[id]) {
tie(x, a, b) = p;
if (coin < a)
continue;
if (t + b < dp[x][coin - a]) {
dp[x][coin - a] = t + b;
pq.push({-t - b, x, coin - a});
}
}
}
vector<long long> ans(n + 1, (1LL << 60));
for (i = 2; i <= n; i++) {
for (j = 0; j <= MAXCOIN; j++)
ans[i] = min(ans[i], dp[i][j]);
}
for (i = 2; i <= n; i++)
cout << ans[i] << "\n";
return 0;
}
| insert | 45 | 45 | 45 | 46 | 0 | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
struct Edge {
long to, c, t;
};
const long inf = 1e18;
const long mx = 5000;
long n, m, s;
vector<Edge> g[50];
long ca[50];
long ti[50];
vector<long> sub() {
using PP = pair<long, long>;
using TT = pair<long, PP>;
priority_queue<TT, vector<TT>, greater<TT>> q;
q.push(TT(0, PP(0, min(s, mx))));
vector<vector<long>> dist(n, vector<long>(mx + 1, inf));
while (!q.empty()) {
auto tt = q.top();
q.pop();
long d = tt.first;
long from = tt.second.first;
long mon = tt.second.second;
if (dist[from][mon] != inf)
continue;
dist[from][mon] = d;
for (auto e : g[from]) {
long to = e.to;
long c = e.c;
long t = e.t;
if (mon >= c) {
q.push(TT(d + t, PP(to, mon - c)));
}
}
q.push(TT(d + ti[from], PP(from, mon + ca[from])));
}
vector<long> ret(n);
for (int i = 0; i < n; ++i) {
long mn = inf;
for (int j = 0; j <= mx; ++j) {
mn = min(mn, dist[i][j]);
}
ret[i] = mn;
}
return ret;
}
int main() {
cin >> n >> m >> s;
for (int j = 0; j < m; ++j) {
int u, v, a, b;
cin >> u >> v >> a >> b;
--u;
--v;
g[u].push_back(Edge{v, a, b});
g[v].push_back(Edge{u, a, b});
}
for (int i = 0; i < n; ++i) {
cin >> ca[i] >> ti[i];
}
auto v = sub();
for (int i = 1; i < n; ++i) {
cout << v[i] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
struct Edge {
long to, c, t;
};
const long inf = 1e18;
const long mx = 5000;
long n, m, s;
vector<Edge> g[50];
long ca[50];
long ti[50];
vector<long> sub() {
using PP = pair<long, long>;
using TT = pair<long, PP>;
priority_queue<TT, vector<TT>, greater<TT>> q;
q.push(TT(0, PP(0, min(s, mx))));
vector<vector<long>> dist(n, vector<long>(mx + 1, inf));
while (!q.empty()) {
auto tt = q.top();
q.pop();
long d = tt.first;
long from = tt.second.first;
long mon = tt.second.second;
if (dist[from][mon] != inf)
continue;
dist[from][mon] = d;
for (auto e : g[from]) {
long to = e.to;
long c = e.c;
long t = e.t;
if (mon >= c) {
q.push(TT(d + t, PP(to, mon - c)));
}
}
q.push(TT(d + ti[from], PP(from, min(mon + ca[from], mx))));
}
vector<long> ret(n);
for (int i = 0; i < n; ++i) {
long mn = inf;
for (int j = 0; j <= mx; ++j) {
mn = min(mn, dist[i][j]);
}
ret[i] = mn;
}
return ret;
}
int main() {
cin >> n >> m >> s;
for (int j = 0; j < m; ++j) {
int u, v, a, b;
cin >> u >> v >> a >> b;
--u;
--v;
g[u].push_back(Edge{v, a, b});
g[v].push_back(Edge{u, a, b});
}
for (int i = 0; i < n; ++i) {
cin >> ca[i] >> ti[i];
}
auto v = sub();
for (int i = 1; i < n; ++i) {
cout << v[i] << endl;
}
} | replace | 39 | 40 | 39 | 40 | 0 | |
p02703 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define int long long
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;
int n, m, s;
struct S {
int from, coin, cost;
bool operator>(const S &s) const { return cost > s.cost; }
};
struct edge {
int to, fee, cost;
};
vector<edge> G[51];
int sil[51], tim[51];
int d[51][2501];
struct Dijkstra {
Dijkstra() {}
void calc() {
REP(i, n) REP(j, 2501) d[i][j] = INF;
if (s >= 2500)
s = 2500;
d[0][s] = 0;
priority_queue<S, vector<S>, greater<S>> q;
q.push({0, s, 0});
while (!q.empty()) {
S p = q.top();
q.pop();
int from = p.from;
int coin = p.coin;
int cost = p.cost;
if (d[from][coin] < cost)
continue;
{
int nextCoin = coin + sil[from];
int nextCost = cost + tim[from];
if (d[from][nextCoin] > nextCost) {
d[from][nextCoin] = nextCost;
q.push({from, nextCoin, nextCost});
}
}
rep(i, 0, G[from].size()) {
int next = G[from][i].to;
int nextCoin = coin - G[from][i].fee;
int nextCost = cost + G[from][i].cost;
if (nextCoin >= 0 && d[next][nextCoin] > nextCost) {
d[next][nextCoin] = nextCost;
q.push({next, nextCoin, nextCost});
}
}
}
}
};
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> n >> m >> s;
REP(i, m) {
int u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
G[u].push_back({v, a, b});
G[v].push_back({u, a, b});
}
REP(i, n) { cin >> sil[i] >> tim[i]; }
Dijkstra dk;
dk.calc();
rep(i, 1, n) {
int ans = INF;
REP(j, 2501) ans = min(ans, d[i][j]);
cout << ans << endl;
}
} | #include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define int long long
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;
int n, m, s;
struct S {
int from, coin, cost;
bool operator>(const S &s) const { return cost > s.cost; }
};
struct edge {
int to, fee, cost;
};
vector<edge> G[51];
int sil[51], tim[51];
int d[51][2501];
struct Dijkstra {
Dijkstra() {}
void calc() {
REP(i, n) REP(j, 2501) d[i][j] = INF;
if (s >= 2500)
s = 2500;
d[0][s] = 0;
priority_queue<S, vector<S>, greater<S>> q;
q.push({0, s, 0});
while (!q.empty()) {
S p = q.top();
q.pop();
int from = p.from;
int coin = p.coin;
int cost = p.cost;
if (d[from][coin] < cost)
continue;
{
int nextCoin = coin + sil[from];
int nextCost = cost + tim[from];
if (nextCoin > 2500)
nextCoin = 2500;
if (d[from][nextCoin] > nextCost) {
d[from][nextCoin] = nextCost;
q.push({from, nextCoin, nextCost});
}
}
rep(i, 0, G[from].size()) {
int next = G[from][i].to;
int nextCoin = coin - G[from][i].fee;
int nextCost = cost + G[from][i].cost;
if (nextCoin >= 0 && d[next][nextCoin] > nextCost) {
d[next][nextCoin] = nextCost;
q.push({next, nextCoin, nextCost});
}
}
}
}
};
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> n >> m >> s;
REP(i, m) {
int u, v, a, b;
cin >> u >> v >> a >> b;
u--;
v--;
G[u].push_back({v, a, b});
G[v].push_back({u, a, b});
}
REP(i, n) { cin >> sil[i] >> tim[i]; }
Dijkstra dk;
dk.calc();
rep(i, 1, n) {
int ans = INF;
REP(j, 2501) ans = min(ans, d[i][j]);
cout << ans << endl;
}
} | insert | 44 | 44 | 44 | 46 | 0 | |
p02703 | C++ | Time Limit Exceeded | #pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
const int N = 205;
typedef long long ll;
struct Edge {
int to, next, c;
ll w;
} e[N << 1];
struct node {
int pos, res;
ll len;
bool operator<(const node &x) const { return x.len < len; }
};
priority_queue<node> q;
int n, m, s, cnt, first[N], maxt;
ll dis[N][5005], c[N], d[N], ans[N];
void add(int u, int v, int c, ll w) {
e[++cnt] = (Edge){v, first[u], c, w};
first[u] = cnt;
}
int calc(int x) { return min(x, maxt); }
void Dijkstra() {
memset(dis, 0x7f, sizeof(dis));
dis[1][calc(s)] = 0;
q.push((node){1, calc(s), 0});
while (!q.empty()) {
node tem = q.top();
q.pop();
int u = tem.pos, res = tem.res;
ll len = tem.len;
if (len != dis[u][res])
continue;
for (int i = 1; i; i++) {
int now = calc(res + i * c[u]);
ll k = i * d[u] + len;
if (dis[u][now] > k) {
dis[u][now] = k;
q.push((node){u, now, k});
}
if (now == maxt)
break;
}
for (int i = first[u]; i; i = e[i].next) {
int v = e[i].to;
ll k = len + e[i].w;
if (res < e[i].c)
continue;
if (dis[v][res - e[i].c] > k) {
dis[v][res - e[i].c] = k;
q.push((node){v, res - e[i].c, k});
}
}
}
memset(ans, 0x7f, sizeof(ans));
for (int i = 2; i <= n; i++)
for (int j = 0; j <= maxt; j++)
ans[i] = min(ans[i], dis[i][j]);
}
int main() {
cin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
int u, v, x;
ll y;
cin >> u >> v >> x >> y;
add(u, v, x, y);
add(v, u, x, y);
maxt = max(maxt, x);
}
maxt = maxt * m;
for (int i = 1; i <= n; i++)
cin >> c[i] >> d[i];
Dijkstra();
for (int i = 2; i <= n; i++)
cout << ans[i] << endl;
return 0;
} | #pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
const int N = 205;
typedef long long ll;
struct Edge {
int to, next, c;
ll w;
} e[N << 1];
struct node {
int pos, res;
ll len;
bool operator<(const node &x) const { return x.len < len; }
};
priority_queue<node> q;
int n, m, s, cnt, first[N], maxt;
ll dis[N][5005], c[N], d[N], ans[N];
void add(int u, int v, int c, ll w) {
e[++cnt] = (Edge){v, first[u], c, w};
first[u] = cnt;
}
int calc(int x) { return min(x, maxt); }
void Dijkstra() {
memset(dis, 0x7f, sizeof(dis));
dis[1][calc(s)] = 0;
q.push((node){1, calc(s), 0});
while (!q.empty()) {
node tem = q.top();
q.pop();
int u = tem.pos, res = tem.res;
ll len = tem.len;
if (len != dis[u][res])
continue;
for (int i = 1; i; i++) {
int now = calc(res + i * c[u]);
ll k = i * d[u] + len;
if (dis[u][now] > k) {
dis[u][now] = k;
q.push((node){u, now, k});
}
if (now == maxt)
break;
}
for (int i = first[u]; i; i = e[i].next) {
int v = e[i].to;
ll k = len + e[i].w;
if (res < e[i].c)
continue;
if (dis[v][res - e[i].c] > k) {
dis[v][res - e[i].c] = k;
q.push((node){v, res - e[i].c, k});
}
}
}
memset(ans, 0x7f, sizeof(ans));
for (int i = 2; i <= n; i++)
for (int j = 0; j <= maxt; j++)
ans[i] = min(ans[i], dis[i][j]);
}
int main() {
cin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
int u, v, x;
ll y;
cin >> u >> v >> x >> y;
add(u, v, x, y);
add(v, u, x, y);
maxt = max(maxt, x);
}
maxt = maxt * (n + 1);
for (int i = 1; i <= n; i++)
cin >> c[i] >> d[i];
Dijkstra();
for (int i = 2; i <= n; i++)
cout << ans[i] << endl;
return 0;
} | replace | 75 | 76 | 75 | 76 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.