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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VRSORT(v) sort(v.begin(), v.end(), greater<int>());
#define INF 999999999
#define MOD 1000000007
#define M_PI 3.14159265358979323846
#define ALL(X) (X).begin(), (X).end()
#ifdef __LOCAL
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif
#ifdef __LOCAL
#include <filesystem>
namespace fs = std::filesystem;
#endif
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int dx[] = {1, 0, -1, 0, 0};
int dy[] = {0, 1, 0, -1, 0};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);
const int NIL = -1;
static mt19937 _g(time(nullptr));
std::string pad(int num) {
char buffer[4];
std::snprintf(buffer, sizeof(buffer), "%03d", num);
return buffer;
}
inline ll randint(ll a, ll b) {
ll w = (_g() << 31LL) ^ _g();
return a + w % (b - a + 1);
}
inline void fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
};
template <typename T> inline T sign(T x) { return T(x > 0) - T(x < 0); }
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
cout << "[" << p.first << ";" << p.second << "]";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T> inline T fetch() {
T ret;
cin >> ret;
return ret;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
vector<T> ret(sz);
for (auto &elem : ret)
cin >> elem;
return ret;
}
int N, M;
vector<int> A, B;
void input() {
fast_io();
#ifdef __LOCAL
fs::path p = __FILE__;
fs::path input, output;
input = output = p.parent_path();
input += string("/input/") + string(p.stem()) + string(".txt");
output += string("/output/") + string(p.stem()) + string(".txt");
freopen(input.c_str(), "r", stdin);
freopen(output.c_str(), "w", stdout);
#endif
cin >> N >> M;
// A.assign(N,0);
// B.assign(N,0);
REP(i, N) {
int a, b;
cin >> a >> b;
if (a > M)
continue;
A.push_back(a);
B.push_back(b);
}
N = A.size();
}
int solve() {
input();
priority_queue<pii, vector<pii>, greater<pii>> pq_0;
priority_queue<pii> pq_1;
REP(i, N) pq_0.push({A[i], B[i]});
ll ans = 0;
// while(!pq_0.empty()){
// int a,b,tmp_a;
// tmp_a = a = pq_0.top().first;
// while(!pq_0.empty()){
// tie(a,b) = pq_0.top();
// if(tmp_a!=a) break;
// pq_1.push({b,a});
// pq_0.pop();
// tmp_a = a;
// }
// for(int i=M-tmp_a;i>=max(M-a,0);i--){
// if(pq_1.empty()) continue;
// ans += pq_1.top().first;
// pq_1.pop();
// }
// }
for (int i = 1; i <= M; i++) {
int a, b;
while (!pq_0.empty()) {
tie(a, b) = pq_0.top();
if (a > i)
break;
pq_0.pop();
pq_1.push({b, a});
}
ans += pq_1.top().first;
pq_1.pop();
}
cout << ans << endl;
return 0;
}
int main() {
solve();
return 0;
}
| #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VRSORT(v) sort(v.begin(), v.end(), greater<int>());
#define INF 999999999
#define MOD 1000000007
#define M_PI 3.14159265358979323846
#define ALL(X) (X).begin(), (X).end()
#ifdef __LOCAL
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif
#ifdef __LOCAL
#include <filesystem>
namespace fs = std::filesystem;
#endif
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int dx[] = {1, 0, -1, 0, 0};
int dy[] = {0, 1, 0, -1, 0};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);
const int NIL = -1;
static mt19937 _g(time(nullptr));
std::string pad(int num) {
char buffer[4];
std::snprintf(buffer, sizeof(buffer), "%03d", num);
return buffer;
}
inline ll randint(ll a, ll b) {
ll w = (_g() << 31LL) ^ _g();
return a + w % (b - a + 1);
}
inline void fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
};
template <typename T> inline T sign(T x) { return T(x > 0) - T(x < 0); }
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
cout << "[" << p.first << ";" << p.second << "]";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T> inline T fetch() {
T ret;
cin >> ret;
return ret;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
vector<T> ret(sz);
for (auto &elem : ret)
cin >> elem;
return ret;
}
int N, M;
vector<int> A, B;
void input() {
fast_io();
#ifdef __LOCAL
fs::path p = __FILE__;
fs::path input, output;
input = output = p.parent_path();
input += string("/input/") + string(p.stem()) + string(".txt");
output += string("/output/") + string(p.stem()) + string(".txt");
freopen(input.c_str(), "r", stdin);
freopen(output.c_str(), "w", stdout);
#endif
cin >> N >> M;
// A.assign(N,0);
// B.assign(N,0);
REP(i, N) {
int a, b;
cin >> a >> b;
if (a > M)
continue;
A.push_back(a);
B.push_back(b);
}
N = A.size();
}
int solve() {
input();
priority_queue<pii, vector<pii>, greater<pii>> pq_0;
priority_queue<pii> pq_1;
REP(i, N) pq_0.push({A[i], B[i]});
ll ans = 0;
// while(!pq_0.empty()){
// int a,b,tmp_a;
// tmp_a = a = pq_0.top().first;
// while(!pq_0.empty()){
// tie(a,b) = pq_0.top();
// if(tmp_a!=a) break;
// pq_1.push({b,a});
// pq_0.pop();
// tmp_a = a;
// }
// for(int i=M-tmp_a;i>=max(M-a,0);i--){
// if(pq_1.empty()) continue;
// ans += pq_1.top().first;
// pq_1.pop();
// }
// }
for (int i = 1; i <= M; i++) {
int a, b;
while (!pq_0.empty()) {
tie(a, b) = pq_0.top();
if (a > i)
break;
pq_0.pop();
pq_1.push({b, a});
}
if (!pq_1.empty()) {
ans += pq_1.top().first;
pq_1.pop();
}
}
cout << ans << endl;
return 0;
}
int main() {
solve();
return 0;
}
| replace | 138 | 140 | 138 | 142 | -11 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define endl "\n"
#define pb push_back
#define PI 3.1415926535897932384626433832795l
#define F first
#define S second
#define mp make_pair
#define f(i, n) for (int i = 0; i < n; ++i)
#define loop(i, a, b) for (int i = a; i < b; ++i)
#define fastio \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define gcd(a, b) __gcd((a), (b))
#define fill(a, value) memset(a, value, sizeof(a));
#define minn(v) *min_element(v.begin(), v.end());
#define maxx(v) *max_element(v.begin(), v.end());
#define print(x) cout << (x) << endl;
#define sum(v) +x accumulate(v.begin(), v.end(), x);
typedef pair<int, int> pii;
typedef vector<int> vi;
signed main() {
fastio;
cout << fixed << setprecision(12);
int n, m;
cin >> n >> m;
vector<vector<int>> a(n + 1);
f(i, n) {
int x, y;
cin >> x >> y;
a[x].pb(y);
}
multiset<int> s;
int ans = 0;
for (int i = 1; i <= m; i++) {
for (auto p : a[i]) {
s.insert(p);
}
if (s.size()) {
ans += *s.rbegin();
s.erase(--s.end());
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define endl "\n"
#define pb push_back
#define PI 3.1415926535897932384626433832795l
#define F first
#define S second
#define mp make_pair
#define f(i, n) for (int i = 0; i < n; ++i)
#define loop(i, a, b) for (int i = a; i < b; ++i)
#define fastio \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define gcd(a, b) __gcd((a), (b))
#define fill(a, value) memset(a, value, sizeof(a));
#define minn(v) *min_element(v.begin(), v.end());
#define maxx(v) *max_element(v.begin(), v.end());
#define print(x) cout << (x) << endl;
#define sum(v) +x accumulate(v.begin(), v.end(), x);
typedef pair<int, int> pii;
typedef vector<int> vi;
signed main() {
fastio;
cout << fixed << setprecision(12);
int n, m;
cin >> n >> m;
vector<vector<int>> a(100005);
f(i, n) {
int x, y;
cin >> x >> y;
a[x].pb(y);
}
multiset<int> s;
int ans = 0;
for (int i = 1; i <= m; i++) {
for (auto p : a[i]) {
s.insert(p);
}
if (s.size()) {
ans += *s.rbegin();
s.erase(--s.end());
}
}
cout << ans << endl;
return 0;
}
| replace | 32 | 33 | 32 | 33 | -11 | |
p02948 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int maxn = 1e5 + 10;
int read() {
int x = 0, f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-')
f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 3) + (x << 1) + c - 48;
c = getchar();
}
return x * f;
}
struct node {
int x, y;
} a[maxn];
int cmp(node x, node y) {
if (x.x == y.x)
return x.y > y.y;
return x.x < y.x;
}
priority_queue<int> q;
int main() {
int k, m, n;
n = read();
m = read();
for (register int i = 1; i <= n; i++) {
a[i].x = read();
a[i].y = read();
}
sort(a + 1, a + n + 1, cmp);
int now = 1, ans = 0, t = 1;
for (register int i = 1; i <= m; i++) {
while (a[now].x <= i) {
q.push(a[now].y);
now++;
}
if (!q.empty()) {
ans += q.top();
q.pop();
}
}
printf("%d\n", ans);
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int maxn = 1e5 + 10;
int read() {
int x = 0, f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-')
f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 3) + (x << 1) + c - 48;
c = getchar();
}
return x * f;
}
struct node {
int x, y;
} a[maxn];
int cmp(node x, node y) {
if (x.x == y.x)
return x.y > y.y;
return x.x < y.x;
}
priority_queue<int> q;
int main() {
int k, m, n;
n = read();
m = read();
for (register int i = 1; i <= n; i++) {
a[i].x = read();
a[i].y = read();
}
sort(a + 1, a + n + 1, cmp);
int now = 1, ans = 0, t = 1;
for (register int i = 1; i <= m; i++) {
while (a[now].x <= i && now <= n) {
q.push(a[now].y);
now++;
}
if (!q.empty()) {
ans += q.top();
q.pop();
}
}
printf("%d\n", ans);
return 0;
} | replace | 48 | 49 | 48 | 49 | -11 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
#define FI(i, a, b) for (int i = (a); i <= (b); ++i)
#define FD(i, b, a) for (int i = (b); i >= (a); --i)
#define DEBUG(x) cerr << #x << ": " << (x) << endl;
const int N = 1e5 + 5;
int n, m, a[N], b[N];
vector<int> g[N];
int main() {
cin >> n >> m;
FI(i, 1, n) scanf("%d%d", &a[i], &b[i]);
FI(i, 1, n) g[m - a[i] + 1].push_back(i);
FI(i, 1, m)
sort(g[i].begin(), g[i].end(), [&](int x, int y) { return b[x] > b[y]; });
int ans = 0;
multiset<int> se;
for (int i = 1; i <= m; i++) {
for (auto j : g[i]) {
se.insert(b[j]);
ans += b[j];
if (se.size() > i) {
ans -= *se.begin();
se.erase(se.begin());
}
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
#define FI(i, a, b) for (int i = (a); i <= (b); ++i)
#define FD(i, b, a) for (int i = (b); i >= (a); --i)
#define DEBUG(x) cerr << #x << ": " << (x) << endl;
const int N = 1e5 + 5;
int n, m, a[N], b[N];
vector<int> g[N];
int main() {
cin >> n >> m;
FI(i, 1, n) scanf("%d%d", &a[i], &b[i]);
FI(i, 1, n) {
if (a[i] > m)
continue;
g[m - a[i] + 1].push_back(i);
}
FI(i, 1, m)
sort(g[i].begin(), g[i].end(), [&](int x, int y) { return b[x] > b[y]; });
int ans = 0;
multiset<int> se;
for (int i = 1; i <= m; i++) {
for (auto j : g[i]) {
se.insert(b[j]);
ans += b[j];
if (se.size() > i) {
ans -= *se.begin();
se.erase(se.begin());
}
}
}
cout << ans << endl;
}
| replace | 15 | 16 | 15 | 20 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int NN = 1e6 + 10;
typedef long long ll;
int N, M;
vector<int> ve[NN];
priority_queue<int> q;
int main() {
// freopen("test.in","r",stdin);
cin >> N >> M;
for (int i = 1; i <= N; i++) {
int a, b;
scanf("%d%d", &a, &b);
ve[M - a + 1].push_back(
b); // 表示M-a+1天之后不能做此份工作(也就是做了赚不到钱)
}
ll earn = 0;
for (int i = M; i >= 1; i--) {
for (int m : ve[i]) {
q.push(m);
}
if (!q.empty()) {
earn += q.top();
q.pop();
}
}
cout << earn << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int NN = 1e6 + 10;
typedef long long ll;
int N, M;
vector<int> ve[NN];
priority_queue<int> q;
int main() {
// freopen("test.in","r",stdin);
cin >> N >> M;
for (int i = 1; i <= N; i++) {
int a, b;
scanf("%d%d", &a, &b);
if (a > M)
continue;
ve[M - a + 1].push_back(
b); // 表示M-a+1天之后不能做此份工作(也就是做了赚不到钱)
}
ll earn = 0;
for (int i = M; i >= 1; i--) {
for (int m : ve[i]) {
q.push(m);
}
if (!q.empty()) {
earn += q.top();
q.pop();
}
}
cout << earn << endl;
return 0;
} | insert | 14 | 14 | 14 | 16 | 0 | |
p02948 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define mod 1000000007
typedef long long ll;
using namespace std;
int main() {
int N, M;
cin >> N >> M;
;
vector<vector<int>> p(101000);
int a, b;
rep(i, N) {
cin >> a >> b;
p[a].pb(b);
}
rep(i, 101000) { sort(p[i].begin(), p[i].end(), greater<int>()); }
int ans = 0;
rep(i, N) ans += p[i][0];
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define mod 1000000007
typedef long long ll;
using namespace std;
int main() {
int N, M;
cin >> N >> M;
;
vector<vector<int>> p(101000);
int a, b;
rep(i, N) {
cin >> a >> b;
p[a].pb(b);
}
rep(i, 101000) { sort(p[i].begin(), p[i].end(), greater<int>()); }
int ans = 0;
priority_queue<int> q;
for (int i = 1; i <= M; i++) {
for (int j = 0; j < (int)p[i].size(); j++) {
q.push(p[i][j]);
}
if (!q.empty()) {
ans += q.top();
q.pop();
}
}
cout << ans << endl;
return 0;
}
| replace | 28 | 29 | 28 | 39 | -11 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll = long long;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD 1000000007
const ll INF = 1e18;
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;
}
int main(int argc, char const *argv[]) {
int n, m;
cin >> n >> m;
vector<vector<int>> jobs(n);
priority_queue<int> pq;
rep(i, n) {
int a, b;
cin >> a >> b;
if (a > m)
continue;
jobs[m - a].pb(b); // jobs[i]:=i日目までに開始しなきゃいけない仕事
}
ll ans = 0;
for (int i = m - 1; i >= 0; i--) { // 後ろからみる
for (int b : jobs[i]) {
pq.push(b);
}
if (!pq.empty()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll = long long;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD 1000000007
const ll INF = 1e18;
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;
}
int main(int argc, char const *argv[]) {
int n, m;
cin >> n >> m;
vector<vector<int>> jobs(m);
priority_queue<int> pq;
rep(i, n) {
int a, b;
cin >> a >> b;
if (a > m)
continue;
jobs[m - a].pb(b); // jobs[i]:=i日目までに開始しなきゃいけない仕事
}
ll ans = 0;
for (int i = m - 1; i >= 0; i--) { // 後ろからみる
for (int b : jobs[i]) {
pq.push(b);
}
if (!pq.empty()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << endl;
return 0;
}
| replace | 31 | 32 | 31 | 32 | -11 | |
p02948 | C++ | Runtime Error | // For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456
// a.cpp -o a.exe in cmd
#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
using namespace std;
vector<int> v[100001];
int main() {
std::ios::sync_with_stdio(false);
int T;
T = 1;
// cin.ignore(); must be there when using getline(cin, s)
while (T--) {
int n, m, i, j, k;
cin >> n >> m;
ll ans = 0;
pll ar[n + 1];
for (i = 1; i <= n; ++i) {
cin >> ar[i].s >> ar[i].f;
v[m - ar[i].s + 1].pb(ar[i].f);
}
set<pii> st;
k = 1;
for (i = m; i >= 1; --i) {
for (j = 0; j < v[i].size(); ++j, ++k) {
st.insert({-1 * v[i][j], k});
}
if (st.size() > 0) {
auto it = st.begin();
ans = ans + -1 * it->f;
st.erase(it);
}
}
cout << ans << endl;
}
return 0;
}
| // For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456
// a.cpp -o a.exe in cmd
#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
using namespace std;
vector<int> v[100001];
int main() {
std::ios::sync_with_stdio(false);
int T;
T = 1;
// cin.ignore(); must be there when using getline(cin, s)
while (T--) {
int n, m, i, j, k;
cin >> n >> m;
ll ans = 0;
pll ar[n + 1];
for (i = 1; i <= n; ++i) {
cin >> ar[i].s >> ar[i].f;
if (m - ar[i].s + 1 <= 0)
continue;
v[m - ar[i].s + 1].pb(ar[i].f);
}
set<pii> st;
k = 1;
for (i = m; i >= 1; --i) {
for (j = 0; j < v[i].size(); ++j, ++k) {
st.insert({-1 * v[i][j], k});
}
if (st.size() > 0) {
auto it = st.begin();
ans = ans + -1 * it->f;
st.erase(it);
}
}
cout << ans << endl;
}
return 0;
}
| insert | 41 | 41 | 41 | 43 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0, i##__ = (n); i < i##__; ++i)
#define ALL(o) (o).begin(), (o).end()
#define PUT(a) cout << a << "\n"
#define VEC2D(v, y, x, s) vector<vector<int>> v(y, vector<int>(x, s))
// ここまでテンプレ
int main() {
priority_queue<int> pq;
int N, M;
cin >> N >> M;
VEC2D(AB, 10 ^ 5 + 1, 0, 0);
REP(i, N) {
int A, B;
cin >> A >> B;
if (A <= M)
AB.at(A - 1).push_back(B);
}
REP(i, M) sort(ALL(AB[i]));
int ans = 0;
REP(i, M) {
REP(j, AB[i].size()) { pq.push(AB[i][j]); }
if (pq.size() > 0) {
ans += pq.top();
pq.pop();
}
}
PUT(ans);
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0, i##__ = (n); i < i##__; ++i)
#define ALL(o) (o).begin(), (o).end()
#define PUT(a) cout << a << "\n"
#define VEC2D(v, y, x, s) vector<vector<int>> v(y, vector<int>(x, s))
// ここまでテンプレ
int main() {
priority_queue<int> pq;
int N, M;
cin >> N >> M;
VEC2D(AB, pow(10, 5), 0, 0);
REP(i, N) {
int A, B;
cin >> A >> B;
if (A <= M)
AB.at(A - 1).push_back(B);
}
REP(i, M) sort(ALL(AB[i]));
int ans = 0;
REP(i, M) {
REP(j, AB[i].size()) { pq.push(AB[i][j]); }
if (pq.size() > 0) {
ans += pq.top();
pq.pop();
}
}
PUT(ans);
} | replace | 12 | 13 | 12 | 13 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, M;
cin >> N >> M;
// Reward[n] : n日後に報酬が得られるバイトの報酬をVectorにしたやつ
vector<int> Reward[100001];
int a, b;
for (int n = 0; n < N; n++) {
cin >> a >> b;
Reward[a].emplace_back(b);
}
long answer = 0;
priority_queue<int> albeit_reward;
for (int m = 1; m < M + 1; m++) {
for (int r : Reward[m]) {
albeit_reward.push(r);
}
answer += albeit_reward.top();
albeit_reward.pop();
}
cout << answer << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, M;
cin >> N >> M;
// Reward[n] : n日後に報酬が得られるバイトの報酬をVectorにしたやつ
vector<int> Reward[100001];
int a, b;
for (int n = 0; n < N; n++) {
cin >> a >> b;
Reward[a].emplace_back(b);
}
long answer = 0;
priority_queue<int> albeit_reward;
for (int m = 1; m < M + 1; m++) {
for (int r : Reward[m]) {
albeit_reward.push(r);
}
if (albeit_reward.size() != 0) {
answer += albeit_reward.top();
albeit_reward.pop();
}
}
cout << answer << "\n";
return 0;
}
| replace | 24 | 26 | 24 | 28 | -11 | |
p02948 | C++ | Runtime Error | #pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()
#pragma region UnionFind
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int root(int x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma endregion
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> c(10 * 5 + 5);
rep(i, n) {
int a, b;
cin >> a >> b;
c[a].push_back(b);
}
priority_queue<int> q;
ll res = 0;
for (int i = 1; i <= m; ++i) {
for (int x : c[i])
q.push(x);
if (q.empty())
continue;
res += q.top();
q.pop();
}
cout << res << endl;
} | #pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()
#pragma region UnionFind
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int root(int x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma endregion
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> c(1e5 + 5);
rep(i, n) {
int a, b;
cin >> a >> b;
c[a].push_back(b);
}
priority_queue<int> q;
ll res = 0;
for (int i = 1; i <= m; ++i) {
for (int x : c[i])
q.push(x);
if (q.empty())
continue;
res += q.top();
q.pop();
}
cout << res << endl;
} | replace | 88 | 89 | 88 | 89 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
map<int, priority_queue<int>> jobs;
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
jobs[a].push(b);
}
int64_t gurantee = 0;
auto j = jobs.begin(), je = jobs.end();
priority_queue<pair<int, int>> works;
for (int i = 1; i <= M; i++) {
for (; j != je && j->first <= i; j++) {
works.push({j->second.top(), j->first});
j->second.pop();
}
auto p = works.top();
works.pop();
gurantee += p.first;
if (!jobs[p.second].empty()) {
works.push({jobs[p.second].top(), p.second});
jobs[p.second].pop();
}
}
cout << gurantee << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
map<int, priority_queue<int>> jobs;
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
jobs[a].push(b);
}
int64_t gurantee = 0;
auto j = jobs.begin(), je = jobs.end();
priority_queue<pair<int, int>> works;
for (int i = 1; i <= M; i++) {
for (; j != je && j->first <= i; j++) {
works.push({j->second.top(), j->first});
j->second.pop();
}
if (!works.empty()) {
auto p = works.top();
works.pop();
gurantee += p.first;
if (!jobs[p.second].empty()) {
works.push({jobs[p.second].top(), p.second});
jobs[p.second].pop();
}
}
}
cout << gurantee << endl;
} | replace | 24 | 30 | 24 | 32 | -11 | |
p02948 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> v(m);
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
v[m - a].push_back(b);
}
long long ans = 0;
priority_queue<int> q;
for (int i = m - 1; i >= 0; i--) {
for (auto &&j : v[i]) {
q.push(j);
}
if (!q.empty()) {
ans += (long long)q.top();
q.pop();
}
}
cout << ans << endl;
return 0;
} | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> v(m);
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
if (a > m) {
continue;
}
v[m - a].push_back(b);
}
long long ans = 0;
priority_queue<int> q;
for (int i = m - 1; i >= 0; i--) {
for (auto &&j : v[i]) {
q.push(j);
}
if (!q.empty()) {
ans += (long long)q.top();
q.pop();
}
}
cout << ans << endl;
return 0;
} | insert | 13 | 13 | 13 | 17 | 0 | |
p02948 | 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() {
int n, m;
cin >> n >> m;
vector<vector<int>> A(n);
rep(i, n) {
int a, b;
cin >> a >> b;
if (a > m)
continue;
A[m - a].push_back(b);
}
ll ans = 0;
priority_queue<int> pq;
for (int i = m - 1; i >= 0; --i) {
for (int b : A[i]) {
pq.push(b);
}
if (!pq.empty()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> A(m);
rep(i, n) {
int a, b;
cin >> a >> b;
if (a > m)
continue;
A[m - a].push_back(b);
}
ll ans = 0;
priority_queue<int> pq;
for (int i = m - 1; i >= 0; --i) {
for (int b : A[i]) {
pq.push(b);
}
if (!pq.empty()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << endl;
return 0;
} | replace | 8 | 9 | 8 | 9 | -11 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fo(i, w, n) for (int i = (int)w; i < (int)n; i++)
#define qi queue<int>
#define vi vector<int>
#define ll long long
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fi first
#define se second
#define prq priority_queue
#define foreach(it, c) \
for (__typeof((c).begin()) it = c.begin(); it != c.end(); it++)
using namespace std;
inline ll read() {
ll x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-')
f = -f;
for (; isdigit(c); c = getchar())
x = x * 10 + (c - '0');
return x * f;
}
inline void out(int x) {
if (x >= 10)
out(x / 10);
putchar(x % 10 + '0');
}
int n, m, ans = 0;
pii a[101010];
prq<int> pq;
int main() {
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
cin >> n >> m;
fo(i, 0, n) cin >> a[i].fi >> a[i].se;
sort(a, a + n);
int k = 0;
for (int i = m; i >= 0; i--) {
while (a[k].fi <= m - i)
pq.push(a[k++].se);
if (!pq.empty()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define fo(i, w, n) for (int i = (int)w; i < (int)n; i++)
#define qi queue<int>
#define vi vector<int>
#define ll long long
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fi first
#define se second
#define prq priority_queue
#define foreach(it, c) \
for (__typeof((c).begin()) it = c.begin(); it != c.end(); it++)
using namespace std;
inline ll read() {
ll x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-')
f = -f;
for (; isdigit(c); c = getchar())
x = x * 10 + (c - '0');
return x * f;
}
inline void out(int x) {
if (x >= 10)
out(x / 10);
putchar(x % 10 + '0');
}
int n, m, ans = 0;
pii a[101010];
prq<int> pq;
int main() {
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
cin >> n >> m;
fo(i, 0, n) cin >> a[i].fi >> a[i].se;
sort(a, a + n);
int k = 0;
for (int i = m; i >= 0; i--) {
while (a[k].fi <= m - i && k < n)
pq.push(a[k++].se);
if (!pq.empty()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << endl;
return 0;
}
| replace | 44 | 45 | 44 | 45 | -11 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> v(m);
REP(i, n) {
int a, b;
cin >> a >> b;
v[m - a].push_back(b);
}
priority_queue<int> q;
ll ans = 0;
for (int i = m - 1; i >= 0; i--) {
for (int x : v[i]) {
q.push(x);
}
if (!q.empty()) {
ans += q.top();
q.pop();
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> v(m);
REP(i, n) {
int a, b;
cin >> a >> b;
if (a > m)
continue;
v[m - a].push_back(b);
}
priority_queue<int> q;
ll ans = 0;
for (int i = m - 1; i >= 0; i--) {
for (int x : v[i]) {
q.push(x);
}
if (!q.empty()) {
ans += q.top();
q.pop();
}
}
cout << ans << endl;
}
| replace | 14 | 15 | 14 | 16 | 0 | |
p02948 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define MAX_A 100000
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> A[MAX_A];
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
A[a].emplace_back(b);
}
priority_queue<int> que;
int ans = 0;
for (int i = 1; i <= M; i++) {
for (int id : A[i]) {
que.push(id);
}
if (!que.empty()) {
ans += que.top();
que.pop();
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define MAX_A 100001
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> A[MAX_A];
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
A[a].emplace_back(b);
}
priority_queue<int> que;
int ans = 0;
for (int i = 1; i <= M; i++) {
for (int id : A[i]) {
que.push(id);
}
if (!que.empty()) {
ans += que.top();
que.pop();
}
}
cout << ans << endl;
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02948 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define ll long long
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define repm(i, a, b) for (int i = (int)(a); i > (int)(b); --i)
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<vector<int>> jobs(10010);
repp(i, 0, N) {
int a, b;
cin >> a >> b;
jobs[a].push_back(b);
}
priority_queue<int> q;
ll ans = 0;
repp(i, 1, M + 1) {
for (auto x : jobs[i])
q.push(x);
if (!q.empty())
ans += q.top(), q.pop();
}
cout << ans << endl;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define ll long long
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define repm(i, a, b) for (int i = (int)(a); i > (int)(b); --i)
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<vector<int>> jobs(100100);
repp(i, 0, N) {
int a, b;
cin >> a >> b;
jobs[a].push_back(b);
}
priority_queue<int> q;
ll ans = 0;
repp(i, 1, M + 1) {
for (auto x : jobs[i])
q.push(x);
if (!q.empty())
ans += q.top(), q.pop();
}
cout << ans << endl;
}
| replace | 19 | 20 | 19 | 20 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MAX = 5100000;
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
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(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 以下から
const int MAX_N = 100000;
struct Edge {
int to;
int id;
};
vector<vector<Edge>> g;
vector<int> ans;
const int MN = 15;
vector<int> searched(MN, 0);
vector<int> color(MN, -1);
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> days(m, vector<int>());
rep(i, n) {
int a, b;
cin >> a >> b;
a--;
days[a].push_back(b);
}
priority_queue<int> pq;
int sum = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < days[i].size(); j++) {
pq.push(days[i][j]);
}
if (!pq.empty()) {
int val = pq.top();
pq.pop();
sum += val;
}
}
cout << sum << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MAX = 5100000;
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
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(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 以下から
const int MAX_N = 100000;
struct Edge {
int to;
int id;
};
vector<vector<Edge>> g;
vector<int> ans;
const int MN = 15;
vector<int> searched(MN, 0);
vector<int> color(MN, -1);
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> days(100000);
rep(i, n) {
int a, b;
cin >> a >> b;
a--;
days[a].push_back(b);
}
priority_queue<int> pq;
int sum = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < days[i].size(); j++) {
pq.push(days[i][j]);
}
if (!pq.empty()) {
int val = pq.top();
pq.pop();
sum += val;
}
}
cout << sum << endl;
}
| replace | 48 | 49 | 48 | 49 | -11 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int main() {
int N, M;
cin >> N >> M;
int a[N], b[N];
vector<P> ab;
for (int i = 0; i < N; i++) {
cin >> a[i] >> b[i];
ab.push_back(P(a[i], b[i]));
}
sort(ab.begin(), ab.end());
int num = 0;
int ans = 0;
priority_queue<P> que;
for (int A = 1; A <= M; A++) {
while (ab[num].first <= A) {
que.push(P(ab[num].second, ab[num].first));
num++;
}
if (!que.empty()) {
P p = que.top();
ans += p.first;
que.pop();
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int main() {
int N, M;
cin >> N >> M;
int a[N], b[N];
vector<P> ab;
for (int i = 0; i < N; i++) {
cin >> a[i] >> b[i];
ab.push_back(P(a[i], b[i]));
}
sort(ab.begin(), ab.end());
int num = 0;
int ans = 0;
priority_queue<P> que;
for (int A = 1; A <= M; A++) {
while (ab[num].first <= A && num < N) {
que.push(P(ab[num].second, ab[num].first));
num++;
}
if (!que.empty()) {
P p = que.top();
ans += p.first;
que.pop();
}
}
cout << ans << endl;
} | replace | 20 | 21 | 20 | 21 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> ev[10010];
for (int i = 0; i < N; i++) {
int x, y;
cin >> x >> y;
if (M - x >= 0)
ev[M - x].emplace_back(y);
}
long long ret = 0;
priority_queue<int> que;
for (int i = M; i >= 0; i--) {
for (auto &p : ev[i])
que.emplace(p);
if (que.size()) {
ret += que.top();
que.pop();
}
}
cout << ret << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> ev[100100];
for (int i = 0; i < N; i++) {
int x, y;
cin >> x >> y;
if (M - x >= 0)
ev[M - x].emplace_back(y);
}
long long ret = 0;
priority_queue<int> que;
for (int i = M; i >= 0; i--) {
for (auto &p : ev[i])
que.emplace(p);
if (que.size()) {
ret += que.top();
que.pop();
}
}
cout << ret << endl;
}
| replace | 7 | 8 | 7 | 8 | 0 | |
p02948 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<pair<int, int>> AB(N);
for (auto &ab : AB)
cin >> ab.first >> ab.second;
sort(AB.begin(), AB.end(),
[](const pair<int, int> &_a, const pair<int, int> &_b) {
return _a.second > _b.second;
});
int ans = 0;
vector<int> V(M);
for (int i = 0; i < M; i++) {
V[i] = i + 1;
}
set<int> S(V.begin(), V.end());
for (const auto &ab : AB) {
if (S.empty())
break;
auto it = lower_bound(S.begin(), S.end(), ab.first);
if (it == S.end())
continue;
S.erase(*it);
ans += ab.second;
}
printf("%d\n", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<pair<int, int>> AB(N);
for (auto &ab : AB)
cin >> ab.first >> ab.second;
sort(AB.begin(), AB.end());
priority_queue<int> Q;
int ans = 0, j = 0;
for (int i = 1; i <= M; i++) {
while (j < N && AB[j].first == i) {
Q.push(AB[j].second);
j++;
}
if (!Q.empty()) {
ans += Q.top();
Q.pop();
}
}
printf("%d\n", ans);
return 0;
} | replace | 10 | 28 | 10 | 22 | TLE | |
p02948 | C++ | Time Limit Exceeded | #ifdef DEBUG_IS_VALID
#define DEB 1
#define _LIBCPP_DEBUG 0
#else
#define DEB 0
#define NDEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, x, n) for (int i = x; i >= n; i--)
#define rrep(i, n) RREP(i, n, 0)
#define pb push_back
#define fi first
#define se second
#pragma GCC optimize("-O3")
using namespace std;
#define DUMPOUT cout
#define dump(...) \
if (DEB) \
DUMPOUT << " " << #__VA_ARGS__ << " :[" << __LINE__ << ":" \
<< __FUNCTION__ << "]" << endl \
<< " "; \
if (DEB) \
dump_func(__VA_ARGS__)
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
cout << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
os << "{";
for (int i = 0; i < vec.size(); i++)
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &st) {
cout << "{";
for (auto itr = st.begin(); itr != st.end(); itr++)
cout << *itr << (next(itr) != st.end() ? ", " : "");
cout << "}";
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, map<T1, T2> mp) {
cout << "{";
for (auto itr = mp.begin(); itr != mp.end(); itr++)
cout << "(" << (itr->first) << ", " << (itr->second) << ")"
<< (next(itr) != mp.end() ? "," : "");
cout << "}";
return os;
}
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) == 0) {
DUMPOUT << " ";
} else {
DUMPOUT << ", ";
}
dump_func(std::move(tail)...);
}
template <class T> inline bool chmax(T &a, T const &b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T> inline bool chmin(T &a, T const &b) {
if (a <= b)
return false;
a = b;
return true;
}
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<Pl>;
using vvp = vector<vp>;
const int INF = 1 << 29;
const long long LINF = 1LL << 59;
void solve(long long N, long long M, std::vector<long long> A,
std::vector<long long> B) {
vector<Pl> c(N);
rep(i, N) c[i] = {-B[i], A[i]};
sort(ALL(c));
set<ll> st;
rep(i, M) st.insert(-i);
ll ans = 0;
rep(i, N) {
ll a, b;
a = c[i].second;
b = -c[i].first;
auto itr = lower_bound(st.begin(), st.end(), a - M);
if (itr == st.end()) {
continue;
}
ans += b;
st.erase(itr);
}
cout << ans << endl;
}
int main() {
std::cout << std::fixed << std::setprecision(15);
long long N;
scanf("%lld", &N);
long long M;
scanf("%lld", &M);
std::vector<long long> A(N);
std::vector<long long> B(N);
for (int i = 0; i < N; i++) {
scanf("%lld", &A[i]);
scanf("%lld", &B[i]);
}
solve(N, M, std::move(A), std::move(B));
return 0;
}
| #ifdef DEBUG_IS_VALID
#define DEB 1
#define _LIBCPP_DEBUG 0
#else
#define DEB 0
#define NDEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, x, n) for (int i = x; i >= n; i--)
#define rrep(i, n) RREP(i, n, 0)
#define pb push_back
#define fi first
#define se second
#pragma GCC optimize("-O3")
using namespace std;
#define DUMPOUT cout
#define dump(...) \
if (DEB) \
DUMPOUT << " " << #__VA_ARGS__ << " :[" << __LINE__ << ":" \
<< __FUNCTION__ << "]" << endl \
<< " "; \
if (DEB) \
dump_func(__VA_ARGS__)
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
cout << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
os << "{";
for (int i = 0; i < vec.size(); i++)
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &st) {
cout << "{";
for (auto itr = st.begin(); itr != st.end(); itr++)
cout << *itr << (next(itr) != st.end() ? ", " : "");
cout << "}";
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, map<T1, T2> mp) {
cout << "{";
for (auto itr = mp.begin(); itr != mp.end(); itr++)
cout << "(" << (itr->first) << ", " << (itr->second) << ")"
<< (next(itr) != mp.end() ? "," : "");
cout << "}";
return os;
}
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) == 0) {
DUMPOUT << " ";
} else {
DUMPOUT << ", ";
}
dump_func(std::move(tail)...);
}
template <class T> inline bool chmax(T &a, T const &b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T> inline bool chmin(T &a, T const &b) {
if (a <= b)
return false;
a = b;
return true;
}
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<Pl>;
using vvp = vector<vp>;
const int INF = 1 << 29;
const long long LINF = 1LL << 59;
void solve(long long N, long long M, std::vector<long long> A,
std::vector<long long> B) {
vector<Pl> c(N);
rep(i, N) c[i] = {-B[i], A[i]};
sort(ALL(c));
set<ll> st;
rep(i, M) st.insert(-i);
ll ans = 0;
rep(i, N) {
ll a, b;
a = c[i].second;
b = -c[i].first;
auto itr = st.lower_bound(a - M);
if (itr == st.end()) {
continue;
}
ans += b;
st.erase(itr);
}
cout << ans << endl;
}
int main() {
std::cout << std::fixed << std::setprecision(15);
long long N;
scanf("%lld", &N);
long long M;
scanf("%lld", &M);
std::vector<long long> A(N);
std::vector<long long> B(N);
for (int i = 0; i < N; i++) {
scanf("%lld", &A[i]);
scanf("%lld", &B[i]);
}
solve(N, M, std::move(A), std::move(B));
return 0;
}
| replace | 109 | 110 | 109 | 110 | TLE | |
p02948 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const auto MOD = 1000000007;
const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef vector<int> VI;
typedef vector<long long> VLL;
int n, m;
bool jfsort(PII a, PII b) { return a.first > b.first; }
bool jssort(PII a, PII b) { return a.second < b.second; }
// index が条件を満たすかどうか vector<>maeに検索したいvectorを入れる
// keyを超える最小の要素番号を返す
bool isOK(int index, long long key, deque<PII> &mae) {
if (mae[index].second >= key)
return true;
else
return false;
}
// 汎用的な二分探索のテンプレ
int binary_search(long long key, deque<PII> &mae) {
int left = -1; // 「index = 0」が条件を満たすこともあるので、初期値は -1
int right = (int)mae.size(); // 「index =
// a.size()-1」が条件を満たさないこともあるので、初期値は
// a.size()
/* どんな二分探索でもここの書き方を変えずにできる! */
while (right - left > 1) {
int mid = left + (right - left) / 2;
if (isOK(mid, key, mae))
right = mid;
else
left = mid;
}
/* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
*/
return right;
}
int main() {
cin >> n >> m;
long long ans = 0;
vector<PII> job(n);
for (int i = 0; i < n; i++) {
cin >> job[i].first >> job[i].second; // fir日sec報酬
}
sort(job.begin(), job.end(), jfsort);
deque<PII> temp;
int iter = n - 1;
for (int i = 0; i < m; i++) {
while (iter + 1) {
if (job[iter].first == i + 1) {
int a = binary_search(job[iter].second, temp);
temp.insert(temp.begin() + a, job[iter]);
iter--;
} else {
break;
}
}
int siz = temp.size();
if (siz - m + i > 0) {
temp.erase(temp.begin(), temp.begin() + (siz - m + i));
} //
if (temp.size()) {
sort(temp.begin(), temp.end(), jssort);
ans += temp.back().second;
temp.pop_back();
}
}
cout << ans << endl;
} | #include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const auto MOD = 1000000007;
const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef vector<int> VI;
typedef vector<long long> VLL;
int n, m;
bool jfsort(PII a, PII b) { return a.first > b.first; }
bool jssort(PII a, PII b) { return a.second < b.second; }
// index が条件を満たすかどうか vector<>maeに検索したいvectorを入れる
// keyを超える最小の要素番号を返す
bool isOK(int index, long long key, deque<PII> &mae) {
if (mae[index].second >= key)
return true;
else
return false;
}
// 汎用的な二分探索のテンプレ
int binary_search(long long key, deque<PII> &mae) {
int left = -1; // 「index = 0」が条件を満たすこともあるので、初期値は -1
int right = (int)mae.size(); // 「index =
// a.size()-1」が条件を満たさないこともあるので、初期値は
// a.size()
/* どんな二分探索でもここの書き方を変えずにできる! */
while (right - left > 1) {
int mid = left + (right - left) / 2;
if (isOK(mid, key, mae))
right = mid;
else
left = mid;
}
/* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
*/
return right;
}
int main() {
cin >> n >> m;
long long ans = 0;
vector<PII> job(n);
for (int i = 0; i < n; i++) {
cin >> job[i].first >> job[i].second; // fir日sec報酬
}
sort(job.begin(), job.end(), jfsort);
deque<PII> temp;
int iter = n - 1;
for (int i = 0; i < m; i++) {
while (iter + 1) {
if (job[iter].first == i + 1) {
int a = binary_search(job[iter].second, temp);
temp.insert(temp.begin() + a, job[iter]);
iter--;
} else {
break;
}
}
int siz = temp.size();
if (siz - m + i > 0) {
temp.erase(temp.begin(), temp.begin() + (siz - m + i));
} //
if (temp.size()) {
ans += temp.back().second;
temp.pop_back();
}
}
cout << ans << endl;
} | delete | 80 | 81 | 80 | 80 | TLE | |
p02948 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <cassert>
typedef long long int ll;
using namespace std;
#if defined(DEBUG)
#include <sys/fcntl.h>
#include <unistd.h>
#define DLOG(...) cerr << dbgFormat(__VA_ARGS__) << endl
#define DCALL(func, ...) func(__VA_ARGS__)
template <class... Args> string dbgFormat(const char *fmt, Args... args) {
size_t len = snprintf(nullptr, 0, fmt, args...);
char buf[len + 1];
snprintf(buf, len + 1, fmt, args...);
return string(buf);
}
#else // defined(DEBUG)
#define DLOG(...)
#define DCALL(func, ...)
#endif // defined(DEBUG)
int main(int argc, char *argv[]) {
#if defined(DEBUG)
// GDB on Cygwin ignores redirection at run command.
if (argc == 2)
dup2(open(argv[1], 0), 0);
#else
// For performance. We should not use C-style stdio functions
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cout << setprecision(20);
int N, M;
cin >> N >> M;
vector<int> A(N), B(N);
for (int i = 0; i < N; i++)
cin >> A.at(i) >> B.at(i);
vector<int> ord(N);
for (int i = 0; i < N; i++)
ord.at(i) = i;
sort(ord.begin(), ord.end(), [A](int x, int y) { return A.at(x) < A.at(y); });
// priority_queue<int, vector<int>, greater<int>> que;
priority_queue<int> que;
int i = 0;
int ans = 0;
for (int d = 1; d <= M; d++) {
while (i < N && A.at(ord.at(i)) <= d) {
que.push(B.at(ord.at(i)));
i++;
}
if (!que.empty()) {
ans += que.top();
que.pop();
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#include <cassert>
typedef long long int ll;
using namespace std;
#if defined(DEBUG)
#include <sys/fcntl.h>
#include <unistd.h>
#define DLOG(...) cerr << dbgFormat(__VA_ARGS__) << endl
#define DCALL(func, ...) func(__VA_ARGS__)
template <class... Args> string dbgFormat(const char *fmt, Args... args) {
size_t len = snprintf(nullptr, 0, fmt, args...);
char buf[len + 1];
snprintf(buf, len + 1, fmt, args...);
return string(buf);
}
#else // defined(DEBUG)
#define DLOG(...)
#define DCALL(func, ...)
#endif // defined(DEBUG)
int main(int argc, char *argv[]) {
#if defined(DEBUG)
// GDB on Cygwin ignores redirection at run command.
if (argc == 2)
dup2(open(argv[1], 0), 0);
#else
// For performance. We should not use C-style stdio functions
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cout << setprecision(20);
int N, M;
cin >> N >> M;
vector<int> A(N), B(N);
for (int i = 0; i < N; i++)
cin >> A.at(i) >> B.at(i);
vector<int> ord(N);
for (int i = 0; i < N; i++)
ord.at(i) = i;
sort(ord.begin(), ord.end(), [&](int x, int y) { return A.at(x) < A.at(y); });
priority_queue<int> que;
int i = 0;
int ans = 0;
for (int d = 1; d <= M; d++) {
while (i < N && A.at(ord.at(i)) <= d) {
que.push(B.at(ord.at(i)));
i++;
}
if (!que.empty()) {
ans += que.top();
que.pop();
}
}
cout << ans << endl;
return 0;
}
| replace | 46 | 48 | 46 | 47 | TLE | |
p02948 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld", n)
#define pri(n) printf("%d", n)
#define pln() printf("\n")
#define priln(n) printf("%d\n", n)
#define lpriln(n) printf("%lld\n", n)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
using namespace std;
const ll mod = 1e9 + 7;
const int N = 1e5 + 4;
typedef pair<int, int> pii;
int main() {
int n, m;
scn(n);
scn(m);
vector<pii> v;
rep(i, 0, n) {
pii p;
scn(p.S);
scn(p.F);
v.pb(p);
}
set<int> s;
rep(i, 0, m + 1) s.insert(i);
sort(v.rbegin(), v.rend());
ll total = 0;
for (auto it : v) {
if (it.S > m)
continue;
if (s.find(m - it.S) != s.end()) {
s.erase(m - it.S);
total += (ll)it.F;
} else {
auto iter = upper_bound(s.begin(), s.end(), m - it.S);
if (iter == s.begin())
continue;
iter--;
s.erase(*iter);
total += (ll)it.F;
}
}
lpri(total);
} | #include <bits/stdc++.h>
#define ll long long
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld", n)
#define pri(n) printf("%d", n)
#define pln() printf("\n")
#define priln(n) printf("%d\n", n)
#define lpriln(n) printf("%lld\n", n)
#define rep(i, init, n) for (int i = init; i < n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
using namespace std;
const ll mod = 1e9 + 7;
const int N = 1e5 + 4;
typedef pair<int, int> pii;
int main() {
int n, m;
scn(n);
scn(m);
vector<pii> v;
rep(i, 0, n) {
pii p;
scn(p.S);
scn(p.F);
v.pb(p);
}
set<int> s;
rep(i, 0, m + 1) s.insert(i);
sort(v.rbegin(), v.rend());
ll total = 0;
for (auto it : v) {
if (it.S > m)
continue;
if (s.find(m - it.S) != s.end()) {
s.erase(m - it.S);
total += (ll)it.F;
} else {
auto iter = s.upper_bound(m - it.S);
if (iter == s.begin())
continue;
iter--;
s.erase(*iter);
total += (ll)it.F;
}
}
lpri(total);
} | replace | 43 | 44 | 43 | 44 | TLE | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
typedef long long int ll;
int main() {
int N, M;
cin >> N >> M;
vector<int> v[M + 1];
for (int i = 0; i < N; i++) {
int A, B;
cin >> A >> B;
v[A].push_back(B);
}
int ans = 0;
priority_queue<int> pq;
for (int i = 1; i <= M; i++) {
for (auto e : v[i])
pq.push(e);
if (!pq.empty()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
typedef long long int ll;
int main() {
int N, M;
cin >> N >> M;
vector<int> v[1000007];
for (int i = 0; i < N; i++) {
int A, B;
cin >> A >> B;
v[A].push_back(B);
}
int ans = 0;
priority_queue<int> pq;
for (int i = 1; i <= M; i++) {
for (auto e : v[i])
pq.push(e);
if (!pq.empty()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << endl;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(pos, mp) for (ll pos = mp.begin(); pos != mp.end(); pos - ++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll LINF = 1LL << 62;
const int INF = 1e9 + 7;
int main() {
ll n, m;
cin >> n >> m;
// reward[i] : i+1日後にもらえる報酬のベクトル
vector<vector<ll>> reward(m);
rep(i, n) {
ll a, b;
cin >> a >> b;
reward[a - 1].push_back(b);
}
// 未来から逆算,Mまでにもらえる最大報酬のバイトをする
priority_queue<ll> que;
ll ans = 0;
rep(i, m) {
for (ll r_i : reward[i])
que.push(r_i);
if (!que.empty()) {
ans += que.top();
que.pop();
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(pos, mp) for (ll pos = mp.begin(); pos != mp.end(); pos - ++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll LINF = 1LL << 62;
const int INF = 1e9 + 7;
int main() {
ll n, m;
cin >> n >> m;
// reward[i] : i+1日後にもらえる報酬のベクトル
vector<vector<ll>> reward(100010);
rep(i, n) {
ll a, b;
cin >> a >> b;
reward[a - 1].push_back(b);
}
// 未来から逆算,Mまでにもらえる最大報酬のバイトをする
priority_queue<ll> que;
ll ans = 0;
rep(i, m) {
for (ll r_i : reward[i])
que.push(r_i);
if (!que.empty()) {
ans += que.top();
que.pop();
}
}
cout << ans << endl;
} | replace | 19 | 20 | 19 | 20 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MAX = 5100000;
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
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(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 以下から
const int MAX_N = 100000;
struct Edge {
int to;
int id;
};
vector<vector<Edge>> g;
vector<int> ans;
void dfs(int v, int c = -1, int p = -1) {
int k = 1;
rep(i, g[v].size()) {
int u = g[v][i].to, ei = g[v][i].id;
if (u == p)
continue;
if (k == c)
++k;
ans[ei] = k;
++k;
dfs(u, ans[ei], v);
}
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> v(m + 5);
rep(i, n) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
}
int ans = 0;
priority_queue<int> pq;
for (int i = 1; i <= m; i++) {
rep(j, v[i].size()) { pq.push(v[i][j]); }
if (!pq.empty()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MAX = 5100000;
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
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(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 以下から
const int MAX_N = 100000;
struct Edge {
int to;
int id;
};
vector<vector<Edge>> g;
vector<int> ans;
void dfs(int v, int c = -1, int p = -1) {
int k = 1;
rep(i, g[v].size()) {
int u = g[v][i].to, ei = g[v][i].id;
if (u == p)
continue;
if (k == c)
++k;
ans[ei] = k;
++k;
dfs(u, ans[ei], v);
}
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> v(100005);
rep(i, n) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
}
int ans = 0;
priority_queue<int> pq;
for (int i = 1; i <= m; i++) {
rep(j, v[i].size()) { pq.push(v[i][j]); }
if (!pq.empty()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << endl;
}
| replace | 58 | 59 | 58 | 59 | -11 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(a, b, c) for (register int a = b, _n = c; a <= _n; ++a)
#define DREP(a, b, c) for (register int a = b, _n = c; a >= _n; --a)
#define FOR(a, b, c) for (register int a = b, _n = c; a < _n; ++a)
#define DFOR(a, b, c) for (register int a = b, _n = c; a > _n; --a)
#define EREP(a, b) for (register int a = first[b]; a; a = edge[a].nxt)
const int SIZE = 100005;
int n, m;
int ans;
struct Node {
int a, b;
} x[SIZE];
struct cmp {
bool operator()(const Node &x1, const Node &x2) const { return x1.b > x2.b; }
};
int fa[100005];
int Find(int x) {
if (x == fa[x])
return x;
return fa[x] = Find(fa[x]);
}
void _main() {
scanf("%d%d", &n, &m);
REP(i, 1, m) fa[i] = i;
REP(i, 1, n) { scanf("%d%d", &x[i].a, &x[i].b); }
sort(x + 1, x + n + 1, cmp());
REP(i, 1, n) {
int fx = Find(m - x[i].a + 1);
if (fx) {
ans += x[i].b;
fa[fx] = Find(fx - 1);
}
}
printf("%d\n", ans);
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
_main();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(a, b, c) for (register int a = b, _n = c; a <= _n; ++a)
#define DREP(a, b, c) for (register int a = b, _n = c; a >= _n; --a)
#define FOR(a, b, c) for (register int a = b, _n = c; a < _n; ++a)
#define DFOR(a, b, c) for (register int a = b, _n = c; a > _n; --a)
#define EREP(a, b) for (register int a = first[b]; a; a = edge[a].nxt)
const int SIZE = 100005;
int n, m;
int ans;
struct Node {
int a, b;
} x[SIZE];
struct cmp {
bool operator()(const Node &x1, const Node &x2) const { return x1.b > x2.b; }
};
int fa[100005];
int Find(int x) {
if (x == fa[x])
return x;
return fa[x] = Find(fa[x]);
}
void _main() {
scanf("%d%d", &n, &m);
REP(i, 1, m) fa[i] = i;
REP(i, 1, n) { scanf("%d%d", &x[i].a, &x[i].b); }
sort(x + 1, x + n + 1, cmp());
REP(i, 1, n) if (m >= x[i].a) {
int fx = Find(m - x[i].a + 1);
if (fx) {
ans += x[i].b;
fa[fx] = Find(fx - 1);
}
}
printf("%d\n", ans);
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
_main();
return 0;
}
| replace | 37 | 38 | 37 | 38 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> k(m, vector<int>(1, 0));
int ans = 0;
priority_queue<int, vector<int>, less<int>> q;
rep(i, n) {
int a, b;
cin >> a >> b;
k[a - 1].push_back(b);
}
rep(i, m) {
rep(j, k[i].size()) {
int w = k[i][j];
q.push(w);
}
if (q.size() >= 1) {
ans += q.top();
q.pop();
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> k(m, vector<int>(1, 0));
int ans = 0;
priority_queue<int, vector<int>, less<int>> q;
rep(i, n) {
int a, b;
cin >> a >> b;
if (a > m)
continue;
k[a - 1].push_back(b);
}
rep(i, m) {
rep(j, k[i].size()) {
int w = k[i][j];
q.push(w);
}
if (q.size() >= 1) {
ans += q.top();
q.pop();
}
}
cout << ans << endl;
}
| insert | 12 | 12 | 12 | 14 | 0 | |
p02948 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<int> ve[12000];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
int x, y;
for (int i = 0; i < n; i++) {
cin >> x >> y;
if (x > k)
continue;
int tt = k - x + 1;
ve[tt].push_back(y);
}
priority_queue<int> q;
int ans = 0;
for (int i = k; i >= 1; i--) {
for (int j = 0; j < ve[i].size(); j++)
q.push(ve[i][j]);
if (!q.empty()) {
ans += q.top();
q.pop();
}
}
cout << ans << "\n";
return 0;
} | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<int> ve[120000];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
int x, y;
for (int i = 0; i < n; i++) {
cin >> x >> y;
if (x > k)
continue;
int tt = k - x + 1;
ve[tt].push_back(y);
}
priority_queue<int> q;
int ans = 0;
for (int i = k; i >= 1; i--) {
for (int j = 0; j < ve[i].size(); j++)
q.push(ve[i][j]);
if (!q.empty()) {
ans += q.top();
q.pop();
}
}
cout << ans << "\n";
return 0;
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02948 | C++ | Runtime Error | #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, n) for (ll i##_len = (n), i = i##_len - 1; i >= 0; --i)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define printd(val) std::cout << #val " = " << val << "\n";
#include <bits/stdc++.h>
using ll = long long;
using pii = std::pair<int, int>;
using namespace std;
template <class T> T chmin(T a, T b) {
if (a > b) {
a ^= b;
b ^= a;
a ^= b;
return 1;
}
return 0;
}
template <class T> T chmax(T b, T a) {
if (a < b) {
a ^= b;
b ^= a;
a ^= b;
return 1;
}
return 0;
}
int n, m;
vector<vector<ll>> v;
priority_queue<ll> qt;
void input() {
cin >> n >> m;
int a, b;
v.resize(100010);
rep(i, n) {
cin >> a >> b;
v.at(a).push_back(b);
}
}
void src() {
ll sum = 0;
rep(day, m) {
if (!v.at(day + 1).empty()) {
for (auto i : v.at(day + 1)) {
qt.push(i);
}
}
sum += qt.top();
qt.pop();
}
cout << sum;
return;
}
int main(int argc, char *argv[]) {
ios::sync_with_stdio(false);
// ifstream in( argv[1] ); cin.rdbuf(in.rdbuf());
input();
src();
return 0;
} | #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, n) for (ll i##_len = (n), i = i##_len - 1; i >= 0; --i)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define printd(val) std::cout << #val " = " << val << "\n";
#include <bits/stdc++.h>
using ll = long long;
using pii = std::pair<int, int>;
using namespace std;
template <class T> T chmin(T a, T b) {
if (a > b) {
a ^= b;
b ^= a;
a ^= b;
return 1;
}
return 0;
}
template <class T> T chmax(T b, T a) {
if (a < b) {
a ^= b;
b ^= a;
a ^= b;
return 1;
}
return 0;
}
int n, m;
vector<vector<ll>> v;
priority_queue<ll> qt;
void input() {
cin >> n >> m;
int a, b;
v.resize(100010);
rep(i, n) {
cin >> a >> b;
v.at(a).push_back(b);
}
}
void src() {
ll sum = 0;
rep(day, m) {
if (!v.at(day + 1).empty()) {
for (auto i : v.at(day + 1)) {
qt.push(i);
}
}
if (!qt.empty()) {
sum += qt.top();
qt.pop();
}
}
cout << sum;
return;
}
int main(int argc, char *argv[]) {
ios::sync_with_stdio(false);
// ifstream in( argv[1] ); cin.rdbuf(in.rdbuf());
input();
src();
return 0;
} | replace | 48 | 50 | 48 | 52 | -11 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int N, M;
cin >> N >> M;
vector<pair<int, int>> A(N, pair<int, int>(0, 0));
for (int i = 0; i < N; i++) {
cin >> A.at(i).second >> A.at(i).first;
}
sort(A.begin(), A.end());
reverse(A.begin(), A.end());
vector<int> D(M + 1, 0);
int count = 0;
int shortcut = 1;
int C = M;
ll sum = 0;
for (int i = 0; i < N; i++) {
for (int j = max(A.at(i).second, shortcut); j <= C + 1; j++) {
if (j == C + 1) {
C = A.at(i).second;
break;
}
if (D.at(j) == 0) {
D.at(j) = A.at(i).first;
sum += D.at(j);
count++;
if (shortcut >= A.at(i).second) {
shortcut = j;
}
break;
}
}
if (count == M) {
break;
}
}
// ll sum=0;
// for(int i=1;i<=M;i++){
// sum += D.at(i);
// }
cout << sum << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int N, M;
cin >> N >> M;
vector<pair<int, int>> A(N, pair<int, int>(0, 0));
for (int i = 0; i < N; i++) {
cin >> A.at(i).second >> A.at(i).first;
}
sort(A.begin(), A.end());
reverse(A.begin(), A.end());
vector<int> D(M + 1, 0);
int count = 0;
int shortcut = 1;
int C = M;
ll sum = 0;
for (int i = 0; i < N; i++) {
for (int j = max(A.at(i).second, shortcut); j <= C + 1; j++) {
if (j == C + 1) {
C = A.at(i).second - 1;
break;
}
if (D.at(j) == 0) {
D.at(j) = A.at(i).first;
sum += D.at(j);
count++;
if (shortcut >= A.at(i).second) {
shortcut = j;
}
break;
}
}
if (count == M) {
break;
}
}
// ll sum=0;
// for(int i=1;i<=M;i++){
// sum += D.at(i);
// }
cout << sum << endl;
} | replace | 21 | 22 | 21 | 22 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> g[M + 10];
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
}
priority_queue<int> que;
int ans = 0;
for (int i = 1; i <= M; i++) {
for (auto v : g[i]) {
que.push(v);
}
if (!que.empty()) {
int res = que.top();
que.pop();
ans += res;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> g[100100];
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
}
priority_queue<int> que;
int ans = 0;
for (int i = 1; i <= M; i++) {
for (auto v : g[i]) {
que.push(v);
}
if (!que.empty()) {
int res = que.top();
que.pop();
ans += res;
}
}
cout << ans << endl;
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> a;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
if (x <= m) {
a.push_back(make_pair(x, y));
}
}
ll ans = 0;
sort(a.begin(), a.end());
priority_queue<int> pq;
ll x = 0;
for (int i = 1; i <= m; i++) {
while (x < n and a[x].first <= i) {
pq.push(a[x].second);
x++;
}
if (pq.size()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> a;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
if (x <= m) {
a.push_back(make_pair(x, y));
}
}
ll ans = 0;
sort(a.begin(), a.end());
priority_queue<int> pq;
ll x = 0;
for (int i = 1; i <= m; i++) {
while (x < a.size() and a[x].first <= i) {
pq.push(a[x].second);
x++;
}
if (pq.size()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << endl;
return 0;
} | replace | 22 | 23 | 22 | 23 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int n, m;
const int N = 1e5 + 7;
pair<int, int> prs[N];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.in", "rt", stdin);
#endif
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> prs[i].second >> prs[i].first;
}
sort(prs, prs + n, greater<>());
set<int> ss;
for (int i = 0; i < m; ++i) {
ss.insert(i + 1);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
auto it = ss.lower_bound(prs[i].second);
if (it == ss.end())
continue;
ss.erase(it);
ans += prs[i].first;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int n, m;
const int N = 1e5 + 7;
pair<int, int> prs[N];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> prs[i].second >> prs[i].first;
}
sort(prs, prs + n, greater<>());
set<int> ss;
for (int i = 0; i < m; ++i) {
ss.insert(i + 1);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
auto it = ss.lower_bound(prs[i].second);
if (it == ss.end())
continue;
ss.erase(it);
ans += prs[i].first;
}
cout << ans << endl;
} | delete | 9 | 12 | 9 | 9 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;
int main() {
int n, m;
cin >> n >> m;
priority_queue<int> pq;
vector<vi> v(m + 2);
rep(i, n) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
}
int ans = 0;
for (int i = 1; i <= m; i++) {
rep(j, v[i].size()) pq.push(v[i][j]);
if (!pq.empty()) {
int k = pq.top();
ans += k;
pq.pop();
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;
int main() {
int n, m;
cin >> n >> m;
priority_queue<int> pq;
vector<vi> v(110000);
rep(i, n) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
}
int ans = 0;
for (int i = 1; i <= m; i++) {
rep(j, v[i].size()) pq.push(v[i][j]);
if (!pq.empty()) {
int k = pq.top();
ans += k;
pq.pop();
}
}
cout << ans << endl;
return 0;
} | replace | 15 | 16 | 15 | 16 | 0 | |
p02948 | C++ | Time Limit Exceeded | // #include <bits/stdc++.h>
#include <algorithm> // std::sort, std::min, std::next_permutation, std::lower_bound
#include <cmath> // std::ceil
#include <cmath> // std::atan2
#include <iomanip> // std::setprecision
#include <iostream>
#include <limits> // std::numeric_limits<long int>::max()
#include <map>
#include <math.h>
#include <numeric> // std::gcd, std::lcm
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define INF 1999999999
#define MODA 1000000007
#define rep(i, n) for (long long i = 0; i < (n); ++i)
int main() {
ll N, M;
cin >> N >> M;
vector<ll> A(N), B(N);
rep(i, N) cin >> A[i] >> B[i];
vector<pair<ll, ll>> works;
rep(i, N) works.push_back(make_pair(B[i], A[i]));
sort(works.begin(), works.end());
map<ll, bool> days;
rep(i, M + 1) days.insert(make_pair(i, true));
ll reward = 0;
for (int i = N - 1; i >= 0; --i) {
// cerr << works[i].first <<" "<< works[i].second << endl;
ll daylimit = M - works.at(i).second;
while (daylimit >= 0) {
if (days.find(daylimit) != days.end()) {
reward += works.at(i).first;
days.erase(daylimit);
break;
} else {
daylimit--;
}
}
}
cout << reward << endl;
return 0;
}
| // #include <bits/stdc++.h>
#include <algorithm> // std::sort, std::min, std::next_permutation, std::lower_bound
#include <cmath> // std::ceil
#include <cmath> // std::atan2
#include <iomanip> // std::setprecision
#include <iostream>
#include <limits> // std::numeric_limits<long int>::max()
#include <map>
#include <math.h>
#include <numeric> // std::gcd, std::lcm
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define INF 1999999999
#define MODA 1000000007
#define rep(i, n) for (long long i = 0; i < (n); ++i)
int main() {
ll N, M;
cin >> N >> M;
vector<ll> A(N), B(N);
rep(i, N) cin >> A[i] >> B[i];
vector<pair<ll, ll>> works;
rep(i, N) works.push_back(make_pair(B[i], A[i]));
sort(works.begin(), works.end());
map<ll, bool> days;
rep(i, M + 1) days.insert(make_pair(i, true));
ll reward = 0;
for (int i = N - 1; i >= 0; --i) {
// cerr << works[i].first <<" "<< works[i].second << endl;
ll daylimit = M - works.at(i).second;
auto iter = days.upper_bound(daylimit);
if (iter != days.begin()) {
iter--;
reward += works.at(i).first;
days.erase(iter);
}
}
cout << reward << endl;
return 0;
}
| replace | 40 | 48 | 40 | 45 | TLE | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<pair<int, int>> data;
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
data.emplace_back(a, b);
}
i64 ans = 0;
sort(WHOLE(data));
auto itor = data.begin();
priority_queue<int> rewards;
for (int d = 1; d <= M; d++) {
while (itor->first <= d) {
rewards.push(itor->second);
itor++;
}
if (rewards.empty())
continue;
ans += rewards.top();
rewards.pop();
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<pair<int, int>> data;
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
data.emplace_back(a, b);
}
i64 ans = 0;
sort(WHOLE(data));
auto itor = data.begin();
priority_queue<int> rewards;
for (int d = 1; d <= M; d++) {
while (itor != data.end() && itor->first <= d) {
rewards.push(itor->second);
itor++;
}
if (rewards.empty())
continue;
ans += rewards.top();
rewards.pop();
}
cout << ans << endl;
return 0;
} | replace | 19 | 20 | 19 | 20 | 0 | |
p02948 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define LL long long
#define mod 1000000007
int N, M, ans = 0;
pair<int, int> AB[111111];
priority_queue<int> pq;
int main() {
cin >> N >> M;
for (int i = 0; i < N; i++)
cin >> AB[i].first >> AB[i].second;
sort(AB, AB + N);
int ok = 0;
for (int i = 1; i <= M; i++) {
while (AB[ok].first <= i) {
pq.push(AB[ok].second);
ok++;
}
if (pq.size() == 0)
continue;
ans += pq.top();
pq.pop();
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define LL long long
#define mod 1000000007
int N, M, ans = 0;
pair<int, int> AB[111111];
priority_queue<int> pq;
int main() {
cin >> N >> M;
for (int i = 0; i < N; i++)
cin >> AB[i].first >> AB[i].second;
sort(AB, AB + N);
AB[N].first = 999999;
int ok = 0;
for (int i = 1; i <= M; i++) {
while (AB[ok].first <= i) {
pq.push(AB[ok].second);
ok++;
}
if (pq.size() == 0)
continue;
ans += pq.top();
pq.pop();
}
cout << ans << endl;
return 0;
}
| insert | 26 | 26 | 26 | 27 | -11 | |
p02948 | C++ | Time Limit Exceeded | /// mAAria...
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define all(v) v.begin(), v.end()
#define ld long double
#define ff first
#define ss second
#define pii pair<int, int>
#define pi 3.1415926535897932384626433832
#define mod 1000000007
#define inf 1000000000
const long double PI = acos(-1);
int powm(int a, int b) {
int res = 1;
while (b) {
if (b & 1)
res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
void solve() {
int n, m;
cin >> n >> m;
int t, p;
priority_queue<pii> q;
set<int> v;
for (int i = 1; i <= n; i++) {
cin >> t >> p;
q.push({p, t});
}
for (int i = m + 1; i <= m + m; i++)
v.insert(i);
// for(auto i:v)cout << i << endl;
map<int, bool> vis;
int ans = 0;
while (v.size() && !q.empty()) {
auto x = q.top();
q.pop();
int req = x.second + m;
// cout << req << endl;
auto l = lower_bound(v.begin(), v.end(), req);
if (l == v.end())
continue;
// cout << *l << endl;
// cout << x.second << " " << x.first << endl;
ans += x.first;
v.erase(*l);
}
cout << ans << endl;
}
signed main() {
IOS;
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
| /// mAAria...
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define all(v) v.begin(), v.end()
#define ld long double
#define ff first
#define ss second
#define pii pair<int, int>
#define pi 3.1415926535897932384626433832
#define mod 1000000007
#define inf 1000000000
const long double PI = acos(-1);
int powm(int a, int b) {
int res = 1;
while (b) {
if (b & 1)
res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
void solve() {
int n, m;
cin >> n >> m;
int t, p;
priority_queue<pii> q;
set<int> v;
for (int i = 1; i <= n; i++) {
cin >> t >> p;
q.push({p, t});
}
for (int i = m + 1; i <= m + m; i++)
v.insert(i);
// for(auto i:v)cout << i << endl;
map<int, bool> vis;
int ans = 0;
while (v.size() && !q.empty()) {
auto x = q.top();
q.pop();
int req = x.second + m;
// cout << req << endl;
auto l = v.lower_bound(req);
if (l == v.end())
continue;
// cout << *l << endl;
// cout << x.second << " " << x.first << endl;
ans += x.first;
v.erase(*l);
}
cout << ans << endl;
}
signed main() {
IOS;
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
| replace | 57 | 58 | 57 | 58 | TLE | |
p02948 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> a(m);
rep(i, n) {
int p, q;
cin >> p >> q;
a[p - 1].push_back(q);
}
priority_queue<int> que;
int ans = 0;
rep(i, m) {
for (auto p : a[i])
que.push(p);
if (!que.empty()) {
ans += que.top();
que.pop();
}
}
printf("%d\n", ans);
return 0;
} | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> a(m);
rep(i, n) {
int p, q;
cin >> p >> q;
if (p <= m)
a[p - 1].push_back(q);
}
priority_queue<int> que;
int ans = 0;
rep(i, m) {
for (auto p : a[i])
que.push(p);
if (!que.empty()) {
ans += que.top();
que.pop();
}
}
printf("%d\n", ans);
return 0;
} | replace | 13 | 14 | 13 | 15 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef complex<double> Vec;
ll N, M;
vector<vector<ll>> New;
multiset<ll, greater<ll>> Now;
ll R;
int main() {
cin >> N >> M;
New.resize(M);
for (ll i = 0; i < N; ++i) {
ll a, b;
cin >> a >> b;
New[a - 1].push_back(b);
}
for (ll i = 0; i < M; ++i) {
for (ll b : New[i])
Now.insert(b);
if (Now.empty())
continue;
auto it = Now.begin();
R += *it;
Now.erase(it);
}
cout << R << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef complex<double> Vec;
ll N, M;
vector<vector<ll>> New;
multiset<ll, greater<ll>> Now;
ll R;
int main() {
cin >> N >> M;
New.resize(M);
for (ll i = 0; i < N; ++i) {
ll a, b;
cin >> a >> b;
--a;
if (a >= M)
continue;
New[a].push_back(b);
}
for (ll i = 0; i < M; ++i) {
for (ll b : New[i])
Now.insert(b);
if (Now.empty())
continue;
auto it = Now.begin();
R += *it;
Now.erase(it);
}
cout << R << endl;
return 0;
}
| replace | 17 | 18 | 17 | 21 | 0 | |
p02948 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int N = 1e5 + 4;
int n, m;
int tot;
struct Node {
int a, b;
friend bool operator<(const Node &x, const Node &y) { return x.b < y.b; }
} p[N];
bool cmp(Node c, Node d) { return c.a < d.a; }
priority_queue<Node> q;
inline int read() {
int x = 0;
char c = getchar();
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
x = x * 10 + c - '0', c = getchar();
return x;
}
int main() {
n = read(), m = read();
for (register int i = 1; i <= n; ++i) {
int a = read(), b = read();
if (a <= m)
p[++tot].a = a, p[tot].b = b;
}
sort(p + 1, p + tot + 1, cmp);
int cur = 0, sum = 0;
for (register int i = 1; i <= m; ++i) {
while (p[cur + 1].a <= i && cur < tot) {
++cur;
q.push(p[cur]);
}
sum += q.top().b;
q.pop();
}
printf("%d\n", sum);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int N = 1e5 + 4;
int n, m;
int tot;
struct Node {
int a, b;
friend bool operator<(const Node &x, const Node &y) { return x.b < y.b; }
} p[N];
bool cmp(Node c, Node d) { return c.a < d.a; }
priority_queue<Node> q;
inline int read() {
int x = 0;
char c = getchar();
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
x = x * 10 + c - '0', c = getchar();
return x;
}
int main() {
n = read(), m = read();
for (register int i = 1; i <= n; ++i) {
int a = read(), b = read();
if (a <= m)
p[++tot].a = a, p[tot].b = b;
}
sort(p + 1, p + tot + 1, cmp);
int cur = 0, sum = 0;
for (register int i = 1; i <= m; ++i) {
while (p[cur + 1].a <= i && cur < tot) {
++cur;
q.push(p[cur]);
}
if (!q.empty()) {
sum += q.top().b;
q.pop();
}
}
printf("%d\n", sum);
return 0;
} | replace | 38 | 40 | 38 | 42 | -11 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
int main() {
// ここに処理を記述
int N, M;
cin >> N >> M;
vector<pair<int, int>> v;
int A, B;
for (int i = 0; i < N; i++) {
cin >> A >> B;
v.push_back(make_pair(A, B));
}
// 探索はM日後から(M-1日目の仕事から選ぶ) --> A.first <=
// 1となるもののうち最大報酬を選択
// 探索が進めば,A.firstの候補が増えるがそのうち最大を選び続ける
// A.firstに従ってソート
sort(v.begin(), v.end());
// その時点で実行可能な仕事を優先度付きキューに追加
priority_queue<int> task;
int ans = 0;
int p = 0;
REP(i, M) {
while (v[p].first <= i + 1) {
task.push(v[p].second);
p++;
if (p == v.size())
break;
}
// cout << task.top() << "\n";
if (task.size() != 0) {
ans += task.top();
task.pop();
}
}
cout << ans << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
int main() {
// ここに処理を記述
int N, M;
cin >> N >> M;
vector<pair<int, int>> v;
int A, B;
for (int i = 0; i < N; i++) {
cin >> A >> B;
v.push_back(make_pair(A, B));
}
// 探索はM日後から(M-1日目の仕事から選ぶ) --> A.first <=
// 1となるもののうち最大報酬を選択
// 探索が進めば,A.firstの候補が増えるがそのうち最大を選び続ける
// A.firstに従ってソート
sort(v.begin(), v.end());
// その時点で実行可能な仕事を優先度付きキューに追加
priority_queue<int> task;
int ans = 0;
int p = 0;
REP(i, M) {
while (v[p].first <= i + 1) {
task.push(v[p].second);
p++;
if (p >= v.size())
break;
}
// cout << task.top() << "\n";
if (task.size() != 0) {
ans += task.top();
task.pop();
}
}
cout << ans << "\n";
return 0;
} | replace | 36 | 37 | 36 | 37 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define sz size()
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cout << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " , ";
__f(comma + 1, args...);
}
#define traceloop(x, a) \
{ \
cerr << #a << ": "; \
for (ll ix = 0; ix < x; ix++) \
cerr << a[ix] << " "; \
cerr << endl; \
}
int main() {
ios::sync_with_stdio(0);
int n, i, j, m;
cin >> n >> m;
vector<pii> v;
for (i = 0; i < n; ++i) {
int u, v1;
cin >> u >> v1;
v.pb({u, v1});
}
sort(v.begin(), v.end());
priority_queue<int> pq;
i = j = 0;
ll ans = 0;
for (i = 1; i <= m; ++i) {
while (j < n and v[j].first <= i) {
pq.push(v[j].second);
j++;
}
ans += pq.top();
pq.pop();
}
cout << ans << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define sz size()
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cout << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " , ";
__f(comma + 1, args...);
}
#define traceloop(x, a) \
{ \
cerr << #a << ": "; \
for (ll ix = 0; ix < x; ix++) \
cerr << a[ix] << " "; \
cerr << endl; \
}
int main() {
ios::sync_with_stdio(0);
int n, i, j, m;
cin >> n >> m;
vector<pii> v;
for (i = 0; i < n; ++i) {
int u, v1;
cin >> u >> v1;
v.pb({u, v1});
}
sort(v.begin(), v.end());
priority_queue<int> pq;
i = j = 0;
ll ans = 0;
for (i = 1; i <= m; ++i) {
while (j < n and v[j].first <= i) {
pq.push(v[j].second);
j++;
}
if (!pq.empty()) {
ans += pq.top();
pq.pop();
}
}
cout << ans << "\n";
return 0;
} | replace | 50 | 52 | 50 | 54 | -11 | |
p02948 | C++ | Runtime Error | /*
*/
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, a, b) for (int i = a; i < b; i++)
#define MAX_N 10000
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;
const int INF = 1 << 30;
const ll MOD = ll(1e9 + 7);
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> jobs(m, vector<int>());
rep(i, n) {
int a, b;
cin >> a >> b;
jobs[a - 1].push_back(b);
}
priority_queue<int> que;
int ans = 0;
rep(i, m) {
for (auto &e : jobs[i])
que.push(e);
if (!que.empty()) {
ans += que.top();
que.pop();
}
}
cout << ans << endl;
// system("read -n 1 -s -p \"Press any key to continue...\"");
}
| /*
*/
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, a, b) for (int i = a; i < b; i++)
#define MAX_N 10000
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;
const int INF = 1 << 30;
const ll MOD = ll(1e9 + 7);
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> jobs(m, vector<int>());
rep(i, n) {
int a, b;
cin >> a >> b;
a--;
if (a < m)
jobs[a].push_back(b);
}
priority_queue<int> que;
int ans = 0;
rep(i, m) {
for (auto &e : jobs[i])
que.push(e);
if (!que.empty()) {
ans += que.top();
que.pop();
}
}
cout << ans << endl;
// system("read -n 1 -s -p \"Press any key to continue...\"");
}
| replace | 33 | 34 | 33 | 36 | 0 | |
p02948 | C++ | Time Limit Exceeded | // abc137_d.cpp
// Sat Aug 10 20:49:51 2019
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
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;
}
}
bool first_compare_by_b(pair<int, int> a, pair<int, int> b) {
if (a.first != b.first) {
return a.first > b.first;
} else {
return a.second > b.second;
}
}
int main() {
int n, m;
cin >> n >> m;
vector<P> pair;
for (int i = 0; i < n; i++) {
ll a, b;
cin >> a >> b;
pair.push_back(make_pair(a, b));
}
sort(pair.begin(), pair.end(), compare_by_b);
ll day[m + 1];
fill(day, day + m + 1, 0);
ll smallestday = 1;
ll biggestday = m;
int flag = 0;
for (int i = 0; i < n; i++) {
for (int j = max(pair[i].first, smallestday); j <= m; j++) {
if (day[j] == 0) {
day[j] = pair[i].second;
if (j == smallestday) {
int smallflag = 1;
for (int k = smallestday + 1; k <= m; k++) {
if (day[k] == 0) {
smallestday = k;
smallflag = 0;
break;
}
}
if (smallflag) {
flag = 1;
}
}
if (j == biggestday) {
for (int k = biggestday - 1; k >= 1; k--) {
if (day[k] == 0) {
biggestday = k;
break;
}
}
}
break;
}
}
if (flag) {
break;
}
}
ll ans = 0;
for (int i = 1; i <= m; i++) {
ans += day[i];
// cout << day[i] << " ";
}
cout << ans << endl;
} | // abc137_d.cpp
// Sat Aug 10 20:49:51 2019
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
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;
}
}
bool first_compare_by_b(pair<int, int> a, pair<int, int> b) {
if (a.first != b.first) {
return a.first > b.first;
} else {
return a.second > b.second;
}
}
int main() {
int n, m;
cin >> n >> m;
vector<P> pair;
for (int i = 0; i < n; i++) {
ll a, b;
cin >> a >> b;
pair.push_back(make_pair(a, b));
}
sort(pair.begin(), pair.end(), compare_by_b);
ll day[m + 1];
fill(day, day + m + 1, 0);
ll smallestday = 1;
ll biggestday = m;
int flag = 0;
for (int i = 0; i < n; i++) {
if (pair[i].first > biggestday) {
continue;
}
for (int j = max(pair[i].first, smallestday); j <= biggestday; j++) {
if (day[j] == 0) {
day[j] = pair[i].second;
if (j == smallestday) {
int smallflag = 1;
for (int k = smallestday + 1; k <= m; k++) {
if (day[k] == 0) {
smallestday = k;
smallflag = 0;
break;
}
}
if (smallflag) {
flag = 1;
}
}
if (j == biggestday) {
for (int k = biggestday - 1; k >= 1; k--) {
if (day[k] == 0) {
biggestday = k;
break;
}
}
}
break;
}
}
if (flag) {
break;
}
}
ll ans = 0;
for (int i = 1; i <= m; i++) {
ans += day[i];
// cout << day[i] << " ";
}
cout << ans << endl;
} | replace | 53 | 54 | 53 | 57 | TLE | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define REP(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define next asdnext
#define prev asdprev
int main() {
int N, M;
cin >> N >> M;
vector<int> *v = new vector<int>[M + 1];
REP(i, N) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
}
priority_queue<int> pq;
int ans = 0;
REP(i, 1, M + 1) {
for (auto x : v[i])
pq.push(x);
if (pq.empty())
continue;
ans += pq.top();
pq.pop();
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define REP(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define next asdnext
#define prev asdprev
int main() {
int N, M;
cin >> N >> M;
vector<int> v[100001];
REP(i, N) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
}
priority_queue<int> pq;
int ans = 0;
REP(i, 1, M + 1) {
for (auto x : v[i])
pq.push(x);
if (pq.empty())
continue;
ans += pq.top();
pq.pop();
}
cout << ans << endl;
} | replace | 20 | 21 | 20 | 21 | 0 | |
p02948 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main() {
int N, M;
cin >> N >> M;
vector<int> P[100000];
int A, B;
rep(i, N) {
cin >> A >> B;
P[A].emplace_back(B);
}
priority_queue<int> que;
long long res = 0;
for (int i = 1; i <= M; i++) {
for (int p : P[i])
que.push(p);
if (!que.empty()) {
res += que.top();
que.pop();
}
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main() {
int N, M;
cin >> N >> M;
vector<int> P[100001];
int A, B;
rep(i, N) {
cin >> A >> B;
P[A].emplace_back(B);
}
priority_queue<int> que;
long long res = 0;
for (int i = 1; i <= M; i++) {
for (int p : P[i])
que.push(p);
if (!que.empty()) {
res += que.top();
que.pop();
}
}
cout << res << endl;
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02948 | C++ | Runtime Error | #include <algorithm>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
const int mod = 1000000007;
class carr {
int **c;
int level;
public:
carr(int n, int t = 0) {
for (int i = 0; i < 30; i++) {
if (1 << i >= n) {
level = i + 1;
break;
}
}
c = new int *[level];
for (int i = 0; i < level; i++) {
c[i] = new int[1 << (level - 1 - i)];
for (int j = 0; j < 1 << (level - 1 - i); j++) {
if (i == 0) {
c[0][j] = j < n ? t : 0;
} else {
c[i][j] = c[i - 1][j << 1] + c[i - 1][(j << 1) + 1];
}
}
}
}
carr(int n, int *t) {
for (int i = 0; i < 30; i++) {
if (1 << i >= n) {
level = i + 1;
break;
}
}
c = new int *[level];
for (int i = 0; i < level; i++) {
c[i] = new int[1 << (level - 1 - i)];
for (int j = 0; j < 1 << (level - 1 - i); j++) {
if (i == 0) {
c[0][j] = j < n ? t[j] : 0;
} else {
c[i][j] = c[i - 1][j << 1] + c[i - 1][(j << 1) + 1];
}
}
}
}
void cadd(int n, int a = 1) {
for (int i = 0; i < level; i++) {
c[i][n >> i] += a;
}
}
void csub(int n, int a = 1) {
for (int i = 0; i < level; i++) {
c[i][n >> i] -= a;
}
}
int prev(int n) {
if (c[0][n])
return n;
for (int i = 0; i < level; i++) {
if (n & (1 << i) && c[i][(n >> i) - 1]) {
int r = (n >> i) - 1;
for (int j = i - 1; j >= 0; j--) {
r <<= 1;
if (c[j][r | 1])
r |= 1;
}
return r;
}
}
return -1;
}
};
int main() {
int N, M;
scanf("%d %d", &N, &M);
std::pair<int, int> *d = new std::pair<int, int>[N];
for (int i = 0; i < N; i++) {
int A, B;
scanf("%d %d", &A, &B);
d[i].first = -B;
d[i].second = A;
}
std::sort(d, d + N);
carr c(M, 1);
int res = 0;
for (int i = 0; i < N; i++) {
int r = c.prev(M - d[i].second);
if (r != -1) {
res -= d[i].first;
c.csub(r);
}
}
printf("%d", res);
} | #include <algorithm>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
const int mod = 1000000007;
class carr {
int **c;
int level;
public:
carr(int n, int t = 0) {
for (int i = 0; i < 30; i++) {
if (1 << i >= n) {
level = i + 1;
break;
}
}
c = new int *[level];
for (int i = 0; i < level; i++) {
c[i] = new int[1 << (level - 1 - i)];
for (int j = 0; j < 1 << (level - 1 - i); j++) {
if (i == 0) {
c[0][j] = j < n ? t : 0;
} else {
c[i][j] = c[i - 1][j << 1] + c[i - 1][(j << 1) + 1];
}
}
}
}
carr(int n, int *t) {
for (int i = 0; i < 30; i++) {
if (1 << i >= n) {
level = i + 1;
break;
}
}
c = new int *[level];
for (int i = 0; i < level; i++) {
c[i] = new int[1 << (level - 1 - i)];
for (int j = 0; j < 1 << (level - 1 - i); j++) {
if (i == 0) {
c[0][j] = j < n ? t[j] : 0;
} else {
c[i][j] = c[i - 1][j << 1] + c[i - 1][(j << 1) + 1];
}
}
}
}
void cadd(int n, int a = 1) {
for (int i = 0; i < level; i++) {
c[i][n >> i] += a;
}
}
void csub(int n, int a = 1) {
for (int i = 0; i < level; i++) {
c[i][n >> i] -= a;
}
}
int prev(int n) {
if (c[0][n])
return n;
for (int i = 0; i < level; i++) {
if (n & (1 << i) && c[i][(n >> i) - 1]) {
int r = (n >> i) - 1;
for (int j = i - 1; j >= 0; j--) {
r <<= 1;
if (c[j][r | 1])
r |= 1;
}
return r;
}
}
return -1;
}
};
int main() {
int N, M;
scanf("%d %d", &N, &M);
std::pair<int, int> *d = new std::pair<int, int>[N];
for (int i = 0; i < N; i++) {
int A, B;
scanf("%d %d", &A, &B);
d[i].first = -B;
d[i].second = A;
}
std::sort(d, d + N);
carr c(M, 1);
int res = 0;
for (int i = 0; i < N; i++) {
if (d[i].second > M)
continue;
int r = c.prev(M - d[i].second);
if (r != -1) {
res -= d[i].first;
c.csub(r);
}
}
printf("%d", res);
} | insert | 91 | 91 | 91 | 93 | 0 | |
p02948 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#define maxn 100005
using namespace std;
priority_queue<int> q;
vector<int> G[maxn];
int read() {
int cnt = 0;
char ch = getchar();
while (ch < '0' || '9' < ch)
ch = getchar();
while ('0' <= ch && ch <= '9') {
cnt = cnt * 10 + ch - '0';
ch = getchar();
}
return cnt;
}
int main() {
int n = read(), m = read();
for (int i = 1; i <= n; ++i) {
int a = read(), b = read();
G[m - a + 1].push_back(b);
}
int ans = 0;
for (int i = m; i; --i) {
for (int j = 0; j < G[i].size(); ++j)
q.push(G[i][j]);
if (q.size()) {
ans += q.top();
q.pop();
}
}
cout << ans << endl;
}
| #include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#define maxn 100005
using namespace std;
priority_queue<int> q;
vector<int> G[maxn];
int read() {
int cnt = 0;
char ch = getchar();
while (ch < '0' || '9' < ch)
ch = getchar();
while ('0' <= ch && ch <= '9') {
cnt = cnt * 10 + ch - '0';
ch = getchar();
}
return cnt;
}
int main() {
int n = read(), m = read();
for (int i = 1; i <= n; ++i) {
int a = read(), b = read();
if (a <= m)
G[m - a + 1].push_back(b);
}
int ans = 0;
for (int i = m; i; --i) {
for (int j = 0; j < G[i].size(); ++j)
q.push(G[i][j]);
if (q.size()) {
ans += q.top();
q.pop();
}
}
cout << ans << endl;
}
| replace | 23 | 24 | 23 | 25 | 0 | |
p02949 | C++ | Runtime Error | // Optimise
#include <bits/stdc++.h>
using namespace std;
// #define multitest 1
#ifdef Debug
#define db(...) ZZ(#__VA_ARGS__, __VA_ARGS__);
#define pc(...) PC(#__VA_ARGS__, __VA_ARGS__);
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &p) {
out << '[' << p.first << ", " << p.second << ']';
return out;
}
template <typename Arg> void PC(const char *name, Arg &&arg) {
std::cerr << name << " { ";
for (const auto &v : arg)
cerr << v << ' ';
cerr << " }\n";
}
template <typename Arg1> void ZZ(const char *name, Arg1 &&arg1) {
std::cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void ZZ(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
std::cerr.write(names, comma - names) << " = " << arg1;
ZZ(comma, args...);
}
#else
#define db(...)
#define pc(...)
#endif
using ll = long long;
#define f first
#define s second
#define pb push_back
const long long mod = 1000000007;
auto TimeStart = chrono::steady_clock::now();
const int nax = 3e3 + 10;
pair<int, pair<int, int>> Edges[nax];
int n, m, p, u, v, w;
void solve() {
cin >> n >> m >> p;
for (int i = 0; i < m; ++i) {
cin >> u >> v >> w;
Edges[i] = {v, {u, w - p}};
}
vector<vector<ll>> dp(2, vector<ll>(n + 1, -1e18));
bool curr, prev;
prev = true;
curr = false;
dp[prev][1] = 0;
for (int step = 1; step < 5 * n; ++step) {
for (int i = 0; i < m; ++i) {
int parent = Edges[i].s.f;
int cost = Edges[i].s.s;
int child = Edges[i].f;
dp[curr][child] = max(dp[curr][child], cost + dp[prev][parent]);
}
prev = !prev;
curr = !curr;
}
ll here = max(dp[curr][n], dp[prev][n]);
for (int step = 1; step < 5 * n; ++step) {
for (int i = 0; i < m; ++i) {
int parent = Edges[i].s.f;
int cost = Edges[i].s.s;
int child = Edges[i].f;
dp[curr][child] = max(dp[curr][child], cost + dp[prev][parent]);
}
prev = !prev;
curr = !curr;
}
ll here2 = max(dp[curr][n], dp[prev][n]);
db(here, here2);
if (here2 > here)
cout << -1 << '\n';
else
cout << max(here, 0LL) << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
#ifdef multitest
cin >> t;
#endif
while (t--)
solve();
#ifdef WIN32
cerr << "\n\nTime elapsed: "
<< chrono::duration<double>(chrono::steady_clock::now() - TimeStart)
.count()
<< " seconds.\n";
#endif
return 0;
} | // Optimise
#include <bits/stdc++.h>
using namespace std;
// #define multitest 1
#ifdef Debug
#define db(...) ZZ(#__VA_ARGS__, __VA_ARGS__);
#define pc(...) PC(#__VA_ARGS__, __VA_ARGS__);
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &p) {
out << '[' << p.first << ", " << p.second << ']';
return out;
}
template <typename Arg> void PC(const char *name, Arg &&arg) {
std::cerr << name << " { ";
for (const auto &v : arg)
cerr << v << ' ';
cerr << " }\n";
}
template <typename Arg1> void ZZ(const char *name, Arg1 &&arg1) {
std::cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void ZZ(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
std::cerr.write(names, comma - names) << " = " << arg1;
ZZ(comma, args...);
}
#else
#define db(...)
#define pc(...)
#endif
using ll = long long;
#define f first
#define s second
#define pb push_back
const long long mod = 1000000007;
auto TimeStart = chrono::steady_clock::now();
const int nax = 5e3 + 10;
pair<int, pair<int, int>> Edges[nax];
int n, m, p, u, v, w;
void solve() {
cin >> n >> m >> p;
for (int i = 0; i < m; ++i) {
cin >> u >> v >> w;
Edges[i] = {v, {u, w - p}};
}
vector<vector<ll>> dp(2, vector<ll>(n + 1, -1e18));
bool curr, prev;
prev = true;
curr = false;
dp[prev][1] = 0;
for (int step = 1; step < 5 * n; ++step) {
for (int i = 0; i < m; ++i) {
int parent = Edges[i].s.f;
int cost = Edges[i].s.s;
int child = Edges[i].f;
dp[curr][child] = max(dp[curr][child], cost + dp[prev][parent]);
}
prev = !prev;
curr = !curr;
}
ll here = max(dp[curr][n], dp[prev][n]);
for (int step = 1; step < 5 * n; ++step) {
for (int i = 0; i < m; ++i) {
int parent = Edges[i].s.f;
int cost = Edges[i].s.s;
int child = Edges[i].f;
dp[curr][child] = max(dp[curr][child], cost + dp[prev][parent]);
}
prev = !prev;
curr = !curr;
}
ll here2 = max(dp[curr][n], dp[prev][n]);
db(here, here2);
if (here2 > here)
cout << -1 << '\n';
else
cout << max(here, 0LL) << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
#ifdef multitest
cin >> t;
#endif
while (t--)
solve();
#ifdef WIN32
cerr << "\n\nTime elapsed: "
<< chrono::duration<double>(chrono::steady_clock::now() - TimeStart)
.count()
<< " seconds.\n";
#endif
return 0;
} | replace | 40 | 41 | 40 | 41 | 0 | |
p02949 | 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;
const int INF = 1001001001;
vector<int> to[2505];
vector<int> rto[2505];
bool reachableFrom1[2505];
bool reachableToN[2505];
bool ok[2505];
void dfs(int v) {
if (reachableFrom1[v])
return;
reachableFrom1[v] = true;
for (int u : to[v]) {
dfs(u);
}
}
void rdfs(int v) {
if (reachableToN[v])
return;
reachableToN[v] = true;
for (int u : rto[v]) {
rdfs(u);
}
}
int main() {
int n, m, p;
cin >> n >> m >> p;
vector<tuple<int, int, int>> edges;
rep(i, m) {
int a, b, c;
--a;
--b;
c -= p;
c = -c;
edges.emplace_back(a, b, c);
to[a].push_back(b);
rto[b].push_back(a);
}
dfs(0);
rdfs(n - 1);
rep(i, n) ok[i] = reachableFrom1[i] & reachableToN[i];
{ // bellman-ford
vector<int> d(n, INF);
d[0] = 0;
bool upd = true;
int step = 0;
while (upd) {
upd = false;
rep(i, m) {
int a, b, c;
tie(a, b, c) = edges[i];
if (!ok[a])
continue;
if (!ok[b])
continue;
int newD = d[a] + c;
if (newD < d[b]) {
upd = true;
d[b] = newD;
}
}
step++;
if (step > n) {
puts("-1");
return 0;
}
}
int ans = -d[n - 1];
ans = max(ans, 0);
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int INF = 1001001001;
vector<int> to[2505];
vector<int> rto[2505];
bool reachableFrom1[2505];
bool reachableToN[2505];
bool ok[2505];
void dfs(int v) {
if (reachableFrom1[v])
return;
reachableFrom1[v] = true;
for (int u : to[v]) {
dfs(u);
}
}
void rdfs(int v) {
if (reachableToN[v])
return;
reachableToN[v] = true;
for (int u : rto[v]) {
rdfs(u);
}
}
int main() {
int n, m, p;
cin >> n >> m >> p;
vector<tuple<int, int, int>> edges;
rep(i, m) {
int a, b, c;
cin >> a >> b >> c;
--a;
--b;
c -= p;
c = -c;
edges.emplace_back(a, b, c);
to[a].push_back(b);
rto[b].push_back(a);
}
dfs(0);
rdfs(n - 1);
rep(i, n) ok[i] = reachableFrom1[i] & reachableToN[i];
{ // bellman-ford
vector<int> d(n, INF);
d[0] = 0;
bool upd = true;
int step = 0;
while (upd) {
upd = false;
rep(i, m) {
int a, b, c;
tie(a, b, c) = edges[i];
if (!ok[a])
continue;
if (!ok[b])
continue;
int newD = d[a] + c;
if (newD < d[b]) {
upd = true;
d[b] = newD;
}
}
step++;
if (step > n) {
puts("-1");
return 0;
}
}
int ans = -d[n - 1];
ans = max(ans, 0);
cout << ans << endl;
}
return 0;
} | insert | 36 | 36 | 36 | 37 | -11 | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;
template <typename T> std::vector<T> make_v(size_t a) {
return std::vector<T>(a);
}
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
int main() {
int n, m;
i64 p;
scanf("%d%d%lld", &n, &m, &p);
std::vector<int> loop, a(m), b(m);
std::vector<i64> c(m);
std::vector<std::vector<std::pair<int, i64>>> g(n), rg(n);
for (int i = 0; i < m; i++) {
scanf("%d%d%lld", &a[i], &b[i], &c[i]);
if (a[i] == b[i]) {
a[i]--;
b[i]--;
loop.push_back(i);
continue;
}
g[a[i] - 1].push_back({b[i] - 1, c[i] - p});
rg[b[i] - 1].push_back({a[i] - 1, c[i] - p});
a[i]--;
b[i]--;
}
std::vector<bool> from0(n, false), from1(n, false);
from0[0] = from1[n - 1] = true;
std::queue<int> qu;
qu.push(n - 1);
while (!qu.empty()) {
auto p = qu.front();
qu.pop();
for (auto e : rg[p]) {
if (from1[e.first])
continue;
from1[e.first] = true;
qu.push(e.first);
}
}
qu.push(0);
while (!qu.empty()) {
auto p = qu.front();
qu.pop();
for (auto e : g[p]) {
if (from0[e.first])
continue;
from0[e.first] = true;
qu.push(e.first);
}
}
for (auto i : loop) {
if (c[i] - p <= 0)
continue;
if (c[i] - p > 0 and from0[a[i]] and from1[a[i]]) {
printf("-1\n");
return 0;
}
}
std::vector<std::vector<std::pair<int, i64>>> G(n);
for (int i = 0; i < n; i++) {
if (a[i] == b[i])
continue;
if (from0[a[i]] and from1[b[i]]) {
G[a[i]].push_back({b[i], p - c[i]});
}
}
bool isFinish = true;
const i64 INF = 1LL << 60;
std::vector<i64> dist(n, INF);
dist[0] = 0;
for (int i = 0; i < n; i++) {
for (int v = 0; v < n; v++) {
for (auto e : G[v]) {
if (dist[v] != INF and dist[e.first] > dist[v] + e.second) {
dist[e.first] = dist[v] + e.second;
if (i == n - 1)
isFinish = false;
}
}
}
}
if (!isFinish) {
printf("-1\n");
return 0;
}
printf("%lld\n", std::max(0LL, -dist[n - 1]));
return 0;
}
| #include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;
template <typename T> std::vector<T> make_v(size_t a) {
return std::vector<T>(a);
}
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
int main() {
int n, m;
i64 p;
scanf("%d%d%lld", &n, &m, &p);
std::vector<int> loop, a(m), b(m);
std::vector<i64> c(m);
std::vector<std::vector<std::pair<int, i64>>> g(n), rg(n);
for (int i = 0; i < m; i++) {
scanf("%d%d%lld", &a[i], &b[i], &c[i]);
if (a[i] == b[i]) {
a[i]--;
b[i]--;
loop.push_back(i);
continue;
}
g[a[i] - 1].push_back({b[i] - 1, c[i] - p});
rg[b[i] - 1].push_back({a[i] - 1, c[i] - p});
a[i]--;
b[i]--;
}
std::vector<bool> from0(n, false), from1(n, false);
from0[0] = from1[n - 1] = true;
std::queue<int> qu;
qu.push(n - 1);
while (!qu.empty()) {
auto p = qu.front();
qu.pop();
for (auto e : rg[p]) {
if (from1[e.first])
continue;
from1[e.first] = true;
qu.push(e.first);
}
}
qu.push(0);
while (!qu.empty()) {
auto p = qu.front();
qu.pop();
for (auto e : g[p]) {
if (from0[e.first])
continue;
from0[e.first] = true;
qu.push(e.first);
}
}
for (auto i : loop) {
if (c[i] - p <= 0)
continue;
if (c[i] - p > 0 and from0[a[i]] and from1[a[i]]) {
printf("-1\n");
return 0;
}
}
std::vector<std::vector<std::pair<int, i64>>> G(n);
for (int i = 0; i < m; i++) {
if (a[i] == b[i])
continue;
if (from0[a[i]] and from1[b[i]]) {
G[a[i]].push_back({b[i], p - c[i]});
}
}
bool isFinish = true;
const i64 INF = 1LL << 60;
std::vector<i64> dist(n, INF);
dist[0] = 0;
for (int i = 0; i < n; i++) {
for (int v = 0; v < n; v++) {
for (auto e : G[v]) {
if (dist[v] != INF and dist[e.first] > dist[v] + e.second) {
dist[e.first] = dist[v] + e.second;
if (i == n - 1)
isFinish = false;
}
}
}
}
if (!isFinish) {
printf("-1\n");
return 0;
}
printf("%lld\n", std::max(0LL, -dist[n - 1]));
return 0;
}
| replace | 75 | 76 | 75 | 76 | 0 | |
p02949 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
#define Priq priority_queue<int, vi, greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll << 60)
signed main() {
int N, M, P;
cin >> N >> M >> P;
vector<vector<pii>> A(N);
vii B(N);
rep(i, 0, M) {
int X, Y, Z;
cin >> X >> Y >> Z;
A[X - 1].pb(mp(Y - 1, Z - P));
B[Y - 1].pb(X - 1);
}
queue<int> q;
q.push(N - 1);
vector<bool> F(N);
F[N - 1] = 1;
while (q.size() > 0) {
int X = q.front();
q.pop();
rep(i, 0, B[X].size()) {
int Y = B[X][i];
if (!F[Y]) {
F[Y] = 1;
q.push(Y);
}
}
}
priority_queue<pii> Q;
Q.push(mp(0, 0));
vi C(N), G(N, -INF);
G[0] = 0;
while (Q.size() > 0) {
int X = Q.top().second, Y = Q.top().first;
Q.pop();
if (G[X] > Y)
continue;
if (C[X] > N + M) {
cout << -1 << endl;
return 0;
}
rep(i, 0, A[X].size()) {
int P = A[X][i].first, R = Y + A[X][i].second;
if (F[P] && G[P] < R) {
G[P] = R;
Q.push(mp(R, P));
C[P]++;
}
}
}
cout << max(0ll, G[N - 1]) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
#define Priq priority_queue<int, vi, greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll << 60)
signed main() {
int N, M, P;
cin >> N >> M >> P;
vector<vector<pii>> A(N);
vii B(N);
rep(i, 0, M) {
int X, Y, Z;
cin >> X >> Y >> Z;
A[X - 1].pb(mp(Y - 1, Z - P));
B[Y - 1].pb(X - 1);
}
queue<int> q;
q.push(N - 1);
vector<bool> F(N);
F[N - 1] = 1;
while (q.size() > 0) {
int X = q.front();
q.pop();
rep(i, 0, B[X].size()) {
int Y = B[X][i];
if (!F[Y]) {
F[Y] = 1;
q.push(Y);
}
}
}
priority_queue<pii> Q;
Q.push(mp(0, 0));
vi C(N), G(N, -INF);
G[0] = 0;
while (Q.size() > 0) {
int X = Q.top().second, Y = Q.top().first;
Q.pop();
if (G[X] > Y)
continue;
if (C[X] > M) {
cout << -1 << endl;
return 0;
}
rep(i, 0, A[X].size()) {
int P = A[X][i].first, R = Y + A[X][i].second;
if (F[P] && G[P] < R) {
G[P] = R;
Q.push(mp(R, P));
C[P]++;
}
}
}
cout << max(0ll, G[N - 1]) << endl;
}
| replace | 53 | 54 | 53 | 54 | TLE | |
p02949 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
int n, m, p;
cin >> n >> m >> p;
vector<int> a(m), b(m), c(m);
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> c[i];
a[i]--;
b[i]--;
c[i] = p - c[i];
}
vector<bool> reach_fwd(n, false);
reach_fwd[0] = true;
queue<int> que_fwd;
que_fwd.push(0);
while (!que_fwd.empty()) {
int fwd = que_fwd.front();
que_fwd.pop();
for (int i = 0; i < m; i++) {
if (a[i] == fwd && !reach_fwd[b[i]]) {
reach_fwd[b[i]] = true;
que_fwd.push(b[i]);
}
}
}
vector<bool> reach_rev(n, false);
reach_rev[n - 1] = true;
queue<int> que_rev;
que_rev.push(n - 1);
while (!que_rev.empty()) {
int rev = que_rev.front();
que_rev.pop();
for (int i = 0; i < m; i++) {
if (b[i] == rev && !reach_rev[a[i]]) {
reach_rev[a[i]] = true;
que_rev.push(a[i]);
}
}
}
vector<long long> dist(n, 1LL << 50);
dist[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (reach_fwd[a[i]] && reach_rev[a[i]] && reach_fwd[b[i]] &&
reach_rev[b[i]] && dist[b[j]] > dist[a[j]] + c[j]) {
dist[b[j]] = dist[a[j]] + c[j];
}
}
}
bool flag = true;
for (int i = 0; i < m; i++) {
if (reach_fwd[a[i]] && reach_rev[a[i]] && reach_fwd[b[i]] &&
reach_rev[b[i]] && dist[b[i]] > dist[a[i]] + c[i]) {
flag = false;
}
}
if (flag) {
cout << (dist[n - 1] < 0 ? -dist[n - 1] : 0) << endl;
} else {
cout << -1 << endl;
}
}
| #include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
int n, m, p;
cin >> n >> m >> p;
vector<int> a(m), b(m), c(m);
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> c[i];
a[i]--;
b[i]--;
c[i] = p - c[i];
}
vector<bool> reach_fwd(n, false);
reach_fwd[0] = true;
queue<int> que_fwd;
que_fwd.push(0);
while (!que_fwd.empty()) {
int fwd = que_fwd.front();
que_fwd.pop();
for (int i = 0; i < m; i++) {
if (a[i] == fwd && !reach_fwd[b[i]]) {
reach_fwd[b[i]] = true;
que_fwd.push(b[i]);
}
}
}
vector<bool> reach_rev(n, false);
reach_rev[n - 1] = true;
queue<int> que_rev;
que_rev.push(n - 1);
while (!que_rev.empty()) {
int rev = que_rev.front();
que_rev.pop();
for (int i = 0; i < m; i++) {
if (b[i] == rev && !reach_rev[a[i]]) {
reach_rev[a[i]] = true;
que_rev.push(a[i]);
}
}
}
vector<long long> dist(n, 1LL << 50);
dist[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (reach_fwd[a[j]] && reach_rev[a[j]] && reach_fwd[b[j]] &&
reach_rev[b[j]] && dist[b[j]] > dist[a[j]] + c[j]) {
dist[b[j]] = dist[a[j]] + c[j];
}
}
}
bool flag = true;
for (int i = 0; i < m; i++) {
if (reach_fwd[a[i]] && reach_rev[a[i]] && reach_fwd[b[i]] &&
reach_rev[b[i]] && dist[b[i]] > dist[a[i]] + c[i]) {
flag = false;
}
}
if (flag) {
cout << (dist[n - 1] < 0 ? -dist[n - 1] : 0) << endl;
} else {
cout << -1 << endl;
}
}
| replace | 55 | 57 | 55 | 57 | 0 | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)
const ll MOD = 1000000007;
const ll INF = (ll)1e15;
struct Edge {
ll to;
ll from;
ll cost;
};
Edge E[5005];
ll C[2005];
bool U[2005];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N, M, P;
cin >> N >> M >> P;
REP(i, M) {
ll a, b, c;
cin >> a >> b >> c;
E[i].to = b - 1;
E[i].from = a - 1;
E[i].cost = -(c - P);
}
REP(i, N) { C[i] = INF; }
C[0] = 0;
memset(U, 0, sizeof(U));
queue<ll> q;
q.push(N - 1);
U[N - 1] = true;
while (!q.empty()) {
ll t = q.front();
q.pop();
REP(i, M) {
if (E[i].to != t)
continue;
ll n = E[i].from;
if (U[n])
continue;
U[n] = true;
q.push(n);
}
}
REP(_, N + 5) {
bool update = false;
REP(i, M) {
const auto &e = E[i];
if (C[e.from] == INF)
continue;
ll cost = C[e.from] + e.cost;
if (C[e.to] > cost) {
update = true;
C[e.to] = cost;
}
}
if (!update)
break;
}
REP(i, M) {
const auto &e = E[i];
if (!U[e.to])
continue;
if (C[e.from] == INF)
continue;
ll cost = C[e.from] + e.cost;
if (C[e.to] > cost) {
cout << -1 << endl;
return 0;
}
}
cout << std::max(0LL, -C[N - 1]) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)
const ll MOD = 1000000007;
const ll INF = (ll)1e15;
struct Edge {
ll to;
ll from;
ll cost;
};
Edge E[5005];
ll C[2505];
bool U[2505];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N, M, P;
cin >> N >> M >> P;
REP(i, M) {
ll a, b, c;
cin >> a >> b >> c;
E[i].to = b - 1;
E[i].from = a - 1;
E[i].cost = -(c - P);
}
REP(i, N) { C[i] = INF; }
C[0] = 0;
memset(U, 0, sizeof(U));
queue<ll> q;
q.push(N - 1);
U[N - 1] = true;
while (!q.empty()) {
ll t = q.front();
q.pop();
REP(i, M) {
if (E[i].to != t)
continue;
ll n = E[i].from;
if (U[n])
continue;
U[n] = true;
q.push(n);
}
}
REP(_, N + 5) {
bool update = false;
REP(i, M) {
const auto &e = E[i];
if (C[e.from] == INF)
continue;
ll cost = C[e.from] + e.cost;
if (C[e.to] > cost) {
update = true;
C[e.to] = cost;
}
}
if (!update)
break;
}
REP(i, M) {
const auto &e = E[i];
if (!U[e.to])
continue;
if (C[e.from] == INF)
continue;
ll cost = C[e.from] + e.cost;
if (C[e.to] > cost) {
cout << -1 << endl;
return 0;
}
}
cout << std::max(0LL, -C[N - 1]) << endl;
return 0;
} | replace | 21 | 23 | 21 | 23 | 0 | |
p02949 | C++ | Runtime Error |
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 100000000000000
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(a, n) \
rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define EB emplace_back
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WG = vector<Edges<T>>;
using UG = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;
int n, m;
Edges<ll> es;
bool ans = false;
template <typename T> vector<T> bellman_ford(Edges<T> &edges, int V, int s) {
const auto lim = numeric_limits<T>::max();
vector<T> dist(V, lim);
dist[s] = 0;
rep(i, V - 1) {
for (auto &e : edges) {
if (dist[e.src] == lim)
continue;
dist[e.to] = min(dist[e.to], dist[e.src] + e.cost);
}
}
bool neg[2000] = {};
rep(i, V) {
for (auto &e : edges) {
if (dist[e.src] == lim)
continue;
if (dist[e.to] > dist[e.src] + e.cost) {
dist[e.to] = dist[e.src] + e.cost;
neg[e.to] = true;
}
if (neg[e.src])
neg[e.to] = true;
}
}
if (neg[V - 1])
return vector<T>();
return dist;
}
int main() {
int p;
cin >> n >> m >> p;
rep(i, m) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
c -= p;
es.PB(edge<ll>(a, b, -c));
}
vector<ll> s = bellman_ford(es, n, 0);
if (s.empty())
cout << -1 << endl;
else
cout << max(0LL, -s[n - 1]) << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 100000000000000
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(a, n) \
rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define EB emplace_back
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WG = vector<Edges<T>>;
using UG = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;
int n, m;
Edges<ll> es;
bool ans = false;
template <typename T> vector<T> bellman_ford(Edges<T> &edges, int V, int s) {
const auto lim = numeric_limits<T>::max();
vector<T> dist(V, lim);
dist[s] = 0;
rep(i, V - 1) {
for (auto &e : edges) {
if (dist[e.src] == lim)
continue;
dist[e.to] = min(dist[e.to], dist[e.src] + e.cost);
}
}
bool neg[20000] = {};
rep(i, V) {
for (auto &e : edges) {
if (dist[e.src] == lim)
continue;
if (dist[e.to] > dist[e.src] + e.cost) {
dist[e.to] = dist[e.src] + e.cost;
neg[e.to] = true;
}
if (neg[e.src])
neg[e.to] = true;
}
}
if (neg[V - 1])
return vector<T>();
return dist;
}
int main() {
int p;
cin >> n >> m >> p;
rep(i, m) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
c -= p;
es.PB(edge<ll>(a, b, -c));
}
vector<ll> s = bellman_ford(es, n, 0);
if (s.empty())
cout << -1 << endl;
else
cout << max(0LL, -s[n - 1]) << endl;
} | replace | 62 | 63 | 62 | 63 | 0 | |
p02949 | C++ | Runtime Error | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = 1e+18 + 1;
int INF = 1e+9 + 1;
#define MV 1000
struct edge {
int to, cost;
};
vector<edge> G[MV];
int V, E;
ll d[MV + 1];
bool bellmanford(int s) {
rep(i, V) { d[i] = INFl; }
d[s] = 0;
int c = 0;
// bool upd = false;
bool goalupd = false; // 出発点から目的地までの経路での負の閉路の存在判定
while (true) {
// upd = false;
goalupd = false;
for (int i = 0; i < V; i++) {
for (int j = 0; j < G[i].size(); j++) {
edge e = G[i][j];
if (d[i] != INFl && d[e.to] > d[i] + e.cost) {
// printf("c:%d i:%d to:%d oldcost:%lld
// newcost:%lld\n",c,i,e.to,d[e.to],d[i] + e.cost);
d[e.to] = d[i] + e.cost;
// upd = true;
if (e.to == V - 1)
goalupd = true;
}
}
}
if (c >= V - 1 && goalupd)
return true;
if (c == 2 * V - 1)
return false;
c++;
}
}
int main() {
int P, A[5001], B[5001], C[5001];
scan(V >> E >> P);
rep(i, E) {
scan(A[i] >> B[i] >> C[i]);
edge e;
e.to = B[i] - 1;
e.cost = -C[i] + P;
G[A[i] - 1].push_back(e);
}
bool flag = bellmanford(0);
if (flag)
prin(-1);
else {
prin(max(0LL, -d[V - 1]));
}
return 0;
}
| #include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = 1e+18 + 1;
int INF = 1e+9 + 1;
#define MV 5000
struct edge {
int to, cost;
};
vector<edge> G[MV];
int V, E;
ll d[MV + 1];
bool bellmanford(int s) {
rep(i, V) { d[i] = INFl; }
d[s] = 0;
int c = 0;
// bool upd = false;
bool goalupd = false; // 出発点から目的地までの経路での負の閉路の存在判定
while (true) {
// upd = false;
goalupd = false;
for (int i = 0; i < V; i++) {
for (int j = 0; j < G[i].size(); j++) {
edge e = G[i][j];
if (d[i] != INFl && d[e.to] > d[i] + e.cost) {
// printf("c:%d i:%d to:%d oldcost:%lld
// newcost:%lld\n",c,i,e.to,d[e.to],d[i] + e.cost);
d[e.to] = d[i] + e.cost;
// upd = true;
if (e.to == V - 1)
goalupd = true;
}
}
}
if (c >= V - 1 && goalupd)
return true;
if (c == 2 * V - 1)
return false;
c++;
}
}
int main() {
int P, A[5001], B[5001], C[5001];
scan(V >> E >> P);
rep(i, E) {
scan(A[i] >> B[i] >> C[i]);
edge e;
e.to = B[i] - 1;
e.cost = -C[i] + P;
G[A[i] - 1].push_back(e);
}
bool flag = bellmanford(0);
if (flag)
prin(-1);
else {
prin(max(0LL, -d[V - 1]));
}
return 0;
}
| replace | 30 | 31 | 30 | 31 | 0 | |
p02949 | C++ | Time Limit Exceeded | #include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ll long long
using namespace std;
vector<int> to[2505];
vector<int> rto[2505];
bool from1[2505];
bool toN[2505];
void dfs(int v) {
if (from1[v])
return;
from1[v] = true;
for (int u : to[v]) {
dfs(u);
}
}
void rdfs(int v) {
if (toN[v])
return;
toN[v] = true;
for (int u : rto[v]) {
rdfs(u);
}
}
bool ok[2525];
int main() {
int n, m, p;
cin >> n >> m >> p;
vector<tuple<int, int, int>> edges;
rep(i, m) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
c -= p;
c = -c;
edges.emplace_back(a, b, c);
to[a].push_back(b);
rto[b].push_back(a);
}
dfs(0);
rdfs(n - 1);
rep(i, n) ok[i] = from1[i] & toN[i];
// bellman_ford
vector<int> d(n, 1001001001);
d[0] = 0;
bool upd = true;
int step = 0;
while (upd) {
upd = false;
rep(i, m) {
int a, b, c;
tie(a, b, c) = edges[i];
if (!ok[a])
continue;
if (!ok[b])
continue;
int newD = d[a] + c;
if (newD < d[b]) {
upd = true;
d[b] = newD;
}
}
step++;
}
if (step > n) {
cout << -1 << endl;
return 0;
}
int ans = -d[n - 1];
ans = max(0, ans);
cout << ans << endl;
return 0;
} | #include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ll long long
using namespace std;
vector<int> to[2505];
vector<int> rto[2505];
bool from1[2505];
bool toN[2505];
void dfs(int v) {
if (from1[v])
return;
from1[v] = true;
for (int u : to[v]) {
dfs(u);
}
}
void rdfs(int v) {
if (toN[v])
return;
toN[v] = true;
for (int u : rto[v]) {
rdfs(u);
}
}
bool ok[2525];
int main() {
int n, m, p;
cin >> n >> m >> p;
vector<tuple<int, int, int>> edges;
rep(i, m) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
c -= p;
c = -c;
edges.emplace_back(a, b, c);
to[a].push_back(b);
rto[b].push_back(a);
}
dfs(0);
rdfs(n - 1);
rep(i, n) ok[i] = from1[i] & toN[i];
// bellman_ford
vector<int> d(n, 1001001001);
d[0] = 0;
bool upd = true;
int step = 0;
while (upd) {
upd = false;
rep(i, m) {
int a, b, c;
tie(a, b, c) = edges[i];
if (!ok[a])
continue;
if (!ok[b])
continue;
int newD = d[a] + c;
if (newD < d[b]) {
upd = true;
d[b] = newD;
}
}
step++;
if (step > n) {
cout << -1 << endl;
return 0;
}
}
int ans = -d[n - 1];
ans = max(0, ans);
cout << ans << endl;
return 0;
} | replace | 71 | 75 | 71 | 75 | TLE | |
p02949 | C++ | Runtime Error | // #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma warning(disable : 4786)
#pragma warning(disable : 4996)
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
template <class c>
typename enable_if<sizeof dud<c>(0) != 1, debug &>::type operator<<(c i) {
cerr << boolalpha << i;
return *this;
}
template <class c, int = 0>
typename enable_if<sizeof dud<c>(0) == 1, debug &>::type operator<<(c i) {
return *this << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
};
#define watch(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ((X) > 0 ? (X) : (-(X)))
#define S(X) ((X) * (X))
#define SZ(V) (int)V.size()
#define FORN(i, n) for (int i = 0; i < n; i++)
#define FORAB(i, a, b) for (int i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C) ((B) <= (A) && (A) <= (C))
#define AIN(A, B, C) assert(IN(A, B, C))
// typedef int LL;
typedef long long int LL;
// typedef __int128 LLL;
typedef long long LLL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VP;
typedef vector<double> VD;
typedef vector<vector<int>> VVI;
typedef long double ld;
const LL MOD = 998244353;
int vis[2502][2502];
VP V[2502];
int n, m;
vector<tuple<int, int, int>> E;
void dfs(int at, int u) {
if (vis[u][at])
return;
vis[u][at] = 1;
for (auto &p : V[at]) {
dfs(p.first, u);
}
}
int dist[2500];
void solve(int ks) {
int p;
scanf("%d %d %d", &n, &m, &p);
FORN(i, m) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
E.push_back(make_tuple(a, b, -(c - p)));
V[a].push_back({b, c - p});
}
for (int i = 1; i <= n; i++)
dfs(i, i);
FORAB(i, 1, n) { dist[i] = 1000000000; }
dist[1] = 0;
int edit;
FORAB(i, 1, n) {
edit = 0;
for (auto &p : E) {
if (dist[get<0>(p)] + get<2>(p) < dist[get<1>(p)]) {
if (vis[1][get<0>(p)] && vis[get<1>(p)][n]) {
edit = 1;
}
dist[get<1>(p)] = dist[get<0>(p)] + get<2>(p);
}
}
}
LL ans = 0;
if (edit) {
ans = -1;
} else {
ans = -dist[n];
if (ans < 0)
ans = 0;
}
printf("%lld\n", ans);
}
int main() {
#ifdef LOCAL
double start_time = clock();
freopen("C:\\Home\\ContestCodes\\sample.in", "r", stdin);
// freopen("out.out", "w", stdout);
#endif
if (0) {
int T;
scanf("%d", &T);
// AIN(T, 1, 25);
for (int ks = 1; ks <= T; ks++) {
solve(ks);
fprintf(stderr, "%d done\n", ks);
}
} else {
solve(0);
}
#ifdef LOCAL
double end_time = clock();
fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC);
#endif
return 0;
}
| // #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma warning(disable : 4786)
#pragma warning(disable : 4996)
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
template <class c>
typename enable_if<sizeof dud<c>(0) != 1, debug &>::type operator<<(c i) {
cerr << boolalpha << i;
return *this;
}
template <class c, int = 0>
typename enable_if<sizeof dud<c>(0) == 1, debug &>::type operator<<(c i) {
return *this << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
};
#define watch(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ((X) > 0 ? (X) : (-(X)))
#define S(X) ((X) * (X))
#define SZ(V) (int)V.size()
#define FORN(i, n) for (int i = 0; i < n; i++)
#define FORAB(i, a, b) for (int i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C) ((B) <= (A) && (A) <= (C))
#define AIN(A, B, C) assert(IN(A, B, C))
// typedef int LL;
typedef long long int LL;
// typedef __int128 LLL;
typedef long long LLL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VP;
typedef vector<double> VD;
typedef vector<vector<int>> VVI;
typedef long double ld;
const LL MOD = 998244353;
int vis[2502][2502];
VP V[2502];
int n, m;
vector<tuple<int, int, int>> E;
void dfs(int at, int u) {
if (vis[u][at])
return;
vis[u][at] = 1;
for (auto &p : V[at]) {
dfs(p.first, u);
}
}
int dist[2502];
void solve(int ks) {
int p;
scanf("%d %d %d", &n, &m, &p);
FORN(i, m) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
E.push_back(make_tuple(a, b, -(c - p)));
V[a].push_back({b, c - p});
}
for (int i = 1; i <= n; i++)
dfs(i, i);
FORAB(i, 1, n) { dist[i] = 1000000000; }
dist[1] = 0;
int edit;
FORAB(i, 1, n) {
edit = 0;
for (auto &p : E) {
if (dist[get<0>(p)] + get<2>(p) < dist[get<1>(p)]) {
if (vis[1][get<0>(p)] && vis[get<1>(p)][n]) {
edit = 1;
}
dist[get<1>(p)] = dist[get<0>(p)] + get<2>(p);
}
}
}
LL ans = 0;
if (edit) {
ans = -1;
} else {
ans = -dist[n];
if (ans < 0)
ans = 0;
}
printf("%lld\n", ans);
}
int main() {
#ifdef LOCAL
double start_time = clock();
freopen("C:\\Home\\ContestCodes\\sample.in", "r", stdin);
// freopen("out.out", "w", stdout);
#endif
if (0) {
int T;
scanf("%d", &T);
// AIN(T, 1, 25);
for (int ks = 1; ks <= T; ks++) {
solve(ks);
fprintf(stderr, "%d done\n", ks);
}
} else {
solve(0);
}
#ifdef LOCAL
double end_time = clock();
fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC);
#endif
return 0;
}
| replace | 119 | 120 | 119 | 120 | 0 | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
struct edge {
int to, cost;
};
vector<edge> G[3010];
vector<int> RG[3010], can_use(3010, 0);
vector<int> dist;
int bellmanford(int n, int s) { // 頂点数、始点
const int INF = 1LL << 60;
dist = vector<int>(n, -INF);
dist[s] = 0;
for (int i = 0; i < n; i++) {
for (int u = 0; u < n; u++) {
if (!can_use[u])
continue;
for (auto e : G[u]) {
if (dist[u] != -INF && dist[e.to] < dist[u] + e.cost) {
dist[e.to] = dist[u] + e.cost;
// n回目にも更新があるなら負の閉路が存在
if (i == n - 1)
return 1;
}
}
}
}
return 0;
}
signed main() {
int n, m, p;
cin >> n >> m >> p;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
G[a].push_back({b, c - p});
RG[b].push_back(a);
}
function<void(int, int)> dfs = [&](int u, int pre) {
can_use[u] = 1;
for (int v : RG[u]) {
if (v == pre)
continue;
dfs(v, u);
}
};
dfs(n - 1, -9);
if (bellmanford(n, 0)) {
cout << -1 << endl;
} else {
cout << max(0LL, dist[n - 1]) << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
struct edge {
int to, cost;
};
vector<edge> G[3010];
vector<int> RG[3010], can_use(3010, 0);
vector<int> dist;
int bellmanford(int n, int s) { // 頂点数、始点
const int INF = 1LL << 60;
dist = vector<int>(n, -INF);
dist[s] = 0;
for (int i = 0; i < n; i++) {
for (int u = 0; u < n; u++) {
if (!can_use[u])
continue;
for (auto e : G[u]) {
if (dist[u] != -INF && dist[e.to] < dist[u] + e.cost) {
dist[e.to] = dist[u] + e.cost;
// n回目にも更新があるなら負の閉路が存在
if (i == n - 1)
return 1;
}
}
}
}
return 0;
}
signed main() {
int n, m, p;
cin >> n >> m >> p;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
G[a].push_back({b, c - p});
RG[b].push_back(a);
}
function<void(int, int)> dfs = [&](int u, int pre) {
if (can_use[u])
return;
can_use[u] = 1;
for (int v : RG[u]) {
if (v == pre)
continue;
dfs(v, u);
}
};
dfs(n - 1, -9);
if (bellmanford(n, 0)) {
cout << -1 << endl;
} else {
cout << max(0LL, dist[n - 1]) << endl;
}
return 0;
}
| insert | 49 | 49 | 49 | 51 | 0 | |
p02949 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, sta, n) \
for (long long i = (sta), i##Len_ = (n); i < i##Len_; i++)
typedef long long ll;
static const ll MOD = 1000000007;
// static const ll INF = 1000000000000000000LL;
const ll mod = 998244353;
const ll INF = mod * mod;
struct edge {
int to;
ll cost;
};
const int em_max = 2500;
vector<edge> G[em_max];
vector<int> rG[em_max];
ll score[em_max];
bool canGoal[em_max];
bool canStart[em_max];
// 最短経路問題
// 負の辺がでてくる
int main() {
ll N, M, P;
cin >> N >> M >> P;
REP(i, N) {
// edge:a->b; cost:c
int a, b, c;
cin >> a >> b >> c;
--a;
--b;
G[a].push_back({b, c});
rG[b].push_back(a);
}
// 初期化
std::fill(score, score + N, -INF);
score[0] = 0;
// 0 -> N-1
queue<int> q;
q.push(N - 1);
canGoal[N - 1] = true;
while (!q.empty()) {
int id = q.front();
q.pop();
REP(j, rG[id].size()) {
int to = rG[id][j];
// すでにゴールに到達できることがわかっている
if (canGoal[to])
continue;
canGoal[to] = true;
q.push(to);
}
}
// q.empty() == true
q.push(0);
canStart[0] = true;
while (!q.empty()) {
int id = q.front();
q.pop();
REP(j, G[id].size()) {
int to = G[id][j].to;
if (canStart[to])
continue;
canStart[to] = true;
q.push(to);
}
}
REP(i, N) {
if (canStart[i] == false) {
canGoal[i] = false;
}
}
REP(t, 5000) {
REP(i, N) {
if (canGoal[i] == false)
continue;
REP(j, G[i].size()) {
int to = G[i][j].to;
if (canGoal[to] == false)
continue;
// 1辺の移動のたびに -P される
ll ns = score[i] + G[i][j].cost - P;
if (score[to] < ns) {
score[to] = ns;
}
}
}
}
// まだ最大値が更新されるなら、存在しない
REP(i, N) {
if (canGoal[i] == false)
continue;
REP(j, G[i].size()) {
int to = G[i][j].to;
if (canGoal[to] == false)
continue;
// 1辺の移動のたびに -P される
ll ns = score[i] + G[i][j].cost - P;
if (score[to] < ns) {
score[to] = ns;
cout << -1 << endl;
return 0;
}
}
}
if (score[N - 1] < 0)
score[N - 1] = 0;
cout << score[N - 1] << endl;
return 0;
} | #include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, sta, n) \
for (long long i = (sta), i##Len_ = (n); i < i##Len_; i++)
typedef long long ll;
static const ll MOD = 1000000007;
// static const ll INF = 1000000000000000000LL;
const ll mod = 998244353;
const ll INF = mod * mod;
struct edge {
int to;
ll cost;
};
const int em_max = 2500;
vector<edge> G[em_max];
vector<int> rG[em_max];
ll score[em_max];
bool canGoal[em_max];
bool canStart[em_max];
// 最短経路問題
// 負の辺がでてくる
int main() {
ll N, M, P;
cin >> N >> M >> P;
REP(i, M) {
// edge:a->b; cost:c
int a, b, c;
cin >> a >> b >> c;
--a;
--b;
G[a].push_back({b, c});
rG[b].push_back(a);
}
// 初期化
std::fill(score, score + N, -INF);
score[0] = 0;
// 0 -> N-1
queue<int> q;
q.push(N - 1);
canGoal[N - 1] = true;
while (!q.empty()) {
int id = q.front();
q.pop();
REP(j, rG[id].size()) {
int to = rG[id][j];
// すでにゴールに到達できることがわかっている
if (canGoal[to])
continue;
canGoal[to] = true;
q.push(to);
}
}
// q.empty() == true
q.push(0);
canStart[0] = true;
while (!q.empty()) {
int id = q.front();
q.pop();
REP(j, G[id].size()) {
int to = G[id][j].to;
if (canStart[to])
continue;
canStart[to] = true;
q.push(to);
}
}
REP(i, N) {
if (canStart[i] == false) {
canGoal[i] = false;
}
}
REP(t, 5000) {
REP(i, N) {
if (canGoal[i] == false)
continue;
REP(j, G[i].size()) {
int to = G[i][j].to;
if (canGoal[to] == false)
continue;
// 1辺の移動のたびに -P される
ll ns = score[i] + G[i][j].cost - P;
if (score[to] < ns) {
score[to] = ns;
}
}
}
}
// まだ最大値が更新されるなら、存在しない
REP(i, N) {
if (canGoal[i] == false)
continue;
REP(j, G[i].size()) {
int to = G[i][j].to;
if (canGoal[to] == false)
continue;
// 1辺の移動のたびに -P される
ll ns = score[i] + G[i][j].cost - P;
if (score[to] < ns) {
score[to] = ns;
cout << -1 << endl;
return 0;
}
}
}
if (score[N - 1] < 0)
score[N - 1] = 0;
cout << score[N - 1] << endl;
return 0;
} | replace | 40 | 42 | 40 | 41 | 0 | |
p02949 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define N 2007
#define M 5007
#define int long long
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
const int INF = 1e18; // longlongµÄINF²»ÊÇ0x3f3f3f
int n, m, p;
int f[N];
struct Edge {
int u, v, d;
} E[M];
signed main() {
n = read(), m = read(), p = read();
for (int i = 1, u, v, d; i <= m; ++i) {
u = read(), v = read(), d = read();
E[i] = (Edge){u, v, d - p};
}
for (int i = 1; i <= n; ++i)
f[i] = -INF;
f[1] = 0;
for (int i = 1; i <= n * 2; ++i) {
for (int j = 1; j <= m; ++j) {
int u = E[j].u, v = E[j].v, d = E[j].d;
if (f[u] == -INF)
continue;
if (f[u] + d > f[v]) {
f[v] = i <= n ? f[u] + d : INF;
}
}
}
if (f[n] == INF)
puts("-1");
else if (f[n] < 0)
puts("0");
else
printf("%lld\n", f[n]);
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define N 2507
#define M 5007
#define int long long
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
const int INF = 1e18; // longlongµÄINF²»ÊÇ0x3f3f3f
int n, m, p;
int f[N];
struct Edge {
int u, v, d;
} E[M];
signed main() {
n = read(), m = read(), p = read();
for (int i = 1, u, v, d; i <= m; ++i) {
u = read(), v = read(), d = read();
E[i] = (Edge){u, v, d - p};
}
for (int i = 1; i <= n; ++i)
f[i] = -INF;
f[1] = 0;
for (int i = 1; i <= n * 2; ++i) {
for (int j = 1; j <= m; ++j) {
int u = E[j].u, v = E[j].v, d = E[j].d;
if (f[u] == -INF)
continue;
if (f[u] + d > f[v]) {
f[v] = i <= n ? f[u] + d : INF;
}
}
}
if (f[n] == INF)
puts("-1");
else if (f[n] < 0)
puts("0");
else
printf("%lld\n", f[n]);
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m, p;
cin >> n >> m >> p;
int a[5010], b[5010];
long long c[5010], dist[5010];
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> c[i];
c[i] = -c[i];
c[i] += p;
a[i]--, b[i]--;
}
dist[0] = 0;
for (int i = 1; i < n; i++) {
dist[i] = 1000000009;
}
for (int j = 0; j < n - 1; j++) {
for (int i = 0; i < m; i++) {
if (dist[a[i]] == 1000000009)
continue;
if (dist[b[i]] > dist[a[i]] + c[i]) {
dist[b[i]] = dist[a[i]] + c[i];
}
}
}
long long ans = dist[n - 1];
bool abc[1010];
for (int i = 0; i < n; i++) {
abc[i] = false;
}
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
if (dist[a[i]] == 1000000009)
continue;
if (dist[b[i]] > dist[a[i]] + c[i]) {
dist[b[i]] = dist[a[i]] + c[i];
abc[b[i]] = true;
}
if (abc[a[i]])
abc[b[i]] = true;
}
}
if (abc[n - 1])
cout << "-1" << endl;
else
cout << max((long long)0, -ans) << endl;
}
| #include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m, p;
cin >> n >> m >> p;
int a[5010], b[5010];
long long c[5010], dist[5010];
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> c[i];
c[i] = -c[i];
c[i] += p;
a[i]--, b[i]--;
}
dist[0] = 0;
for (int i = 1; i < n; i++) {
dist[i] = 1000000009;
}
for (int j = 0; j < n - 1; j++) {
for (int i = 0; i < m; i++) {
if (dist[a[i]] == 1000000009)
continue;
if (dist[b[i]] > dist[a[i]] + c[i]) {
dist[b[i]] = dist[a[i]] + c[i];
}
}
}
long long ans = dist[n - 1];
bool abc[5010];
for (int i = 0; i < n; i++) {
abc[i] = false;
}
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
if (dist[a[i]] == 1000000009)
continue;
if (dist[b[i]] > dist[a[i]] + c[i]) {
dist[b[i]] = dist[a[i]] + c[i];
abc[b[i]] = true;
}
if (abc[a[i]])
abc[b[i]] = true;
}
}
if (abc[n - 1])
cout << "-1" << endl;
else
cout << max((long long)0, -ans) << endl;
}
| replace | 41 | 42 | 41 | 42 | 0 | |
p02949 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define For(i, n1, n2) for (int i = (int)(n1); i < (int)(n2); i++)
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define RREP(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define FOR(i, n1, n2) for (ll i = (ll)(n1); i < (ll)(n2); i++)
#define RFOR(i, n1, n2) for (ll i = ((ll)(n1)-1); i >= (ll)(n2); i--)
#define all(a) (a).begin(), (a).end()
#define SORT(a) sort((a).begin(), (a).end())
#define oorret 0
#define oor(x) \
[&]() { \
try { \
x; \
} catch (const out_of_range &oor) { \
return oorret; \
} \
return x; \
}()
#define IOS cin.tie(0), ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
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;
}
template <class Type> struct is_vector : std::false_type {};
template <class ValueType, class Alloc>
struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
if (v.empty())
return out;
constexpr bool is_vector_v = is_vector<T>::value;
if (is_vector_v)
for (auto itr = v.begin(); itr != v.end();)
out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
else
for (auto itr = v.begin(); itr != v.end();)
out << (*itr), out << ((++itr != v.end()) ? " " : "");
return out;
}
inline void put() {}
template <class T> inline void put(const T &first) {
std::cout << first << "\n";
}
template <class T, class... N>
inline void put(const T &first, const N &...rest) {
std::cout << first << " ";
put(rest...);
}
inline void putn() {}
template <class T, class... N>
inline void putn(const T &first, const N &...rest) {
std::cout << first << "\n";
putn(rest...);
}
struct edge {
ll to, cost, index;
edge(ll to, ll cost, ll index) : to(to), cost(cost), index(index) {}
};
void dfs(vector<vector<edge>> &path, vector<int> &st, int now) {
REP(i, path[now].size()) {
if (st[path[now][i].index] == 0) {
st[path[now][i].index] = 1;
dfs(path, st, path[now][i].to);
}
}
}
int main() {
IOS;
int n, m, p;
cin >> n >> m >> p;
vector<ll> a(m), b(m), c(m);
vector<vector<edge>> path(n);
vector<vector<edge>> path2(n);
vector<vector<edge>> path3(n);
vector<int> st(m, 0), dt(m, 0);
REP(i, m) {
cin >> a[i] >> b[i] >> c[i];
a[i]--;
b[i]--;
path[a[i]].push_back(edge(b[i], c[i] - p, i));
path2[b[i]].push_back(edge(a[i], c[i] - p, i));
}
vector<ll> dist(n, -1e15);
int src = 0, dest = n - 1;
dfs(path, st, src);
dfs(path2, dt, dest);
dist[src] = 0;
REP(i, m) {
if (st[i] && dt[i]) {
path3[a[i]].push_back(edge(b[i], c[i] - p, i));
}
}
REP(i, n * 2) {
REP(j, n) {
REP(k, path[j].size()) {
if (chmax(dist[path3[j][k].to], dist[j] + path3[j][k].cost)) {
}
}
}
}
REP(i, n) {
REP(j, path3[i].size()) {
if (dist[i] + path3[i][j].cost > dist[path3[i][j].to]) {
put(-1);
return 0;
}
}
}
put(max(dist[dest], 0ll));
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define For(i, n1, n2) for (int i = (int)(n1); i < (int)(n2); i++)
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define RREP(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define FOR(i, n1, n2) for (ll i = (ll)(n1); i < (ll)(n2); i++)
#define RFOR(i, n1, n2) for (ll i = ((ll)(n1)-1); i >= (ll)(n2); i--)
#define all(a) (a).begin(), (a).end()
#define SORT(a) sort((a).begin(), (a).end())
#define oorret 0
#define oor(x) \
[&]() { \
try { \
x; \
} catch (const out_of_range &oor) { \
return oorret; \
} \
return x; \
}()
#define IOS cin.tie(0), ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
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;
}
template <class Type> struct is_vector : std::false_type {};
template <class ValueType, class Alloc>
struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
if (v.empty())
return out;
constexpr bool is_vector_v = is_vector<T>::value;
if (is_vector_v)
for (auto itr = v.begin(); itr != v.end();)
out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
else
for (auto itr = v.begin(); itr != v.end();)
out << (*itr), out << ((++itr != v.end()) ? " " : "");
return out;
}
inline void put() {}
template <class T> inline void put(const T &first) {
std::cout << first << "\n";
}
template <class T, class... N>
inline void put(const T &first, const N &...rest) {
std::cout << first << " ";
put(rest...);
}
inline void putn() {}
template <class T, class... N>
inline void putn(const T &first, const N &...rest) {
std::cout << first << "\n";
putn(rest...);
}
struct edge {
ll to, cost, index;
edge(ll to, ll cost, ll index) : to(to), cost(cost), index(index) {}
};
void dfs(vector<vector<edge>> &path, vector<int> &st, int now) {
REP(i, path[now].size()) {
if (st[path[now][i].index] == 0) {
st[path[now][i].index] = 1;
dfs(path, st, path[now][i].to);
}
}
}
int main() {
IOS;
int n, m, p;
cin >> n >> m >> p;
vector<ll> a(m), b(m), c(m);
vector<vector<edge>> path(n);
vector<vector<edge>> path2(n);
vector<vector<edge>> path3(n);
vector<int> st(m, 0), dt(m, 0);
REP(i, m) {
cin >> a[i] >> b[i] >> c[i];
a[i]--;
b[i]--;
path[a[i]].push_back(edge(b[i], c[i] - p, i));
path2[b[i]].push_back(edge(a[i], c[i] - p, i));
}
vector<ll> dist(n, -1e15);
int src = 0, dest = n - 1;
dfs(path, st, src);
dfs(path2, dt, dest);
dist[src] = 0;
REP(i, m) {
if (st[i] && dt[i]) {
path3[a[i]].push_back(edge(b[i], c[i] - p, i));
}
}
REP(i, n * 2) {
REP(j, n) {
REP(k, path3[j].size()) {
if (chmax(dist[path3[j][k].to], dist[j] + path3[j][k].cost)) {
}
}
}
}
REP(i, n) {
REP(j, path3[i].size()) {
if (dist[i] + path3[i][j].cost > dist[path3[i][j].to]) {
put(-1);
return 0;
}
}
}
put(max(dist[dest], 0ll));
return 0;
}
| replace | 112 | 113 | 112 | 113 | 0 | |
p02949 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define For(i, n1, n2) for (int i = (int)(n1); i < (int)(n2); i++)
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define RREP(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define FOR(i, n1, n2) for (ll i = (ll)(n1); i < (ll)(n2); i++)
#define RFOR(i, n1, n2) for (ll i = ((ll)(n1)-1); i >= (ll)(n2); i--)
#define all(a) (a).begin(), (a).end()
#define SORT(a) sort((a).begin(), (a).end())
#define oorret 0
#define oor(x) \
[&]() { \
try { \
x; \
} catch (const out_of_range &oor) { \
return oorret; \
} \
return x; \
}()
#define IOS cin.tie(0), ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
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;
}
template <class Type> struct is_vector : std::false_type {};
template <class ValueType, class Alloc>
struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
if (v.empty())
return out;
constexpr bool is_vector_v = is_vector<T>::value;
if (is_vector_v)
for (auto itr = v.begin(); itr != v.end();)
out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
else
for (auto itr = v.begin(); itr != v.end();)
out << (*itr), out << ((++itr != v.end()) ? " " : "");
return out;
}
inline void put() {}
template <class T> inline void put(const T &first) {
std::cout << first << "\n";
}
template <class T, class... N>
inline void put(const T &first, const N &...rest) {
std::cout << first << " ";
put(rest...);
}
inline void putn() {}
template <class T, class... N>
inline void putn(const T &first, const N &...rest) {
std::cout << first << "\n";
putn(rest...);
}
class BellmanFord {
public:
struct edge {
int to;
ll cost;
edge(int to, ll cost) : to(to), cost(cost) {}
};
struct node {
int from;
ll cost;
bool neg;
node(int from, ll cost) : from(from), cost(cost) { neg = false; }
};
vector<vector<edge>> path;
int n;
ll inf = LLONG_MAX / 2;
BellmanFord(int n) : n(n) { path.resize(n); }
void push_edge(int a, int b, ll c) { path[a].push_back(edge(b, c)); }
vector<node> solve(int start) {
vector<node> dist(n, node(-1, inf));
dist[start].cost = 0;
REP(i, this->n - 1) {
REP(j, this->n) {
REP(k, path[j].size()) {
if (dist[j].cost != this->inf) {
if (chmin(dist[path[j][k].to].cost,
dist[j].cost + path[j][k].cost)) {
dist[path[j][k].to].from = j;
}
}
}
}
}
REP(i, this->n - 1) {
REP(j, this->n) {
REP(k, path[i].size()) {
int from = j;
int to = path[j][k].to;
ll cost = path[j][k].cost;
if (dist[to].cost != this->inf &&
dist[from].cost + cost < dist[to].cost) {
dist[to].cost = dist[from].cost + cost;
dist[to].neg = true;
}
if (dist[from].neg) {
dist[to].neg = true;
}
}
}
}
return dist;
}
vector<int> restore(vector<node> &dist, int end) {
vector<int> res;
if (dist[end].neg || dist[end].cost == this->inf) {
return res;
} else {
int now = end;
while (now >= 0) {
res.push_back(now);
now = dist[now].from;
}
reverse(res.begin(), res.end());
return res;
}
}
};
int main() {
IOS;
int n, m, p;
cin >> n >> m >> p;
vector<ll> a(m), b(m), c(m);
BellmanFord bf(n);
REP(i, m) {
cin >> a[i] >> b[i] >> c[i];
a[i]--;
b[i]--;
bf.push_edge(a[i], b[i], -(c[i] - p));
}
int start = 0, end = n - 1;
auto dist = bf.solve(start);
if (dist[end].neg) {
put(-1);
} else {
put(max(-dist[end].cost, 0ll));
}
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define For(i, n1, n2) for (int i = (int)(n1); i < (int)(n2); i++)
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define RREP(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define FOR(i, n1, n2) for (ll i = (ll)(n1); i < (ll)(n2); i++)
#define RFOR(i, n1, n2) for (ll i = ((ll)(n1)-1); i >= (ll)(n2); i--)
#define all(a) (a).begin(), (a).end()
#define SORT(a) sort((a).begin(), (a).end())
#define oorret 0
#define oor(x) \
[&]() { \
try { \
x; \
} catch (const out_of_range &oor) { \
return oorret; \
} \
return x; \
}()
#define IOS cin.tie(0), ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
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;
}
template <class Type> struct is_vector : std::false_type {};
template <class ValueType, class Alloc>
struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
if (v.empty())
return out;
constexpr bool is_vector_v = is_vector<T>::value;
if (is_vector_v)
for (auto itr = v.begin(); itr != v.end();)
out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
else
for (auto itr = v.begin(); itr != v.end();)
out << (*itr), out << ((++itr != v.end()) ? " " : "");
return out;
}
inline void put() {}
template <class T> inline void put(const T &first) {
std::cout << first << "\n";
}
template <class T, class... N>
inline void put(const T &first, const N &...rest) {
std::cout << first << " ";
put(rest...);
}
inline void putn() {}
template <class T, class... N>
inline void putn(const T &first, const N &...rest) {
std::cout << first << "\n";
putn(rest...);
}
class BellmanFord {
public:
struct edge {
int to;
ll cost;
edge(int to, ll cost) : to(to), cost(cost) {}
};
struct node {
int from;
ll cost;
bool neg;
node(int from, ll cost) : from(from), cost(cost) { neg = false; }
};
vector<vector<edge>> path;
int n;
ll inf = LLONG_MAX / 2;
BellmanFord(int n) : n(n) { path.resize(n); }
void push_edge(int a, int b, ll c) { path[a].push_back(edge(b, c)); }
vector<node> solve(int start) {
vector<node> dist(n, node(-1, inf));
dist[start].cost = 0;
REP(i, this->n - 1) {
REP(j, this->n) {
REP(k, path[j].size()) {
if (dist[j].cost != this->inf) {
if (chmin(dist[path[j][k].to].cost,
dist[j].cost + path[j][k].cost)) {
dist[path[j][k].to].from = j;
}
}
}
}
}
REP(i, this->n - 1) {
REP(j, this->n) {
REP(k, path[j].size()) {
int from = j;
int to = path[j][k].to;
ll cost = path[j][k].cost;
if (dist[to].cost != this->inf &&
dist[from].cost + cost < dist[to].cost) {
dist[to].cost = dist[from].cost + cost;
dist[to].neg = true;
}
if (dist[from].neg) {
dist[to].neg = true;
}
}
}
}
return dist;
}
vector<int> restore(vector<node> &dist, int end) {
vector<int> res;
if (dist[end].neg || dist[end].cost == this->inf) {
return res;
} else {
int now = end;
while (now >= 0) {
res.push_back(now);
now = dist[now].from;
}
reverse(res.begin(), res.end());
return res;
}
}
};
int main() {
IOS;
int n, m, p;
cin >> n >> m >> p;
vector<ll> a(m), b(m), c(m);
BellmanFord bf(n);
REP(i, m) {
cin >> a[i] >> b[i] >> c[i];
a[i]--;
b[i]--;
bf.push_edge(a[i], b[i], -(c[i] - p));
}
int start = 0, end = n - 1;
auto dist = bf.solve(start);
if (dist[end].neg) {
put(-1);
} else {
put(max(-dist[end].cost, 0ll));
}
return 0;
}
| replace | 106 | 107 | 106 | 107 | -11 | |
p02949 | Python | Runtime Error | from collections import defaultdict, deque
N, M, P = map(int, input().split())
G = defaultdict(lambda: defaultdict(lambda: 10000000))
G_rev = defaultdict(lambda: defaultdict(lambda: 10000000))
for i in range(1, N + 1):
G[i] = dict()
G_rev[i] = dict()
for _ in range(M):
A, B, C = map(int, input().split())
G[A][B] = min(G[A][B], P - C)
G_rev[B][A] = 1
reachable_N = [0] * (N + 1)
reachable_N[N] = 1
queue = deque([N])
while len(queue) > 0:
p = queue.popleft()
for q in G_rev[p].keys():
if reachable_N[q] == 0:
reachable_N[q] = 1
queue.append(q)
reachable_1 = [0] * (N + 1)
reachable_1[1] = 1
queue = deque([1])
while len(queue) > 0:
p = queue.popleft()
for q in G[p].keys():
if reachable_1[q] == 0:
reachable_1[q] = 1
queue.append(q)
reachable = [reachable_1[i] * reachable_N[i] for i in range(N + 1)]
dist = [10000000000 for i in range(N + 1)]
res = 0
dist[1] = 0
queue = deque([1])
cnt = 0
update = True
while update:
update = False
for p in range(1, N + 1):
if reachable[p] == 0:
continue
for q in G[p].keys():
if reachable[q] == 0:
pass
elif dist[p] + G[p][q] < dist[q]:
dist[q] = dist[p] + G[p][q]
update = True
cnt += 1
if cnt > N:
res = -1
break
if res == -1:
print(res)
else:
print(max(0, -dist[N]))
| from collections import defaultdict, deque
N, M, P = map(int, input().split())
G = defaultdict(lambda: defaultdict(lambda: 10000000))
G_rev = defaultdict(lambda: defaultdict(lambda: 10000000))
for _ in range(M):
A, B, C = map(int, input().split())
G[A][B] = min(G[A][B], P - C)
G_rev[B][A] = 1
reachable_N = [0] * (N + 1)
reachable_N[N] = 1
queue = deque([N])
while len(queue) > 0:
p = queue.popleft()
for q in G_rev[p].keys():
if reachable_N[q] == 0:
reachable_N[q] = 1
queue.append(q)
reachable_1 = [0] * (N + 1)
reachable_1[1] = 1
queue = deque([1])
while len(queue) > 0:
p = queue.popleft()
for q in G[p].keys():
if reachable_1[q] == 0:
reachable_1[q] = 1
queue.append(q)
reachable = [reachable_1[i] * reachable_N[i] for i in range(N + 1)]
dist = [10000000000 for i in range(N + 1)]
res = 0
dist[1] = 0
queue = deque([1])
cnt = 0
update = True
while update:
update = False
for p in range(1, N + 1):
if reachable[p] == 0:
continue
for q in G[p].keys():
if reachable[q] == 0:
pass
elif dist[p] + G[p][q] < dist[q]:
dist[q] = dist[p] + G[p][q]
update = True
cnt += 1
if cnt > N:
res = -1
break
if res == -1:
print(res)
else:
print(max(0, -dist[N]))
| delete | 5 | 9 | 5 | 5 | KeyError: 2 | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02949/Python/s582998390.py", line 13, in <module>
G[A][B] = min(G[A][B], P - C)
KeyError: 2
|
p02949 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = 2500;
const long long INF = 1e17;
int N, M, PF, vis[MAXN];
int Ok[MAXN], cnt[MAXN];
long long dist[MAXN], Ans;
vector<int> P[MAXN], T[MAXN], F[MAXN];
queue<int> Q;
void SPFA(int u, int val) {
for (int i = 1; i <= N; i++)
dist[i] = INF;
while (!Q.empty())
Q.pop();
dist[u] = val;
Q.push(u);
cnt[u]++;
while (!Q.empty()) {
u = Q.front();
Q.pop();
int size = P[u].size();
for (int i = 0; i < size; i++) {
int v = P[u][i];
if (dist[v] > dist[u] + T[u][i]) {
dist[v] = dist[u] + T[u][i];
if (cnt[v] > 30 * N + 5) {
Ok[v] = 1;
continue;
}
cnt[v]++;
Q.push(v);
}
}
}
if (dist[N] == INF)
Ans = -1;
else
Ans = max(0 * 1LL, -dist[N]);
}
void FA(int u) {
int size = F[u].size();
vis[u] = 1;
if (Ok[u])
Ans = -1;
for (int i = 0; i < size; i++) {
int v = F[u][i];
if (vis[v])
continue;
FA(v);
}
}
int main() {
scanf("%d%d%d", &N, &M, &PF);
for (int i = 1, x, y, z; i <= M; i++) {
scanf("%d%d%d", &x, &y, &z);
P[x].push_back(y);
F[y].push_back(x);
T[x].push_back(PF - z);
}
SPFA(1, 0);
FA(N);
printf("%lld\n", Ans);
} | #include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = 2500;
const long long INF = 1e17;
int N, M, PF, vis[MAXN];
int Ok[MAXN], cnt[MAXN];
long long dist[MAXN], Ans;
vector<int> P[MAXN], T[MAXN], F[MAXN];
queue<int> Q;
void SPFA(int u, int val) {
for (int i = 1; i <= N; i++)
dist[i] = INF;
while (!Q.empty())
Q.pop();
dist[u] = val;
Q.push(u);
cnt[u]++;
while (!Q.empty()) {
u = Q.front();
Q.pop();
int size = P[u].size();
for (int i = 0; i < size; i++) {
int v = P[u][i];
if (dist[v] > dist[u] + T[u][i]) {
dist[v] = dist[u] + T[u][i];
if (cnt[v] > 8 * N + 5) {
Ok[v] = 1;
continue;
}
cnt[v]++;
Q.push(v);
}
}
}
if (dist[N] == INF)
Ans = -1;
else
Ans = max(0 * 1LL, -dist[N]);
}
void FA(int u) {
int size = F[u].size();
vis[u] = 1;
if (Ok[u])
Ans = -1;
for (int i = 0; i < size; i++) {
int v = F[u][i];
if (vis[v])
continue;
FA(v);
}
}
int main() {
scanf("%d%d%d", &N, &M, &PF);
for (int i = 1, x, y, z; i <= M; i++) {
scanf("%d%d%d", &x, &y, &z);
P[x].push_back(y);
F[y].push_back(x);
T[x].push_back(PF - z);
}
SPFA(1, 0);
FA(N);
printf("%lld\n", Ans);
} | replace | 28 | 29 | 28 | 29 | TLE | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
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 vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(ll x, ll y) {
ll r = 1;
while (y) {
if (y & 1)
r *= x;
x *= x;
y >>= 1;
}
return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
ll r = 1;
while (y) {
if (y & 1)
r *= x;
x *= x;
r %= m;
x %= m;
y >>= 1;
}
return r;
}
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;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
ll ME = 11;
vector<vp> g(2600);
ll n, m;
vll a(ME), b(ME), c(ME), score(ME);
ll ans = -INF;
void bf() {
score[1] = 0;
repr(i, 2, n + 1) { score[i] = INF; }
for (ll i = 0;; i++) {
bool finf = 0;
bool finfv = 0;
rep(j, m) {
ll u = a[j];
ll v = b[j];
if (score[v] > score[u] + c[j]) {
score[v] = score[u] + c[j];
finf = 1;
if (v == n) {
finfv = 1;
}
}
}
if (!finf) {
ans = score[n];
return;
}
if (finfv && i >= n) {
ans = INF;
return;
}
if (i >= 3 * n) {
ans = score[n];
return;
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll p;
cin >> n >> m >> p;
rep(i, m) {
cin >> a[i] >> b[i] >> c[i];
c[i] -= p;
c[i] = -c[i];
g[a[i]].emplace_back(b[i], c[i]);
}
bf();
if (ans < INF) {
cout << max(-score[n], 0LL) << endl;
} else {
cout << -1 << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
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 vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(ll x, ll y) {
ll r = 1;
while (y) {
if (y & 1)
r *= x;
x *= x;
y >>= 1;
}
return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
ll r = 1;
while (y) {
if (y & 1)
r *= x;
x *= x;
r %= m;
x %= m;
y >>= 1;
}
return r;
}
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;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
ll ME = 5050;
vector<vp> g(2600);
ll n, m;
vll a(ME), b(ME), c(ME), score(ME);
ll ans = -INF;
void bf() {
score[1] = 0;
repr(i, 2, n + 1) { score[i] = INF; }
for (ll i = 0;; i++) {
bool finf = 0;
bool finfv = 0;
rep(j, m) {
ll u = a[j];
ll v = b[j];
if (score[v] > score[u] + c[j]) {
score[v] = score[u] + c[j];
finf = 1;
if (v == n) {
finfv = 1;
}
}
}
if (!finf) {
ans = score[n];
return;
}
if (finfv && i >= n) {
ans = INF;
return;
}
if (i >= 3 * n) {
ans = score[n];
return;
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll p;
cin >> n >> m >> p;
rep(i, m) {
cin >> a[i] >> b[i] >> c[i];
c[i] -= p;
c[i] = -c[i];
g[a[i]].emplace_back(b[i], c[i]);
}
bf();
if (ans < INF) {
cout << max(-score[n], 0LL) << endl;
} else {
cout << -1 << endl;
}
return 0;
} | replace | 62 | 63 | 62 | 63 | 0 | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
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 vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(ll x, ll y) {
ll r = 1;
while (y) {
if (y & 1)
r *= x;
x *= x;
y >>= 1;
}
return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
ll r = 1;
while (y) {
if (y & 1)
r *= x;
x *= x;
r %= m;
x %= m;
y >>= 1;
}
return r;
}
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;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
const ll ME = 12;
const ll MV = 12;
vector<vp> g(2600);
ll n, m;
vll a(ME), b(ME), c(ME), score(ME);
ll ans = -INF;
vi finfv(MV, 0);
void bf() {
score[1] = 0;
repr(i, 2, n + 1) { score[i] = INF; }
rep(i, n - 1) {
bool finf = 0;
rep(j, m) {
ll u = a[j];
ll v = b[j];
if (score[v] > score[u] + c[j]) {
score[v] = score[u] + c[j];
finf = 1;
}
}
if (!finf) {
ans = score[n];
return;
}
dump(score);
}
rep(i, n) {
rep(j, m) {
ll u = a[j];
ll v = b[j];
if (score[u] >= INF / 2)
continue;
if (score[v] > score[u] + c[j]) {
score[v] = score[u] + c[j];
finfv[v] = 1;
}
if (finfv[u]) {
finfv[v] = 1;
}
}
dump(score);
dump(finfv);
}
ans = (finfv[n] ? INF : score[n]);
return;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll p;
cin >> n >> m >> p;
rep(i, m) {
cin >> a[i] >> b[i] >> c[i];
c[i] -= p;
c[i] = -c[i];
g[a[i]].emplace_back(b[i], c[i]);
}
bf();
if (finfv[n]) {
cout << -1 << endl;
} else {
cout << max(-score[n], 0LL) << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
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 vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(ll x, ll y) {
ll r = 1;
while (y) {
if (y & 1)
r *= x;
x *= x;
y >>= 1;
}
return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
ll r = 1;
while (y) {
if (y & 1)
r *= x;
x *= x;
r %= m;
x %= m;
y >>= 1;
}
return r;
}
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;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
const ll ME = 5050;
const ll MV = 5050;
vector<vp> g(2600);
ll n, m;
vll a(ME), b(ME), c(ME), score(ME);
ll ans = -INF;
vi finfv(MV, 0);
void bf() {
score[1] = 0;
repr(i, 2, n + 1) { score[i] = INF; }
rep(i, n - 1) {
bool finf = 0;
rep(j, m) {
ll u = a[j];
ll v = b[j];
if (score[v] > score[u] + c[j]) {
score[v] = score[u] + c[j];
finf = 1;
}
}
if (!finf) {
ans = score[n];
return;
}
dump(score);
}
rep(i, n) {
rep(j, m) {
ll u = a[j];
ll v = b[j];
if (score[u] >= INF / 2)
continue;
if (score[v] > score[u] + c[j]) {
score[v] = score[u] + c[j];
finfv[v] = 1;
}
if (finfv[u]) {
finfv[v] = 1;
}
}
dump(score);
dump(finfv);
}
ans = (finfv[n] ? INF : score[n]);
return;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll p;
cin >> n >> m >> p;
rep(i, m) {
cin >> a[i] >> b[i] >> c[i];
c[i] -= p;
c[i] = -c[i];
g[a[i]].emplace_back(b[i], c[i]);
}
bf();
if (finfv[n]) {
cout << -1 << endl;
} else {
cout << max(-score[n], 0LL) << endl;
}
return 0;
} | replace | 62 | 64 | 62 | 64 | 0 | |
p02949 | 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;
const int INF = 1001001001;
vector<int> to[2505];
vector<int> rto[2505];
bool reachableFrom1[2505];
bool reachableToN[2505];
bool ok[2505];
void dfs(int v) {
if (reachableFrom1[v])
return;
reachableFrom1[v] = true;
for (int u : to[v]) {
dfs(u);
}
}
void rdfs(int v) {
if (reachableToN[v])
return;
reachableToN[v] = true;
for (int u : rto[v]) {
rdfs(u);
}
}
int main() {
int n, m, p;
cin >> n >> m >> p;
vector<tuple<int, int, int>> edges;
rep(i, m) {
int a, b, c;
cin >> a >> b >> c;
--a;
--b;
c -= p;
c = -c;
edges.emplace_back(a, b, c);
to[a].push_back(b);
rto[b].push_back(a);
}
dfs(0);
rdfs(n - 1);
rep(i, n) ok[i] = reachableFrom1[i] & reachableToN[i];
{ // bellman-ford
vector<int> d(n, INF);
d[0] = 0;
bool upd = true;
int step = 0;
while (upd) {
upd = false;
rep(i, n) {
int a, b, c;
tie(a, b, c) = edges[i];
if (!ok[a])
continue;
if (!ok[b])
continue;
int newD = d[a] + c;
if (newD < d[b]) {
upd = true;
d[b] = newD;
}
}
step++;
if (step > n) {
puts("-1");
return 0;
}
}
int ans = -d[n - 1];
ans = max(ans, 0);
cout << ans << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
const int INF = 1001001001;
vector<int> to[2505];
vector<int> rto[2505];
bool reachableFrom1[2505];
bool reachableToN[2505];
bool ok[2505];
void dfs(int v) {
if (reachableFrom1[v])
return;
reachableFrom1[v] = true;
for (int u : to[v]) {
dfs(u);
}
}
void rdfs(int v) {
if (reachableToN[v])
return;
reachableToN[v] = true;
for (int u : rto[v]) {
rdfs(u);
}
}
int main() {
int n, m, p;
cin >> n >> m >> p;
vector<tuple<int, int, int>> edges;
rep(i, m) {
int a, b, c;
cin >> a >> b >> c;
--a;
--b;
c -= p;
c = -c;
edges.emplace_back(a, b, c);
to[a].push_back(b);
rto[b].push_back(a);
}
dfs(0);
rdfs(n - 1);
rep(i, n) ok[i] = reachableFrom1[i] & reachableToN[i];
{ // bellman-ford
vector<int> d(n, INF);
d[0] = 0;
bool upd = true;
int step = 0;
while (upd) {
upd = false;
rep(i, m) {
int a, b, c;
tie(a, b, c) = edges[i];
if (!ok[a])
continue;
if (!ok[b])
continue;
int newD = d[a] + c;
if (newD < d[b]) {
upd = true;
d[b] = newD;
}
}
step++;
if (step > n) {
puts("-1");
return 0;
}
}
int ans = -d[n - 1];
ans = max(ans, 0);
cout << ans << endl;
}
return 0;
}
| replace | 54 | 55 | 54 | 55 | 0 | |
p02949 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
struct Edge {
int to;
int cost;
Edge(int t, int c) : to(t), cost(c) {}
};
typedef vector<vector<Edge>> AdjList;
AdjList graph;
vector<int> dist;
vector<int> par;
bool bellman_ford(int n, int s) { // n: number of edges, s: source
bool negloop = false;
dist = vector<int>(n, inf);
dist[s] = 0;
for (int i = 0; i < n; i++) {
for (int v = 0; v < n; v++) {
for (int k = 0; k < graph[v].size(); k++) {
Edge e = graph[v][k];
if (dist[v] != inf && dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
par[e.to] = v;
if (i == n - 1) {
dist[e.to] = -inf;
negloop = true;
}
}
}
}
}
return negloop;
}
vector<int> buildPath(int t) {
vector<int> path;
for (int u = t; u >= 0; u = par[u])
path.push_back(u);
reverse(path.begin(), path.end());
return path;
}
int main() {
int N, M, P;
cin >> N >> M >> P;
graph = AdjList(N);
par = vector<int>(N, -1);
rep(i, M) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
graph[a].push_back(Edge(b, -(c - P)));
}
bool ret = bellman_ford(N, 0);
if (ret) {
vector<int> path = buildPath(N - 1);
for (int p : path) {
if (dist[p] == -inf) {
cout << -1 << endl;
return 0;
}
}
}
cout << max(0, -dist[N - 1]) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
struct Edge {
int to;
int cost;
Edge(int t, int c) : to(t), cost(c) {}
};
typedef vector<vector<Edge>> AdjList;
AdjList graph;
vector<int> dist;
vector<int> par;
bool bellman_ford(int n, int s) { // n: number of edges, s: source
bool negloop = false;
dist = vector<int>(n, inf);
dist[s] = 0;
for (int i = 0; i < n; i++) {
for (int v = 0; v < n; v++) {
for (int k = 0; k < graph[v].size(); k++) {
Edge e = graph[v][k];
if (dist[v] != inf && dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
par[e.to] = v;
if (i == n - 1) {
dist[e.to] = -inf;
negloop = true;
}
}
}
}
}
return negloop;
}
vector<int> buildPath(int t) {
vector<int> path;
for (int u = t; u >= 0; u = par[u])
path.push_back(u);
reverse(path.begin(), path.end());
return path;
}
int main() {
int N, M, P;
cin >> N >> M >> P;
graph = AdjList(N);
par = vector<int>(N, -1);
rep(i, M) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
graph[a].push_back(Edge(b, -(c - P)));
}
bool ret = bellman_ford(N, 0);
if (ret) {
vector<int> path;
for (int u = N - 1; u >= 0 && path.size() < N; u = par[u])
path.push_back(u);
reverse(path.begin(), path.end());
for (int p : path) {
if (dist[p] == -inf) {
cout << -1 << endl;
return 0;
}
}
}
cout << max(0, -dist[N - 1]) << endl;
return 0;
}
| replace | 63 | 64 | 63 | 67 | TLE | |
p02949 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = int64_t;
ll const MM = 1000000007;
#define pv(val) cerr << #val << '=' << (val) << endl
#define pl cerr << '@' << __LINE__ << endl
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, b, e) for (ll i = (b); i < (e); i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
template <class T> ostream &operator<<(ostream &os, vector<T> const &vec);
template <class T, size_t S>
ostream &operator<<(ostream &os, array<T, S> const &arr);
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p);
template <class... Args>
ostream &operator<<(ostream &os, tuple<Args...> const &t);
template <class T> ostream &operator<<(ostream &os, vector<T> const &vec) {
if (vec.empty()) {
os << "{}";
} else {
os << '{';
for (size_t i = 0; i < vec.size() - 1; i++)
os << vec[i] << ", ";
os << vec.back() << '}';
}
return os;
}
template <class T, size_t S>
ostream &operator<<(ostream &os, array<T, S> const &arr) {
if (arr.empty()) {
os << "{}";
} else {
os << '{';
for (size_t i = 0; i < arr.size() - 1; i++)
os << arr[i] << ", ";
os << arr.back() << '}';
}
return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
os << '(' << p.first << ", " << p.second << ')';
return os;
}
template <size_t I, class... Args>
typename enable_if<sizeof...(Args) == (I + 1)>::type
print_tuple(ostream &os, tuple<Args...> const &t) {
os << get<I>(t);
}
template <size_t I, class... Args>
typename enable_if<sizeof...(Args) != (I + 1)>::type
print_tuple(ostream &os, tuple<Args...> const &t) {
os << get<I>(t) << ", ";
print_tuple<I + 1, Args...>(os, t);
}
template <class... Args>
ostream &operator<<(ostream &os, tuple<Args...> const &t) {
os << '(';
print_tuple<0, Args...>(os, t);
os << ')';
return os;
}
namespace math {
ll gcd(ll u, ll v) {
while (v != 0) {
ll r = u % v;
u = v;
v = r;
}
return u;
}
ll lcm(ll m, ll n) {
if ((0 == m) || (0 == n))
return 0;
return ((m / math::gcd(m, n)) * n);
}
vector<ll> divisor(ll n) {
vector<ll> ret;
for (ll 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;
}
set<ll> divisor_set(ll n) {
set<ll> ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.insert(i);
if (i * i != n)
ret.insert(n / i);
}
}
return ret;
}
// https://qiita.com/ofutonfuton/items/92b1a6f4a7775f00b6ae
struct Combination {
vector<ll> fac;
vector<ll> ifac;
Combination() : fac(300001), ifac(300001) {
fac[0] = 1;
ifac[0] = 1;
for (ll i = 0; i < 300000; i++) {
fac[i + 1] = fac[i] * (i + 1) % MM;
ifac[i + 1] = ifac[i] * this->mpow(i + 1, MM - 2) % MM;
}
}
static ll mpow(ll x, ll n) {
ll ans = 1;
while (n != 0) {
if (n & 1)
ans = ans * x % MM;
x = x * x % MM;
n = n >> 1;
}
return ans;
}
ll operator()(ll a, ll b) const { return this->comb(a, b); }
ll comb(ll a, ll b) const {
if (a == 0 && b == 0)
return 1;
if (a < b || a < 0 || b < 0)
return 0;
ll const tmp = ifac[a - b] * ifac[b] % MM;
return tmp * fac[a] % MM;
}
} comb;
} // namespace math
struct UnionFind {
vector<ll> data;
UnionFind(ll size) : data(size, -1) {}
bool unionSet(ll x, ll y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool findSet(ll x, ll y) { return root(x) == root(y); }
ll root(ll x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
ll size(ll x) { return -data[root(x)]; }
};
ll bound(function<bool(ll)> f, ll ng, ll ok) {
while (abs(ng - ok) > 1) {
ll mid = (ng + ok) / 2;
if (f(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
int main(void) {
ll N, M, P;
cin >> N >> M >> P;
vector<tuple<ll, ll, ll>> e(M);
vector<vector<ll>> adj(M), radj(M);
rep(j, M) {
ll A, B, C;
cin >> A >> B >> C;
A--, B--;
e[j] = {A, B, -C + P};
adj[A].push_back(B);
radj[B].push_back(A);
}
ll const inf = 0.1 * numeric_limits<ll>::max();
vector<ll> l(N, inf);
l[0] = 0;
vector<bool> r(N, false);
vector<bool> r2(N, false);
vector<ll> stk;
stk.push_back(0);
vector<bool> visited(N, false);
while (!stk.empty()) {
ll const t = stk.back();
stk.pop_back();
if (visited[t])
continue;
visited[t] = true;
r[t] = true;
for (ll const v : adj[t])
stk.push_back(v);
}
stk.push_back(N - 1);
fill(all(visited), false);
while (!stk.empty()) {
ll const t = stk.back();
stk.pop_back();
if (visited[t])
continue;
visited[t] = true;
r2[t] = true;
for (ll const v : radj[t])
stk.push_back(v);
}
rep(i, N) r[i] = r[i] && r2[i];
rep(i, N) rep(j, M) {
ll A, B, W;
tie(A, B, W) = e[j];
if (!r[A] || !r[B])
continue;
l[B] = min(l[B], l[A] + W);
}
bool neg = false;
rep(j, M) {
ll A, B, W;
tie(A, B, W) = e[j];
if (!r[A] || !r[B])
continue;
if (l[A] + W < l[B])
neg = true;
}
if (neg) {
cout << -1 << endl;
} else {
cout << max((ll)(0), -l[N - 1]) << endl;
}
return 0;
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = int64_t;
ll const MM = 1000000007;
#define pv(val) cerr << #val << '=' << (val) << endl
#define pl cerr << '@' << __LINE__ << endl
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, b, e) for (ll i = (b); i < (e); i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
template <class T> ostream &operator<<(ostream &os, vector<T> const &vec);
template <class T, size_t S>
ostream &operator<<(ostream &os, array<T, S> const &arr);
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p);
template <class... Args>
ostream &operator<<(ostream &os, tuple<Args...> const &t);
template <class T> ostream &operator<<(ostream &os, vector<T> const &vec) {
if (vec.empty()) {
os << "{}";
} else {
os << '{';
for (size_t i = 0; i < vec.size() - 1; i++)
os << vec[i] << ", ";
os << vec.back() << '}';
}
return os;
}
template <class T, size_t S>
ostream &operator<<(ostream &os, array<T, S> const &arr) {
if (arr.empty()) {
os << "{}";
} else {
os << '{';
for (size_t i = 0; i < arr.size() - 1; i++)
os << arr[i] << ", ";
os << arr.back() << '}';
}
return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
os << '(' << p.first << ", " << p.second << ')';
return os;
}
template <size_t I, class... Args>
typename enable_if<sizeof...(Args) == (I + 1)>::type
print_tuple(ostream &os, tuple<Args...> const &t) {
os << get<I>(t);
}
template <size_t I, class... Args>
typename enable_if<sizeof...(Args) != (I + 1)>::type
print_tuple(ostream &os, tuple<Args...> const &t) {
os << get<I>(t) << ", ";
print_tuple<I + 1, Args...>(os, t);
}
template <class... Args>
ostream &operator<<(ostream &os, tuple<Args...> const &t) {
os << '(';
print_tuple<0, Args...>(os, t);
os << ')';
return os;
}
namespace math {
ll gcd(ll u, ll v) {
while (v != 0) {
ll r = u % v;
u = v;
v = r;
}
return u;
}
ll lcm(ll m, ll n) {
if ((0 == m) || (0 == n))
return 0;
return ((m / math::gcd(m, n)) * n);
}
vector<ll> divisor(ll n) {
vector<ll> ret;
for (ll 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;
}
set<ll> divisor_set(ll n) {
set<ll> ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.insert(i);
if (i * i != n)
ret.insert(n / i);
}
}
return ret;
}
// https://qiita.com/ofutonfuton/items/92b1a6f4a7775f00b6ae
struct Combination {
vector<ll> fac;
vector<ll> ifac;
Combination() : fac(300001), ifac(300001) {
fac[0] = 1;
ifac[0] = 1;
for (ll i = 0; i < 300000; i++) {
fac[i + 1] = fac[i] * (i + 1) % MM;
ifac[i + 1] = ifac[i] * this->mpow(i + 1, MM - 2) % MM;
}
}
static ll mpow(ll x, ll n) {
ll ans = 1;
while (n != 0) {
if (n & 1)
ans = ans * x % MM;
x = x * x % MM;
n = n >> 1;
}
return ans;
}
ll operator()(ll a, ll b) const { return this->comb(a, b); }
ll comb(ll a, ll b) const {
if (a == 0 && b == 0)
return 1;
if (a < b || a < 0 || b < 0)
return 0;
ll const tmp = ifac[a - b] * ifac[b] % MM;
return tmp * fac[a] % MM;
}
} comb;
} // namespace math
struct UnionFind {
vector<ll> data;
UnionFind(ll size) : data(size, -1) {}
bool unionSet(ll x, ll y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool findSet(ll x, ll y) { return root(x) == root(y); }
ll root(ll x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
ll size(ll x) { return -data[root(x)]; }
};
ll bound(function<bool(ll)> f, ll ng, ll ok) {
while (abs(ng - ok) > 1) {
ll mid = (ng + ok) / 2;
if (f(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
int main(void) {
ll N, M, P;
cin >> N >> M >> P;
vector<tuple<ll, ll, ll>> e(M);
vector<vector<ll>> adj(N), radj(N);
rep(j, M) {
ll A, B, C;
cin >> A >> B >> C;
A--, B--;
e[j] = {A, B, -C + P};
adj[A].push_back(B);
radj[B].push_back(A);
}
ll const inf = 0.1 * numeric_limits<ll>::max();
vector<ll> l(N, inf);
l[0] = 0;
vector<bool> r(N, false);
vector<bool> r2(N, false);
vector<ll> stk;
stk.push_back(0);
vector<bool> visited(N, false);
while (!stk.empty()) {
ll const t = stk.back();
stk.pop_back();
if (visited[t])
continue;
visited[t] = true;
r[t] = true;
for (ll const v : adj[t])
stk.push_back(v);
}
stk.push_back(N - 1);
fill(all(visited), false);
while (!stk.empty()) {
ll const t = stk.back();
stk.pop_back();
if (visited[t])
continue;
visited[t] = true;
r2[t] = true;
for (ll const v : radj[t])
stk.push_back(v);
}
rep(i, N) r[i] = r[i] && r2[i];
rep(i, N) rep(j, M) {
ll A, B, W;
tie(A, B, W) = e[j];
if (!r[A] || !r[B])
continue;
l[B] = min(l[B], l[A] + W);
}
bool neg = false;
rep(j, M) {
ll A, B, W;
tie(A, B, W) = e[j];
if (!r[A] || !r[B])
continue;
if (l[A] + W < l[B])
neg = true;
}
if (neg) {
cout << -1 << endl;
} else {
cout << max((ll)(0), -l[N - 1]) << endl;
}
return 0;
}
| replace | 207 | 208 | 207 | 208 | 0 | |
p02949 | C++ | Runtime Error | #include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long int INF = 99999999999999;
// INFを宣言すること。Edgeに辺の情報を入れよう。
class Graph {
// メンバ変数の説明
// Edge:一つ目の番号が辺が出る方向
// MaxV,MaxE:頂点数と辺の数
// Result系:各メソッドの答え
// InfinteUpdate:BellmanFordで各番号の頂点までの最短経路が更新されるか
//
public:
vector<pair<long long int, long long int>> *Edge;
long long int MaxV, MaxE;
long long int *BellmanFordResult, *DijkstraResult, **WarshallFloydResult;
bool *InfiniteUpdate;
bool NegativeLoop = false;
Graph(long long int InputV, long long int InputE) {
MaxV = InputV;
MaxE = InputE;
Edge = new vector<pair<long long int, long long int>>[InputV + 1];
}
void BellmanFord(long long int Start) {
BellmanFordResult = new long long int[MaxV];
InfiniteUpdate = new bool[MaxV];
for (int i = 0; i < MaxV; i++) {
BellmanFordResult[i] = INF;
InfiniteUpdate[i] = false;
}
BellmanFordResult[Start] = 0;
long long int turn = 0;
while (true) {
turn++;
bool update = false;
for (int i = 0; i < MaxE; i++) {
for (int j = 0; j < Edge[i].size(); j++) {
pair<long long int, long long int> e = Edge[i][j];
if (turn >= MaxV && InfiniteUpdate[i]) {
InfiniteUpdate[e.first] = true;
}
if (BellmanFordResult[i] != INF &&
BellmanFordResult[e.first] > BellmanFordResult[i] + e.second) {
BellmanFordResult[e.first] = BellmanFordResult[i] + e.second;
update = true;
if (turn >= MaxV) {
InfiniteUpdate[e.first] = true;
}
}
}
}
if (!update) {
NegativeLoop = false;
break;
}
if (turn >= 2 * MaxV - 1) {
NegativeLoop = true;
break;
}
}
}
void Dijkstra(long long int Start) {
DijkstraResult = new long long int[MaxV];
priority_queue<pair<long long int, long long int>,
vector<pair<long long int, long long int>>,
greater<pair<long long int, long long int>>>
que;
fill(DijkstraResult, DijkstraResult + MaxV, INF);
DijkstraResult[Start] = 0;
que.push(pair<long long int, long long int>(0, Start));
while (!que.empty()) {
pair<long long int, long long int> p = que.top();
que.pop();
long long int v = p.second;
if (DijkstraResult[v] < p.first)
continue;
for (int i = 0; i < Edge[v].size(); i++) {
pair<long long int, long long int> e = Edge[v][i];
if (DijkstraResult[e.first] > DijkstraResult[v] + e.second) {
DijkstraResult[e.first] = DijkstraResult[v] + e.second;
que.push(pair<long long int, long long int>(DijkstraResult[e.first],
e.first));
}
}
}
}
void WarshallFloyd() {
WarshallFloydResult = new long long int *[MaxV];
for (int i = 0; i < MaxV; i++) {
WarshallFloydResult[i] = new long long int[MaxV];
}
for (int i = 0; i < MaxV; i++) {
for (int j = 0; j < Edge[i].size(); j++) {
WarshallFloydResult[i][Edge[i][j].first] = Edge[i][j].second;
}
}
for (int k = 0; k < MaxV; k++) {
for (int i = 0; i < MaxV; i++) {
for (int j = 0; j < MaxV; j++) {
WarshallFloydResult[i][j] =
min(WarshallFloydResult[i][j],
WarshallFloydResult[i][k] + WarshallFloydResult[k][j]);
}
}
}
}
};
int main() {
long long int N, M, P;
cin >> N >> M >> P;
Graph graph(N, M);
for (int i = 0; i < M; i++) {
long long int from, to, cost;
cin >> from >> to >> cost;
from--;
to--;
cost -= P;
cost = -cost;
graph.Edge[from].push_back({to, cost});
}
graph.BellmanFord(0);
if (graph.InfiniteUpdate[N - 1]) {
cout << -1;
} else {
cout << max(0LL, -graph.BellmanFordResult[N - 1]);
}
} | #include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long int INF = 99999999999999;
// INFを宣言すること。Edgeに辺の情報を入れよう。
class Graph {
// メンバ変数の説明
// Edge:一つ目の番号が辺が出る方向
// MaxV,MaxE:頂点数と辺の数
// Result系:各メソッドの答え
// InfinteUpdate:BellmanFordで各番号の頂点までの最短経路が更新されるか
//
public:
vector<pair<long long int, long long int>> *Edge;
long long int MaxV, MaxE;
long long int *BellmanFordResult, *DijkstraResult, **WarshallFloydResult;
bool *InfiniteUpdate;
bool NegativeLoop = false;
Graph(long long int InputV, long long int InputE) {
MaxV = InputV;
MaxE = InputE;
Edge = new vector<pair<long long int, long long int>>[InputV + 1];
}
void BellmanFord(long long int Start) {
BellmanFordResult = new long long int[MaxV];
InfiniteUpdate = new bool[MaxV];
for (int i = 0; i < MaxV; i++) {
BellmanFordResult[i] = INF;
InfiniteUpdate[i] = false;
}
BellmanFordResult[Start] = 0;
long long int turn = 0;
while (true) {
turn++;
bool update = false;
for (int i = 0; i < MaxV; i++) {
for (int j = 0; j < Edge[i].size(); j++) {
pair<long long int, long long int> e = Edge[i][j];
if (turn >= MaxV && InfiniteUpdate[i]) {
InfiniteUpdate[e.first] = true;
}
if (BellmanFordResult[i] != INF &&
BellmanFordResult[e.first] > BellmanFordResult[i] + e.second) {
BellmanFordResult[e.first] = BellmanFordResult[i] + e.second;
update = true;
if (turn >= MaxV) {
InfiniteUpdate[e.first] = true;
}
}
}
}
if (!update) {
NegativeLoop = false;
break;
}
if (turn >= 2 * MaxV - 1) {
NegativeLoop = true;
break;
}
}
}
void Dijkstra(long long int Start) {
DijkstraResult = new long long int[MaxV];
priority_queue<pair<long long int, long long int>,
vector<pair<long long int, long long int>>,
greater<pair<long long int, long long int>>>
que;
fill(DijkstraResult, DijkstraResult + MaxV, INF);
DijkstraResult[Start] = 0;
que.push(pair<long long int, long long int>(0, Start));
while (!que.empty()) {
pair<long long int, long long int> p = que.top();
que.pop();
long long int v = p.second;
if (DijkstraResult[v] < p.first)
continue;
for (int i = 0; i < Edge[v].size(); i++) {
pair<long long int, long long int> e = Edge[v][i];
if (DijkstraResult[e.first] > DijkstraResult[v] + e.second) {
DijkstraResult[e.first] = DijkstraResult[v] + e.second;
que.push(pair<long long int, long long int>(DijkstraResult[e.first],
e.first));
}
}
}
}
void WarshallFloyd() {
WarshallFloydResult = new long long int *[MaxV];
for (int i = 0; i < MaxV; i++) {
WarshallFloydResult[i] = new long long int[MaxV];
}
for (int i = 0; i < MaxV; i++) {
for (int j = 0; j < Edge[i].size(); j++) {
WarshallFloydResult[i][Edge[i][j].first] = Edge[i][j].second;
}
}
for (int k = 0; k < MaxV; k++) {
for (int i = 0; i < MaxV; i++) {
for (int j = 0; j < MaxV; j++) {
WarshallFloydResult[i][j] =
min(WarshallFloydResult[i][j],
WarshallFloydResult[i][k] + WarshallFloydResult[k][j]);
}
}
}
}
};
int main() {
long long int N, M, P;
cin >> N >> M >> P;
Graph graph(N, M);
for (int i = 0; i < M; i++) {
long long int from, to, cost;
cin >> from >> to >> cost;
from--;
to--;
cost -= P;
cost = -cost;
graph.Edge[from].push_back({to, cost});
}
graph.BellmanFord(0);
if (graph.InfiniteUpdate[N - 1]) {
cout << -1;
} else {
cout << max(0LL, -graph.BellmanFordResult[N - 1]);
}
} | replace | 50 | 51 | 50 | 51 | 0 | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
#define vi vector<ll>
#define vvi vector<vi>
#define llINF 1e18
#define ll long long
using namespace std;
bool vis[2222];
struct Edge {
ll to, cost;
};
const ll MAX_V = 2010;
vector<vector<Edge>> E(MAX_V);
vi dist(MAX_V, llINF);
bool bellman_ford(ll s) {
queue<ll> que;
que.push(s);
dist[s] = 0;
for (int i = 0; i < MAX_V; i++) {
ll n = que.size();
bool used[MAX_V] = {};
for (int j = 0; j < n; j++) {
ll now = que.front();
que.pop();
for (int k = 0; k < E[now].size(); k++) {
Edge e = E[now][k];
if (dist[e.to] > dist[now] + e.cost) {
dist[e.to] = dist[now] + e.cost;
if (i == MAX_V - 1)
if (vis[e.to])
return true;
if (used[e.to] == false)
que.push(e.to);
used[e.to] = true;
}
}
}
}
return false;
}
vvi E2(MAX_V);
int main() {
ll n, m, p;
cin >> n >> m >> p;
for (int i = 0; i < m; i++) {
ll a, b, c;
cin >> a >> b >> c;
E[--a].push_back({--b, -(c - p)});
E2[b].push_back(a);
}
queue<ll> que;
que.push(n - 1);
vis[n - 1] = true;
while (que.size()) {
ll hoge = que.front();
que.pop();
for (auto a : E2[hoge])
if (vis[a] == false) {
que.push(a);
vis[a] = true;
}
}
if (bellman_ford(0) == false) {
cout << max(0LL, -dist[n - 1]) << endl;
} else
cout << -1 << endl;
}
| #include <bits/stdc++.h>
#define vi vector<ll>
#define vvi vector<vi>
#define llINF 1e18
#define ll long long
using namespace std;
bool vis[2222];
struct Edge {
ll to, cost;
};
const ll MAX_V = 2510;
vector<vector<Edge>> E(MAX_V);
vi dist(MAX_V, llINF);
bool bellman_ford(ll s) {
queue<ll> que;
que.push(s);
dist[s] = 0;
for (int i = 0; i < MAX_V; i++) {
ll n = que.size();
bool used[MAX_V] = {};
for (int j = 0; j < n; j++) {
ll now = que.front();
que.pop();
for (int k = 0; k < E[now].size(); k++) {
Edge e = E[now][k];
if (dist[e.to] > dist[now] + e.cost) {
dist[e.to] = dist[now] + e.cost;
if (i == MAX_V - 1)
if (vis[e.to])
return true;
if (used[e.to] == false)
que.push(e.to);
used[e.to] = true;
}
}
}
}
return false;
}
vvi E2(MAX_V);
int main() {
ll n, m, p;
cin >> n >> m >> p;
for (int i = 0; i < m; i++) {
ll a, b, c;
cin >> a >> b >> c;
E[--a].push_back({--b, -(c - p)});
E2[b].push_back(a);
}
queue<ll> que;
que.push(n - 1);
vis[n - 1] = true;
while (que.size()) {
ll hoge = que.front();
que.pop();
for (auto a : E2[hoge])
if (vis[a] == false) {
que.push(a);
vis[a] = true;
}
}
if (bellman_ford(0) == false) {
cout << max(0LL, -dist[n - 1]) << endl;
} else
cout << -1 << endl;
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p02949 | C++ | Runtime Error | // Hack it and have it ;) //
/*author* Priyanshu Shrivastav (from IIT Palakkad) *
* *_ __ ___ _ ______ ___ _ ____ ___ ___| |_ *
* | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
* | | | | | | | | (_| (_) | | | \ V /| | (__| |_ *
* |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
When I wrote this, only God and I understood what I was doing
** * * * * * * * Now, only God knows * * * * * * */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr)
#define PREC \
cout.precision(10); \
cout << fixed
#define x first
#define y second
#define bg(x) " [ " << #x << " : " << (x) << " ] "
#define un(x) \
sort(x.begin(), x.end()), x.erase(unique(x.begin(), x.end()), x.end())
using ll = long long;
using ull = unsigned long long;
using ff = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
struct chash {
int operator()(pii x) const { return x.x * 31 + x.y; }
};
gp_hash_table<pii, int, chash> mp;
seed_seq seq{(uint64_t)chrono::duration_cast<chrono::nanoseconds>(
chrono::high_resolution_clock::now().time_since_epoch())
.count(),
(uint64_t)__builtin_ia32_rdtsc(),
(uint64_t)(uintptr_t)make_unique<char>().get()};
mt19937 rng(seq); // uniform_int_distribution<int> (l, h)(rng); //[low, high]
#define debug(args...) \
{ \
/* WARNING : do NOT compile this debug func calls with following flags: // \
* // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
void err(istream_iterator<string> it) {
it->empty();
cerr << " (Line : " << __LINE__ << ")" << '\n';
}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << fixed << setprecision(15) << " [ " << *it << " : " << a << " ] "
<< ' ';
err(++it, args...);
}
/*****************************************************************************/
#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
const ll inf = (ll)1e18;
long long dist[N];
int Par[N];
int n, m, p;
vector<vector<pair<int, ll>>> Adj, AdjT;
bool from1[N], from2[N];
void dfs(int u, bool vis[], vector<vector<pair<int, ll>>> &Adj) {
vis[u] = true;
for (auto v_pair : Adj[u]) {
int v = v_pair.x;
if (!vis[v])
dfs(v, vis, Adj);
}
}
bool spfa(int src, int target, bool print_path = false) {
vector<int> inQueue(n, false);
int cnt[N] = {0};
for (int i = 0; i < N; ++i)
Par[i] = -1, dist[i] = inf;
queue<int> Q;
Q.push(src);
dist[src] = 0;
inQueue[src] = true;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
inQueue[u] = false;
if (!(from1[u] && from2[u]))
continue;
for (auto vPair : Adj[u]) {
int v = vPair.x;
long long w = vPair.y;
if (dist[u] != inf && dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
Par[v] = u;
if (!inQueue[v]) {
Q.push(v);
inQueue[v] = true;
++cnt[v];
if (cnt[v] > n)
return false;
}
}
}
}
// if (print_path) {
// if (dist[target] == inf)
// cout << -1 << '\n';
// else {
// vector <int> Path;
// int v = target;
// while (true) {
// Path.push_back(v);
// if (v == src) break;
// v = Par[v];
// }
// reverse(Path.begin(), Path.end());
// for_each(Path.begin(), Path.end(), [&] (const int &v)->void { cout
// << v + 1 << ' '; }); cout << '\n';
// }
// }
return true;
}
signed main() {
IOS;
PREC;
cin >> n >> m >> p;
Adj.assign(n, vector<pair<int, ll>>());
AdjT.assign(n, vector<pair<int, ll>>());
fill(from1, from1 + n, false);
fill(from2, from2 + n, false);
for (int e = 0; e < m; ++e) {
int u, v, Ci;
cin >> u >> v >> Ci;
--u, --v;
Adj[u].emplace_back(v, -1ll * (Ci - p));
AdjT[v].emplace_back(u, -1ll * (Ci - p));
}
dfs(0, from1, Adj), dfs(n - 1, from2, AdjT);
bool ok = spfa(0, n - 1);
if (ok) {
ll ans = max(0ll, -1 * dist[n - 1]);
cout << ans << '\n';
} else
cout << -1 << '\n';
return EXIT_SUCCESS;
}
| // Hack it and have it ;) //
/*author* Priyanshu Shrivastav (from IIT Palakkad) *
* *_ __ ___ _ ______ ___ _ ____ ___ ___| |_ *
* | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
* | | | | | | | | (_| (_) | | | \ V /| | (__| |_ *
* |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
When I wrote this, only God and I understood what I was doing
** * * * * * * * Now, only God knows * * * * * * */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr)
#define PREC \
cout.precision(10); \
cout << fixed
#define x first
#define y second
#define bg(x) " [ " << #x << " : " << (x) << " ] "
#define un(x) \
sort(x.begin(), x.end()), x.erase(unique(x.begin(), x.end()), x.end())
using ll = long long;
using ull = unsigned long long;
using ff = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
struct chash {
int operator()(pii x) const { return x.x * 31 + x.y; }
};
gp_hash_table<pii, int, chash> mp;
seed_seq seq{(uint64_t)chrono::duration_cast<chrono::nanoseconds>(
chrono::high_resolution_clock::now().time_since_epoch())
.count(),
(uint64_t)__builtin_ia32_rdtsc(),
(uint64_t)(uintptr_t)make_unique<char>().get()};
mt19937 rng(seq); // uniform_int_distribution<int> (l, h)(rng); //[low, high]
#define debug(args...) \
{ \
/* WARNING : do NOT compile this debug func calls with following flags: // \
* // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
void err(istream_iterator<string> it) {
it->empty();
cerr << " (Line : " << __LINE__ << ")" << '\n';
}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << fixed << setprecision(15) << " [ " << *it << " : " << a << " ] "
<< ' ';
err(++it, args...);
}
/*****************************************************************************/
#include <bits/stdc++.h>
using namespace std;
const int N = 2510;
const ll inf = (ll)1e18;
long long dist[N];
int Par[N];
int n, m, p;
vector<vector<pair<int, ll>>> Adj, AdjT;
bool from1[N], from2[N];
void dfs(int u, bool vis[], vector<vector<pair<int, ll>>> &Adj) {
vis[u] = true;
for (auto v_pair : Adj[u]) {
int v = v_pair.x;
if (!vis[v])
dfs(v, vis, Adj);
}
}
bool spfa(int src, int target, bool print_path = false) {
vector<int> inQueue(n, false);
int cnt[N] = {0};
for (int i = 0; i < N; ++i)
Par[i] = -1, dist[i] = inf;
queue<int> Q;
Q.push(src);
dist[src] = 0;
inQueue[src] = true;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
inQueue[u] = false;
if (!(from1[u] && from2[u]))
continue;
for (auto vPair : Adj[u]) {
int v = vPair.x;
long long w = vPair.y;
if (dist[u] != inf && dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
Par[v] = u;
if (!inQueue[v]) {
Q.push(v);
inQueue[v] = true;
++cnt[v];
if (cnt[v] > n)
return false;
}
}
}
}
// if (print_path) {
// if (dist[target] == inf)
// cout << -1 << '\n';
// else {
// vector <int> Path;
// int v = target;
// while (true) {
// Path.push_back(v);
// if (v == src) break;
// v = Par[v];
// }
// reverse(Path.begin(), Path.end());
// for_each(Path.begin(), Path.end(), [&] (const int &v)->void { cout
// << v + 1 << ' '; }); cout << '\n';
// }
// }
return true;
}
signed main() {
IOS;
PREC;
cin >> n >> m >> p;
Adj.assign(n, vector<pair<int, ll>>());
AdjT.assign(n, vector<pair<int, ll>>());
fill(from1, from1 + n, false);
fill(from2, from2 + n, false);
for (int e = 0; e < m; ++e) {
int u, v, Ci;
cin >> u >> v >> Ci;
--u, --v;
Adj[u].emplace_back(v, -1ll * (Ci - p));
AdjT[v].emplace_back(u, -1ll * (Ci - p));
}
dfs(0, from1, Adj), dfs(n - 1, from2, AdjT);
bool ok = spfa(0, n - 1);
if (ok) {
ll ans = max(0ll, -1 * dist[n - 1]);
cout << ans << '\n';
} else
cout << -1 << '\n';
return EXIT_SUCCESS;
}
| replace | 73 | 74 | 73 | 74 | 0 | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define Int long long
#define int long long
// TEMPLATE
// START---------------8<---------------8<---------------8<---------------8<---------------//
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vst;
typedef vector<bool> vb;
typedef vector<ld> vld;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int>> vvi;
const int INF = (0x7FFFFFFFL);
const ll INFF = (0x7FFFFFFFFFFFFFFFL);
const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int MOD = 1e9 + 7;
const int MODD = 998244353;
const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1.0);
const double EPS = 1e-9;
const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[9] = {0, 1, 0, -1, -1, -1, 1, 1, 0};
#define ln '\n'
#define scnaf scanf
#define sacnf scanf
#define sancf scanf
#define SS(type, ...) \
type __VA_ARGS__; \
MACRO_VAR_Scan(__VA_ARGS__);
template <typename T> void MACRO_VAR_Scan(T &t) { cin >> t; }
template <typename First, typename... Rest>
void MACRO_VAR_Scan(First &first, Rest &...rest) {
cin >> first;
MACRO_VAR_Scan(rest...);
}
#define SV(type, c, n) \
vector<type> c(n); \
for (auto &i : c) \
cin >> i;
#define SVV(type, c, n, m) \
vector<vector<type>> c(n, vector<type>(m)); \
for (auto &r : c) \
for (auto &i : r) \
cin >> i;
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &j) {
o << "{" << j.first << ", " << j.second << "}";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &j) {
o << "{";
for (auto t = j.begin(); t != j.end(); ++t)
o << (t != j.begin() ? ", " : "") << *t;
o << "}";
return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &j) {
o << "{";
for (auto t = j.begin(); t != j.end(); ++t)
o << (t != j.begin() ? ", " : "") << *t;
o << "}";
return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &j) {
o << "{";
for (int i = 0; i < (int)j.size(); ++i)
o << (i > 0 ? ", " : "") << j[i];
o << "}";
return o;
}
inline int print(void) {
cout << endl;
return 0;
}
template <class Head> int print(Head &&head) {
cout << head;
print();
return 0;
}
template <class Head, class... Tail> int print(Head &&head, Tail &&...tail) {
cout << head << " ";
print(forward<Tail>(tail)...);
return 0;
}
inline int debug(void) {
cerr << endl;
return 0;
}
template <class Head> int debug(Head &&head) {
cerr << head;
debug();
return 0;
}
template <class Head, class... Tail> int debug(Head &&head, Tail &&...tail) {
cerr << head << " ";
debug(forward<Tail>(tail)...);
return 0;
}
template <typename T> void PA(T &a) {
int ASIZE = sizeof(a) / sizeof(a[0]);
for (int ii = 0; ii < ASIZE; ++ii) {
cout << a[ii] << " \n"[ii == ASIZE - 1];
}
}
template <typename T> void PV(T &v) {
int VSIZE = v.size();
for (int ii = 0; ii < VSIZE; ++ii) {
cout << v[ii] << " \n"[ii == VSIZE - 1];
}
}
#define ER(x) cerr << #x << " = " << (x) << endl;
#define ERV(v) \
{ \
cerr << #v << " : "; \
for (const auto &xxx : v) { \
cerr << xxx << " "; \
} \
cerr << "\n"; \
}
inline int YES(bool x) {
cout << ((x) ? "YES" : "NO") << endl;
return 0;
}
inline int Yes(bool x) {
cout << ((x) ? "Yes" : "No") << endl;
return 0;
}
inline int yes(bool x) {
cout << ((x) ? "yes" : "no") << endl;
return 0;
}
inline int yES(bool x) {
cout << ((x) ? "yES" : "nO") << endl;
return 0;
}
inline int Yay(bool x) {
cout << ((x) ? "Yay!" : ":(") << endl;
return 0;
}
template <typename A, typename B> void sankou(bool x, A a, B b) {
cout << ((x) ? (a) : (b)) << endl;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _REP(i, n) REPI(i, 0, n)
#define REPI(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define REP(...) _overload3(__VA_ARGS__, REPI, _REP, )(__VA_ARGS__)
#define _RREP(i, n) RREPI(i, n, 0)
#define RREPI(i, a, b) for (ll i = ll(a); i >= ll(b); --i)
#define RREP(...) _overload3(__VA_ARGS__, RREPI, _RREP, )(__VA_ARGS__)
#define EACH(e, v) for (auto &e : v)
#define PERM(v) \
sort((v).begin(), (v).end()); \
for (bool c##p = 1; c##p; c##p = next_permutation((v).begin(), (v).end()))
#define ADD(a, b) a = (a + ll(b)) % MOD
#define MUL(a, b) a = (a * ll(b)) % MOD
inline ll MOP(ll x, ll n, ll m = MOD) {
ll r = 1;
while (n > 0) {
if (n & 1)
(r *= x) %= m;
(x *= x) %= m;
n >>= 1;
}
return r;
}
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); }
inline ll POW(ll a, ll b) {
ll c = 1ll;
do {
if (b & 1)
c *= 1ll * a;
a *= 1ll * a;
} while (b >>= 1);
return c;
}
template <typename T, typename A, typename B>
inline bool between(T x, A a, B b) {
return ((a <= x) && (x < b));
}
template <class T> inline T sqr(T x) { return x * x; }
template <typename A, typename B> inline bool chmax(A &a, const B &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename A, typename B> inline bool chmin(A &a, const B &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define tmax(x, y, z) max((x), max((y), (z)))
#define tmin(x, y, z) min((x), min((y), (z)))
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define EXIST(s, e) (find((s).begin(), (s).end(), (e)) != (s).end())
#define EXISTST(s, c) (((s).find(c)) != string::npos)
#define POSL(x, val) (lower_bound(x.begin(), x.end(), val) - x.begin())
#define POSU(x, val) (upper_bound(x.begin(), x.end(), val) - x.begin())
#define GEQ(x, val) (int)(x).size() - POSL((x), (val))
#define GREATER(x, val) (int)(x).size() - POSU((x), (val))
#define LEQ(x, val) POSU((x), (val))
#define LESS(x, val) POSL((x), (val))
#define SZV(a) int((a).size())
#define SZA(a) sizeof(a) / sizeof(a[0])
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MEMINF(a) memset(a, 0x3f, sizeof(a))
#define FILL(a, b) memset(a, b, sizeof(a))
#define UNIQUE(v) \
sort((v).begin(), (v).end()); \
(v).erase(unique((v).begin(), (v).end()), (v).end())
struct abracadabra {
abracadabra() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
cerr << fixed << setprecision(5);
};
} ABRACADABRA;
// TEMPLATE
// END---------------8<---------------8<---------------8<---------------8<---------------//
/*
・グラフ
> Dijkstra
> BellmanFord
> Warshall-Floyd
> Kruskal
[応用] 単一終点最短路問題は, すべての有向辺を逆向きに張り替えると,
単一始点最短路問題に帰着できる. [使用例] Graph<int> g(N); //
頂点数N, 重さの型がintのグラフを宣言 add_edge(g,a,b,c); // グラフgに,
aからbへの重さcの無向辺を追加 add_arc(g,a,b,c); // グラフgに,
aからbへの重さcの有向辺を追加 add_to_edges(edges,a,b,c); // 辺集合edgesに,
始点a, 終点b, 重さcの辺を追加
*/
template <typename T> struct Edge {
int from, to;
T weight;
Edge() : from(0), to(0), weight(0) {}
Edge(int f, int t, T w) : from(f), to(t), weight(w) {}
};
template <typename T> using Edges = vector<Edge<T>>;
template <typename T> using Graph = vector<Edges<T>>;
template <typename T> void add_edge(Graph<T> &g, int from, int to, T w = 1) {
g[from].emplace_back(from, to, w);
g[to].emplace_back(to, from, w);
}
template <typename T> void add_arc(Graph<T> &g, int from, int to, T w = 1) {
g[from].emplace_back(from, to, w);
}
template <typename T>
void add_to_edges(Edges<T> &e, int from, int to, T w = 1) {
e.emplace_back(from, to, w);
}
vector<vector<int>> gyaku;
// bool ok[2666];
vector<bool> ok;
/*
・BellmanFord
> O(EV) [E:辺の数, V:頂点の数]
[備考]
グラフ(負辺が存在してもよい)に対する単一始点全点間最短路を求めるアルゴリズム
負閉路が存在しているかの判定も可能 -> 存在していたら空列を返す
[注意] 結果を足し合わせる際, INFの大きさに注意
[使用例]
Edges<int> edges; // 全ての辺 (重さ: int)
add_to_edges(edges,a,b,c); // 辺集合edgesに, 始点a, 終点b,
重さcの辺を追加 auto bf = BellmanFord(edges,V,s); // 辺edges,
頂点数Vのグラフにおける, 始点sからの最短路
*/
template <typename T>
vector<T> BellmanFord(Edges<T> &edges, int vertex, int from) {
const auto INF = numeric_limits<T>::max() / 1000;
vector<T> dist(vertex, INF);
dist[from] = 0;
for (int i = 0; i < vertex - 1; ++i) {
for (auto &e : edges) {
if (dist[e.from] == INF)
continue;
dist[e.to] = min(dist[e.to], dist[e.from] + e.weight);
}
}
for (auto &e : edges) {
if (dist[e.from] == INF)
continue;
if (dist[e.from] + e.weight < dist[e.to] and ok[e.from])
return vector<T>();
}
return dist;
}
void dfs(int cur, int prv) {
ok[cur] = true;
EACH(nxt, gyaku[cur]) if (nxt != prv) { dfs(nxt, cur); }
}
signed main() {
SS(int, N, M, P);
Edges<ll> edges;
gyaku.resize(N);
REP(i, M) {
SS(int, A, B, C);
--A, --B;
C -= P;
add_to_edges(edges, A, B, -C);
gyaku[B].PB(A);
}
ok.assign(N, false);
dfs(N - 1, -1);
auto bf = BellmanFord(edges, N, 0);
if (bf.size() == 0) {
print(-1);
} else {
print(max<ll>(-bf[N - 1], 0));
}
// for (int i = 0; i < N; ++i) {
// print(i, ok[i]);
// }
} | #include <bits/stdc++.h>
using namespace std;
#define Int long long
#define int long long
// TEMPLATE
// START---------------8<---------------8<---------------8<---------------8<---------------//
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vst;
typedef vector<bool> vb;
typedef vector<ld> vld;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int>> vvi;
const int INF = (0x7FFFFFFFL);
const ll INFF = (0x7FFFFFFFFFFFFFFFL);
const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int MOD = 1e9 + 7;
const int MODD = 998244353;
const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1.0);
const double EPS = 1e-9;
const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[9] = {0, 1, 0, -1, -1, -1, 1, 1, 0};
#define ln '\n'
#define scnaf scanf
#define sacnf scanf
#define sancf scanf
#define SS(type, ...) \
type __VA_ARGS__; \
MACRO_VAR_Scan(__VA_ARGS__);
template <typename T> void MACRO_VAR_Scan(T &t) { cin >> t; }
template <typename First, typename... Rest>
void MACRO_VAR_Scan(First &first, Rest &...rest) {
cin >> first;
MACRO_VAR_Scan(rest...);
}
#define SV(type, c, n) \
vector<type> c(n); \
for (auto &i : c) \
cin >> i;
#define SVV(type, c, n, m) \
vector<vector<type>> c(n, vector<type>(m)); \
for (auto &r : c) \
for (auto &i : r) \
cin >> i;
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &j) {
o << "{" << j.first << ", " << j.second << "}";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &j) {
o << "{";
for (auto t = j.begin(); t != j.end(); ++t)
o << (t != j.begin() ? ", " : "") << *t;
o << "}";
return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &j) {
o << "{";
for (auto t = j.begin(); t != j.end(); ++t)
o << (t != j.begin() ? ", " : "") << *t;
o << "}";
return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &j) {
o << "{";
for (int i = 0; i < (int)j.size(); ++i)
o << (i > 0 ? ", " : "") << j[i];
o << "}";
return o;
}
inline int print(void) {
cout << endl;
return 0;
}
template <class Head> int print(Head &&head) {
cout << head;
print();
return 0;
}
template <class Head, class... Tail> int print(Head &&head, Tail &&...tail) {
cout << head << " ";
print(forward<Tail>(tail)...);
return 0;
}
inline int debug(void) {
cerr << endl;
return 0;
}
template <class Head> int debug(Head &&head) {
cerr << head;
debug();
return 0;
}
template <class Head, class... Tail> int debug(Head &&head, Tail &&...tail) {
cerr << head << " ";
debug(forward<Tail>(tail)...);
return 0;
}
template <typename T> void PA(T &a) {
int ASIZE = sizeof(a) / sizeof(a[0]);
for (int ii = 0; ii < ASIZE; ++ii) {
cout << a[ii] << " \n"[ii == ASIZE - 1];
}
}
template <typename T> void PV(T &v) {
int VSIZE = v.size();
for (int ii = 0; ii < VSIZE; ++ii) {
cout << v[ii] << " \n"[ii == VSIZE - 1];
}
}
#define ER(x) cerr << #x << " = " << (x) << endl;
#define ERV(v) \
{ \
cerr << #v << " : "; \
for (const auto &xxx : v) { \
cerr << xxx << " "; \
} \
cerr << "\n"; \
}
inline int YES(bool x) {
cout << ((x) ? "YES" : "NO") << endl;
return 0;
}
inline int Yes(bool x) {
cout << ((x) ? "Yes" : "No") << endl;
return 0;
}
inline int yes(bool x) {
cout << ((x) ? "yes" : "no") << endl;
return 0;
}
inline int yES(bool x) {
cout << ((x) ? "yES" : "nO") << endl;
return 0;
}
inline int Yay(bool x) {
cout << ((x) ? "Yay!" : ":(") << endl;
return 0;
}
template <typename A, typename B> void sankou(bool x, A a, B b) {
cout << ((x) ? (a) : (b)) << endl;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _REP(i, n) REPI(i, 0, n)
#define REPI(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define REP(...) _overload3(__VA_ARGS__, REPI, _REP, )(__VA_ARGS__)
#define _RREP(i, n) RREPI(i, n, 0)
#define RREPI(i, a, b) for (ll i = ll(a); i >= ll(b); --i)
#define RREP(...) _overload3(__VA_ARGS__, RREPI, _RREP, )(__VA_ARGS__)
#define EACH(e, v) for (auto &e : v)
#define PERM(v) \
sort((v).begin(), (v).end()); \
for (bool c##p = 1; c##p; c##p = next_permutation((v).begin(), (v).end()))
#define ADD(a, b) a = (a + ll(b)) % MOD
#define MUL(a, b) a = (a * ll(b)) % MOD
inline ll MOP(ll x, ll n, ll m = MOD) {
ll r = 1;
while (n > 0) {
if (n & 1)
(r *= x) %= m;
(x *= x) %= m;
n >>= 1;
}
return r;
}
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); }
inline ll POW(ll a, ll b) {
ll c = 1ll;
do {
if (b & 1)
c *= 1ll * a;
a *= 1ll * a;
} while (b >>= 1);
return c;
}
template <typename T, typename A, typename B>
inline bool between(T x, A a, B b) {
return ((a <= x) && (x < b));
}
template <class T> inline T sqr(T x) { return x * x; }
template <typename A, typename B> inline bool chmax(A &a, const B &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename A, typename B> inline bool chmin(A &a, const B &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define tmax(x, y, z) max((x), max((y), (z)))
#define tmin(x, y, z) min((x), min((y), (z)))
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define EXIST(s, e) (find((s).begin(), (s).end(), (e)) != (s).end())
#define EXISTST(s, c) (((s).find(c)) != string::npos)
#define POSL(x, val) (lower_bound(x.begin(), x.end(), val) - x.begin())
#define POSU(x, val) (upper_bound(x.begin(), x.end(), val) - x.begin())
#define GEQ(x, val) (int)(x).size() - POSL((x), (val))
#define GREATER(x, val) (int)(x).size() - POSU((x), (val))
#define LEQ(x, val) POSU((x), (val))
#define LESS(x, val) POSL((x), (val))
#define SZV(a) int((a).size())
#define SZA(a) sizeof(a) / sizeof(a[0])
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MEMINF(a) memset(a, 0x3f, sizeof(a))
#define FILL(a, b) memset(a, b, sizeof(a))
#define UNIQUE(v) \
sort((v).begin(), (v).end()); \
(v).erase(unique((v).begin(), (v).end()), (v).end())
struct abracadabra {
abracadabra() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
cerr << fixed << setprecision(5);
};
} ABRACADABRA;
// TEMPLATE
// END---------------8<---------------8<---------------8<---------------8<---------------//
/*
・グラフ
> Dijkstra
> BellmanFord
> Warshall-Floyd
> Kruskal
[応用] 単一終点最短路問題は, すべての有向辺を逆向きに張り替えると,
単一始点最短路問題に帰着できる. [使用例] Graph<int> g(N); //
頂点数N, 重さの型がintのグラフを宣言 add_edge(g,a,b,c); // グラフgに,
aからbへの重さcの無向辺を追加 add_arc(g,a,b,c); // グラフgに,
aからbへの重さcの有向辺を追加 add_to_edges(edges,a,b,c); // 辺集合edgesに,
始点a, 終点b, 重さcの辺を追加
*/
template <typename T> struct Edge {
int from, to;
T weight;
Edge() : from(0), to(0), weight(0) {}
Edge(int f, int t, T w) : from(f), to(t), weight(w) {}
};
template <typename T> using Edges = vector<Edge<T>>;
template <typename T> using Graph = vector<Edges<T>>;
template <typename T> void add_edge(Graph<T> &g, int from, int to, T w = 1) {
g[from].emplace_back(from, to, w);
g[to].emplace_back(to, from, w);
}
template <typename T> void add_arc(Graph<T> &g, int from, int to, T w = 1) {
g[from].emplace_back(from, to, w);
}
template <typename T>
void add_to_edges(Edges<T> &e, int from, int to, T w = 1) {
e.emplace_back(from, to, w);
}
vector<vector<int>> gyaku;
// bool ok[2666];
vector<bool> ok;
/*
・BellmanFord
> O(EV) [E:辺の数, V:頂点の数]
[備考]
グラフ(負辺が存在してもよい)に対する単一始点全点間最短路を求めるアルゴリズム
負閉路が存在しているかの判定も可能 -> 存在していたら空列を返す
[注意] 結果を足し合わせる際, INFの大きさに注意
[使用例]
Edges<int> edges; // 全ての辺 (重さ: int)
add_to_edges(edges,a,b,c); // 辺集合edgesに, 始点a, 終点b,
重さcの辺を追加 auto bf = BellmanFord(edges,V,s); // 辺edges,
頂点数Vのグラフにおける, 始点sからの最短路
*/
template <typename T>
vector<T> BellmanFord(Edges<T> &edges, int vertex, int from) {
const auto INF = numeric_limits<T>::max() / 1000;
vector<T> dist(vertex, INF);
dist[from] = 0;
for (int i = 0; i < vertex - 1; ++i) {
for (auto &e : edges) {
if (dist[e.from] == INF)
continue;
dist[e.to] = min(dist[e.to], dist[e.from] + e.weight);
}
}
for (auto &e : edges) {
if (dist[e.from] == INF)
continue;
if (dist[e.from] + e.weight < dist[e.to] and ok[e.from])
return vector<T>();
}
return dist;
}
void dfs(int cur, int prv) {
if (ok[cur])
return;
ok[cur] = true;
EACH(nxt, gyaku[cur]) if (nxt != prv) { dfs(nxt, cur); }
}
signed main() {
SS(int, N, M, P);
Edges<ll> edges;
gyaku.resize(N);
REP(i, M) {
SS(int, A, B, C);
--A, --B;
C -= P;
add_to_edges(edges, A, B, -C);
gyaku[B].PB(A);
}
ok.assign(N, false);
dfs(N - 1, -1);
auto bf = BellmanFord(edges, N, 0);
if (bf.size() == 0) {
print(-1);
} else {
print(max<ll>(-bf[N - 1], 0));
}
// for (int i = 0; i < N; ++i) {
// print(i, ok[i]);
// }
} | insert | 315 | 315 | 315 | 317 | 0 | |
p02949 | C++ | Runtime Error | // ██████╗ ██╗ ██╗██╗ ██████╗ ███████╗
// ██╔══██╗██║ ██║██║ ██╔══██╗██╔════╝
// ██████╔╝██║ ██║██║█████╗██║ ██║█████╗
// ██╔══██╗██║ ██║██║╚════╝██║ ██║██╔══╝
// ██║ ██║╚██████╔╝██║ ██████╔╝███████╗
// ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚══════╝
#include <bits/stdc++.h>
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
using namespace std;
#define fst first
#define snd second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))
#define ADD_OPERATORS_IN(T, COMP) \
bool operator<(const T &ot) const { return COMP(ot) == -1; } \
bool operator>(const T &ot) const { return COMP(ot) == 1; } \
bool operator==(const T &ot) const { return COMP(ot) == 0; } \
bool operator!=(const T &ot) const { return COMP(ot) != 0; }
#define ADD_OPERATORS_OUT(T, COMP) \
bool operator<(const T &a, const T &b) const { return COMP(a, b) == -1; } \
bool operator>(const T &a, const T &b) const { return COMP(a, b) == 1; } \
bool operator==(const T &a, const T &b) const { return COMP(a, b) == 0; } \
bool operator!=(const T &a, const T &b) const { return COMP(a, b) != 0; }
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());
template <typename T1, typename T2> inline bool upmax(T1 &a, T2 b) {
return (a < b ? (a = b, true) : false);
}
template <typename T1, typename T2> inline bool upmin(T1 &a, T2 b) {
return (b < a ? (a = b, true) : false);
}
const int maxn = (int)1e6 + 10;
const int maxlog = 21;
const int base = 998'244'353;
const ld eps = (ld)1e-9;
const ld PI = acos(-1.);
const int pp = 41;
const int INF = (int)2e9;
const ll llinf = (ll)1e18;
struct edge {
int a, b, cost;
};
int n, m, p;
vector<edge> e, e1;
// const int INF = 1000000000;
vector<int> g[maxn];
bool used[maxn];
void dfs(int v) {
used[v] = 1;
for (int to : g[v]) {
if (used[to])
continue;
dfs(to);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("input.txt", "r", stdin);
cin >> n >> m >> p;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
c -= p;
e1.pb({a, b, -c});
g[b].pb(a);
}
dfs(n - 1);
for (auto &t : e1) {
if (used[t.a] && used[t.b]) {
e.pb(t);
}
}
vector<int> d(n, INF);
d[0] = 0;
vector<int> p(n, -1);
int x;
for (int i = 0; i < n; ++i) {
x = -1;
for (int j = 0; j < m; ++j)
if (d[e[j].a] < INF)
if (d[e[j].b] > d[e[j].a] + e[j].cost) {
d[e[j].b] = max(-INF, d[e[j].a] + e[j].cost);
p[e[j].b] = e[j].a;
x = e[j].b;
}
}
if (x != -1) {
cout << -1;
} else {
cout << max(0, -d[n - 1]);
}
return 0;
} | // ██████╗ ██╗ ██╗██╗ ██████╗ ███████╗
// ██╔══██╗██║ ██║██║ ██╔══██╗██╔════╝
// ██████╔╝██║ ██║██║█████╗██║ ██║█████╗
// ██╔══██╗██║ ██║██║╚════╝██║ ██║██╔══╝
// ██║ ██║╚██████╔╝██║ ██████╔╝███████╗
// ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚══════╝
#include <bits/stdc++.h>
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
using namespace std;
#define fst first
#define snd second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))
#define ADD_OPERATORS_IN(T, COMP) \
bool operator<(const T &ot) const { return COMP(ot) == -1; } \
bool operator>(const T &ot) const { return COMP(ot) == 1; } \
bool operator==(const T &ot) const { return COMP(ot) == 0; } \
bool operator!=(const T &ot) const { return COMP(ot) != 0; }
#define ADD_OPERATORS_OUT(T, COMP) \
bool operator<(const T &a, const T &b) const { return COMP(a, b) == -1; } \
bool operator>(const T &a, const T &b) const { return COMP(a, b) == 1; } \
bool operator==(const T &a, const T &b) const { return COMP(a, b) == 0; } \
bool operator!=(const T &a, const T &b) const { return COMP(a, b) != 0; }
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());
template <typename T1, typename T2> inline bool upmax(T1 &a, T2 b) {
return (a < b ? (a = b, true) : false);
}
template <typename T1, typename T2> inline bool upmin(T1 &a, T2 b) {
return (b < a ? (a = b, true) : false);
}
const int maxn = (int)1e6 + 10;
const int maxlog = 21;
const int base = 998'244'353;
const ld eps = (ld)1e-9;
const ld PI = acos(-1.);
const int pp = 41;
const int INF = (int)2e9;
const ll llinf = (ll)1e18;
struct edge {
int a, b, cost;
};
int n, m, p;
vector<edge> e, e1;
// const int INF = 1000000000;
vector<int> g[maxn];
bool used[maxn];
void dfs(int v) {
used[v] = 1;
for (int to : g[v]) {
if (used[to])
continue;
dfs(to);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("input.txt", "r", stdin);
cin >> n >> m >> p;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
c -= p;
e1.pb({a, b, -c});
g[b].pb(a);
}
dfs(n - 1);
for (auto &t : e1) {
if (used[t.a] && used[t.b]) {
e.pb(t);
}
}
m = sz(e);
vector<int> d(n, INF);
d[0] = 0;
vector<int> p(n, -1);
int x;
for (int i = 0; i < n; ++i) {
x = -1;
for (int j = 0; j < m; ++j)
if (d[e[j].a] < INF)
if (d[e[j].b] > d[e[j].a] + e[j].cost) {
d[e[j].b] = max(-INF, d[e[j].a] + e[j].cost);
p[e[j].b] = e[j].a;
x = e[j].b;
}
}
if (x != -1) {
cout << -1;
} else {
cout << max(0, -d[n - 1]);
}
return 0;
} | insert | 104 | 104 | 104 | 105 | 0 | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if (!(p))
#define until(p) while (!(p))
using ll = std::int64_t;
constexpr int INF = 1001001001;
int N, M, P;
int A[2600], B[2600], C[2600];
std::vector<int> G[2600], rG[2600];
bool used[2600], re1[2600], reN[2600];
int d[2600];
int calc() {
std::fill(d, d + N, INF);
d[0] = 0;
for (int i = 1; i <= N; ++i) {
bool updated = false;
for (int j = 0; j < M; ++j) {
if (!(re1[A[j]] && reN[B[j]])) {
continue;
}
if (d[B[j]] > d[A[j]] + C[j]) {
d[B[j]] = d[A[j]] + C[j];
updated = true;
}
}
if (updated && i == N) {
return -1;
}
}
return std::max(-d[N - 1], 0);
}
void dfs(int v, std::vector<int> (&G)[2600], bool (&re)[2600]) {
used[v] = true;
re[v] = true;
for (int w : G[v]) {
unless(used[w]) { dfs(w, G, re); }
}
}
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cin >> N >> M >> P;
for (int i = 0; i < M; ++i) {
std::cin >> A[i] >> B[i] >> C[i];
A[i] -= 1;
B[i] -= 1;
C[i] = P - C[i];
G[A[i]].emplace_back(B[i]);
rG[B[i]].emplace_back(A[i]);
}
dfs(0, G, re1);
memset(used, 0, sizeof(used));
dfs(N - 1, rG, reN);
std::cout << calc() << std::endl;
}
| #include <bits/stdc++.h>
#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if (!(p))
#define until(p) while (!(p))
using ll = std::int64_t;
constexpr int INF = 1001001001;
int N, M, P;
int A[5100], B[5100], C[5100];
std::vector<int> G[2600], rG[2600];
bool used[2600], re1[2600], reN[2600];
int d[2600];
int calc() {
std::fill(d, d + N, INF);
d[0] = 0;
for (int i = 1; i <= N; ++i) {
bool updated = false;
for (int j = 0; j < M; ++j) {
if (!(re1[A[j]] && reN[B[j]])) {
continue;
}
if (d[B[j]] > d[A[j]] + C[j]) {
d[B[j]] = d[A[j]] + C[j];
updated = true;
}
}
if (updated && i == N) {
return -1;
}
}
return std::max(-d[N - 1], 0);
}
void dfs(int v, std::vector<int> (&G)[2600], bool (&re)[2600]) {
used[v] = true;
re[v] = true;
for (int w : G[v]) {
unless(used[w]) { dfs(w, G, re); }
}
}
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cin >> N >> M >> P;
for (int i = 0; i < M; ++i) {
std::cin >> A[i] >> B[i] >> C[i];
A[i] -= 1;
B[i] -= 1;
C[i] = P - C[i];
G[A[i]].emplace_back(B[i]);
rG[B[i]].emplace_back(A[i]);
}
dfs(0, G, re1);
memset(used, 0, sizeof(used));
dfs(N - 1, rG, reN);
std::cout << calc() << std::endl;
}
| replace | 12 | 13 | 12 | 13 | 0 | |
p02949 | C++ | Runtime Error | #include "bits/stdc++.h"
typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v) \
cerr << #v << ":"; \
rep(i, v.size()) cerr << " " << v[i]; \
cerr << endl
using namespace std;
int MOD = 998244353;
struct edge {
int from;
int to;
int cost;
};
int n, m;
int d[2510];
signed main() {
vector<edge> es;
int n, m, p;
cin >> n >> m >> p;
rep(i, m) {
int a, b, c;
cin >> a >> b >> c;
edge e1 = {a - 1, b - 1, c - p};
es.push_back(e1);
}
fill(d, d + n, -INF);
d[0] = 0;
rep(i, n) {
rep(j, m) {
struct edge e = es[j];
if (d[e.from] != -INF && d[e.to] < d[e.from] + e.cost) {
d[e.to] = d[e.from] + e.cost;
}
}
}
int neg[1010] = {0};
rep(i, n) {
rep(j, m) {
struct edge e = es[j];
if (d[e.from] != -INF && d[e.to] < d[e.from] + e.cost) {
d[e.to] = d[e.from] + e.cost;
neg[e.to] = 1;
}
}
}
// rep(i,n) cerr << d[i] << " ";
// cerr << endl;
if (neg[n - 1] == 1)
cout << -1 << endl;
else
cout << max(d[n - 1], 0LL) << endl;
return 0;
}
// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl
// https://cf17-final-open.contest.atcoder.jp/tasks/cf17_final_a rm -r -f
// test;oj dl http://abc137.contest.atcoder.jp/tasks/abc137_b
| #include "bits/stdc++.h"
typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v) \
cerr << #v << ":"; \
rep(i, v.size()) cerr << " " << v[i]; \
cerr << endl
using namespace std;
int MOD = 998244353;
struct edge {
int from;
int to;
int cost;
};
int n, m;
int d[2510];
signed main() {
vector<edge> es;
int n, m, p;
cin >> n >> m >> p;
rep(i, m) {
int a, b, c;
cin >> a >> b >> c;
edge e1 = {a - 1, b - 1, c - p};
es.push_back(e1);
}
fill(d, d + n, -INF);
d[0] = 0;
rep(i, n) {
rep(j, m) {
struct edge e = es[j];
if (d[e.from] != -INF && d[e.to] < d[e.from] + e.cost) {
d[e.to] = d[e.from] + e.cost;
}
}
}
int neg[2510] = {0};
rep(i, n) {
rep(j, m) {
struct edge e = es[j];
if (d[e.from] != -INF && d[e.to] < d[e.from] + e.cost) {
d[e.to] = d[e.from] + e.cost;
neg[e.to] = 1;
}
}
}
// rep(i,n) cerr << d[i] << " ";
// cerr << endl;
if (neg[n - 1] == 1)
cout << -1 << endl;
else
cout << max(d[n - 1], 0LL) << endl;
return 0;
}
// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl
// https://cf17-final-open.contest.atcoder.jp/tasks/cf17_final_a rm -r -f
// test;oj dl http://abc137.contest.atcoder.jp/tasks/abc137_b
| replace | 52 | 53 | 52 | 53 | 0 | |
p02949 | C++ | Time Limit Exceeded | //
// main.cpp
//
#include <algorithm>
#include <array>
#include <assert.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template <typename T> vector<T> make_vec_nd(T init, size_t size) {
return vector<T>(size, init);
}
template <typename T, typename... Args>
auto make_vec_nd(T init, size_t size, Args... rest) {
auto inner = make_vec_nd(init, rest...);
return vector<decltype(inner)>(size, inner);
}
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = a - 1; i >= b; i--)
struct Edge {
ll from;
ll to;
ll score;
};
int main() {
ll N, M, P;
cin >> N >> M >> P;
vector<Edge> edges;
rep(i, 0, M) {
ll A, B, C;
cin >> A >> B >> C;
A--;
B--;
edges.push_back({A, B, C - P});
}
vector<ll> score(N, numeric_limits<ll>::min());
score[0] = 0;
unordered_map<ll, bool> loop;
unordered_map<ll, bool> inv_reachable;
inv_reachable[N - 1] = true;
rep(i, 0, 2 * M) {
for (Edge e : edges) {
if (score[e.from] != numeric_limits<ll>::min() &&
score[e.from] + e.score > score[e.to]) {
if (i >= M) {
loop[e.to] = true;
}
score[e.to] = score[e.from] + e.score;
}
if (inv_reachable[e.to]) {
inv_reachable[e.from] = true;
}
}
}
rep(i, 0, N) {
if (loop[i] && inv_reachable[i]) {
cout << -1 << endl;
return 0;
}
}
cout << max((ll)0, score[N - 1]) << endl;
}
| //
// main.cpp
//
#include <algorithm>
#include <array>
#include <assert.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template <typename T> vector<T> make_vec_nd(T init, size_t size) {
return vector<T>(size, init);
}
template <typename T, typename... Args>
auto make_vec_nd(T init, size_t size, Args... rest) {
auto inner = make_vec_nd(init, rest...);
return vector<decltype(inner)>(size, inner);
}
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = a - 1; i >= b; i--)
struct Edge {
ll from;
ll to;
ll score;
};
int main() {
ll N, M, P;
cin >> N >> M >> P;
vector<Edge> edges;
rep(i, 0, M) {
ll A, B, C;
cin >> A >> B >> C;
A--;
B--;
edges.push_back({A, B, C - P});
}
vector<ll> score(N, numeric_limits<ll>::min());
score[0] = 0;
vector<bool> loop(N);
vector<bool> inv_reachable(N);
inv_reachable[N - 1] = true;
rep(i, 0, 2 * M) {
for (Edge e : edges) {
if (score[e.from] != numeric_limits<ll>::min() &&
score[e.from] + e.score > score[e.to]) {
if (i >= M) {
loop[e.to] = true;
}
score[e.to] = score[e.from] + e.score;
}
if (inv_reachable[e.to]) {
inv_reachable[e.from] = true;
}
}
}
rep(i, 0, N) {
if (loop[i] && inv_reachable[i]) {
cout << -1 << endl;
return 0;
}
}
cout << max((ll)0, score[N - 1]) << endl;
}
| replace | 62 | 64 | 62 | 64 | TLE | |
p02949 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define endl "\n"
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define lb(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define ub(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
using Pair = pair<ll, pair<int, int>>;
#define pq priority_queue<Pair, vector<Pair>, greater<Pair>>
const ll mod = 1000000007;
// const ll mod=998244353;
const ld pi = acos(-1.0);
const ll INF = 1LL << 61;
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;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32; で小文字に
// cout << fixed << setprecision (20); 小数点以下20桁まで
// 実行時間制約2秒では2×10^8回くらいまで計算できる
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
ll z;
cin >> n >> m >> z;
vector<int> a(m);
vector<int> b(m);
vector<ll> c(m);
vector<vector<int>> g(n);
vector<vector<int>> gr(n);
vector<bool> ok(n, true);
rep(i, m) {
cin >> a[i] >> b[i] >> c[i];
a[i]--;
b[i]--;
c[i] -= z;
g[a[i]].pb(b[i]);
gr[b[i]].pb(a[i]);
}
// 頂点1からいけない場所をok falseに
vector<ll> dist(n, -1);
deque<int> que;
dist[0] = 0;
que.pb(0);
while (!que.empty()) {
int v = que.front();
que.pop_front();
for (int nv : g[v]) {
if (dist[nv] != -1)
continue;
dist[nv] = dist[v] + 1;
que.pb(nv);
}
}
rep(i, n) {
if (dist[i] == -1)
ok[i] = false;
dist[i] = -1;
}
// 頂点nにいけない場所をok falseに
dist[n - 1] = 0;
que.pb(n - 1);
while (!que.empty()) {
int v = que.front();
que.pop_front();
for (int nv : gr[v]) {
if (dist[nv] != -1)
continue;
dist[nv] = dist[v] + 1;
que.pb(nv);
}
}
rep(i, n) {
if (dist[i] == -1)
ok[i] = false;
dist[i] = -INF;
}
// ok true同志にだけ変をはる
vector<vector<pair<int, ll>>> gp(n);
rep(i, m) {
if (ok[a[i]] && ok[b[i]]) {
gp[a[i]].pb({b[i], c[i]});
}
}
dist[0] = 0;
que.pb(0);
int cnt = 0;
while (!que.empty()) {
cnt++;
if (cnt > 1e6) {
cout << -1 << endl;
return 0;
}
int v = que.front();
que.pop_front();
for (auto x : gp[v]) {
int nv = x.fi;
ll y = x.se;
if (dist[nv] >= dist[v] + y)
continue;
dist[nv] = dist[v] + y;
que.pb(nv);
}
}
cout << max(0ll, dist[n - 1]) << endl;
} | #include <bits/stdc++.h>
#define endl "\n"
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define lb(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define ub(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
using Pair = pair<ll, pair<int, int>>;
#define pq priority_queue<Pair, vector<Pair>, greater<Pair>>
const ll mod = 1000000007;
// const ll mod=998244353;
const ld pi = acos(-1.0);
const ll INF = 1LL << 61;
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;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32; で小文字に
// cout << fixed << setprecision (20); 小数点以下20桁まで
// 実行時間制約2秒では2×10^8回くらいまで計算できる
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
ll z;
cin >> n >> m >> z;
vector<int> a(m);
vector<int> b(m);
vector<ll> c(m);
vector<vector<int>> g(n);
vector<vector<int>> gr(n);
vector<bool> ok(n, true);
rep(i, m) {
cin >> a[i] >> b[i] >> c[i];
a[i]--;
b[i]--;
c[i] -= z;
g[a[i]].pb(b[i]);
gr[b[i]].pb(a[i]);
}
// 頂点1からいけない場所をok falseに
vector<ll> dist(n, -1);
deque<int> que;
dist[0] = 0;
que.pb(0);
while (!que.empty()) {
int v = que.front();
que.pop_front();
for (int nv : g[v]) {
if (dist[nv] != -1)
continue;
dist[nv] = dist[v] + 1;
que.pb(nv);
}
}
rep(i, n) {
if (dist[i] == -1)
ok[i] = false;
dist[i] = -1;
}
// 頂点nにいけない場所をok falseに
dist[n - 1] = 0;
que.pb(n - 1);
while (!que.empty()) {
int v = que.front();
que.pop_front();
for (int nv : gr[v]) {
if (dist[nv] != -1)
continue;
dist[nv] = dist[v] + 1;
que.pb(nv);
}
}
rep(i, n) {
if (dist[i] == -1)
ok[i] = false;
dist[i] = -INF;
}
// ok true同志にだけ変をはる
vector<vector<pair<int, ll>>> gp(n);
rep(i, m) {
if (ok[a[i]] && ok[b[i]]) {
if (a[i] != b[i])
gp[a[i]].pb({b[i], c[i]});
else {
if (c[i] > 0) {
cout << -1 << endl;
return 0;
}
}
}
}
dist[0] = 0;
que.pb(0);
int cnt = 0;
while (!que.empty()) {
cnt++;
if (cnt > 1e6) {
cout << -1 << endl;
return 0;
}
int v = que.front();
que.pop_front();
for (auto x : gp[v]) {
int nv = x.fi;
ll y = x.se;
if (dist[nv] >= dist[v] + y)
continue;
dist[nv] = dist[v] + y;
que.pb(nv);
}
}
cout << max(0ll, dist[n - 1]) << endl;
} | replace | 115 | 116 | 115 | 123 | TLE | |
p02949 | C++ | Runtime Error | #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;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
// order_of_key(val): returns the number of values less than val
// find_by_order(k): returns an iterator to the kth largest element (0-based)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define endl "\n"
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
Container const &x) {
os << "{ ";
for (auto &y : x) {
os << y << " ";
}
return os << "}";
}
template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
return os << "[" << p.ff << ", " << p.ss << "]";
}
const ll L = 2e3 + 5;
vector<pll> adj[L];
vector<pair<ll, pll>> edge(L);
ll gcd(ll a, ll b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
ll modexp(ll a, ll b, ll c) {
a %= c;
ll ans = 1;
while (b) {
if (b & 1) {
ans = (ans * a) % c;
}
a = (a * a) % c;
b >>= 1;
}
return ans;
}
bool visited[L];
bool include[L];
void dfs(ll s) {
assert(!visited[s]);
visited[s] = true;
for (auto i : adj[s]) {
if (!visited[i.ff]) {
// cout << i.ff << " " << i.ss << endl;
dfs(i.ff);
}
}
}
const ll inf = 1e15;
vector<ll> dis(L, inf);
bool bellmanFord(ll s, ll N, ll M) {
dis[s] = 0;
for (ll i = 0; i < N; i++) {
for (ll j = 0; j < M; j++) {
if (include[edge[j].ff] && include[edge[j].ss.ff]) {
// cout << j << " " << dis[edge[j].ss.ff] + edge[j].ss.ss << endl;
dis[edge[j].ss.ff] =
min(dis[edge[j].ss.ff], dis[edge[j].ff] + edge[j].ss.ss);
}
}
}
for (ll j = 0; j < M; j++) {
if (include[edge[j].ff] && include[edge[j].ss.ff] &&
dis[edge[j].ss.ff] > dis[edge[j].ff] + edge[j].ss.ss) {
// cout << j << " " << edge[j].ff << " " << edge[j].ss.ff << endl;
return false;
}
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll N, M, P;
cin >> N >> M >> P;
for (ll i = 0; i < M; i++) {
ll a, b, c;
cin >> a >> b >> c;
adj[a - 1].pb(mp(b - 1, i));
edge[i] = mp(a - 1, mp(b - 1, -c + P));
}
for (ll i = 0; i < L; i++) {
include[i] = true;
}
dfs(0);
for (ll i = 0; i < N - 1; i++) {
if (!visited[i]) {
// cout << "f " << i << endl;
include[i] = false;
}
}
for (ll i = 1; i < N - 1; i++) {
for (ll i = 0; i < N; i++) {
visited[i] = false;
}
dfs(i);
if (!visited[N - 1]) {
// cout << "s " << i << endl;
include[i] = false;
}
}
// for(ll i=0; i<N; i++)
// {
// cout << include[i] << endl;
// }
bool f = bellmanFord(0, N, M);
// cout << f << endl;
if (f && dis[N - 1] != inf) {
cout << max(0LL, -dis[N - 1]) << endl;
}
else {
cout << -1 << endl;
}
return 0;
} | #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;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
// order_of_key(val): returns the number of values less than val
// find_by_order(k): returns an iterator to the kth largest element (0-based)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define endl "\n"
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
Container const &x) {
os << "{ ";
for (auto &y : x) {
os << y << " ";
}
return os << "}";
}
template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
return os << "[" << p.ff << ", " << p.ss << "]";
}
const ll L = 5e3 + 5;
vector<pll> adj[L];
vector<pair<ll, pll>> edge(L);
ll gcd(ll a, ll b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
ll modexp(ll a, ll b, ll c) {
a %= c;
ll ans = 1;
while (b) {
if (b & 1) {
ans = (ans * a) % c;
}
a = (a * a) % c;
b >>= 1;
}
return ans;
}
bool visited[L];
bool include[L];
void dfs(ll s) {
assert(!visited[s]);
visited[s] = true;
for (auto i : adj[s]) {
if (!visited[i.ff]) {
// cout << i.ff << " " << i.ss << endl;
dfs(i.ff);
}
}
}
const ll inf = 1e15;
vector<ll> dis(L, inf);
bool bellmanFord(ll s, ll N, ll M) {
dis[s] = 0;
for (ll i = 0; i < N; i++) {
for (ll j = 0; j < M; j++) {
if (include[edge[j].ff] && include[edge[j].ss.ff]) {
// cout << j << " " << dis[edge[j].ss.ff] + edge[j].ss.ss << endl;
dis[edge[j].ss.ff] =
min(dis[edge[j].ss.ff], dis[edge[j].ff] + edge[j].ss.ss);
}
}
}
for (ll j = 0; j < M; j++) {
if (include[edge[j].ff] && include[edge[j].ss.ff] &&
dis[edge[j].ss.ff] > dis[edge[j].ff] + edge[j].ss.ss) {
// cout << j << " " << edge[j].ff << " " << edge[j].ss.ff << endl;
return false;
}
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll N, M, P;
cin >> N >> M >> P;
for (ll i = 0; i < M; i++) {
ll a, b, c;
cin >> a >> b >> c;
adj[a - 1].pb(mp(b - 1, i));
edge[i] = mp(a - 1, mp(b - 1, -c + P));
}
for (ll i = 0; i < L; i++) {
include[i] = true;
}
dfs(0);
for (ll i = 0; i < N - 1; i++) {
if (!visited[i]) {
// cout << "f " << i << endl;
include[i] = false;
}
}
for (ll i = 1; i < N - 1; i++) {
for (ll i = 0; i < N; i++) {
visited[i] = false;
}
dfs(i);
if (!visited[N - 1]) {
// cout << "s " << i << endl;
include[i] = false;
}
}
// for(ll i=0; i<N; i++)
// {
// cout << include[i] << endl;
// }
bool f = bellmanFord(0, N, M);
// cout << f << endl;
if (f && dis[N - 1] != inf) {
cout << max(0LL, -dis[N - 1]) << endl;
}
else {
cout << -1 << endl;
}
return 0;
} | replace | 42 | 43 | 42 | 43 | 0 | |
p02949 | C++ | Time Limit Exceeded | #include <iostream>
#include <limits>
#include <vector>
#include <unordered_set>
using namespace std;
struct arc {
int to, cost;
};
struct node {
int value;
vector<arc> arcs;
node(int v) : value(v) {}
};
class graph {
protected:
vector<node> nodes;
public:
graph(int N, int value) : nodes(N, value){};
void addArc(int from, int to, int cost) {
nodes[from].arcs.push_back({to, cost});
}
void addEdge(int a, int b, int cost) {
addArc(a, b, cost);
addArc(b, a, cost);
}
node &operator[](int index) { return nodes[index]; }
};
class bellmanford : public graph {
int N;
int changed;
void r(unordered_set<int> froms, int termination) {
bool c = false;
unordered_set<int> f;
for (auto &from : froms) {
for (auto &i : nodes[from].arcs) {
if (nodes[i.to].value > i.cost + nodes[from].value) {
nodes[i.to].value = i.cost + nodes[from].value;
f.insert(i.to);
c = true;
}
}
}
if (!c)
return;
if (!termination) {
changed = c;
return;
}
r(move(f), termination - 1);
}
public:
bellmanford(int N)
: graph::graph(N, std::numeric_limits<int>::max()), N(N), changed(false) {
}
bool run(int start) {
nodes[start].value = 0;
r({start}, N - 1);
return changed;
}
int getValue(int index) { return nodes[index].value; }
};
int main(void) {
int N, M, P;
cin >> N >> M >> P;
graph visit(N, 0);
for (int i = 0; i < M; ++i) {
int a, b, c;
cin >> a >> b >> c;
visit.addArc(b - 1, a - 1, c);
}
visit[N - 1].value = 1;
vector<int> f = {N - 1};
for (int i = 0; i < N - 1; ++i) {
vector<int> tmp;
for (auto &n : f) {
for (auto &a : visit[n].arcs) {
visit[a.to].value = 1;
tmp.push_back(a.to);
}
}
f = move(tmp);
}
bellmanford g(N);
for (int i = 0; i < N; ++i) {
if (visit[i].value == 1) {
for (auto &a : visit[i].arcs) {
g.addArc(a.to, i, P - a.cost);
}
}
}
cout << ((g.run(0)) ? -1 : max(0, -g.getValue(N - 1)));
return 0;
} | #include <iostream>
#include <limits>
#include <vector>
#include <unordered_set>
using namespace std;
struct arc {
int to, cost;
};
struct node {
int value;
vector<arc> arcs;
node(int v) : value(v) {}
};
class graph {
protected:
vector<node> nodes;
public:
graph(int N, int value) : nodes(N, value){};
void addArc(int from, int to, int cost) {
nodes[from].arcs.push_back({to, cost});
}
void addEdge(int a, int b, int cost) {
addArc(a, b, cost);
addArc(b, a, cost);
}
node &operator[](int index) { return nodes[index]; }
};
class bellmanford : public graph {
int N;
int changed;
void r(unordered_set<int> froms, int termination) {
bool c = false;
unordered_set<int> f;
for (auto &from : froms) {
for (auto &i : nodes[from].arcs) {
if (nodes[i.to].value > i.cost + nodes[from].value) {
nodes[i.to].value = i.cost + nodes[from].value;
f.insert(i.to);
c = true;
}
}
}
if (!c)
return;
if (!termination) {
changed = c;
return;
}
r(move(f), termination - 1);
}
public:
bellmanford(int N)
: graph::graph(N, std::numeric_limits<int>::max()), N(N), changed(false) {
}
bool run(int start) {
nodes[start].value = 0;
r({start}, N - 1);
return changed;
}
int getValue(int index) { return nodes[index].value; }
};
int main(void) {
int N, M, P;
cin >> N >> M >> P;
graph visit(N, 0);
for (int i = 0; i < M; ++i) {
int a, b, c;
cin >> a >> b >> c;
visit.addArc(b - 1, a - 1, c);
}
visit[N - 1].value = 1;
vector<int> f = {N - 1};
for (int i = 0; i < N - 1; ++i) {
vector<int> tmp;
for (auto &n : f) {
for (auto &a : visit[n].arcs) {
if (visit[a.to].value)
continue;
visit[a.to].value = 1;
tmp.push_back(a.to);
}
}
f = move(tmp);
}
bellmanford g(N);
for (int i = 0; i < N; ++i) {
if (visit[i].value == 1) {
for (auto &a : visit[i].arcs) {
g.addArc(a.to, i, P - a.cost);
}
}
}
cout << ((g.run(0)) ? -1 : max(0, -g.getValue(N - 1)));
return 0;
} | insert | 95 | 95 | 95 | 97 | TLE | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define LLINF 9223372036854775807
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl
#define MAX_DIST 1LL << 50
#define NMAX 10
#define MMAX 5200
vector<ll> dist(NMAX, 0);
bool negative[NMAX] = {
false,
};
ll a[MMAX];
ll b[MMAX];
ll c[MMAX];
void bellmanford(ll start, ll v, ll e, ll p) {
for (int i = 0; i < NMAX; i++) {
dist[i] = MAX_DIST;
}
dist[start] = 0;
for (int i = 0; i < v - 1; i++) {
for (int j = 0; j < e; j++) {
if (dist[a[j]] == MAX_DIST) {
continue;
}
if (dist[b[j]] > dist[a[j]] + c[j] + p) {
dist[b[j]] = dist[a[j]] + c[j] + p;
}
}
}
for (int i = 0; i < v; i++) {
for (int j = 0; j < e; j++) {
if (dist[a[j]] == MAX_DIST) {
continue;
}
if (dist[b[j]] > dist[a[j]] + c[j] + p) {
dist[b[j]] = dist[a[j]] + c[j] + p;
negative[b[j]] = true;
}
if (negative[a[j]]) {
negative[b[j]] = true;
}
}
}
}
int main() {
ll n, m, p;
cin >> n >> m >> p;
for (int i = 0; i < m; i++) {
ll ta, tb, tc;
cin >> ta >> tb >> tc;
ta--;
tb--;
a[i] = ta;
b[i] = tb;
c[i] = -tc;
}
bellmanford(0, n, m, p);
ll ans = -dist[n - 1];
if (negative[n - 1]) {
cout << -1 << endl;
} else {
cout << max(ans, 0LL) << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define LLINF 9223372036854775807
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl
#define MAX_DIST 1LL << 50
#define NMAX 3000
#define MMAX 5200
vector<ll> dist(NMAX, 0);
bool negative[NMAX] = {
false,
};
ll a[MMAX];
ll b[MMAX];
ll c[MMAX];
void bellmanford(ll start, ll v, ll e, ll p) {
for (int i = 0; i < NMAX; i++) {
dist[i] = MAX_DIST;
}
dist[start] = 0;
for (int i = 0; i < v - 1; i++) {
for (int j = 0; j < e; j++) {
if (dist[a[j]] == MAX_DIST) {
continue;
}
if (dist[b[j]] > dist[a[j]] + c[j] + p) {
dist[b[j]] = dist[a[j]] + c[j] + p;
}
}
}
for (int i = 0; i < v; i++) {
for (int j = 0; j < e; j++) {
if (dist[a[j]] == MAX_DIST) {
continue;
}
if (dist[b[j]] > dist[a[j]] + c[j] + p) {
dist[b[j]] = dist[a[j]] + c[j] + p;
negative[b[j]] = true;
}
if (negative[a[j]]) {
negative[b[j]] = true;
}
}
}
}
int main() {
ll n, m, p;
cin >> n >> m >> p;
for (int i = 0; i < m; i++) {
ll ta, tb, tc;
cin >> ta >> tb >> tc;
ta--;
tb--;
a[i] = ta;
b[i] = tb;
c[i] = -tc;
}
bellmanford(0, n, m, p);
ll ans = -dist[n - 1];
if (negative[n - 1]) {
cout << -1 << endl;
} else {
cout << max(ans, 0LL) << endl;
}
return 0;
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 5050
#define INF (1LL << 60LL)
typedef pair<int, int> P;
int V;
struct edge2 {
int from, to, cost;
edge2() {}
edge2(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};
edge2 es[MAX];
int d[MAX];
int ok[MAX];
int used[MAX];
int shortest_path(int s, int e) {
for (int i = 0; i < V; i++)
d[i] = INF;
d[s] = 0;
for (int j = 0; j < V; j++) {
bool update = 0;
for (int i = 0; i < e; i++) {
edge2 e = es[i];
if (!ok[e.from] || !ok[e.to])
continue;
if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
d[e.to] = d[e.from] + e.cost;
update = 1;
}
}
if (!update)
break;
if (j == V - 1) {
cout << -1 << endl;
exit(0);
}
}
return d[V - 1];
}
vector<int> G[1111];
void dfs2(int v) {
if (used[v])
return;
used[v] = 1;
for (int u : G[v]) {
dfs2(u);
}
}
signed main() {
int v, e, p;
cin >> v >> e >> p;
for (int i = 0; i < e; i++) {
int u, v, d;
cin >> u >> v >> d;
u--;
v--;
G[u].push_back(v);
es[i] = edge2(u, v, p - d);
}
for (int i = 0; i < v; i++) {
memset(used, 0, sizeof(used));
dfs2(i);
ok[i] = used[v - 1];
}
V = v;
cout << max<int>(-shortest_path(0, e), 0) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 5050
#define INF (1LL << 60LL)
typedef pair<int, int> P;
int V;
struct edge2 {
int from, to, cost;
edge2() {}
edge2(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};
edge2 es[MAX];
int d[MAX];
int ok[MAX];
int used[MAX];
int shortest_path(int s, int e) {
for (int i = 0; i < V; i++)
d[i] = INF;
d[s] = 0;
for (int j = 0; j < V; j++) {
bool update = 0;
for (int i = 0; i < e; i++) {
edge2 e = es[i];
if (!ok[e.from] || !ok[e.to])
continue;
if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
d[e.to] = d[e.from] + e.cost;
update = 1;
}
}
if (!update)
break;
if (j == V - 1) {
cout << -1 << endl;
exit(0);
}
}
return d[V - 1];
}
vector<int> G[MAX];
void dfs2(int v) {
if (used[v])
return;
used[v] = 1;
for (int u : G[v]) {
dfs2(u);
}
}
signed main() {
int v, e, p;
cin >> v >> e >> p;
for (int i = 0; i < e; i++) {
int u, v, d;
cin >> u >> v >> d;
u--;
v--;
G[u].push_back(v);
es[i] = edge2(u, v, p - d);
}
for (int i = 0; i < v; i++) {
memset(used, 0, sizeof(used));
dfs2(i);
ok[i] = used[v - 1];
}
V = v;
cout << max<int>(-shortest_path(0, e), 0) << endl;
return 0;
}
| replace | 41 | 42 | 41 | 42 | 0 | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double Double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// chmax, chmin
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;
}
void solve(long long N, long long M, long long P, std::vector<long long> A,
std::vector<long long> B, std::vector<long long> C) {
long long dist[2550] = {};
bool reachable1[2550] = {};
bool reachable2[2550] = {};
reachable1[0] = true;
reachable2[N - 1] = true;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
reachable1[B[j] - 1] |= reachable1[A[j] - 1];
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
reachable2[A[j] - 1] |= reachable2[B[j] - 1];
}
}
for (int i = 0; i < N; i++)
dist[i] = -1e9;
dist[0] = 0;
long long ans = 0;
for (int i = 0; i <= N; i++) {
vector<int> visit(N);
for (int j = 0; j < M; j++) {
visit[B[j] - 1] |= chmax(dist[B[j] - 1], dist[A[j] - 1] + C[j] - P);
}
if (i == N) {
for (int j = 0; j < M; j++)
if (visit[j] && reachable1[j] && reachable2[j]) {
cout << -1 << endl;
return;
}
}
}
cout << max(0ll, dist[N - 1]) << endl;
}
int main() {
long long N;
scanf("%lld", &N);
long long M;
scanf("%lld", &M);
long long P;
scanf("%lld", &P);
std::vector<long long> A(M);
std::vector<long long> B(M);
std::vector<long long> C(M);
for (int i = 0; i < M; i++) {
scanf("%lld", &A[i]);
scanf("%lld", &B[i]);
scanf("%lld", &C[i]);
}
solve(N, M, P, std::move(A), std::move(B), std::move(C));
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double Double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// chmax, chmin
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;
}
void solve(long long N, long long M, long long P, std::vector<long long> A,
std::vector<long long> B, std::vector<long long> C) {
long long dist[2550] = {};
bool reachable1[2550] = {};
bool reachable2[2550] = {};
reachable1[0] = true;
reachable2[N - 1] = true;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
reachable1[B[j] - 1] |= reachable1[A[j] - 1];
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
reachable2[A[j] - 1] |= reachable2[B[j] - 1];
}
}
for (int i = 0; i < N; i++)
dist[i] = -1e9;
dist[0] = 0;
long long ans = 0;
for (int i = 0; i <= N; i++) {
vector<int> visit(N);
for (int j = 0; j < M; j++) {
visit[B[j] - 1] |= chmax(dist[B[j] - 1], dist[A[j] - 1] + C[j] - P);
}
if (i == N) {
for (int j = 0; j < N; j++)
if (visit[j] && reachable1[j] && reachable2[j]) {
cout << -1 << endl;
return;
}
}
}
cout << max(0ll, dist[N - 1]) << endl;
}
int main() {
long long N;
scanf("%lld", &N);
long long M;
scanf("%lld", &M);
long long P;
scanf("%lld", &P);
std::vector<long long> A(M);
std::vector<long long> B(M);
std::vector<long long> C(M);
for (int i = 0; i < M; i++) {
scanf("%lld", &A[i]);
scanf("%lld", &B[i]);
scanf("%lld", &C[i]);
}
solve(N, M, P, std::move(A), std::move(B), std::move(C));
return 0;
}
| replace | 57 | 58 | 57 | 58 | 0 | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define SIZE 2505
struct Edge {
void set(int arg_from, int arg_to, ll arg_value) {
from = arg_from;
to = arg_to;
value = arg_value;
}
int from, to;
ll value;
};
ll N, M, P;
bool visited[SIZE];
ll min_dist[SIZE];
Edge edge[SIZE];
vector<int> rev_G[SIZE];
void dfs(int node_id) {
for (int i = 0; i < rev_G[node_id].size(); i++) {
int next = rev_G[node_id][i];
if (visited[next])
continue;
visited[next] = true;
dfs(next);
}
}
int main() {
scanf("%lld %lld %lld", &N, &M, &P);
int from, to;
ll value;
for (int i = 0; i < M; i++) {
scanf("%d %d %lld", &from, &to, &value);
from--;
to--;
edge[i].set(from, to, P - value);
rev_G[to].push_back(from);
}
for (int i = 0; i < N; i++) {
visited[i] = false;
}
visited[N - 1] = true;
dfs(N - 1);
min_dist[0] = 0;
for (int i = 1; i < N; i++) {
min_dist[i] = HUGE_NUM;
}
bool FLG = true, updated;
for (int i = 0; i < N; i++) {
updated = false;
for (int k = 0; k < M; k++) {
Edge tmp = edge[k];
if (!visited[tmp.from] || !visited[tmp.to])
continue;
if (min_dist[tmp.from] != HUGE_NUM &&
min_dist[tmp.to] > min_dist[tmp.from] + tmp.value) {
min_dist[tmp.to] = min_dist[tmp.from] + tmp.value;
updated = true;
if (i == N - 1) {
FLG = false;
break;
}
}
}
if (!updated)
break;
if (!FLG)
break;
}
if (!FLG) {
printf("-1\n");
return 0;
}
printf("%lld\n", max(0LL, min_dist[N - 1] * (-1)));
return 0;
}
| #include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define SIZE 2505
struct Edge {
void set(int arg_from, int arg_to, ll arg_value) {
from = arg_from;
to = arg_to;
value = arg_value;
}
int from, to;
ll value;
};
ll N, M, P;
bool visited[SIZE];
ll min_dist[SIZE];
Edge edge[2 * SIZE];
vector<int> rev_G[SIZE];
void dfs(int node_id) {
for (int i = 0; i < rev_G[node_id].size(); i++) {
int next = rev_G[node_id][i];
if (visited[next])
continue;
visited[next] = true;
dfs(next);
}
}
int main() {
scanf("%lld %lld %lld", &N, &M, &P);
int from, to;
ll value;
for (int i = 0; i < M; i++) {
scanf("%d %d %lld", &from, &to, &value);
from--;
to--;
edge[i].set(from, to, P - value);
rev_G[to].push_back(from);
}
for (int i = 0; i < N; i++) {
visited[i] = false;
}
visited[N - 1] = true;
dfs(N - 1);
min_dist[0] = 0;
for (int i = 1; i < N; i++) {
min_dist[i] = HUGE_NUM;
}
bool FLG = true, updated;
for (int i = 0; i < N; i++) {
updated = false;
for (int k = 0; k < M; k++) {
Edge tmp = edge[k];
if (!visited[tmp.from] || !visited[tmp.to])
continue;
if (min_dist[tmp.from] != HUGE_NUM &&
min_dist[tmp.to] > min_dist[tmp.from] + tmp.value) {
min_dist[tmp.to] = min_dist[tmp.from] + tmp.value;
updated = true;
if (i == N - 1) {
FLG = false;
break;
}
}
}
if (!updated)
break;
if (!FLG)
break;
}
if (!FLG) {
printf("-1\n");
return 0;
}
printf("%lld\n", max(0LL, min_dist[N - 1] * (-1)));
return 0;
}
| replace | 24 | 25 | 24 | 25 | 0 | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 5e18;
int main() {
ll n, m, p;
cin >> n >> m >> p;
vec d(n, -inf);
d[0] = 0;
vec a(n);
vec b(n);
vec c(n);
for (ll i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> c[i];
a[i]--;
b[i]--;
c[i] -= p;
}
for (ll i = 0; i < 2 * n; i++) {
for (ll j = 0; j < m; j++) {
if (d[a[j]] == -inf)
continue;
if (d[a[j]] + c[j] > d[b[j]]) {
if (i < n - 1) {
d[b[j]] = d[a[j]] + c[j];
} else {
d[b[j]] = inf;
}
}
}
}
if (d[n - 1] == inf) {
cout << -1 << endl;
} else {
cout << max(d[n - 1], 0LL) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 5e18;
int main() {
ll n, m, p;
cin >> n >> m >> p;
vec d(n, -inf);
d[0] = 0;
vec a(m);
vec b(m);
vec c(m);
for (ll i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> c[i];
a[i]--;
b[i]--;
c[i] -= p;
}
for (ll i = 0; i < 2 * n; i++) {
for (ll j = 0; j < m; j++) {
if (d[a[j]] == -inf)
continue;
if (d[a[j]] + c[j] > d[b[j]]) {
if (i < n - 1) {
d[b[j]] = d[a[j]] + c[j];
} else {
d[b[j]] = inf;
}
}
}
}
if (d[n - 1] == inf) {
cout << -1 << endl;
} else {
cout << max(d[n - 1], 0LL) << endl;
}
} | replace | 15 | 18 | 15 | 18 | 0 | |
p02949 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#include <cassert>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
const long long mod = 998244353;
const long long inf = 1ll << 61;
const double eps = 0.000000000001;
typedef pair<int, int> P;
typedef pair<P, int> PP;
struct edge {
int from, to, cost;
};
edge es[5005];
edge res[5005];
int d[2505];
int df[2505];
int V, E;
void bel(int s) {
rep(i, V) d[i] = inf;
d[s] = 0;
while (1) {
bool upd = false;
rep(i, E) {
edge e = res[i];
if (d[e.from] != inf && d[e.to] > d[e.from] + e.cost) {
d[e.to] = d[e.from] + e.cost;
upd = true;
}
}
if (!upd)
break;
}
}
bool findloop(int s, int t) {
fill(df, df + V, inf);
df[s] = 0;
rep(i, 2 * V) {
rep(j, E) {
edge e = es[j];
if (df[e.from] != inf && df[e.to] > df[e.from] + e.cost) {
df[e.to] = df[e.from] + e.cost;
if (i >= V - 1 && e.to == t)
return true;
}
}
}
return false;
}
signed main() {
int p;
cin >> V >> E >> p;
rep(i, E) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
c -= p;
es[i] = {a, b, -c};
}
if (findloop(0, V - 1))
cout << -1 << endl;
else {
vector<edge> v;
rep(i, E) {
if (es[i].from != es[i].to)
v.push_back(es[i]);
}
rep(i, v.size()) res[i] = v[i];
E = v.size();
bel(0);
cout << max(0ll, -d[V - 1]) << endl;
}
} | #include "bits/stdc++.h"
#include <cassert>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
const long long mod = 998244353;
const long long inf = 1ll << 61;
const double eps = 0.000000000001;
typedef pair<int, int> P;
typedef pair<P, int> PP;
struct edge {
int from, to, cost;
};
edge es[5005];
edge res[5005];
int d[2505];
int df[2505];
int V, E;
void bel(int s) {
rep(i, V) d[i] = inf;
d[s] = 0;
while (1) {
bool upd = false;
rep(i, E) {
edge e = res[i];
if (d[e.from] != inf && d[e.to] > d[e.from] + e.cost) {
d[e.to] = d[e.from] + e.cost;
upd = true;
}
}
if (!upd)
break;
}
}
bool findloop(int s, int t) {
fill(df, df + V, inf);
df[s] = 0;
rep(i, 2 * V) {
rep(j, E) {
edge e = es[j];
if (df[e.from] != inf && df[e.to] > df[e.from] + e.cost) {
df[e.to] = df[e.from] + e.cost;
if (i >= V - 1 && e.to == t)
return true;
}
}
}
return false;
}
signed main() {
int p;
cin >> V >> E >> p;
rep(i, E) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
c -= p;
es[i] = {a, b, -c};
}
if (findloop(0, V - 1))
cout << -1 << endl;
else {
cout << max(0ll, -df[V - 1]) << endl;
}
} | replace | 65 | 74 | 65 | 66 | TLE | |
p02949 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
typedef pair<int, int> pii;
queue<int> q;
int ver[3009], edge[5009 * 2], nxt[5009 * 2], head[5009 * 2], tot = 0;
ll d[3009];
int v[3009];
void add(int x, int y, int z) {
ver[++tot] = y;
edge[tot] = z;
nxt[tot] = head[x];
head[x] = tot;
}
int ban[3009], cntru[3009];
int dfsv[3009];
int n, m, p;
void spfa() {
memset(d, 0xcf, sizeof(d));
memset(v, 0, sizeof(v));
d[1] = 0;
v[1] = 1;
q.push(1);
while (q.size()) {
int x = q.front();
q.pop();
v[x] = 0;
for (int i = head[x]; i; i = nxt[i]) {
int y = ver[i], z = edge[i];
if (ban[y])
continue;
if (d[y] < d[x] + z) {
d[y] = d[x] + z;
if (!v[y]) {
++cntru[y];
if (cntru[y] >= n) {
puts("-1");
exit(0);
}
q.push(y), v[y] = 1;
}
}
}
}
cout << max(0ll, d[n]) << endl;
}
int dfs(int x) {
if (dfsv[x])
return 0;
if (x == n)
return 1;
dfsv[x] = 1;
for (int i = head[x]; i; i = nxt[i]) {
if (dfs(ver[i]))
return 1;
}
return 0;
}
int main() {
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= m; i++) {
int u, vx, vv;
scanf("%d%d%d", &u, &vx, &vv);
add(u, vx, vv - p);
}
for (int i = 1; i <= n; i++) {
memset(dfsv, 0, sizeof(dfsv));
if (!dfs(i))
ban[i] = 1;
}
spfa();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
typedef pair<int, int> pii;
queue<int> q;
int ver[5009 * 2], edge[5009 * 2], nxt[5009 * 2], head[3009], tot = 0;
ll d[3009];
int v[3009];
void add(int x, int y, int z) {
ver[++tot] = y;
edge[tot] = z;
nxt[tot] = head[x];
head[x] = tot;
}
int ban[3009], cntru[3009];
int dfsv[3009];
int n, m, p;
void spfa() {
memset(d, 0xcf, sizeof(d));
memset(v, 0, sizeof(v));
d[1] = 0;
v[1] = 1;
q.push(1);
while (q.size()) {
int x = q.front();
q.pop();
v[x] = 0;
for (int i = head[x]; i; i = nxt[i]) {
int y = ver[i], z = edge[i];
if (ban[y])
continue;
if (d[y] < d[x] + z) {
d[y] = d[x] + z;
if (!v[y]) {
++cntru[y];
if (cntru[y] >= n) {
puts("-1");
exit(0);
}
q.push(y), v[y] = 1;
}
}
}
}
cout << max(0ll, d[n]) << endl;
}
int dfs(int x) {
if (dfsv[x])
return 0;
if (x == n)
return 1;
dfsv[x] = 1;
for (int i = head[x]; i; i = nxt[i]) {
if (dfs(ver[i]))
return 1;
}
return 0;
}
int main() {
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= m; i++) {
int u, vx, vv;
scanf("%d%d%d", &u, &vx, &vv);
add(u, vx, vv - p);
}
for (int i = 1; i <= n; i++) {
memset(dfsv, 0, sizeof(dfsv));
if (!dfs(i))
ban[i] = 1;
}
spfa();
return 0;
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02949 | C++ | Time Limit Exceeded | #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;
vector<int> dp, path, pathid;
vector<bool> hasloop, touched, reachable;
vector<vector<pair<int, int>>> adj, radj;
#define INF (2000000000)
int p;
// void solve(int id, int step, int d) {
// if (pathid[id] != -1) {
// if (d > path[id]) {
// hasloop[id] = true;
// }
// return;
// }
//
// dp[id] = max(dp[id], d);
// if (touched[id]) return;
//
// touched[id] = true;
// path[id] = d;
// pathid[id] = step;
//
// for (auto p : adj[id]) {
// solve(p.first, step + 1, d + p.second);
// }
//
// path[id] = -INF;
// pathid[id] = -1;
//
// }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, a, b, c, i, j, k, id, ans, d;
cin >> n >> m >> p;
adj.resize(n + 1);
radj.resize(n + 1);
for (i = 0; i < m; i++) {
cin >> a >> b >> c;
adj[a].push_back({b, c - p});
radj[b].push_back({a, c - p});
}
dp.assign(n + 1, -INF);
path.assign(n + 1, -INF);
pathid.assign(n + 1, -1);
touched.assign(n + 1, false);
hasloop.assign(n + 1, false);
priority_queue<pair<pair<int, int>, vector<int>>> pq;
pq.push({{0, 1}, path});
while (!pq.empty()) {
id = pq.top().first.second;
d = pq.top().first.first;
path = pq.top().second;
pq.pop();
touched[id] = true;
if ((path[id] != -INF) && (d > path[id])) {
hasloop[id] = true;
} else {
if ((path[id] == -INF) && (dp[id] < d)) {
dp[id] = d;
path[id] = d;
for (auto p : adj[id])
pq.push({{d + p.second, p.first}, path});
path[id] = -INF;
}
}
}
reachable.assign(n + 1, false);
queue<int> q;
q.push(n);
reachable[n] = true;
while (!q.empty()) {
id = q.front();
q.pop();
for (auto p : radj[id]) {
if (!reachable[p.first]) {
reachable[p.first] = true;
q.push(p.first);
}
}
}
bool foundloop = false;
for (i = 1; i <= n; i++) {
if (hasloop[i] && reachable[i])
foundloop = true;
}
if (foundloop)
ans = -1;
else
ans = max(0, dp[n]);
cout << ans << "\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;
vector<int> dp, path, pathid;
vector<bool> hasloop, touched, reachable;
vector<vector<pair<int, int>>> adj, radj;
#define INF (2000000000)
int p;
// void solve(int id, int step, int d) {
// if (pathid[id] != -1) {
// if (d > path[id]) {
// hasloop[id] = true;
// }
// return;
// }
//
// dp[id] = max(dp[id], d);
// if (touched[id]) return;
//
// touched[id] = true;
// path[id] = d;
// pathid[id] = step;
//
// for (auto p : adj[id]) {
// solve(p.first, step + 1, d + p.second);
// }
//
// path[id] = -INF;
// pathid[id] = -1;
//
// }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, a, b, c, i, j, k, id, ans, d;
cin >> n >> m >> p;
adj.resize(n + 1);
radj.resize(n + 1);
for (i = 0; i < m; i++) {
cin >> a >> b >> c;
adj[a].push_back({b, c - p});
radj[b].push_back({a, c - p});
}
dp.assign(n + 1, -INF);
path.assign(n + 1, -INF);
pathid.assign(n + 1, -1);
touched.assign(n + 1, false);
hasloop.assign(n + 1, false);
priority_queue<pair<pair<int, int>, vector<int>>> pq;
pq.push({{0, 1}, path});
while (!pq.empty()) {
id = pq.top().first.second;
d = pq.top().first.first;
path = pq.top().second;
pq.pop();
touched[id] = true;
if ((path[id] != -INF) && (d > path[id])) {
hasloop[id] = true;
} else {
if ((path[id] == -INF) && (dp[id] < d)) {
dp[id] = d;
path[id] = d;
for (auto p : adj[id]) {
if (dp[p.first] < d + p.second)
pq.push({{d + p.second, p.first}, path});
}
path[id] = -INF;
}
}
}
reachable.assign(n + 1, false);
queue<int> q;
q.push(n);
reachable[n] = true;
while (!q.empty()) {
id = q.front();
q.pop();
for (auto p : radj[id]) {
if (!reachable[p.first]) {
reachable[p.first] = true;
q.push(p.first);
}
}
}
bool foundloop = false;
for (i = 1; i <= n; i++) {
if (hasloop[i] && reachable[i])
foundloop = true;
}
if (foundloop)
ans = -1;
else
ans = max(0, dp[n]);
cout << ans << "\n";
return 0;
}
| replace | 89 | 91 | 89 | 93 | TLE | |
p02950 | 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 pli pair<ll, int>
#define pil pair<int, ll>
#define pll pair<ll, ll>
#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; }
template <typename T, typename U> T pow_(T a, U 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;
}
template <class T> void operator+=(vector<T> &v, vector<T> v2) {
rep(i, sz(v2)) v.eb(v2[i]);
}
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(11);
int T;
T = 1;
while (T--)
solve();
}
const int MAX = 2000005;
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
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(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
void solve() {
int p;
cin >> p;
mod = p; // debuged
vector<int> a(p);
cin >> a;
COMinit();
vector<int> b(p);
rep(i, p) {
if (a[i] == 0)
continue;
b[0]++;
// (x-i)^(p-1) = (p-1)Cj x^j (-i)^(p-1-j)
rep(j, p) {
(b[j] += mod - COM(p - 1, j) * modpow(-i, p - 1 - j, mod) % mod) %= mod;
}
}
cout << b << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define pll pair<ll, ll>
#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; }
template <typename T, typename U> T pow_(T a, U 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;
}
template <class T> void operator+=(vector<T> &v, vector<T> v2) {
rep(i, sz(v2)) v.eb(v2[i]);
}
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(11);
int T;
T = 1;
while (T--)
solve();
}
const int MAX = 2000005;
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
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(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
void solve() {
int p;
cin >> p;
mod = p; // debuged
vector<int> a(p);
cin >> a;
COMinit();
vector<int> b(p);
rep(i, p) {
if (a[i] == 0)
continue;
b[0]++;
// (x-i)^(p-1) = (p-1)Cj x^j (-i)^(p-1-j)
ll x = 1;
for (int j = p - 1; j >= 0; --j) {
(b[j] += mod - COM(p - 1, j) * x % mod) %= mod;
x = (p + x * (-i) % p) % p;
}
}
cout << b << endl;
}
| replace | 124 | 126 | 124 | 128 | TLE | |
p02950 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int P;
ll powmod(ll n, ll k) {
ll r = 1, t = n % P;
for (; k != 0; k /= 2) {
if (k & 1)
r = r * t % P;
t = t * t % P;
}
return r;
}
ll inv(ll n) { return powmod(n, P - 2); }
vector<int> f0, f1;
void init(int n) {
f0.resize(n + 1);
f0[0] = 1;
for (int i = 1; i <= n; i++) {
f0[i] = (ll)f0[i - 1] * i % P;
}
f1.resize(n + 1);
f1[n] = inv(f0[n]);
for (int i = n; i > 0; i--) {
f1[i - 1] = (ll)f1[i] * i % P;
}
}
ll fact(int k) { return f0[k]; }
ll comb(int n, int k) {
if (n < k || k < 0)
return 0;
return (ll)f0[n] * f1[k] % P * f1[n - k] % P;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> P;
init(P - 1);
vector<ll> b(P);
for (int i = 0; i < P; i++) {
int a;
cin >> a;
if (a) {
b[0] += 1;
int s = 1;
for (int j = 0; j < P; j++) {
b[P - 1 - j] -= comb(P - 1, j) * powmod(i, j) * s;
s *= -1;
}
}
}
for (int j = 0; j < P; j++) {
ll t = b[j] % P;
if (t < 0)
t += P;
cout << t << " \n"[j == P - 1];
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int P;
ll powmod(ll n, ll k) {
ll r = 1, t = n % P;
for (; k != 0; k /= 2) {
if (k & 1)
r = r * t % P;
t = t * t % P;
}
return r;
}
ll inv(ll n) { return powmod(n, P - 2); }
vector<int> f0, f1;
void init(int n) {
f0.resize(n + 1);
f0[0] = 1;
for (int i = 1; i <= n; i++) {
f0[i] = (ll)f0[i - 1] * i % P;
}
f1.resize(n + 1);
f1[n] = inv(f0[n]);
for (int i = n; i > 0; i--) {
f1[i - 1] = (ll)f1[i] * i % P;
}
}
ll fact(int k) { return f0[k]; }
ll comb(int n, int k) {
if (n < k || k < 0)
return 0;
return (ll)f0[n] * f1[k] % P * f1[n - k] % P;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> P;
init(P - 1);
vector<ll> b(P);
for (int i = 0; i < P; i++) {
int a;
cin >> a;
if (a) {
b[0] += 1;
int s = 1;
for (int j = 0; j < P; j++) {
b[P - 1 - j] -= comb(P - 1, j) * s;
s *= -i;
s %= P;
}
}
}
for (int j = 0; j < P; j++) {
ll t = b[j] % P;
if (t < 0)
t += P;
cout << t << " \n"[j == P - 1];
}
return 0;
} | replace | 60 | 62 | 60 | 63 | TLE | |
p02950 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1000 + 17;
const int MOD = 1000 * 1000 * 1000 + 7;
struct poly {
int P;
vector<long long> a;
poly(int P) : P(P), a(vector<long long>(P, 0ll)) {}
int len() const {
int res = 0;
for (int i = 0; i < P; ++i)
if (a[i] != 0)
res = i + 1;
return res;
}
long long &operator[](int i) { return a[i]; }
const long long operator[](int i) const { return a[i]; }
};
poly operator*(const poly &a, const poly &b) {
auto P = a.P;
poly res(P);
auto alen = a.len();
auto blen = b.len();
for (int i = 0; i < alen; ++i)
for (int j = 0; j < blen; ++j)
res[(i + j) % (P - 1)] += a[i] * b[j];
for (int i = 0; i < P; ++i)
if (res[i] >= P)
res[i] %= P;
return move(res);
}
int n;
int a[N];
vector<int> vec[2];
poly binpow(poly f, int n) {
auto res = poly(f.P);
res[0] = 1;
for (; n > 0; n /= 2, f = f * f)
if (n & 1)
res = res * f;
return move(res);
}
bool read() {
if (!(cin >> n))
return false;
for (int i = 0; i < n; ++i)
cin >> a[i];
return true;
}
void solve() {
for (int i = 1; i < n; ++i)
vec[a[i] ^ a[0]].emplace_back(i);
vector<poly> f(2, poly(n));
for (int k = 0; k < 2; ++k) {
f[k][0] = 1;
for (auto x : vec[k]) {
poly cur(n);
cur[0] = (n - x) % n;
cur[1] = 1;
f[k] = f[k] * cur;
}
}
++f[1][0];
auto res = binpow(f[0], n - 1) * f[1];
res[n - 1] = res[0];
res[0] = a[0];
for (int i = 0; i < n; ++i)
cout << (i > 0 && a[0] ? (n - res[i]) % n : res[i]) << ' ';
cout << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
while (read())
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 200 * 1000 + 17;
const int MOD = 1000 * 1000 * 1000 + 7;
struct poly {
int P;
vector<long long> a;
poly(int P) : P(P), a(vector<long long>(P, 0ll)) {}
int len() const {
int res = 0;
for (int i = 0; i < P; ++i)
if (a[i] != 0)
res = i + 1;
return res;
}
long long &operator[](int i) { return a[i]; }
const long long operator[](int i) const { return a[i]; }
};
poly operator*(const poly &a, const poly &b) {
auto P = a.P;
poly res(P);
auto alen = a.len();
auto blen = b.len();
for (int i = 0; i < alen; ++i)
for (int j = 0; j < blen; ++j)
res[(i + j) % (P - 1)] += a[i] * b[j];
for (int i = 0; i < P; ++i)
if (res[i] >= P)
res[i] %= P;
return move(res);
}
int n;
int a[N];
vector<int> vec[2];
poly binpow(poly f, int n) {
auto res = poly(f.P);
res[0] = 1;
for (; n > 0; n /= 2, f = f * f)
if (n & 1)
res = res * f;
return move(res);
}
bool read() {
if (!(cin >> n))
return false;
for (int i = 0; i < n; ++i)
cin >> a[i];
return true;
}
void solve() {
for (int i = 1; i < n; ++i)
vec[a[i] ^ a[0]].emplace_back(i);
vector<poly> f(2, poly(n));
for (int k = 0; k < 2; ++k) {
f[k][0] = 1;
for (auto x : vec[k]) {
poly cur(n);
cur[0] = (n - x) % n;
cur[1] = 1;
f[k] = f[k] * cur;
}
}
++f[1][0];
auto res = binpow(f[0], n - 1) * f[1];
res[n - 1] = res[0];
res[0] = a[0];
for (int i = 0; i < n; ++i)
cout << (i > 0 && a[0] ? (n - res[i]) % n : res[i]) << ' ';
cout << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
while (read())
solve();
return 0;
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02950 | C++ | Time Limit Exceeded | #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 ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//
struct RuntimeModInt {
public:
using value_type = long long;
RuntimeModInt(value_type val = 0)
: val(val < 0 ? (mod() - (-val % mod())) % mod() : val % mod()) {}
explicit operator bool() const noexcept { return val; }
bool operator==(const RuntimeModInt &rhs) const noexcept {
return val == rhs.val;
}
bool operator!=(const RuntimeModInt &rhs) const noexcept {
return !(*this == rhs);
}
RuntimeModInt operator+() const noexcept { return RuntimeModInt(*this); }
RuntimeModInt operator-() const noexcept { return RuntimeModInt(0) -= *this; }
RuntimeModInt operator+(const RuntimeModInt &rhs) const noexcept {
return RuntimeModInt(*this) += rhs;
}
RuntimeModInt operator-(const RuntimeModInt &rhs) const noexcept {
return RuntimeModInt(*this) -= rhs;
}
RuntimeModInt operator*(const RuntimeModInt &rhs) const noexcept {
return RuntimeModInt(*this) *= rhs;
}
RuntimeModInt operator/(const RuntimeModInt &rhs) const noexcept {
return RuntimeModInt(*this) /= rhs;
}
RuntimeModInt &operator+=(const RuntimeModInt &rhs) noexcept {
val += rhs.val;
if (val >= mod())
val -= mod();
return *this;
}
RuntimeModInt &operator-=(const RuntimeModInt &rhs) noexcept {
if (val < rhs.val)
val += mod();
val -= rhs.val;
return *this;
}
RuntimeModInt &operator*=(const RuntimeModInt &rhs) noexcept {
val = val * rhs.val % mod();
return *this;
}
RuntimeModInt &operator/=(const RuntimeModInt &rhs) noexcept {
*this *= rhs.inverse();
return *this;
}
RuntimeModInt pow(value_type n) const {
RuntimeModInt res = 1, x = val;
if (n < 0) {
x = x.inverse();
n = -n;
}
while (n) {
if (n & 1)
res *= x;
x *= x;
n >>= 1;
}
return res;
}
RuntimeModInt inverse() const {
long long a = val, a1 = 1, a2 = 0, b = mod(), b1 = 0, b2 = 1;
while (b > 0) {
value_type q = a / b, r = a % b;
value_type nb1 = a1 - q * b1, nb2 = a2 - q * b2;
a = b;
b = r;
a1 = b1;
b1 = nb1;
a2 = b2;
b2 = nb2;
}
assert(a == 1);
return a1;
}
const value_type &get() const noexcept { return val; }
static void set_mod(int M) noexcept { mod() = M; }
static int get_mod() noexcept { return mod(); }
friend std::ostream &operator<<(std::ostream &os, const RuntimeModInt &rhs) {
return os << rhs.val;
}
friend std::istream &operator>>(std::istream &is, RuntimeModInt &rhs) {
value_type x;
is >> x;
rhs = RuntimeModInt(x);
return is;
}
private:
value_type val;
static int &mod() {
static int M = 2;
return M;
}
};
using mint = RuntimeModInt;
template <typename T> struct Combination {
public:
using size_type = std::size_t;
Combination(size_type sz = 1) : _fact(1, 1), _finv(1, 1), _inv(1, 1) {
build(sz);
}
T fact(size_type k) {
if (k >= T::get_mod())
return 0;
build(k);
return _fact[k];
}
T finv(size_type k) {
assert(k < T::get_mod());
build(k);
return _finv[k];
}
T inv(size_type k) {
assert(k > 0 && k < T::get_mod());
build(k);
return _inv[k];
}
T operator()(int n, int r) { return c(n, r); }
T c(int n, int r) {
if (r < 0 || n < r)
return 0;
return fact(n) * finv(r) * finv(n - r);
}
private:
std::vector<T> _fact, _finv, _inv;
static constexpr size_type MAX_LIMIT = 50000000;
void build(size_type k) {
if (_fact.size() > k)
return;
assert(k < MAX_LIMIT);
size_type sz = std::min({MAX_LIMIT, static_cast<size_type>(T::get_mod()),
std::max(_fact.size() * 2, k + 1)});
size_type presz = _fact.size();
_fact.resize(sz);
_finv.resize(sz);
_inv.resize(sz);
for (size_type i = presz; i < sz; ++i)
_fact[i] = _fact[i - 1] * i;
_finv[sz - 1] = T(_fact[sz - 1]).inverse();
for (size_type i = sz - 1; i > presz; --i) {
_finv[i - 1] = _finv[i] * i;
_inv[i] = _fact[i - 1] * _finv[i];
}
_inv[presz] = _fact[presz - 1] * _finv[presz];
}
};
using Comb = Combination<mint>;
Comb comb;
int main() {
int p;
cin >> p;
mint::set_mod(p);
vector<mint> A(p);
REP(i, p) cin >> A[i];
vector<mint> ans(p);
REP(i, p) if (A[i]) {
ans[0] += 1;
REP(j, p) { ans[j] -= mint(-i).pow(p - 1 - j) * comb(p - 1, j); }
}
REP(i, p) cout << ans[i] << (i + 1 == p ? '\n' : ' ');
cout << 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 ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//
struct RuntimeModInt {
public:
using value_type = long long;
RuntimeModInt(value_type val = 0)
: val(val < 0 ? (mod() - (-val % mod())) % mod() : val % mod()) {}
explicit operator bool() const noexcept { return val; }
bool operator==(const RuntimeModInt &rhs) const noexcept {
return val == rhs.val;
}
bool operator!=(const RuntimeModInt &rhs) const noexcept {
return !(*this == rhs);
}
RuntimeModInt operator+() const noexcept { return RuntimeModInt(*this); }
RuntimeModInt operator-() const noexcept { return RuntimeModInt(0) -= *this; }
RuntimeModInt operator+(const RuntimeModInt &rhs) const noexcept {
return RuntimeModInt(*this) += rhs;
}
RuntimeModInt operator-(const RuntimeModInt &rhs) const noexcept {
return RuntimeModInt(*this) -= rhs;
}
RuntimeModInt operator*(const RuntimeModInt &rhs) const noexcept {
return RuntimeModInt(*this) *= rhs;
}
RuntimeModInt operator/(const RuntimeModInt &rhs) const noexcept {
return RuntimeModInt(*this) /= rhs;
}
RuntimeModInt &operator+=(const RuntimeModInt &rhs) noexcept {
val += rhs.val;
if (val >= mod())
val -= mod();
return *this;
}
RuntimeModInt &operator-=(const RuntimeModInt &rhs) noexcept {
if (val < rhs.val)
val += mod();
val -= rhs.val;
return *this;
}
RuntimeModInt &operator*=(const RuntimeModInt &rhs) noexcept {
val = val * rhs.val % mod();
return *this;
}
RuntimeModInt &operator/=(const RuntimeModInt &rhs) noexcept {
*this *= rhs.inverse();
return *this;
}
RuntimeModInt pow(value_type n) const {
RuntimeModInt res = 1, x = val;
if (n < 0) {
x = x.inverse();
n = -n;
}
while (n) {
if (n & 1)
res *= x;
x *= x;
n >>= 1;
}
return res;
}
RuntimeModInt inverse() const {
long long a = val, a1 = 1, a2 = 0, b = mod(), b1 = 0, b2 = 1;
while (b > 0) {
value_type q = a / b, r = a % b;
value_type nb1 = a1 - q * b1, nb2 = a2 - q * b2;
a = b;
b = r;
a1 = b1;
b1 = nb1;
a2 = b2;
b2 = nb2;
}
assert(a == 1);
return a1;
}
const value_type &get() const noexcept { return val; }
static void set_mod(int M) noexcept { mod() = M; }
static int get_mod() noexcept { return mod(); }
friend std::ostream &operator<<(std::ostream &os, const RuntimeModInt &rhs) {
return os << rhs.val;
}
friend std::istream &operator>>(std::istream &is, RuntimeModInt &rhs) {
value_type x;
is >> x;
rhs = RuntimeModInt(x);
return is;
}
private:
value_type val;
static int &mod() {
static int M = 2;
return M;
}
};
using mint = RuntimeModInt;
template <typename T> struct Combination {
public:
using size_type = std::size_t;
Combination(size_type sz = 1) : _fact(1, 1), _finv(1, 1), _inv(1, 1) {
build(sz);
}
T fact(size_type k) {
if (k >= T::get_mod())
return 0;
build(k);
return _fact[k];
}
T finv(size_type k) {
assert(k < T::get_mod());
build(k);
return _finv[k];
}
T inv(size_type k) {
assert(k > 0 && k < T::get_mod());
build(k);
return _inv[k];
}
T operator()(int n, int r) { return c(n, r); }
T c(int n, int r) {
if (r < 0 || n < r)
return 0;
return fact(n) * finv(r) * finv(n - r);
}
private:
std::vector<T> _fact, _finv, _inv;
static constexpr size_type MAX_LIMIT = 50000000;
void build(size_type k) {
if (_fact.size() > k)
return;
assert(k < MAX_LIMIT);
size_type sz = std::min({MAX_LIMIT, static_cast<size_type>(T::get_mod()),
std::max(_fact.size() * 2, k + 1)});
size_type presz = _fact.size();
_fact.resize(sz);
_finv.resize(sz);
_inv.resize(sz);
for (size_type i = presz; i < sz; ++i)
_fact[i] = _fact[i - 1] * i;
_finv[sz - 1] = T(_fact[sz - 1]).inverse();
for (size_type i = sz - 1; i > presz; --i) {
_finv[i - 1] = _finv[i] * i;
_inv[i] = _fact[i - 1] * _finv[i];
}
_inv[presz] = _fact[presz - 1] * _finv[presz];
}
};
using Comb = Combination<mint>;
Comb comb;
int main() {
int p;
cin >> p;
mint::set_mod(p);
vector<mint> A(p);
REP(i, p) cin >> A[i];
vector<mint> ans(p);
REP(i, p) if (A[i]) {
ans[0] += 1;
mint po = 1;
for (int j = p - 1; j >= 0; --j) {
ans[j] -= po * comb(p - 1, j);
po *= mint(-i);
}
}
REP(i, p) cout << ans[i] << (i + 1 == p ? '\n' : ' ');
cout << endl;
return 0;
}
| replace | 204 | 205 | 204 | 209 | TLE | |
p02950 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod;
struct Modint {
ll num;
Modint() : num() { num = 0; }
Modint(ll x) : num() {
num = x % mod;
if (num < 0)
num += mod;
}
inline Modint &operator=(int x) {
num = x % mod;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator=(ll x) {
num = x % mod;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator=(Modint x) {
num = x.num;
return *this;
}
inline Modint operator+(int x) const { return Modint(num + x); }
inline Modint operator+(ll x) const { return Modint(num + x); }
inline Modint operator+(Modint x) const {
ll a = num + x.num;
if (a >= mod)
a -= mod;
return Modint{a};
}
inline Modint &operator+=(int x) {
num += x;
num %= mod;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator+=(ll x) {
num += x;
num %= mod;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator+=(Modint x) {
num += x.num;
if (num >= mod)
num -= mod;
return *this;
}
inline Modint &operator++() {
if (num == mod - 1)
num = 0;
else
num++;
return *this;
}
inline Modint operator++(int) {
Modint ans = *this;
if (num == mod - 1)
num = 0;
else
num++;
return ans;
}
inline Modint operator-() const {
if (num == 0)
return 0;
return Modint(mod - num);
}
inline Modint operator-(int x) const { return Modint(num - x); }
inline Modint operator-(ll x) const { return Modint(num - x); }
inline Modint operator-(Modint x) const {
ll a = num - x.num;
if (a < 0)
a += mod;
return Modint{a};
}
inline Modint &operator-=(int x) {
num -= x;
num %= mod;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator-=(ll x) {
num -= x;
num %= mod;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator-=(Modint x) {
num -= x.num;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator--() {
if (num == 0)
num = mod - 1;
else
num--;
return *this;
}
inline Modint operator--(int) {
Modint ans = *this;
if (num == 0)
num = mod - 1;
else
num--;
return ans;
}
inline Modint operator*(int x) const { return Modint(num * (x % mod)); }
inline Modint operator*(ll x) const { return Modint(num * (x % mod)); }
inline Modint operator*(Modint x) const { return Modint{num * x.num % mod}; }
inline Modint &operator*=(int x) {
num *= Modint(x);
num %= mod;
return *this;
}
inline Modint &operator*=(ll x) {
num *= Modint(x);
num %= mod;
return *this;
}
inline Modint &operator*=(Modint x) {
num *= x.num;
num %= mod;
return *this;
}
inline Modint pow(ll x) const {
ll i = 1;
Modint ans = 1, cnt = *this;
while (i <= x) {
if (x & i) {
ans *= cnt;
x ^= i;
}
cnt *= cnt;
i *= 2;
}
return ans;
}
inline operator ll() const { return num; }
};
// 階乗、階乗の逆元、逆元
const int MAX = 3000;
Modint fact[MAX], finv[MAX], inv[MAX];
// 階乗、逆元の前処理
void Cinit(ll mod) {
fact[0] = fact[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fact[i] = fact[i - 1] * i;
inv[i] = -(inv[mod % i] * (mod / i));
finv[i] = finv[i - 1] * inv[i];
}
}
// combination
Modint nCk(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fact[n] * (finv[k] * finv[n - k]);
}
int main() {
ll p;
cin >> p;
mod = p;
vector<ll> a(p);
vector<Modint> ret(p, 0);
for (int i = 0; i < p; ++i) {
cin >> a[i];
if (a[i] == 1)
ret[0]++;
}
Cinit(p);
for (int i = 0; i < p; ++i) {
if (a[i] != 1)
continue;
for (int j = 0; j < p; ++j) {
ret[j] += nCk(p - 1, j) * (-Modint(-i).pow(p - 1 - j));
}
}
for (int i = 0; i < p; ++i) {
cout << ret[i] << " ";
}
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod;
struct Modint {
ll num;
Modint() : num() { num = 0; }
Modint(ll x) : num() {
num = x % mod;
if (num < 0)
num += mod;
}
inline Modint &operator=(int x) {
num = x % mod;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator=(ll x) {
num = x % mod;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator=(Modint x) {
num = x.num;
return *this;
}
inline Modint operator+(int x) const { return Modint(num + x); }
inline Modint operator+(ll x) const { return Modint(num + x); }
inline Modint operator+(Modint x) const {
ll a = num + x.num;
if (a >= mod)
a -= mod;
return Modint{a};
}
inline Modint &operator+=(int x) {
num += x;
num %= mod;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator+=(ll x) {
num += x;
num %= mod;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator+=(Modint x) {
num += x.num;
if (num >= mod)
num -= mod;
return *this;
}
inline Modint &operator++() {
if (num == mod - 1)
num = 0;
else
num++;
return *this;
}
inline Modint operator++(int) {
Modint ans = *this;
if (num == mod - 1)
num = 0;
else
num++;
return ans;
}
inline Modint operator-() const {
if (num == 0)
return 0;
return Modint(mod - num);
}
inline Modint operator-(int x) const { return Modint(num - x); }
inline Modint operator-(ll x) const { return Modint(num - x); }
inline Modint operator-(Modint x) const {
ll a = num - x.num;
if (a < 0)
a += mod;
return Modint{a};
}
inline Modint &operator-=(int x) {
num -= x;
num %= mod;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator-=(ll x) {
num -= x;
num %= mod;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator-=(Modint x) {
num -= x.num;
if (num < 0)
num += mod;
return *this;
}
inline Modint &operator--() {
if (num == 0)
num = mod - 1;
else
num--;
return *this;
}
inline Modint operator--(int) {
Modint ans = *this;
if (num == 0)
num = mod - 1;
else
num--;
return ans;
}
inline Modint operator*(int x) const { return Modint(num * (x % mod)); }
inline Modint operator*(ll x) const { return Modint(num * (x % mod)); }
inline Modint operator*(Modint x) const { return Modint{num * x.num % mod}; }
inline Modint &operator*=(int x) {
num *= Modint(x);
num %= mod;
return *this;
}
inline Modint &operator*=(ll x) {
num *= Modint(x);
num %= mod;
return *this;
}
inline Modint &operator*=(Modint x) {
num *= x.num;
num %= mod;
return *this;
}
inline Modint pow(ll x) const {
ll i = 1;
Modint ans = 1, cnt = *this;
while (i <= x) {
if (x & i) {
ans *= cnt;
x ^= i;
}
cnt *= cnt;
i *= 2;
}
return ans;
}
inline operator ll() const { return num; }
};
// 階乗、階乗の逆元、逆元
const int MAX = 3000;
Modint fact[MAX], finv[MAX], inv[MAX];
// 階乗、逆元の前処理
void Cinit(ll mod) {
fact[0] = fact[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fact[i] = fact[i - 1] * i;
inv[i] = -(inv[mod % i] * (mod / i));
finv[i] = finv[i - 1] * inv[i];
}
}
// combination
Modint nCk(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fact[n] * (finv[k] * finv[n - k]);
}
int main() {
ll p;
cin >> p;
mod = p;
vector<ll> a(p);
vector<Modint> ret(p, 0);
for (int i = 0; i < p; ++i) {
cin >> a[i];
if (a[i] == 1)
ret[0]++;
}
Cinit(p);
for (int i = 0; i < p; ++i) {
if (a[i] != 1)
continue;
Modint c(1);
for (int j = p - 1; j >= 0; --j) {
ret[j] -= nCk(p - 1, j) * c;
c *= -i;
}
}
for (int i = 0; i < p; ++i) {
cout << ret[i] << " ";
}
}
| replace | 196 | 198 | 196 | 200 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.