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
|
---|---|---|---|---|---|---|---|---|---|---|---|
p01316 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 1LL << 50
int main() {
int n, m;
while (cin >> n >> m) {
if (n == 0 && m == 0)
break;
vector<int> c, x;
rep(i, m) {
int a;
cin >> a;
c.pb(a);
}
rep(i, n) {
int a;
cin >> a;
x.pb(a);
}
static ll dp[20100][260];
rep(i, 30000) rep(j, 300) dp[i][j] = INF;
dp[0][128] = 0;
for (int i = 0; i < n; i++) {
rep(j, 256) {
rep(k, m) {
int val = j + c[k];
if (val > 255)
val = 255;
if (val < 0)
val = 0;
int pow2 = (x[i] - val) * (x[i] - val);
dp[i + 1][val] = min(dp[i + 1][val], dp[i][j] + pow2);
// if(j-c[k]>=0 && j-c[k]<=255)dp[i][j] =
// min(dp[i][j],dp[i-1][j-c[k]]+pow2);
}
}
}
ll mini = INF;
rep(i, 256) { mini = min(mini, dp[n][i]); }
cout << mini << endl;
}
} | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 1LL << 50
int main() {
int n, m;
while (cin >> n >> m) {
if (n == 0 && m == 0)
break;
vector<int> c, x;
rep(i, m) {
int a;
cin >> a;
c.pb(a);
}
rep(i, n) {
int a;
cin >> a;
x.pb(a);
}
static ll dp[20100][260];
rep(i, 20100) rep(j, 260) dp[i][j] = INF;
dp[0][128] = 0;
for (int i = 0; i < n; i++) {
rep(j, 256) {
rep(k, m) {
int val = j + c[k];
if (val > 255)
val = 255;
if (val < 0)
val = 0;
int pow2 = (x[i] - val) * (x[i] - val);
dp[i + 1][val] = min(dp[i + 1][val], dp[i][j] + pow2);
// if(j-c[k]>=0 && j-c[k]<=255)dp[i][j] =
// min(dp[i][j],dp[i-1][j-c[k]]+pow2);
}
}
}
ll mini = INF;
rep(i, 256) { mini = min(mini, dp[n][i]); }
cout << mini << endl;
}
} | replace | 27 | 28 | 27 | 28 | -11 | |
p01316 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
template <class T, class U> inline void chmin(T &t, U f) {
if (t > f)
t = f;
}
template <class T, class U> inline void chmax(T &t, U f) {
if (t < f)
t = f;
}
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) > (b) ? (b) : (a))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
#define INF INT_MAX / 3
#define MAX_N 1000
ll n, m;
ll dp[200001][256];
ll c[16], x[20000];
ll rec(ll p, ll y) {
if (dp[p][y] >= 0)
return dp[p][y];
if (p == n)
return 0;
long ret = LONG_MAX;
for (ll i = 0; i < m;
++i) { //??????????????????????????¨????????????????????°????????????
ll ny = y + c[i];
if (ny < 0)
ny = 0;
else if (ny > 255)
ny = 255;
ret = MIN(ret, rec(p + 1, ny) + (ny - x[p]) * (ny - x[p]));
}
return dp[p][y] = ret;
}
void solve() {
cin.tie(0);
ios::sync_with_stdio(false);
while (1) {
cin >> n >> m;
if (n == 0)
break;
rep(i, m) cin >> c[i];
rep(i, n) cin >> x[i];
memset(dp, -1, sizeof(dp));
printf("%lld\n", rec(0, 128));
}
}
int main() {
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
template <class T, class U> inline void chmin(T &t, U f) {
if (t > f)
t = f;
}
template <class T, class U> inline void chmax(T &t, U f) {
if (t < f)
t = f;
}
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) > (b) ? (b) : (a))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
#define INF INT_MAX / 3
#define MAX_N 1000
ll n, m;
ll dp[20001][256];
ll c[16], x[20000];
ll rec(ll p, ll y) {
if (dp[p][y] >= 0)
return dp[p][y];
if (p == n)
return 0;
long ret = LONG_MAX;
for (ll i = 0; i < m;
++i) { //??????????????????????????¨????????????????????°????????????
ll ny = y + c[i];
if (ny < 0)
ny = 0;
else if (ny > 255)
ny = 255;
ret = MIN(ret, rec(p + 1, ny) + (ny - x[p]) * (ny - x[p]));
}
return dp[p][y] = ret;
}
void solve() {
cin.tie(0);
ios::sync_with_stdio(false);
while (1) {
cin >> n >> m;
if (n == 0)
break;
rep(i, m) cin >> c[i];
rep(i, n) cin >> x[i];
memset(dp, -1, sizeof(dp));
printf("%lld\n", rec(0, 128));
}
}
int main() {
solve();
return 0;
}
| replace | 35 | 36 | 35 | 36 | MLE | |
p01316 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
while (cin >> n >> m, n) {
vector<int> code(m), value(n);
for (int i = 0; i < m; i++)
cin >> code[i];
for (int i = 0; i < n; i++)
cin >> value[i];
vector<vector<int>> dp(256, vector<int>(n + 1, 1000000000));
dp[0][128] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 256; j++) {
for (int k = 0; k < m; k++) {
int val = min(255, max(0, j + code[k]));
dp[i + 1][val] = min(dp[i + 1][val],
dp[i][j] + (val - value[i]) * (val - value[i]));
}
}
}
int ans = 1000000000;
for (int i = 0; i < 256; i++) {
ans = min(ans, dp[n][i]);
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
while (cin >> n >> m, n) {
vector<int> code(m), value(n);
for (int i = 0; i < m; i++)
cin >> code[i];
for (int i = 0; i < n; i++)
cin >> value[i];
vector<vector<int>> dp(n + 1, vector<int>(256, 1000000000));
dp[0][128] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 256; j++) {
for (int k = 0; k < m; k++) {
int val = min(255, max(0, j + code[k]));
dp[i + 1][val] = min(dp[i + 1][val],
dp[i][j] + (val - value[i]) * (val - value[i]));
}
}
}
int ans = 1000000000;
for (int i = 0; i < 256; i++) {
ans = min(ans, dp[n][i]);
}
cout << ans << endl;
}
return 0;
} | replace | 15 | 16 | 15 | 16 | -6 | free(): invalid size
|
p01316 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
const int MOD = 1000000007;
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps = 1e-9;
const int inf = 1e9;
typedef pair<int, int> P;
struct Point {
double x, y;
Point() {
x = 0;
y = 0;
}
Point(double d_x, double d_y) { x = d_x, y = d_y; }
double operator*(Point obj) { return obj.x * x + obj.y * y; }
double operator%(Point obj) { return obj.y * x - obj.x * y; }
Point operator*(double b) {
Point tmp;
tmp.x = x * b;
tmp.y = y * b;
return tmp;
}
Point operator/(double b) {
Point tmp;
tmp.x = x / b;
tmp.y = y / b;
return tmp;
}
Point operator+(Point obj) {
Point tmp;
tmp.x = x + obj.x;
tmp.y = y + obj.y;
return tmp;
}
Point operator-() {
Point tmp;
tmp.x = -x;
tmp.y = -y;
return tmp;
}
Point operator-(Point obj) {
Point tmp;
tmp.x = x - obj.x;
tmp.y = y - obj.y;
return tmp;
}
Point operator-=(Point obj) {
x -= obj.x;
y -= obj.y;
return *this;
}
Point operator+=(Point obj) {
x += obj.x;
y += obj.y;
return *this;
}
Point operator/=(double b) {
x = x / b;
y = y / b;
return *this;
}
Point operator*=(double b) {
x = x * b;
y = y * b;
return *this;
}
double size() { return hypot(x, y); }
Point unit() { return Point(x / size(), y / size()); }
Point normal() { return Point(y, -x); }
double atan() { return atan2(y, x); }
};
bool operator<(Point a, Point b) { return a.x != b.x ? a.x < b.x : a.y < b.y; }
bool operator>(Point a, Point b) { return b < a; }
bool operator<=(Point a, Point b) { return !(b < a); }
bool operator>=(Point a, Point b) { return !(a < b); }
bool operator==(Point a, Point b) { return (a - b).size() < eps; }
bool operator!=(Point a, Point b) { return !(a == b); }
bool equal(double a, double b) { return abs(a - b) < eps; }
double cross(Point a, Point b) { return a % b; }
double dot(Point a, Point b) { return a * b; }
int ccw(Point a, Point b, Point c) {
b = b - a;
c = c - a;
if (b % c > 0)
return +1;
else if (b % c < 0)
return -1;
else if (b * c < 0)
return +2;
else if (b.size() < c.size())
return -2;
else
return 0;
}
int n, m;
int dp[200005][256];
int main(int argc, char const *argv[]) {
int n, m;
while (1) {
cin >> n >> m;
if (!n)
break;
vi C(m);
vi x(n);
fill_n((int *)dp, sizeof(dp) / sizeof(int), inf);
for (int i = 0; i < m; i++) {
cin >> C[i];
}
for (int i = 0; i < n; i++) {
cin >> x[i];
}
dp[0][128] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 256; j++) {
if (dp[i][j] == inf) {
continue;
}
for (int k = 0; k < m; k++) {
int yn = j + C[k];
if (yn > 255)
yn = 255;
else if (yn < 0)
yn = 0;
int err = (x[i] - yn) * (x[i] - yn);
dp[i + 1][yn] = min(dp[i + 1][yn], dp[i][j] + err);
}
}
}
cout << *min_element(dp[n], dp[n] + 256) << endl;
}
return 0;
}
| #include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
const int MOD = 1000000007;
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps = 1e-9;
const int inf = 1e9;
typedef pair<int, int> P;
struct Point {
double x, y;
Point() {
x = 0;
y = 0;
}
Point(double d_x, double d_y) { x = d_x, y = d_y; }
double operator*(Point obj) { return obj.x * x + obj.y * y; }
double operator%(Point obj) { return obj.y * x - obj.x * y; }
Point operator*(double b) {
Point tmp;
tmp.x = x * b;
tmp.y = y * b;
return tmp;
}
Point operator/(double b) {
Point tmp;
tmp.x = x / b;
tmp.y = y / b;
return tmp;
}
Point operator+(Point obj) {
Point tmp;
tmp.x = x + obj.x;
tmp.y = y + obj.y;
return tmp;
}
Point operator-() {
Point tmp;
tmp.x = -x;
tmp.y = -y;
return tmp;
}
Point operator-(Point obj) {
Point tmp;
tmp.x = x - obj.x;
tmp.y = y - obj.y;
return tmp;
}
Point operator-=(Point obj) {
x -= obj.x;
y -= obj.y;
return *this;
}
Point operator+=(Point obj) {
x += obj.x;
y += obj.y;
return *this;
}
Point operator/=(double b) {
x = x / b;
y = y / b;
return *this;
}
Point operator*=(double b) {
x = x * b;
y = y * b;
return *this;
}
double size() { return hypot(x, y); }
Point unit() { return Point(x / size(), y / size()); }
Point normal() { return Point(y, -x); }
double atan() { return atan2(y, x); }
};
bool operator<(Point a, Point b) { return a.x != b.x ? a.x < b.x : a.y < b.y; }
bool operator>(Point a, Point b) { return b < a; }
bool operator<=(Point a, Point b) { return !(b < a); }
bool operator>=(Point a, Point b) { return !(a < b); }
bool operator==(Point a, Point b) { return (a - b).size() < eps; }
bool operator!=(Point a, Point b) { return !(a == b); }
bool equal(double a, double b) { return abs(a - b) < eps; }
double cross(Point a, Point b) { return a % b; }
double dot(Point a, Point b) { return a * b; }
int ccw(Point a, Point b, Point c) {
b = b - a;
c = c - a;
if (b % c > 0)
return +1;
else if (b % c < 0)
return -1;
else if (b * c < 0)
return +2;
else if (b.size() < c.size())
return -2;
else
return 0;
}
int n, m;
int dp[20005][256];
int main(int argc, char const *argv[]) {
int n, m;
while (1) {
cin >> n >> m;
if (!n)
break;
vi C(m);
vi x(n);
fill_n((int *)dp, sizeof(dp) / sizeof(int), inf);
for (int i = 0; i < m; i++) {
cin >> C[i];
}
for (int i = 0; i < n; i++) {
cin >> x[i];
}
dp[0][128] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 256; j++) {
if (dp[i][j] == inf) {
continue;
}
for (int k = 0; k < m; k++) {
int yn = j + C[k];
if (yn > 255)
yn = 255;
else if (yn < 0)
yn = 0;
int err = (x[i] - yn) * (x[i] - yn);
dp[i + 1][yn] = min(dp[i + 1][yn], dp[i][j] + err);
}
}
}
cout << *min_element(dp[n], dp[n] + 256) << endl;
}
return 0;
}
| replace | 114 | 115 | 114 | 115 | MLE | |
p01316 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <assert.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(j, n) for (int(j) = 0; j < (int)(n); ++j)
#define FOR(j, k, n) for (int(j) = (k); j < (int)(n); ++j)
#define FOREQ(j, k, n) for (int(j) = (k); j <= (int)(n); ++j)
#define MEMSET(z, val) memset(z, val, sizeof(z))
int n, m;
int x[30000];
int c[100];
int memo[30000][300];
inline ll square(ll x) { return x * x; }
ll calc(int depth, int color) {
assert(0 <= color && color <= 255);
if (memo[depth][color] != -1) {
return memo[depth][color];
}
if (depth == n) {
return memo[depth][color] = 0;
}
ll ret = 1LL << 60;
REP(i, m) {
int ncolor = color + c[i];
ncolor = max(0, min(255, ncolor));
int cost = square(abs(x[depth + 1] - ncolor));
ret = min(ret, calc(depth + 1, ncolor) + cost);
}
return memo[depth][color] = ret;
}
int main() {
while (scanf("%d %d", &n, &m), n | m) {
MEMSET(memo, -1);
REP(i, m) { scanf("%d", &c[i]); }
REP(i, n) { scanf("%d", &x[i + 1]); }
printf("%lld\n", calc(0, 128));
}
} | #include <algorithm>
#include <assert.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(j, n) for (int(j) = 0; j < (int)(n); ++j)
#define FOR(j, k, n) for (int(j) = (k); j < (int)(n); ++j)
#define FOREQ(j, k, n) for (int(j) = (k); j <= (int)(n); ++j)
#define MEMSET(z, val) memset(z, val, sizeof(z))
int n, m;
int x[30000];
int c[100];
int memo[20010][260];
inline ll square(ll x) { return x * x; }
ll calc(int depth, int color) {
assert(0 <= color && color <= 255);
if (memo[depth][color] != -1) {
return memo[depth][color];
}
if (depth == n) {
return memo[depth][color] = 0;
}
ll ret = 1LL << 60;
REP(i, m) {
int ncolor = color + c[i];
ncolor = max(0, min(255, ncolor));
int cost = square(abs(x[depth + 1] - ncolor));
ret = min(ret, calc(depth + 1, ncolor) + cost);
}
return memo[depth][color] = ret;
}
int main() {
while (scanf("%d %d", &n, &m), n | m) {
MEMSET(memo, -1);
REP(i, m) { scanf("%d", &c[i]); }
REP(i, n) { scanf("%d", &x[i + 1]); }
printf("%lld\n", calc(0, 128));
}
} | replace | 21 | 22 | 21 | 22 | MLE | |
p01317 | C++ | Time Limit Exceeded | #include <cstdio>
#include <iostream>
using namespace std;
const int MAX_N = 210;
const int MAX_T = 1005;
const int INF = 2 * MAX_N * MAX_T;
int N, M, R;
int sea[MAX_N][MAX_N];
int land[MAX_N][MAX_N];
const int MAX_R = 1005;
int O[MAX_R];
int dp[MAX_R][MAX_N];
void show(char s) {
printf("\n");
if (s == 'L') {
printf("Land\n");
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
int n = land[i][j];
if (n == INF)
printf("INF ");
else
printf("%d ", n);
}
printf("\n");
}
} else if (s == 'S') {
printf("Sea\n");
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
int n = sea[i][j];
if (n == INF)
printf("INF ");
else
printf("%d ", n);
}
printf("\n");
}
}
}
int main(int argc, const char *argv[]) {
while (true) {
scanf("%d %d", &N, &M); // 町の番号は1~Nにする
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (i == j) {
sea[i][j] = land[i][j] = 0;
} else {
sea[i][j] = land[i][j] = INF;
}
}
}
for (int i = 0; i < M; i++) {
int x, y, t;
char s;
scanf("%d %d %d %c", &x, &y, &t, &s);
if (s == 'L') {
land[x][y] = land[y][x] = t;
} else if (s == 'S') {
sea[x][y] = sea[y][x] = t;
}
}
scanf("%d", &R);
for (int i = 0; i < R; i++) {
scanf("%d", &O[i]);
}
for (int k = 1; k <= N; k++) {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
land[i][j] = min(land[i][j], land[i][k] + land[k][j]);
sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);
}
}
}
// 確認
// show('L');
// show('S');
for (int i = 1; i <= N; i++)
dp[0][i] = INF;
dp[0][O[0]] = 0;
for (int i = 1; i < R; i++) {
for (int j = 1; j <= N; j++) {
int minD = INF;
for (int k = 1; k <= N; k++) {
if (j == k) {
minD = min(minD, dp[i - 1][j] + land[O[i - 1]][O[i]]);
} else {
minD = min(minD, dp[i - 1][k] + land[O[i - 1]][k] + sea[k][j] +
land[j][O[i]]);
}
// printf("minD=%d\n",minD);
}
dp[i][j] = minD;
}
}
int ret = INF;
for (int i = 1; i <= N; i++)
ret = min(ret, dp[R - 1][i]);
printf("%d\n", ret);
}
return 0;
} | #include <cstdio>
#include <iostream>
using namespace std;
const int MAX_N = 210;
const int MAX_T = 1005;
const int INF = 2 * MAX_N * MAX_T;
int N, M, R;
int sea[MAX_N][MAX_N];
int land[MAX_N][MAX_N];
const int MAX_R = 1005;
int O[MAX_R];
int dp[MAX_R][MAX_N];
void show(char s) {
printf("\n");
if (s == 'L') {
printf("Land\n");
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
int n = land[i][j];
if (n == INF)
printf("INF ");
else
printf("%d ", n);
}
printf("\n");
}
} else if (s == 'S') {
printf("Sea\n");
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
int n = sea[i][j];
if (n == INF)
printf("INF ");
else
printf("%d ", n);
}
printf("\n");
}
}
}
int main(int argc, const char *argv[]) {
while (true) {
scanf("%d %d", &N, &M); // 町の番号は1~Nにする
if (N == 0 && M == 0)
break;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (i == j) {
sea[i][j] = land[i][j] = 0;
} else {
sea[i][j] = land[i][j] = INF;
}
}
}
for (int i = 0; i < M; i++) {
int x, y, t;
char s;
scanf("%d %d %d %c", &x, &y, &t, &s);
if (s == 'L') {
land[x][y] = land[y][x] = t;
} else if (s == 'S') {
sea[x][y] = sea[y][x] = t;
}
}
scanf("%d", &R);
for (int i = 0; i < R; i++) {
scanf("%d", &O[i]);
}
for (int k = 1; k <= N; k++) {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
land[i][j] = min(land[i][j], land[i][k] + land[k][j]);
sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);
}
}
}
// 確認
// show('L');
// show('S');
for (int i = 1; i <= N; i++)
dp[0][i] = INF;
dp[0][O[0]] = 0;
for (int i = 1; i < R; i++) {
for (int j = 1; j <= N; j++) {
int minD = INF;
for (int k = 1; k <= N; k++) {
if (j == k) {
minD = min(minD, dp[i - 1][j] + land[O[i - 1]][O[i]]);
} else {
minD = min(minD, dp[i - 1][k] + land[O[i - 1]][k] + sea[k][j] +
land[j][O[i]]);
}
// printf("minD=%d\n",minD);
}
dp[i][j] = minD;
}
}
int ret = INF;
for (int i = 1; i <= N; i++)
ret = min(ret, dp[R - 1][i]);
printf("%d\n", ret);
}
return 0;
} | insert | 44 | 44 | 44 | 46 | TLE | |
p01317 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? 1e9 : 1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
if (a > b)
a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
if (a < b)
a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
auto inner = vectors(tail...);
return vector<decltype(inner)>(x, inner);
}
template <class T> void join(T a) {
int b = 0;
for (auto itr : a) {
if (b++ != 0)
cout << " ";
cout << itr;
}
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
#define int ll
signed main() {
int n, m;
while (cin >> n >> m, n || m) {
auto Ld = vectors(301, 301, (ll)INF);
auto Sd = vectors(301, 301, (ll)INF);
rep(i, n) Ld[i][i] = Sd[i][i] = 0;
rep(i, m) {
int a, b, c;
char s;
cin >> a >> b >> c >> s;
--a, --b;
if (s == 'L') {
Ld[b][a] = Ld[a][b] = min(Ld[a][b], c);
} else {
Sd[b][a] = Sd[a][b] = min(Sd[a][b], c);
}
}
rep(k, n) rep(i, n) rep(j, n) {
cmin(Sd[i][j], Sd[i][k] + Sd[k][j]);
cmin(Ld[i][j], Ld[i][k] + Ld[k][j]);
}
int r;
cin >> r;
vector<int> v;
rep(i, r) {
int t;
cin >> t;
v.push_back(--t);
}
auto dp = vectors(301, 301, (ll)INF);
dp[0][v[0]] = 0;
rep(i, r) {
if (i == 0)
continue;
rep(j, n) {
rep(k, n) {
if (j != k)
cmin(dp[i][j],
dp[i - 1][k] + Ld[v[i - 1]][k] + Sd[k][j] + Ld[j][v[i]]);
else
cmin(dp[i][j], dp[i - 1][j] + Ld[v[i - 1]][v[i]]);
}
}
}
int ans = INF;
for (auto val : dp[r - 1])
cmin(ans, val);
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? 1e9 : 1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
if (a > b)
a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
if (a < b)
a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
auto inner = vectors(tail...);
return vector<decltype(inner)>(x, inner);
}
template <class T> void join(T a) {
int b = 0;
for (auto itr : a) {
if (b++ != 0)
cout << " ";
cout << itr;
}
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
#define int ll
signed main() {
int n, m;
while (cin >> n >> m, n || m) {
auto Ld = vectors(301, 301, (ll)INF);
auto Sd = vectors(301, 301, (ll)INF);
rep(i, n) Ld[i][i] = Sd[i][i] = 0;
rep(i, m) {
int a, b, c;
char s;
cin >> a >> b >> c >> s;
--a, --b;
if (s == 'L') {
Ld[b][a] = Ld[a][b] = min(Ld[a][b], c);
} else {
Sd[b][a] = Sd[a][b] = min(Sd[a][b], c);
}
}
rep(k, n) rep(i, n) rep(j, n) {
cmin(Sd[i][j], Sd[i][k] + Sd[k][j]);
cmin(Ld[i][j], Ld[i][k] + Ld[k][j]);
}
int r;
cin >> r;
vector<int> v;
rep(i, r) {
int t;
cin >> t;
v.push_back(--t);
}
auto dp = vectors(2001, 2001, (ll)INF);
dp[0][v[0]] = 0;
rep(i, r) {
if (i == 0)
continue;
rep(j, n) {
rep(k, n) {
if (j != k)
cmin(dp[i][j],
dp[i - 1][k] + Ld[v[i - 1]][k] + Sd[k][j] + Ld[j][v[i]]);
else
cmin(dp[i][j], dp[i - 1][j] + Ld[v[i - 1]][v[i]]);
}
}
}
int ans = INF;
for (auto val : dp[r - 1])
cmin(ans, val);
cout << ans << endl;
}
}
| replace | 72 | 73 | 72 | 73 | 0 | |
p01317 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const ll inf = 0x1f1f1f1f1f1f1f1f; //?造了一个三倍之后不会溢出的?.....
const ll maxn = 205;
ll land[maxn][maxn], sea[maxn][maxn], num[maxn], cost[maxn][maxn];
void init(int n) {
memset(land, inf, sizeof(land));
memset(sea, inf, sizeof(sea));
memset(cost, inf, sizeof(cost));
for (ll i = 1; i <= n; ++i) {
sea[i][i] = land[i][i] = 0;
}
}
ll slove(ll n, ll m, ll w) {
for (ll k = 1; k <= n; ++k) {
for (ll i = 1; i <= n; ++i) {
for (ll j = 1; j <= n; ++j) {
sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);
land[i][j] = min(land[i][j], land[i][k] + land[k][j]);
}
}
}
cost[1][num[1]] = 0;
for (ll i = 1; i <= w; ++i) {
for (ll j = 1; j <= n; ++j) {
cost[i][j] = min(cost[i][j], cost[i - 1][j] + land[num[i - 1]][num[i]]);
for (ll k = 1; k <= n; ++k) {
cost[i][k] = min(cost[i][k], cost[i - 1][j] + land[num[i - 1]][j] +
sea[j][k] + land[k][num[i]]);
}
}
}
ll ans = inf;
for (ll i = 1; i <= n; ++i) {
ans = min(ans, cost[w][i]);
}
// return *min_element(cost[w],cost[w]+n+1);
return ans;
}
int main() {
ll n, m;
while (scanf("%lld%lld", &n, &m), n | m) {
init(n);
for (ll i = 0; i < m; ++i) {
ll a, b, c;
char ch[10];
scanf("%lld%lld%lld%s", &a, &b, &c, ch);
if (ch[0] == 'S') {
sea[a][b] = sea[b][a] = min(sea[a][b], c);
} else {
land[a][b] = land[b][a] = min(land[a][b], c);
}
}
ll w;
scanf("%lld", &w);
for (ll i = 1; i <= w; ++i) {
scanf("%lld", &num[i]);
}
printf("%lld\n", slove(n, m, w));
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const ll inf = 0x1f1f1f1f1f1f1f1f; //?造了一个三倍之后不会溢出的?.....
const ll maxn = 205;
ll land[maxn][maxn], sea[maxn][maxn], num[maxn * 5], cost[maxn * 5][maxn];
void init(int n) {
memset(land, inf, sizeof(land));
memset(sea, inf, sizeof(sea));
memset(cost, inf, sizeof(cost));
for (ll i = 1; i <= n; ++i) {
sea[i][i] = land[i][i] = 0;
}
}
ll slove(ll n, ll m, ll w) {
for (ll k = 1; k <= n; ++k) {
for (ll i = 1; i <= n; ++i) {
for (ll j = 1; j <= n; ++j) {
sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);
land[i][j] = min(land[i][j], land[i][k] + land[k][j]);
}
}
}
cost[1][num[1]] = 0;
for (ll i = 1; i <= w; ++i) {
for (ll j = 1; j <= n; ++j) {
cost[i][j] = min(cost[i][j], cost[i - 1][j] + land[num[i - 1]][num[i]]);
for (ll k = 1; k <= n; ++k) {
cost[i][k] = min(cost[i][k], cost[i - 1][j] + land[num[i - 1]][j] +
sea[j][k] + land[k][num[i]]);
}
}
}
ll ans = inf;
for (ll i = 1; i <= n; ++i) {
ans = min(ans, cost[w][i]);
}
// return *min_element(cost[w],cost[w]+n+1);
return ans;
}
int main() {
ll n, m;
while (scanf("%lld%lld", &n, &m), n | m) {
init(n);
for (ll i = 0; i < m; ++i) {
ll a, b, c;
char ch[10];
scanf("%lld%lld%lld%s", &a, &b, &c, ch);
if (ch[0] == 'S') {
sea[a][b] = sea[b][a] = min(sea[a][b], c);
} else {
land[a][b] = land[b][a] = min(land[a][b], c);
}
}
ll w;
scanf("%lld", &w);
for (ll i = 1; i <= w; ++i) {
scanf("%lld", &num[i]);
}
printf("%lld\n", slove(n, m, w));
}
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p01317 | C++ | Runtime Error | // AOJ 2200 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2200)
#include <algorithm>
#include <cstdio>
#include <limits>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); ++i)
const int MAX_N = 200, MAX_R = 1000, INF = std::numeric_limits<int>::max() >> 4;
int N, M, R;
int land[MAX_N][MAX_N], sea[MAX_N][MAX_N];
int dp[MAX_R + 1][MAX_N];
int z[MAX_N];
int main() {
while (scanf("%d%d", &N, &M), N | M) {
rep(i, N) rep(j, N) land[i][j] = sea[i][j] = i == j ? 0 : INF;
rep(i, M) {
int x, y, t;
char sl[2];
scanf("%d%d%d%s", &x, &y, &t, sl);
--x;
--y;
auto mat = *sl == 'L' ? land : sea;
mat[x][y] = mat[y][x] = std::min(mat[x][y], t);
}
scanf("%d", &R);
rep(i, R) scanf("%d", z + i), --z[i];
rep(k, N) rep(i, N) rep(j, N) {
land[i][j] = std::min(land[i][j], land[i][k] + land[k][j]);
sea[i][j] = std::min(sea[i][j], sea[i][k] + sea[k][j]);
}
rep(i, R + 1) rep(j, N + 1) dp[i][j] = INF;
dp[0][z[0]] = 0;
rep(i, R - 1) rep(j, N) rep(k, N) {
if (j != k)
dp[i + 1][j] =
std::min(dp[i + 1][j],
dp[i][k] + land[z[i]][k] + sea[k][j] + land[j][z[i + 1]]);
else
dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j] + land[z[i]][z[i + 1]]);
}
printf("%d\n", *std::min_element(dp[R - 1], dp[R - 1] + N));
}
return 0;
} | // AOJ 2200 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2200)
#include <algorithm>
#include <cstdio>
#include <limits>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); ++i)
const int MAX_N = 200, MAX_R = 1000, INF = std::numeric_limits<int>::max() >> 4;
int N, M, R;
int land[MAX_N][MAX_N], sea[MAX_N][MAX_N];
int dp[MAX_R + 1][MAX_N];
int z[MAX_R];
int main() {
while (scanf("%d%d", &N, &M), N | M) {
rep(i, N) rep(j, N) land[i][j] = sea[i][j] = i == j ? 0 : INF;
rep(i, M) {
int x, y, t;
char sl[2];
scanf("%d%d%d%s", &x, &y, &t, sl);
--x;
--y;
auto mat = *sl == 'L' ? land : sea;
mat[x][y] = mat[y][x] = std::min(mat[x][y], t);
}
scanf("%d", &R);
rep(i, R) scanf("%d", z + i), --z[i];
rep(k, N) rep(i, N) rep(j, N) {
land[i][j] = std::min(land[i][j], land[i][k] + land[k][j]);
sea[i][j] = std::min(sea[i][j], sea[i][k] + sea[k][j]);
}
rep(i, R + 1) rep(j, N + 1) dp[i][j] = INF;
dp[0][z[0]] = 0;
rep(i, R - 1) rep(j, N) rep(k, N) {
if (j != k)
dp[i + 1][j] =
std::min(dp[i + 1][j],
dp[i][k] + land[z[i]][k] + sea[k][j] + land[j][z[i + 1]]);
else
dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j] + land[z[i]][z[i + 1]]);
}
printf("%d\n", *std::min_element(dp[R - 1], dp[R - 1] + N));
}
return 0;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p01317 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <cstdio>
using namespace std;
#define REP(var, count) for (int var = 0; var < count; var++)
const int IPPAI = 214748364;
int umi[210][210];
int riku[210][210];
int cmpl[1010][210]; //??????????????§?????????????????????
bool used[1010][210];
bool solve(void) {
int N, M;
cin >> N >> M;
if (N == 0) {
return false;
}
REP(i, 210) REP(j, 210) {
int a = (i == j) ? 0 : IPPAI;
umi[i][j] = a;
riku[i][j] = a;
}
REP(i, M) {
int x, y, l;
char ty;
cin >> x >> y >> l >> ty;
if (ty == 'L') {
riku[x][y] = l;
riku[y][x] = l;
} else {
umi[x][y] = l;
umi[y][x] = l;
}
}
// ???
for (int k = 1; k <= N; k++) {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
umi[i][j] = std::min(umi[i][j], umi[i][k] + umi[k][j]);
riku[i][j] = std::min(riku[i][j], riku[i][k] + riku[k][j]);
}
}
}
int R;
int list[1010];
cin >> R;
REP(i, R) { cin >> list[i]; }
typedef pair<int, pair<int, int>> P;
priority_queue<P, vector<P>, greater<P>> que;
REP(i, 210) REP(f, 1010) {
cmpl[f][i] = IPPAI;
used[f][i] = false;
}
cmpl[0][list[0]] = 0;
que.push(make_pair(0, make_pair(0, list[0])));
while (!que.empty()) {
P p = que.top();
que.pop();
pair<int, int> pa = p.second;
int adv = pa.first;
int nowpos = list[adv];
int funepos = pa.second;
if (cmpl[adv][funepos] < p.first) {
continue;
}
if (adv + 1 >= R) {
continue;
}
for (int i = 1; i <= N; i++) {
// 1????????????????????????i???
int nextpos = list[adv + 1];
int cost;
// cout << "adv=" << adv << ", funepos=" << funepos << ", i=" << i <<
// endl;
// ??????????????????
if (funepos == i) {
if (riku[nowpos][nextpos] == IPPAI) {
continue;
}
cost = riku[nowpos][nextpos];
} else {
// ????????\?????´??????????????°???????????§????????§??????
if (riku[nowpos][funepos] == IPPAI) {
continue;
}
cost = riku[nowpos][funepos];
// ?????§ i ?????§?§??????????
if (umi[funepos][i] == IPPAI) {
continue;
}
cost += umi[funepos][i];
if (riku[i][nextpos] == IPPAI) {
continue;
}
cost += riku[i][nextpos];
cout << riku[nowpos][funepos] << " " << umi[funepos][i] << " "
<< riku[i][nextpos] << endl;
}
// cout << "cost = " << cost << endl;
if (cmpl[adv + 1][i] > cmpl[adv][funepos] + cost) {
cmpl[adv + 1][i] = cmpl[adv][funepos] + cost;
que.push(make_pair(cmpl[adv + 1][i], make_pair(adv + 1, i)));
}
}
}
int ret = IPPAI;
for (int i = 1; i <= N; i++) {
ret = std::min(ret, cmpl[R - 1][i]);
}
cout << ret << endl;
return true;
}
int main(void) {
while (solve()) {
}
} | #include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <cstdio>
using namespace std;
#define REP(var, count) for (int var = 0; var < count; var++)
const int IPPAI = 214748364;
int umi[210][210];
int riku[210][210];
int cmpl[1010][210]; //??????????????§?????????????????????
bool used[1010][210];
bool solve(void) {
int N, M;
cin >> N >> M;
if (N == 0) {
return false;
}
REP(i, 210) REP(j, 210) {
int a = (i == j) ? 0 : IPPAI;
umi[i][j] = a;
riku[i][j] = a;
}
REP(i, M) {
int x, y, l;
char ty;
cin >> x >> y >> l >> ty;
if (ty == 'L') {
riku[x][y] = l;
riku[y][x] = l;
} else {
umi[x][y] = l;
umi[y][x] = l;
}
}
// ???
for (int k = 1; k <= N; k++) {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
umi[i][j] = std::min(umi[i][j], umi[i][k] + umi[k][j]);
riku[i][j] = std::min(riku[i][j], riku[i][k] + riku[k][j]);
}
}
}
int R;
int list[1010];
cin >> R;
REP(i, R) { cin >> list[i]; }
typedef pair<int, pair<int, int>> P;
priority_queue<P, vector<P>, greater<P>> que;
REP(i, 210) REP(f, 1010) {
cmpl[f][i] = IPPAI;
used[f][i] = false;
}
cmpl[0][list[0]] = 0;
que.push(make_pair(0, make_pair(0, list[0])));
while (!que.empty()) {
P p = que.top();
que.pop();
pair<int, int> pa = p.second;
int adv = pa.first;
int nowpos = list[adv];
int funepos = pa.second;
if (cmpl[adv][funepos] < p.first) {
continue;
}
if (adv + 1 >= R) {
continue;
}
for (int i = 1; i <= N; i++) {
// 1????????????????????????i???
int nextpos = list[adv + 1];
int cost;
// cout << "adv=" << adv << ", funepos=" << funepos << ", i=" << i <<
// endl;
// ??????????????????
if (funepos == i) {
if (riku[nowpos][nextpos] == IPPAI) {
continue;
}
cost = riku[nowpos][nextpos];
} else {
// ????????\?????´??????????????°???????????§????????§??????
if (riku[nowpos][funepos] == IPPAI) {
continue;
}
cost = riku[nowpos][funepos];
// ?????§ i ?????§?§??????????
if (umi[funepos][i] == IPPAI) {
continue;
}
cost += umi[funepos][i];
if (riku[i][nextpos] == IPPAI) {
continue;
}
cost += riku[i][nextpos];
// cout << riku[nowpos][funepos] << " " << umi[funepos][i] << " " <<
// riku[i][nextpos] << endl;
}
// cout << "cost = " << cost << endl;
if (cmpl[adv + 1][i] > cmpl[adv][funepos] + cost) {
cmpl[adv + 1][i] = cmpl[adv][funepos] + cost;
que.push(make_pair(cmpl[adv + 1][i], make_pair(adv + 1, i)));
}
}
}
int ret = IPPAI;
for (int i = 1; i <= N; i++) {
ret = std::min(ret, cmpl[R - 1][i]);
}
cout << ret << endl;
return true;
}
int main(void) {
while (solve()) {
}
} | replace | 123 | 125 | 123 | 125 | TLE | |
p01317 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 50;
ll land[200][200];
ll sea[200][200];
ll dp[1010][200];
int z[1000];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M, R;
while (cin >> N >> M, N) {
fill((ll *)begin(land), (ll *)end(land), INF);
fill((ll *)begin(sea), (ll *)end(sea), INF);
for (int i = 0; i < N; i++) {
land[i][i] = sea[i][i] = 0;
}
for (int i = 0; i < M; i++) {
int x, y;
ll t;
char sl;
cin >> x >> y >> t >> sl;
x--, y--;
if (sl == 'L') {
t = min(t, land[x][y]);
land[x][y] = land[y][x] = t;
} else {
t = min(t, sea[x][y]);
sea[x][y] = sea[y][x] = t;
}
}
for (int k = 0; k < N; k++) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
land[i][j] = min(land[i][j], land[i][k] + land[k][j]);
sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);
}
}
}
cin >> R;
for (int i = 0; i < R; i++) {
cin >> z[i];
z[i]--;
}
if (R == 1) {
cout << 0 << endl;
continue;
}
fill((ll *)begin(dp), (ll *)end(dp), INF);
dp[z[0]][z[0]] = 0;
for (int i = 0; i < R - 1; i++) {
for (int j = 0; j < N; j++) {
if (dp[i][j] == INF)
continue;
int A = z[i], B = z[i + 1];
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + land[A][B]);
for (int u = 0; u < N; u++) {
ll cost = land[A][j] + sea[j][u] + land[u][B];
if (cost < INF) {
dp[i + 1][u] = min(dp[i + 1][u], dp[i][j] + cost);
}
}
}
}
ll ans = INF;
for (int j = 0; j < N; j++) {
ans = min(ans, dp[R - 1][j]);
}
assert(ans < INF);
cout << ans << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 50;
ll land[200][200];
ll sea[200][200];
ll dp[1010][200];
int z[1000];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M, R;
while (cin >> N >> M, N) {
fill((ll *)begin(land), (ll *)end(land), INF);
fill((ll *)begin(sea), (ll *)end(sea), INF);
for (int i = 0; i < N; i++) {
land[i][i] = sea[i][i] = 0;
}
for (int i = 0; i < M; i++) {
int x, y;
ll t;
char sl;
cin >> x >> y >> t >> sl;
x--, y--;
if (sl == 'L') {
t = min(t, land[x][y]);
land[x][y] = land[y][x] = t;
} else {
t = min(t, sea[x][y]);
sea[x][y] = sea[y][x] = t;
}
}
for (int k = 0; k < N; k++) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
land[i][j] = min(land[i][j], land[i][k] + land[k][j]);
sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);
}
}
}
cin >> R;
for (int i = 0; i < R; i++) {
cin >> z[i];
z[i]--;
}
if (R == 1) {
cout << 0 << endl;
continue;
}
fill((ll *)begin(dp), (ll *)end(dp), INF);
dp[0][z[0]] = 0;
for (int i = 0; i < R - 1; i++) {
for (int j = 0; j < N; j++) {
if (dp[i][j] == INF)
continue;
int A = z[i], B = z[i + 1];
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + land[A][B]);
for (int u = 0; u < N; u++) {
ll cost = land[A][j] + sea[j][u] + land[u][B];
if (cost < INF) {
dp[i + 1][u] = min(dp[i + 1][u], dp[i][j] + cost);
}
}
}
}
ll ans = INF;
for (int j = 0; j < N; j++) {
ans = min(ans, dp[R - 1][j]);
}
assert(ans < INF);
cout << ans << endl;
}
} | replace | 58 | 59 | 58 | 59 | 0 | |
p01317 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 205
#define maxr 1005
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int fl[maxn][maxn], fs[maxn][maxn], dp[maxr][maxn], n, m, r, a[maxn];
int main() {
while (~scanf("%d%d", &n, &m) && (n + m) > 0) {
memset(fl, 0x3f, sizeof(fl));
memset(fs, 0x3f, sizeof(fs));
for (int i = 1; i <= n; i++)
fs[i][i] = fl[i][i] = 0;
for (int i = 1; i <= m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
char c;
while (scanf("%c", &c) && (c != 'S' && c != 'L'))
;
if (c == 'S')
fs[u][v] = fs[v][u] = min(fs[u][v], w);
else
fl[u][v] = fl[v][u] = min(fl[u][v], w);
}
scanf("%d", &r);
for (int i = 1; i <= r; i++)
scanf("%d", &a[i]);
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
fl[i][j] = min(fl[i][j], fl[i][k] + fl[k][j]);
fs[i][j] = min(fs[i][j], fs[i][k] + fs[k][j]);
}
}
}
memset(dp, 0x3f, sizeof(dp));
// a[0] = 1;
for (int i = 1; i <= n; i++)
dp[1][i] = fs[a[1]][i] + fl[i][a[1]];
for (int i = 2; i <= r; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
ll res;
if (j == k)
res = (ll)dp[i - 1][k] + (ll)fl[a[i - 1]][a[i]];
else
res = (ll)dp[i - 1][k] + (ll)fl[a[i - 1]][k] + (ll)fs[k][j] +
(ll)fl[j][a[i]];
dp[i][j] = min((ll)dp[i][j], res);
}
}
}
int ans = inf;
for (int i = 1; i <= n; i++) {
ans = min(ans, dp[r][i]);
}
printf("%d\n", ans);
}
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 205
#define maxr 1005
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int fl[maxn][maxn], fs[maxn][maxn], dp[maxr][maxn], n, m, r, a[maxr];
int main() {
while (~scanf("%d%d", &n, &m) && (n + m) > 0) {
memset(fl, 0x3f, sizeof(fl));
memset(fs, 0x3f, sizeof(fs));
for (int i = 1; i <= n; i++)
fs[i][i] = fl[i][i] = 0;
for (int i = 1; i <= m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
char c;
while (scanf("%c", &c) && (c != 'S' && c != 'L'))
;
if (c == 'S')
fs[u][v] = fs[v][u] = min(fs[u][v], w);
else
fl[u][v] = fl[v][u] = min(fl[u][v], w);
}
scanf("%d", &r);
for (int i = 1; i <= r; i++)
scanf("%d", &a[i]);
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
fl[i][j] = min(fl[i][j], fl[i][k] + fl[k][j]);
fs[i][j] = min(fs[i][j], fs[i][k] + fs[k][j]);
}
}
}
memset(dp, 0x3f, sizeof(dp));
// a[0] = 1;
for (int i = 1; i <= n; i++)
dp[1][i] = fs[a[1]][i] + fl[i][a[1]];
for (int i = 2; i <= r; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
ll res;
if (j == k)
res = (ll)dp[i - 1][k] + (ll)fl[a[i - 1]][a[i]];
else
res = (ll)dp[i - 1][k] + (ll)fl[a[i - 1]][k] + (ll)fs[k][j] +
(ll)fl[j][a[i]];
dp[i][j] = min((ll)dp[i][j], res);
}
}
}
int ans = inf;
for (int i = 1; i <= n; i++) {
ans = min(ans, dp[r][i]);
}
printf("%d\n", ans);
}
return 0;
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p01317 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;
typedef int weight;
struct edge {
int to;
weight cost;
bool operator<(const edge &e) const { return cost < e.cost; }
bool operator>(const edge &e) const { return cost > e.cost; }
};
typedef vector<edge> Edges;
typedef vector<Edges> Graph;
// int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
bool valid(int x, int y, int h, int w) {
return (x >= 0 && y >= 0 && x < h && y < w);
}
int place(int x, int y, int w) { return w * x + y; }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///
int n, m, r;
int dl[201][201], ds[201][201], z[1001];
int dp[1001][201];
int solve(int i, int ship) {
if (i == r - 1)
return 0;
if (dp[i][ship] > 0)
return dp[i][ship];
int res = solve(i + 1, ship) + dl[z[i]][z[i + 1]];
REP(j, n) {
int sum = dl[z[i]][ship] + ds[ship][j] + dl[j][z[i + 1]];
chmin(res, solve(i + 1, j) + sum);
}
return dp[i][ship] = res;
}
int main() {
while (cin >> n >> m, n) {
MS(dp, -1);
REP(i, n) REP(j, n) {
dl[i][j] = (i == j ? 0 : INF);
ds[i][j] = (i == j ? 0 : INF);
}
REP(i, m) {
int x, y, t;
char c;
cin >> x >> y >> t >> c;
x--;
y--;
if (c == 'L')
dl[x][y] = dl[y][x] = t;
else
ds[x][y] = ds[y][x] = t;
}
REP(k, n) REP(i, n) REP(j, n) {
chmin(dl[i][j], dl[i][k] + dl[k][j]);
chmin(ds[i][j], ds[i][k] + ds[k][j]);
}
cin >> r;
REP(i, r) {
cin >> z[i];
z[i]--;
}
cout << solve(z[0], z[0]) << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;
typedef int weight;
struct edge {
int to;
weight cost;
bool operator<(const edge &e) const { return cost < e.cost; }
bool operator>(const edge &e) const { return cost > e.cost; }
};
typedef vector<edge> Edges;
typedef vector<Edges> Graph;
// int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
bool valid(int x, int y, int h, int w) {
return (x >= 0 && y >= 0 && x < h && y < w);
}
int place(int x, int y, int w) { return w * x + y; }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///
int n, m, r;
int dl[201][201], ds[201][201], z[1001];
int dp[1001][201];
int solve(int i, int ship) {
if (i == r - 1)
return 0;
if (dp[i][ship] > 0)
return dp[i][ship];
int res = solve(i + 1, ship) + dl[z[i]][z[i + 1]];
REP(j, n) {
int sum = dl[z[i]][ship] + ds[ship][j] + dl[j][z[i + 1]];
chmin(res, solve(i + 1, j) + sum);
}
return dp[i][ship] = res;
}
int main() {
while (cin >> n >> m, n) {
MS(dp, -1);
REP(i, n) REP(j, n) {
dl[i][j] = (i == j ? 0 : INF);
ds[i][j] = (i == j ? 0 : INF);
}
REP(i, m) {
int x, y, t;
char c;
cin >> x >> y >> t >> c;
x--;
y--;
if (c == 'L')
dl[x][y] = dl[y][x] = t;
else
ds[x][y] = ds[y][x] = t;
}
REP(k, n) REP(i, n) REP(j, n) {
chmin(dl[i][j], dl[i][k] + dl[k][j]);
chmin(ds[i][j], ds[i][k] + ds[k][j]);
}
cin >> r;
REP(i, r) {
cin >> z[i];
z[i]--;
}
cout << solve(0, z[0]) << endl;
}
return 0;
} | replace | 80 | 81 | 80 | 81 | 0 | |
p01317 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
const int INF = 1 << 22;
int N, M, R;
void wf(VVI &adj) {
for (int k = 0; k < N; ++k) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
}
}
}
}
void print_adj(VVI adj) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (adj[i][j] >= INF) {
cout << "INF"
<< " ";
} else {
cout << adj[i][j] << " ";
}
}
cout << endl;
}
}
void db_print(int line[]) {
for (int i = 0; i < N; ++i) {
if (line[i] >= INF) {
cout << "INF"
<< " ";
} else {
cout << line[i] << " ";
}
}
cout << endl;
}
int solve(VVI sea, VVI land, VI que) {
int table[R][N];
fill_n((int *)table, (R + 1) * N, INF);
table[0][que[0]] = 0;
for (int r = 1; r < R; ++r) {
// db_print(table[r-1]);
for (int from = 0; from < N; ++from) {
for (int to = 0; to < N; ++to) {
if (from == to) {
int cost = table[r - 1][from];
cost += land[que[r - 1]][que[r]];
table[r][to] = min(table[r][to], cost);
} else {
int cost = table[r - 1][from];
cost += land[que[r - 1]][from];
cost += sea[from][to];
cost += land[to][que[r]];
table[r][to] = min(table[r][to], cost);
}
}
}
for (int i = 0; i < N; ++i) {
if (table[r][i] > INF) {
table[r][i] = INF;
}
}
}
int res = INF;
// db_print(table[R-1]);
for (int i = 0; i < N; ++i) {
res = min(res, table[R - 1][i]);
}
return res;
}
int main() {
while (true) {
cin >> N >> M;
if ((N | M) == 0) {
break;
}
VVI sea(N, vector<int>(N, INF));
VVI land(N, vector<int>(N, INF));
for (int i = 0; i < N; ++i) {
sea[i][i] = 0;
land[i][i] = 0;
}
for (int i = 0; i < M; ++i) {
int x, y, t;
char sl;
cin >> x >> y >> t >> sl;
--x;
--y;
if (sl == 'S') {
sea[x][y] = sea[y][x] = min(sea[y][x], t);
} else {
land[x][y] = land[y][x] = min(land[y][x], t);
}
}
wf(sea);
wf(land);
// print_adj(sea);
// print_adj(land);
cin >> R;
vector<int> que(R);
for (int i = 0; i < R; ++i) {
cin >> que[i];
--que[i];
}
cout << solve(sea, land, que) << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
const int INF = 1 << 22;
int N, M, R;
void wf(VVI &adj) {
for (int k = 0; k < N; ++k) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
}
}
}
}
void print_adj(VVI adj) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (adj[i][j] >= INF) {
cout << "INF"
<< " ";
} else {
cout << adj[i][j] << " ";
}
}
cout << endl;
}
}
void db_print(int line[]) {
for (int i = 0; i < N; ++i) {
if (line[i] >= INF) {
cout << "INF"
<< " ";
} else {
cout << line[i] << " ";
}
}
cout << endl;
}
int solve(VVI sea, VVI land, VI que) {
int table[R][N];
fill_n((int *)table, R * N, INF);
table[0][que[0]] = 0;
for (int r = 1; r < R; ++r) {
// db_print(table[r-1]);
for (int from = 0; from < N; ++from) {
for (int to = 0; to < N; ++to) {
if (from == to) {
int cost = table[r - 1][from];
cost += land[que[r - 1]][que[r]];
table[r][to] = min(table[r][to], cost);
} else {
int cost = table[r - 1][from];
cost += land[que[r - 1]][from];
cost += sea[from][to];
cost += land[to][que[r]];
table[r][to] = min(table[r][to], cost);
}
}
}
for (int i = 0; i < N; ++i) {
if (table[r][i] > INF) {
table[r][i] = INF;
}
}
}
int res = INF;
// db_print(table[R-1]);
for (int i = 0; i < N; ++i) {
res = min(res, table[R - 1][i]);
}
return res;
}
int main() {
while (true) {
cin >> N >> M;
if ((N | M) == 0) {
break;
}
VVI sea(N, vector<int>(N, INF));
VVI land(N, vector<int>(N, INF));
for (int i = 0; i < N; ++i) {
sea[i][i] = 0;
land[i][i] = 0;
}
for (int i = 0; i < M; ++i) {
int x, y, t;
char sl;
cin >> x >> y >> t >> sl;
--x;
--y;
if (sl == 'S') {
sea[x][y] = sea[y][x] = min(sea[y][x], t);
} else {
land[x][y] = land[y][x] = min(land[y][x], t);
}
}
wf(sea);
wf(land);
// print_adj(sea);
// print_adj(land);
cin >> R;
vector<int> que(R);
for (int i = 0; i < R; ++i) {
cin >> que[i];
--que[i];
}
cout << solve(sea, land, que) << endl;
}
return 0;
} | replace | 45 | 46 | 45 | 46 | 0 | |
p01317 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef ll li;
typedef pair<int, int> PI;
#define EPS (1e-10L)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, n) rep(i, n)
#define F first
#define S second
#define mp(a, b) make_pair((a), (b))
#define list3(a, b, c) mp((a), mp((b), (c)))
#define list4(a, b, c, d) mp((a), list3((b), (c), (d)))
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define CLR(a) memset((a), 0, sizeof(a))
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &val) {
return out << "(" << val.F << ", " << val.S << ")";
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &val) {
out << "{";
rep(i, SZ(val)) out << (i ? ", " : "") << val[i];
return out << "}";
}
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};
int n, m;
ll scost[200][200];
ll lcost[200][200];
int z[1000];
ll cost[1000][200];
void solve() {
rep(i, n) rep(j, n) scost[i][j] = lcost[i][j] = 1LL << 30LL;
rep(i, n) scost[i][i] = lcost[i][i] = 0;
rep(i, m) {
ll x, y, t;
char s;
cin >> x >> y >> t >> s;
--x, --y;
if (s == 'S')
scost[x][y] = scost[y][x] = min(scost[x][y], t);
else
lcost[x][y] = lcost[y][x] = min(lcost[x][y], t);
}
rep(k, n) rep(i, n) rep(j, n) {
scost[i][j] = min(scost[i][j], scost[i][k] + scost[k][j]);
lcost[i][j] = min(lcost[i][j], lcost[i][k] + lcost[k][j]);
}
int r;
cin >> r;
rep(i, r) {
cin >> z[i];
--z[i];
}
rep(i, r + 1) rep(j, n) cost[i][j] = 1LL << 50LL;
cost[0][z[0]] = 0;
ll ans = 1LL << 60LL;
rep(i, r - 1) rep(j, n) {
rep(k, n) {
ll nc = cost[i][j];
if (k == j)
nc += lcost[z[i]][z[i + 1]];
else
nc += lcost[z[i]][j] + scost[j][k] + lcost[k][z[i + 1]];
cost[i + 1][k] = min(cost[i + 1][k], nc);
if (i + 1 == r - 1)
ans = min(ans, cost[i + 1][k]);
}
}
cout << ans << endl;
}
int main() {
while (cin >> n >> m && n)
solve();
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef ll li;
typedef pair<int, int> PI;
#define EPS (1e-10L)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, n) rep(i, n)
#define F first
#define S second
#define mp(a, b) make_pair((a), (b))
#define list3(a, b, c) mp((a), mp((b), (c)))
#define list4(a, b, c, d) mp((a), list3((b), (c), (d)))
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define CLR(a) memset((a), 0, sizeof(a))
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &val) {
return out << "(" << val.F << ", " << val.S << ")";
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &val) {
out << "{";
rep(i, SZ(val)) out << (i ? ", " : "") << val[i];
return out << "}";
}
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};
int n, m;
ll scost[200][200];
ll lcost[200][200];
int z[1000];
ll cost[1000][200];
void solve() {
rep(i, n) rep(j, n) scost[i][j] = lcost[i][j] = 1LL << 30LL;
rep(i, n) scost[i][i] = lcost[i][i] = 0;
rep(i, m) {
ll x, y, t;
char s;
cin >> x >> y >> t >> s;
--x, --y;
if (s == 'S')
scost[x][y] = scost[y][x] = min(scost[x][y], t);
else
lcost[x][y] = lcost[y][x] = min(lcost[x][y], t);
}
rep(k, n) rep(i, n) rep(j, n) {
scost[i][j] = min(scost[i][j], scost[i][k] + scost[k][j]);
lcost[i][j] = min(lcost[i][j], lcost[i][k] + lcost[k][j]);
}
int r;
cin >> r;
rep(i, r) {
cin >> z[i];
--z[i];
}
rep(i, r) rep(j, n) cost[i][j] = 1LL << 50LL;
cost[0][z[0]] = 0;
ll ans = 1LL << 60LL;
rep(i, r - 1) rep(j, n) {
rep(k, n) {
ll nc = cost[i][j];
if (k == j)
nc += lcost[z[i]][z[i + 1]];
else
nc += lcost[z[i]][j] + scost[j][k] + lcost[k][z[i + 1]];
cost[i + 1][k] = min(cost[i + 1][k], nc);
if (i + 1 == r - 1)
ans = min(ans, cost[i + 1][k]);
}
}
cout << ans << endl;
}
int main() {
while (cin >> n >> m && n)
solve();
} | replace | 77 | 78 | 77 | 78 | 0 | |
p01317 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
int v1[203][203], v2[203][203], a1, a2, a3, n, m;
int dp[2][203][203], t, x, pre;
char c;
int main() {
while (cin >> n >> m, n) {
int ans = 1e8;
r(i, 2) r(j, 203) r(k, 203) dp[i][j][k] = 1e8;
r(i, 203) r(j, 203) v1[i][j] = v2[j][i] = (i == j ? 0 : 1e8);
r(i, n) {
cin >> a1 >> a2 >> a3 >> c;
a1--, a2--;
if (c == 'L')
v1[a1][a2] = v1[a2][a1] = a3;
else
v2[a1][a2] = v2[a2][a1] = a3;
}
r(k, n) r(i, n) r(j, n) {
v1[i][j] = min(v1[i][j], v1[i][k] + v1[k][j]);
v2[i][j] = min(v2[i][j], v2[i][k] + v2[k][j]);
}
cin >> t >> pre;
pre--;
dp[0][pre][pre] = 0;
for (int k = 0; k < t - 1; k++) {
cin >> x;
x--;
for (int i = 0; i < n; i++) {
dp[(k + 1) % 2][x][i] =
min(dp[(k + 1) % 2][x][i], dp[k % 2][pre][i] + v1[pre][x]);
for (int j = 0; j < n; j++) {
dp[(k + 1) % 2][x][j] =
min(dp[(k + 1) % 2][x][j],
dp[k % 2][pre][i] + v1[pre][i] + v2[i][j] + v1[j][x]);
}
}
r(i, 203) r(j, 203) dp[k % 2][i][j] = 1e8;
pre = x;
}
r(i, 203) ans = min(ans, dp[(t - 1) % 2][pre][i]);
cout << ans << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
int v1[203][203], v2[203][203], a1, a2, a3, n, m;
int dp[2][203][203], t, x, pre;
char c;
int main() {
while (cin >> n >> m, n) {
int ans = 1e8;
r(i, 2) r(j, 203) r(k, 203) dp[i][j][k] = 1e8;
r(i, 203) r(j, 203) v1[i][j] = v2[j][i] = (i == j ? 0 : 1e8);
r(i, m) {
cin >> a1 >> a2 >> a3 >> c;
a1--, a2--;
if (c == 'L')
v1[a1][a2] = v1[a2][a1] = a3;
else
v2[a1][a2] = v2[a2][a1] = a3;
}
r(k, n) r(i, n) r(j, n) {
v1[i][j] = min(v1[i][j], v1[i][k] + v1[k][j]);
v2[i][j] = min(v2[i][j], v2[i][k] + v2[k][j]);
}
cin >> t >> pre;
pre--;
dp[0][pre][pre] = 0;
for (int k = 0; k < t - 1; k++) {
cin >> x;
x--;
for (int i = 0; i < n; i++) {
dp[(k + 1) % 2][x][i] =
min(dp[(k + 1) % 2][x][i], dp[k % 2][pre][i] + v1[pre][x]);
for (int j = 0; j < n; j++) {
dp[(k + 1) % 2][x][j] =
min(dp[(k + 1) % 2][x][j],
dp[k % 2][pre][i] + v1[pre][i] + v2[i][j] + v1[j][x]);
}
}
r(i, 203) r(j, 203) dp[k % 2][i][j] = 1e8;
pre = x;
}
r(i, 203) ans = min(ans, dp[(t - 1) % 2][pre][i]);
cout << ans << endl;
}
} | replace | 11 | 12 | 11 | 12 | 0 | |
p01317 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;
typedef long long ll;
typedef ll Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<string> vs;
Def inf = sizeof(Def) == sizeof(ll) ? 2e15 : 1e9 + 10;
#include <complex>
typedef complex<double> P;
typedef vector<P> G;
const double PI = acos(-1);
const double EPS = 1e-7;
struct L : public vector<P> {
L(const P &a, const P &b) {
push_back(a);
push_back(b);
}
};
struct C {
P c;
double r;
C(const P &c, double r) : c(c), r(r) {}
};
namespace std {
bool operator<(const P &a, const P &b) {
return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
bool operator==(const P &a, const P &b) {
return a.real() == b.real() && a.imag() == b.imag();
}
} // namespace std
double dot(P a, P b) { return real(conj(a) * b); }
P projection(L a, P p) {
double t = dot(p - a[0], a[0] - a[1]) / norm(a[0] - a[1]);
return a[0] + t * (a[0] - a[1]);
}
double distanceLP(const L &l, const P &p) { return abs(p - projection(l, p)); }
P turn(P p, double t) { return p * exp(P(.0, t * PI / 180.0)); }
vector<L> tangentCC(C a, C b) {
if (a.r < b.r)
swap(a, b);
double d = abs(a.c - b.c);
vector<L> l;
if (d < EPS)
return l;
if (a.r + b.r < d - EPS) {
double t = acos((a.r + b.r) / d);
t = t * 180 / PI;
l.pb(L(a.c + turn(a.r / d * (b.c - a.c), t),
b.c + turn(b.r / d * (a.c - b.c), t)));
l.pb(L(a.c + turn(a.r / d * (b.c - a.c), -t),
b.c + turn(b.r / d * (a.c - b.c), -t)));
} else if (a.r + b.r < d + EPS) {
P p = a.c + a.r / d * (b.c - a.c);
l.pb(L(p, p + turn(b.c - a.c, 90)));
}
if (abs(a.r - b.r) < d - EPS) {
double t1 = acos((a.r - b.r) / d);
t1 = t1 * 180 / PI;
double t2 = 180 - t1;
l.pb(L(a.c + turn(a.r / d * (b.c - a.c), t1),
b.c + turn(b.r / d * (a.c - b.c), -t2)));
l.pb(L(a.c + turn(a.r / d * (b.c - a.c), -t1),
b.c + turn(b.r / d * (a.c - b.c), t2)));
} else if (abs(a.r - b.r) < d + EPS) {
P p = a.c + a.r / d * (b.c - a.c);
l.pb(L(p, p + turn(b.c - a.c, 90)));
}
return l;
}
int main() {
int n, m;
while (cin >> n >> m, n) {
vvi L(n, vi(n, inf));
vvi S(n, vi(n, inf));
rep(i, n) L[i][i] = S[i][i] = 0;
rep(i, m) {
ll a, b, c;
char d;
cin >> a >> b >> c >> d;
a--;
b--;
if (d == 'L')
L[a][b] = L[b][a] = min(L[a][b], c);
if (d == 'S')
S[a][b] = S[b][a] = min(S[a][b], c);
}
rep(k, n) rep(i, n) rep(j, n) {
L[i][j] = min(L[i][j], L[i][k] + L[k][j]);
S[i][j] = min(S[i][j], S[i][k] + S[k][j]);
}
int R;
cin >> R;
vi r(R);
rep(i, R) cin >> r[i];
rep(i, R) r[i]--;
vvi dp(R, vi(n, inf));
dp[r[0]][0] = 0;
rep(i, R - 1) rep(j, n) {
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + L[r[i]][r[i + 1]]);
rep(k, n) {
dp[i + 1][k] =
min(dp[i + 1][k], dp[i][j] + L[r[i]][j] + S[j][k] + L[k][r[i + 1]]);
}
}
// rep(i,R){rep(j,n)cout<<" "<<dp[i][j];cout<<endl;}
ll out = inf;
rep(i, n) out = min(out, dp[R - 1][i]);
cout << out << endl;
}
}
| #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;
typedef long long ll;
typedef ll Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<string> vs;
Def inf = sizeof(Def) == sizeof(ll) ? 2e15 : 1e9 + 10;
#include <complex>
typedef complex<double> P;
typedef vector<P> G;
const double PI = acos(-1);
const double EPS = 1e-7;
struct L : public vector<P> {
L(const P &a, const P &b) {
push_back(a);
push_back(b);
}
};
struct C {
P c;
double r;
C(const P &c, double r) : c(c), r(r) {}
};
namespace std {
bool operator<(const P &a, const P &b) {
return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
bool operator==(const P &a, const P &b) {
return a.real() == b.real() && a.imag() == b.imag();
}
} // namespace std
double dot(P a, P b) { return real(conj(a) * b); }
P projection(L a, P p) {
double t = dot(p - a[0], a[0] - a[1]) / norm(a[0] - a[1]);
return a[0] + t * (a[0] - a[1]);
}
double distanceLP(const L &l, const P &p) { return abs(p - projection(l, p)); }
P turn(P p, double t) { return p * exp(P(.0, t * PI / 180.0)); }
vector<L> tangentCC(C a, C b) {
if (a.r < b.r)
swap(a, b);
double d = abs(a.c - b.c);
vector<L> l;
if (d < EPS)
return l;
if (a.r + b.r < d - EPS) {
double t = acos((a.r + b.r) / d);
t = t * 180 / PI;
l.pb(L(a.c + turn(a.r / d * (b.c - a.c), t),
b.c + turn(b.r / d * (a.c - b.c), t)));
l.pb(L(a.c + turn(a.r / d * (b.c - a.c), -t),
b.c + turn(b.r / d * (a.c - b.c), -t)));
} else if (a.r + b.r < d + EPS) {
P p = a.c + a.r / d * (b.c - a.c);
l.pb(L(p, p + turn(b.c - a.c, 90)));
}
if (abs(a.r - b.r) < d - EPS) {
double t1 = acos((a.r - b.r) / d);
t1 = t1 * 180 / PI;
double t2 = 180 - t1;
l.pb(L(a.c + turn(a.r / d * (b.c - a.c), t1),
b.c + turn(b.r / d * (a.c - b.c), -t2)));
l.pb(L(a.c + turn(a.r / d * (b.c - a.c), -t1),
b.c + turn(b.r / d * (a.c - b.c), t2)));
} else if (abs(a.r - b.r) < d + EPS) {
P p = a.c + a.r / d * (b.c - a.c);
l.pb(L(p, p + turn(b.c - a.c, 90)));
}
return l;
}
int main() {
int n, m;
while (cin >> n >> m, n) {
vvi L(n, vi(n, inf));
vvi S(n, vi(n, inf));
rep(i, n) L[i][i] = S[i][i] = 0;
rep(i, m) {
ll a, b, c;
char d;
cin >> a >> b >> c >> d;
a--;
b--;
if (d == 'L')
L[a][b] = L[b][a] = min(L[a][b], c);
if (d == 'S')
S[a][b] = S[b][a] = min(S[a][b], c);
}
rep(k, n) rep(i, n) rep(j, n) {
L[i][j] = min(L[i][j], L[i][k] + L[k][j]);
S[i][j] = min(S[i][j], S[i][k] + S[k][j]);
}
int R;
cin >> R;
vi r(R);
rep(i, R) cin >> r[i];
rep(i, R) r[i]--;
vvi dp(R, vi(n, inf));
dp[0][r[0]] = 0;
rep(i, R - 1) rep(j, n) {
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + L[r[i]][r[i + 1]]);
rep(k, n) {
dp[i + 1][k] =
min(dp[i + 1][k], dp[i][j] + L[r[i]][j] + S[j][k] + L[k][r[i + 1]]);
}
}
// rep(i,R){rep(j,n)cout<<" "<<dp[i][j];cout<<endl;}
ll out = inf;
rep(i, n) out = min(out, dp[R - 1][i]);
cout << out << endl;
}
}
| replace | 123 | 124 | 123 | 124 | 0 | |
p01318 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vii;
typedef vector<vector<string>> vss;
typedef pair<int, int> pii;
#define x first
#define y second
#define INF 0x7fffffff
// #define INFLL 0x7fffffffffffffff
#define INFLL LLONG_MAX
#define SIZE(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
#define FOR(i, x) for (int i = 0; i < x; i++)
#define FORic(i, y, x) for (int i = y; i <= x; i++)
#define FORdc(i, y, x) for (int i = y; i >= x; i--)
#define FILL(a, v) memset(a, v, sizeof(a))
#define PI 3.14159265358979323846
const double EPS = 1e-7;
typedef complex<double> P; /* _ */
typedef vector<P> G; /* ½p` */
struct L {
P p, q;
L(P p, P q) : p(p), q(q) {}
}; /* üª */
typedef pair<P, double> C; /* ~ */
// complexÉå¬ÖWªKvÈê
namespace std {
bool operator<(const P &a, const P &b) {
return make_pair(real(a), imag(a)) < make_pair(real(b), imag(b));
}
} // namespace std
// àÏEOÏEpx
double dot(P a, P b) { return real(conj(a) * b); }
double cross(P a, P b) { return imag(conj(a) * b); }
double angle(P a, P b) { return arg(conj(a) * b); }
// _pÆ´_Ì£yÌñæzðßé
// iüÍp1-p2ðøÉæêÎ2_ÔÌ£yÌñæzÆÈéj
double pLen2(P p) { return real(p) * real(p) + imag(p) * imag(p); }
// ½Ë(distSPÅgp)
P proj(L l, P p) {
double t = dot(p - l.p, l.p - l.q) / sqrt(pLen2(l.p - l.q));
return l.p + t * (l.p - l.q);
}
// ¼ülÆüªsªð··é©ð»è·é
bool lsIntersect(L l, L s) {
return abs(cross(l.q - l.p, s.p - l.p) * cross(l.q - l.p, s.q - l.p)) < EPS;
}
// üªaÆüªbªð··é©ð»è·é
bool ssIntersect(L a, L b) {
if (abs(imag((a.q - a.p) / (b.q - b.p))) < EPS)
return false;
return cross(a.q - a.p, b.p - a.p) * cross(a.q - a.p, b.q - a.p) < 0 &&
cross(b.q - b.p, a.p - b.p) * cross(b.q - b.p, a.q - b.p) < 0;
}
// _pª¼ülãɶݷé©ð»è
bool lpIntersect(L l, P p) { return abs(imag((p - l.p) / (l.q - l.p))) < EPS; }
// _pªüªlãɶݷé©ð»è·éi¸x¤j
bool spIntersect(L l, P p) {
// return abs( abs(l.p-p)+abs(l.q-p)-abs(l.q-l.p) ) < EPS; //
// ¸xðCɵȢÈç±êH
if (lpIntersect(l, p)) {
if ((real(l.p) - real(p)) * (real(l.q) - real(p)) <= 0) {
if ((imag(l.p) - imag(p)) * (imag(l.q) - imag(p)) <= 0) {
return true;
}
}
}
return false;
}
// üªaÆüªbÌð_ðvZ·éDæÉð·»èªK{D
P ssCrosspoint(L a, L b) {
double A = cross(a.q - a.p, b.q - b.p);
double B = cross(a.q - a.p, a.q - b.p);
return b.p + B / A * (b.q - b.p);
}
// ¼üaƼübª¯êÌàÌ©ð»è·é
bool sameLine(L a, L b) { return lpIntersect(a, b.p) && lpIntersect(a, b.q); }
// üªlÆ_pÌ£ðßé
double distLP(L l, P p) {
return abs(cross(l.q - l.p, p - l.p)) / abs(l.q - l.p);
}
// ¼üƼüÌ£ðßéiÀsÌj
double distLL(L l, L m) { return sameLine(l, m) ? 0.0 : distLP(l, m.p); }
// ¼ülÆüªsÌ£ðßé
double distLS(L l, L s) {
if (lsIntersect(l, s))
return 0.0;
return min(distLP(l, s.p), distLP(l, s.q));
}
// üªsÆ_pÌ£ðßé
double distSP(L s, P p) {
P r = proj(s, p);
if (spIntersect(s, r))
return abs(r - p);
return min(abs(s.p - p), abs(s.q - p));
}
// üªsÆüªtÌ£ðßé
double distSS(L s, L t) {
if (ssIntersect(s, t))
return 0;
return min(min(distSP(s, t.p), distSP(s, t.q)),
min(distSP(t, s.p), distSP(t, s.q)));
}
// üªaÆüªbªI[o[bvµÄ¢é©ð»è·é
bool ssOverlap(L a, L b) {
if (spIntersect(a, b.p) && lpIntersect(a, b.q))
return true;
if (spIntersect(a, b.q) && lpIntersect(a, b.p))
return true;
if (spIntersect(b, a.p) && lpIntersect(b, a.q))
return true;
if (spIntersect(b, a.q) && lpIntersect(b, a.p))
return true;
return false;
}
// ¤ÊÚüðresÉÇÁ·é
void getCommonTangent(vector<L> &res, C c1, C c2) {
if (c1.second > c2.second)
swap(c1, c2);
P dir = c1.first - c2.first;
double dist = abs(c1.first - c2.first);
dir = dir / dist * c2.second;
if (dist + c1.second < c2.second - EPS)
return; // ¬~ªå~Éàï³êÄ¢é
for (int sgn = -1; sgn <= 1; sgn += 2) {
double cs = (c2.second + sgn * c1.second) / dist;
P d = P(cs, sqrt(1 - cs * cs));
res.push_back(L(c2.first + dir * d, c2.first + dir * d * P(1, 1)));
if (imag(d) > EPS)
res.push_back(
L(c2.first + dir * conj(d), c2.first + dir * conj(d) * P(1, 1)));
if (dist + EPS < c1.second + c2.second)
break;
}
}
// ½p`gÌÊÏðßé
double polygonArea(G g) {
double res = 0.0;
for (int i = 0; i < g.size(); i++)
res += cross(g[i], g[(i + 1) % g.size()]);
return abs(0.5 * res);
}
// _pª½p`gÌàÉ é©Ç¤©ð»è·é
bool contains(G g, P p) {
bool in = false;
for (int i = 0; i < g.size(); i++) {
P a(g[i] - p), b(g[(i + 1) % g.size()] - p);
if (imag(a) > imag(b))
swap(a, b);
if (imag(a) <= 0 && 0 < imag(b))
if (cross(a, b) < 0)
in = !in;
if (abs(cross(a, b)) < EPS && dot(a, b) < EPS)
return true; // üãÉpª¶Ý
}
return in;
}
// ¼üÌWresÅd¡·é¼üðíµCresðXV·é
void deleteSameLine(vector<L> &res) {
int n = res.size();
if (n > 100000)
cout << "baka" << endl;
for (int i = 0; i < n; i++) {
vector<L>::iterator it = res.begin() + i + 1;
while (it < res.end()) {
if (sameLine(
res[i],
(*it))) { /* ±±ðϦêÎpªø */
it = res.erase(it);
n--;
} else {
it++;
}
}
}
}
int main() {
int n;
while (cin >> n) {
if (!n)
break;
vi x, y, r, m;
FOR(i, n) {
int tx, ty, tr, tm;
cin >> tx >> ty >> tr >> tm;
x.push_back(tx);
y.push_back(ty);
r.push_back(tr);
m.push_back(tm);
}
if (n == 1) {
cout << 1 << endl;
continue;
}
vector<L> all;
FORic(i, 0, n - 2) {
FORic(j, i + 1, n - 1) {
getCommonTangent(all, C(P(x[i], y[i]), r[i]), C(P(x[j], y[j]), r[j]));
getCommonTangent(all, C(P(x[i], y[i]), r[i] + m[i]),
C(P(x[j], y[j]), r[j]));
getCommonTangent(all, C(P(x[i], y[i]), r[i]),
C(P(x[j], y[j]), r[j] + m[j]));
getCommonTangent(all, C(P(x[i], y[i]), r[i] + m[i]),
C(P(x[j], y[j]), r[j] + m[j]));
}
}
deleteSameLine(all);
int ans = 0;
FOR(i, SIZE(all)) {
int count = 0;
FOR(j, n) {
double d = distLP(all[i], P(x[j], y[j]));
if (r[j] < d + EPS && d - EPS < r[j] + m[j])
count++;
}
ans = max(ans, count);
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vii;
typedef vector<vector<string>> vss;
typedef pair<int, int> pii;
#define x first
#define y second
#define INF 0x7fffffff
// #define INFLL 0x7fffffffffffffff
#define INFLL LLONG_MAX
#define SIZE(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
#define FOR(i, x) for (int i = 0; i < x; i++)
#define FORic(i, y, x) for (int i = y; i <= x; i++)
#define FORdc(i, y, x) for (int i = y; i >= x; i--)
#define FILL(a, v) memset(a, v, sizeof(a))
#define PI 3.14159265358979323846
const double EPS = 1e-7;
typedef complex<double> P; /* _ */
typedef vector<P> G; /* ½p` */
struct L {
P p, q;
L(P p, P q) : p(p), q(q) {}
}; /* üª */
typedef pair<P, double> C; /* ~ */
// complexÉå¬ÖWªKvÈê
namespace std {
bool operator<(const P &a, const P &b) {
return make_pair(real(a), imag(a)) < make_pair(real(b), imag(b));
}
} // namespace std
// àÏEOÏEpx
double dot(P a, P b) { return real(conj(a) * b); }
double cross(P a, P b) { return imag(conj(a) * b); }
double angle(P a, P b) { return arg(conj(a) * b); }
// _pÆ´_Ì£yÌñæzðßé
// iüÍp1-p2ðøÉæêÎ2_ÔÌ£yÌñæzÆÈéj
double pLen2(P p) { return real(p) * real(p) + imag(p) * imag(p); }
// ½Ë(distSPÅgp)
P proj(L l, P p) {
double t = dot(p - l.p, l.p - l.q) / sqrt(pLen2(l.p - l.q));
return l.p + t * (l.p - l.q);
}
// ¼ülÆüªsªð··é©ð»è·é
bool lsIntersect(L l, L s) {
return abs(cross(l.q - l.p, s.p - l.p) * cross(l.q - l.p, s.q - l.p)) < EPS;
}
// üªaÆüªbªð··é©ð»è·é
bool ssIntersect(L a, L b) {
if (abs(imag((a.q - a.p) / (b.q - b.p))) < EPS)
return false;
return cross(a.q - a.p, b.p - a.p) * cross(a.q - a.p, b.q - a.p) < 0 &&
cross(b.q - b.p, a.p - b.p) * cross(b.q - b.p, a.q - b.p) < 0;
}
// _pª¼ülãɶݷé©ð»è
bool lpIntersect(L l, P p) { return abs(imag((p - l.p) / (l.q - l.p))) < EPS; }
// _pªüªlãɶݷé©ð»è·éi¸x¤j
bool spIntersect(L l, P p) {
// return abs( abs(l.p-p)+abs(l.q-p)-abs(l.q-l.p) ) < EPS; //
// ¸xðCɵȢÈç±êH
if (lpIntersect(l, p)) {
if ((real(l.p) - real(p)) * (real(l.q) - real(p)) <= 0) {
if ((imag(l.p) - imag(p)) * (imag(l.q) - imag(p)) <= 0) {
return true;
}
}
}
return false;
}
// üªaÆüªbÌð_ðvZ·éDæÉð·»èªK{D
P ssCrosspoint(L a, L b) {
double A = cross(a.q - a.p, b.q - b.p);
double B = cross(a.q - a.p, a.q - b.p);
return b.p + B / A * (b.q - b.p);
}
// ¼üaƼübª¯êÌàÌ©ð»è·é
bool sameLine(L a, L b) { return lpIntersect(a, b.p) && lpIntersect(a, b.q); }
// üªlÆ_pÌ£ðßé
double distLP(L l, P p) {
return abs(cross(l.q - l.p, p - l.p)) / abs(l.q - l.p);
}
// ¼üƼüÌ£ðßéiÀsÌj
double distLL(L l, L m) { return sameLine(l, m) ? 0.0 : distLP(l, m.p); }
// ¼ülÆüªsÌ£ðßé
double distLS(L l, L s) {
if (lsIntersect(l, s))
return 0.0;
return min(distLP(l, s.p), distLP(l, s.q));
}
// üªsÆ_pÌ£ðßé
double distSP(L s, P p) {
P r = proj(s, p);
if (spIntersect(s, r))
return abs(r - p);
return min(abs(s.p - p), abs(s.q - p));
}
// üªsÆüªtÌ£ðßé
double distSS(L s, L t) {
if (ssIntersect(s, t))
return 0;
return min(min(distSP(s, t.p), distSP(s, t.q)),
min(distSP(t, s.p), distSP(t, s.q)));
}
// üªaÆüªbªI[o[bvµÄ¢é©ð»è·é
bool ssOverlap(L a, L b) {
if (spIntersect(a, b.p) && lpIntersect(a, b.q))
return true;
if (spIntersect(a, b.q) && lpIntersect(a, b.p))
return true;
if (spIntersect(b, a.p) && lpIntersect(b, a.q))
return true;
if (spIntersect(b, a.q) && lpIntersect(b, a.p))
return true;
return false;
}
// ¤ÊÚüðresÉÇÁ·é
void getCommonTangent(vector<L> &res, C c1, C c2) {
if (c1.second > c2.second)
swap(c1, c2);
P dir = c1.first - c2.first;
double dist = abs(c1.first - c2.first);
dir = dir / dist * c2.second;
if (dist + c1.second < c2.second - EPS)
return; // ¬~ªå~Éàï³êÄ¢é
for (int sgn = -1; sgn <= 1; sgn += 2) {
double cs = (c2.second + sgn * c1.second) / dist;
P d = P(cs, sqrt(1 - cs * cs));
res.push_back(L(c2.first + dir * d, c2.first + dir * d * P(1, 1)));
if (imag(d) > EPS)
res.push_back(
L(c2.first + dir * conj(d), c2.first + dir * conj(d) * P(1, 1)));
if (dist + EPS < c1.second + c2.second)
break;
}
}
// ½p`gÌÊÏðßé
double polygonArea(G g) {
double res = 0.0;
for (int i = 0; i < g.size(); i++)
res += cross(g[i], g[(i + 1) % g.size()]);
return abs(0.5 * res);
}
// _pª½p`gÌàÉ é©Ç¤©ð»è·é
bool contains(G g, P p) {
bool in = false;
for (int i = 0; i < g.size(); i++) {
P a(g[i] - p), b(g[(i + 1) % g.size()] - p);
if (imag(a) > imag(b))
swap(a, b);
if (imag(a) <= 0 && 0 < imag(b))
if (cross(a, b) < 0)
in = !in;
if (abs(cross(a, b)) < EPS && dot(a, b) < EPS)
return true; // üãÉpª¶Ý
}
return in;
}
// ¼üÌWresÅd¡·é¼üðíµCresðXV·é
void deleteSameLine(vector<L> &res) {
int n = res.size();
if (n > 100000)
cout << "baka" << endl;
for (int i = 0; i < n; i++) {
vector<L>::iterator it = res.begin() + i + 1;
while (it < res.end()) {
if (sameLine(
res[i],
(*it))) { /* ±±ðϦêÎpªø */
it = res.erase(it);
n--;
} else {
it++;
}
}
}
}
int main() {
int n;
while (cin >> n) {
if (!n)
break;
vi x, y, r, m;
FOR(i, n) {
int tx, ty, tr, tm;
cin >> tx >> ty >> tr >> tm;
x.push_back(tx);
y.push_back(ty);
r.push_back(tr);
m.push_back(tm);
}
if (n == 1) {
cout << 1 << endl;
continue;
}
vector<L> all;
FORic(i, 0, n - 2) {
FORic(j, i + 1, n - 1) {
getCommonTangent(all, C(P(x[i], y[i]), r[i]), C(P(x[j], y[j]), r[j]));
getCommonTangent(all, C(P(x[i], y[i]), r[i] + m[i]),
C(P(x[j], y[j]), r[j]));
getCommonTangent(all, C(P(x[i], y[i]), r[i]),
C(P(x[j], y[j]), r[j] + m[j]));
getCommonTangent(all, C(P(x[i], y[i]), r[i] + m[i]),
C(P(x[j], y[j]), r[j] + m[j]));
}
}
// deleteSameLine(all);
int ans = 0;
FOR(i, SIZE(all)) {
int count = 0;
FOR(j, n) {
double d = distLP(all[i], P(x[j], y[j]));
if (r[j] < d + EPS && d - EPS < r[j] + m[j])
count++;
}
ans = max(ans, count);
}
cout << ans << endl;
}
return 0;
} | replace | 243 | 244 | 243 | 244 | TLE | |
p01319 | C++ | Runtime Error | #include <algorithm>
#include <assert.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))
struct Gate {
int x;
double first;
double second;
Gate() { ; }
Gate(int x, double f, double d) : x(x), first(f), second(d) { ; }
};
int n, m, k;
Gate gate[200];
int exist[1200];
double dp[2][2000];
double watertime[2000];
const int OFFSET = 200;
int main() {
while (scanf("%d %d %d", &n, &m, &k), n | m | k) {
MEMSET(exist, -1);
MEMSET(watertime, 0);
REP(i, n) {
int x, ud;
double l, f, d;
cin >> x >> l >> f >> d >> ud;
f = l / f;
d = l / d;
if (ud) {
swap(f, d);
watertime[x + OFFSET] = d;
}
gate[i] = Gate(x + OFFSET, f, d);
exist[x + OFFSET] = i;
}
fill(dp[0], dp[0] + 2000, -100000);
REP(iter, m) {
double speed;
cin >> speed;
speed = 1.0 / speed;
int next = (iter + 1) & 1;
int prev = iter & 1;
fill(dp[next], dp[next] + 2000, 100000000);
int start = OFFSET - iter;
dp[next][start] = 0;
FOR(x, start, k + OFFSET * 2) {
if (exist[x] == -1) {
dp[next][x + 1] = dp[next][x] + speed;
;
} else if (exist[x] != -1) {
int index = exist[x];
double w = watertime[x];
dp[next][x + 1] = max(dp[next][x], w) + gate[index].first + speed;
watertime[x] =
max(dp[next][x], w) + gate[index].first + gate[index].second;
}
dp[next][x + 1] = max(dp[next][x + 1], dp[prev][x + 2]);
}
}
printf("%.6lf\n", dp[m & 1][k + OFFSET]);
}
} | #include <algorithm>
#include <assert.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))
struct Gate {
int x;
double first;
double second;
Gate() { ; }
Gate(int x, double f, double d) : x(x), first(f), second(d) { ; }
};
int n, m, k;
Gate gate[200];
int exist[2200];
double dp[2][2000];
double watertime[2000];
const int OFFSET = 200;
int main() {
while (scanf("%d %d %d", &n, &m, &k), n | m | k) {
MEMSET(exist, -1);
MEMSET(watertime, 0);
REP(i, n) {
int x, ud;
double l, f, d;
cin >> x >> l >> f >> d >> ud;
f = l / f;
d = l / d;
if (ud) {
swap(f, d);
watertime[x + OFFSET] = d;
}
gate[i] = Gate(x + OFFSET, f, d);
exist[x + OFFSET] = i;
}
fill(dp[0], dp[0] + 2000, -100000);
REP(iter, m) {
double speed;
cin >> speed;
speed = 1.0 / speed;
int next = (iter + 1) & 1;
int prev = iter & 1;
fill(dp[next], dp[next] + 2000, 100000000);
int start = OFFSET - iter;
dp[next][start] = 0;
FOR(x, start, k + OFFSET * 2) {
if (exist[x] == -1) {
dp[next][x + 1] = dp[next][x] + speed;
;
} else if (exist[x] != -1) {
int index = exist[x];
double w = watertime[x];
dp[next][x + 1] = max(dp[next][x], w) + gate[index].first + speed;
watertime[x] =
max(dp[next][x], w) + gate[index].first + gate[index].second;
}
dp[next][x + 1] = max(dp[next][x + 1], dp[prev][x + 2]);
}
}
printf("%.6lf\n", dp[m & 1][k + OFFSET]);
}
} | replace | 30 | 31 | 30 | 31 | 0 | |
p01321 | C++ | Runtime Error | #include <bits/stdc++.h>
#define INF 1 << 29
using namespace std;
int main() {
int a, b;
while (scanf("%d%d", &a), a) {
int MIN = 1 << 29, MAX = -(1 << 29);
for (int c = 0; c < a; c++) {
int s = 0;
int e;
for (int d = 0; d < 5; d++) {
scanf("%d", &e);
s += e;
}
MIN = min(MIN, s);
MAX = max(MAX, s);
}
printf("%d %d\n", MAX, MIN);
}
} | #include <bits/stdc++.h>
#define INF 1 << 29
using namespace std;
int main() {
int a, b;
while (scanf("%d", &a), a) {
int MIN = 1 << 29, MAX = -(1 << 29);
for (int c = 0; c < a; c++) {
int s = 0;
int e;
for (int d = 0; d < 5; d++) {
scanf("%d", &e);
s += e;
}
MIN = min(MIN, s);
MAX = max(MAX, s);
}
printf("%d %d\n", MAX, MIN);
}
} | replace | 6 | 7 | 6 | 7 | -11 | |
p01321 | C++ | Time Limit Exceeded | #include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define INF 10000000
int mini(int a, int b) {
if (a < b) {
return a;
} else {
return b;
}
}
int maxim(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
int main() {
while (1) {
int i, j, n, s, t, max = 0, min = 500;
cin >> n;
for (i = 0; i < n; i++) {
t = 0;
for (j = 0; j < 5; j++) {
cin >> s;
t += s;
}
max = maxim(max, t);
min = mini(min, t);
}
cout << max << ' ' << min << endl;
}
return 0;
} | #include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define INF 10000000
int mini(int a, int b) {
if (a < b) {
return a;
} else {
return b;
}
}
int maxim(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
int main() {
while (1) {
int i, j, n, s, t, max = 0, min = 500;
cin >> n;
if (n == 0)
break;
for (i = 0; i < n; i++) {
t = 0;
for (j = 0; j < 5; j++) {
cin >> s;
t += s;
}
max = maxim(max, t);
min = mini(min, t);
}
cout << max << ' ' << min << endl;
}
return 0;
} | insert | 27 | 27 | 27 | 29 | TLE | |
p01322 | C++ | Time Limit Exceeded | #include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
struct R {
string y;
int C;
};
int main() {
int N, M;
while (1) {
struct R n[101];
cin >> N >> M;
for (int i = 0; i < N; i++) {
cin >> n[i].y;
cin >> n[i].C;
}
string str;
int s = 0;
for (int iii = 0; iii < M; iii++) {
cin >> str;
for (int i = 0; i < N; i++) {
bool f = true;
for (int j = 0; j < 8; j++)
if ((n[i].y[j] != str[j]) && n[i].y[j] != '*') {
f = false;
} // cout<<n[i].y[j]<<"#"<<str[j]<<endl;}
if (f == true)
s += n[i].C;
}
}
cout << s << endl;
}
return 0;
} | #include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
struct R {
string y;
int C;
};
int main() {
int N, M;
while (1) {
struct R n[101];
cin >> N;
if (N == 0)
break;
cin >> M;
for (int i = 0; i < N; i++) {
cin >> n[i].y;
cin >> n[i].C;
}
string str;
int s = 0;
for (int iii = 0; iii < M; iii++) {
cin >> str;
for (int i = 0; i < N; i++) {
bool f = true;
for (int j = 0; j < 8; j++)
if ((n[i].y[j] != str[j]) && n[i].y[j] != '*') {
f = false;
} // cout<<n[i].y[j]<<"#"<<str[j]<<endl;}
if (f == true)
s += n[i].C;
}
}
cout << s << endl;
}
return 0;
} | replace | 17 | 18 | 17 | 21 | TLE | |
p01322 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
bool check(string a, string b) {
for (int i = 0; i < 8; i++) {
if (a[i] == '*')
continue;
if (a[i] != b[i])
return 0;
}
return 1;
}
int main() {
int n, m;
while (1) {
cin >> n >> m;
if (!n && !m)
break;
string a[101], b[101];
int mon[101];
;
for (int i = 0; i < n; i++)
cin >> a[i] >> mon[i];
for (int i = 0; i < m; i++)
cin >> b[i];
long long ans = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (check(a[j], b[i]))
ans += mon[j];
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
bool check(string a, string b) {
for (int i = 0; i < 8; i++) {
if (a[i] == '*')
continue;
if (a[i] != b[i])
return 0;
}
return 1;
}
int main() {
int n, m;
while (1) {
cin >> n >> m;
if (!n && !m)
break;
string a[101], b[1001];
int mon[1001];
;
for (int i = 0; i < n; i++)
cin >> a[i] >> mon[i];
for (int i = 0; i < m; i++)
cin >> b[i];
long long ans = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (check(a[j], b[i]))
ans += mon[j];
cout << ans << endl;
}
return 0;
} | replace | 19 | 21 | 19 | 21 | 0 | |
p01322 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define f first
#define s second
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef unsigned int uint;
typedef long long ll;
struct node {
node *next[11];
int m;
};
node gN[100 * 100];
int cnt;
node *myalloc() {
REP(i, 11) gN[cnt].next[i] = NULL;
gN[cnt].m = 0;
return &gN[cnt++];
}
void mknode(node *nd, char *c, int m) {
if (*c == '\0') {
nd->m = m;
return;
}
int nn = (*c == '*' ? 10 : (*c - '0'));
node *tmp = nd->next[nn];
if (!tmp)
tmp = nd->next[nn] = myalloc();
mknode(tmp, c + 1, m);
}
int check(node *nd, char *c) {
if (!nd)
return 0;
if (*c == '\0')
return nd->m;
// printf("check(%c)\n",*c);
return max(check(nd->next[*c - '0'], c + 1), check(nd->next[10], c + 1));
}
int main() {
int n, m;
while (scanf("%d%d ", &n, &m), n + m) {
char buff[20];
int c;
node *root = myalloc();
REP(i, n) {
scanf("%s %d ", buff, &c);
mknode(root, buff, c);
}
int ans = 0;
REP(i, m) {
scanf("%s", buff);
// printf("%d (%s)\n",check(root,buff),buff);
ans += check(root, buff);
}
printf("%d\n", ans);
}
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define f first
#define s second
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef unsigned int uint;
typedef long long ll;
struct node {
node *next[11];
int m;
};
node gN[100 * 1000];
int cnt;
node *myalloc() {
REP(i, 11) gN[cnt].next[i] = NULL;
gN[cnt].m = 0;
return &gN[cnt++];
}
void mknode(node *nd, char *c, int m) {
if (*c == '\0') {
nd->m = m;
return;
}
int nn = (*c == '*' ? 10 : (*c - '0'));
node *tmp = nd->next[nn];
if (!tmp)
tmp = nd->next[nn] = myalloc();
mknode(tmp, c + 1, m);
}
int check(node *nd, char *c) {
if (!nd)
return 0;
if (*c == '\0')
return nd->m;
// printf("check(%c)\n",*c);
return max(check(nd->next[*c - '0'], c + 1), check(nd->next[10], c + 1));
}
int main() {
int n, m;
while (scanf("%d%d ", &n, &m), n + m) {
char buff[20];
int c;
node *root = myalloc();
REP(i, n) {
scanf("%s %d ", buff, &c);
mknode(root, buff, c);
}
int ans = 0;
REP(i, m) {
scanf("%s", buff);
// printf("%d (%s)\n",check(root,buff),buff);
ans += check(root, buff);
}
printf("%d\n", ans);
}
return 0;
} | replace | 31 | 32 | 31 | 32 | 0 | |
p01322 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb(in, tmp) in.push_back(tmp)
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
using namespace std;
bool sc(string num, string in) {
bool han = true;
rep(i, 8) {
if (num[i] == '*')
continue;
if (num[i] != in[i])
han = false;
}
return han;
}
int main() {
int n, m;
while (cin >> n >> m, n || m) {
vector<string> num(n);
vector<int> money(m);
rep(i, n) { cin >> num[i] >> money[i]; }
int sum = 0;
rep(i, m) {
string in;
cin >> in;
rep(j, n) if (sc(num[j], in)) sum += money[j];
}
cout << sum << endl;
}
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb(in, tmp) in.push_back(tmp)
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
using namespace std;
bool sc(string num, string in) {
bool han = true;
rep(i, 8) {
if (num[i] == '*')
continue;
if (num[i] != in[i])
han = false;
}
return han;
}
int main() {
int n, m;
while (cin >> n >> m, n || m) {
vector<string> num(n);
vector<int> money(n);
rep(i, n) { cin >> num[i] >> money[i]; }
int sum = 0;
rep(i, m) {
string in;
cin >> in;
rep(j, n) if (sc(num[j], in)) sum += money[j];
}
cout << sum << endl;
}
} | replace | 28 | 29 | 28 | 29 | 0 | |
p01323 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
char f[12][6];
bool checked[12][6];
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int checkfrom(int i, int j) {
pair<int, int> tmp;
deque<pair<int, int>> q;
vector<pair<int, int>> modi;
int cnt = 0, nx, ny;
char color = f[i][j];
q.push_back(make_pair(i, j));
checked[i][j] = true;
while (!q.empty()) {
tmp = q.front();
q.pop_front();
modi.push_back(tmp);
cnt++;
for (int i = 0; i < 4; i++) {
nx = tmp.first + dx[i];
ny = tmp.second + dy[i];
if (0 <= nx && nx < 12 && 0 <= ny && ny < 6 && !checked[nx][ny] &&
f[nx][ny] == color) {
q.push_back(make_pair(nx, ny));
checked[nx][ny] = true;
}
}
}
if (cnt >= 4) {
for (int i = 0; i < modi.size(); i++) {
f[modi[i].first][modi[i].second] = '.';
for (int j = 0; j < 4; j++) {
if (f[modi[i].first + dx[j]][modi[i].second + dy[j]] == 'O') {
f[modi[i].first + dx[j]][modi[i].second + dy[j]] = '.';
}
}
}
}
return cnt;
}
int main() {
int n;
scanf("%d", &n);
for (int x = 0; x < n; x++) {
memset(f, 0, sizeof(f));
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 6; j++) {
scanf(" %c", &f[i][j]);
}
}
int max_deleted = 0;
int res = 0, tx, ty;
bool shouldsearch;
while (1) {
max_deleted = 0;
memset(checked, false, sizeof(checked));
for (int i = 11; i >= 0; i--) {
for (int j = 0; j < 6; j++) {
shouldsearch = false;
for (int k = 0; k < 4; k++) {
tx = i + dx[k];
ty = j + dy[k];
if (0 <= tx && tx < 12 && 0 <= ty && ty < 6 &&
f[i][j] == f[tx][ty]) {
shouldsearch = true;
}
}
if (f[i][j] != '.' && f[i][j] != 'O' && !checked[i][j] &&
shouldsearch) {
max_deleted = max(checkfrom(i, j), max_deleted);
}
}
}
for (int i = 0; i < 6; i++) {
int base = 11;
for (int j = 11; j >= 0; j--) {
if (f[j][i] != '.') {
f[base][i] = f[j][i];
base--;
}
}
while (base >= 0) {
f[base][i] = '.';
base--;
}
}
if (max_deleted == 0)
break;
res++;
}
cout << res << endl;
}
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
char f[12][6];
bool checked[12][6];
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int checkfrom(int i, int j) {
pair<int, int> tmp;
deque<pair<int, int>> q;
vector<pair<int, int>> modi;
int cnt = 0, nx, ny;
char color = f[i][j];
q.push_back(make_pair(i, j));
checked[i][j] = true;
while (!q.empty()) {
tmp = q.front();
q.pop_front();
modi.push_back(tmp);
cnt++;
for (int i = 0; i < 4; i++) {
nx = tmp.first + dx[i];
ny = tmp.second + dy[i];
if (0 <= nx && nx < 12 && 0 <= ny && ny < 6 && !checked[nx][ny] &&
f[nx][ny] == color) {
q.push_back(make_pair(nx, ny));
checked[nx][ny] = true;
}
}
}
if (cnt >= 4) {
for (int i = 0; i < modi.size(); i++) {
f[modi[i].first][modi[i].second] = '.';
for (int j = 0; j < 4; j++) {
if (f[modi[i].first + dx[j]][modi[i].second + dy[j]] == 'O') {
f[modi[i].first + dx[j]][modi[i].second + dy[j]] = '.';
}
}
}
}
return cnt;
}
int main() {
int n;
scanf("%d", &n);
for (int x = 0; x < n; x++) {
memset(f, 0, sizeof(f));
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 6; j++) {
scanf(" %c", &f[i][j]);
}
}
int max_deleted = 0;
int res = 0, tx, ty;
bool shouldsearch;
while (1) {
max_deleted = 0;
memset(checked, false, sizeof(checked));
for (int i = 11; i >= 0; i--) {
for (int j = 0; j < 6; j++) {
shouldsearch = false;
for (int k = 0; k < 4; k++) {
tx = i + dx[k];
ty = j + dy[k];
if (0 <= tx && tx < 12 && 0 <= ty && ty < 6 &&
f[i][j] == f[tx][ty]) {
shouldsearch = true;
}
}
if (f[i][j] != '.' && f[i][j] != 'O' && !checked[i][j] &&
shouldsearch) {
max_deleted = max(checkfrom(i, j), max_deleted);
}
}
}
for (int i = 0; i < 6; i++) {
int base = 11;
for (int j = 11; j >= 0; j--) {
if (f[j][i] != '.') {
f[base][i] = f[j][i];
base--;
}
}
while (base >= 0) {
f[base][i] = '.';
base--;
}
}
if (max_deleted < 4)
break;
res++;
}
cout << res << endl;
}
} | replace | 100 | 101 | 100 | 101 | TLE | |
p01324 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
map<string, vector<pair<string, int>>> b;
map<string, int> c;
int fn(string s, int p) {
int i;
if (c.count(s)) {
if (c[s] != p)
return 1;
else
return 0;
} else {
c[s] = p;
for (i = 0; i < (int)b[s].size(); i++) {
if (fn(b[s][i].first, p + b[s][i].second))
return 1;
}
c.erase(s);
return 0;
}
}
int main() {
int i;
int n;
while (cin >> n, n) {
set<string> a;
b.clear();
for (i = 0; i < n; i++) {
string s, t;
char c;
int p;
cin >> s >> s >> c >> c >> c >> c >> p >> t;
a.insert(s);
a.insert(t);
b[s].push_back(make_pair(t, -p));
b[t].push_back(make_pair(s, p));
}
set<string>::iterator it;
for (it = a.begin(); it != a.end(); it++) {
c.clear();
if (fn(*it, 0))
break;
}
if (it != a.end())
cout << "No" << endl;
else
cout << "Yes" << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
map<string, vector<pair<string, int>>> b;
map<string, int> c;
int fn(string s, int p) {
int i;
if (c.count(s)) {
if (c[s] != p)
return 1;
else
return 0;
} else {
c[s] = p;
for (i = 0; i < (int)b[s].size(); i++) {
if (fn(b[s][i].first, p + b[s][i].second))
return 1;
}
return 0;
}
}
int main() {
int i;
int n;
while (cin >> n, n) {
set<string> a;
b.clear();
for (i = 0; i < n; i++) {
string s, t;
char c;
int p;
cin >> s >> s >> c >> c >> c >> c >> p >> t;
a.insert(s);
a.insert(t);
b[s].push_back(make_pair(t, -p));
b[t].push_back(make_pair(s, p));
}
set<string>::iterator it;
for (it = a.begin(); it != a.end(); it++) {
c.clear();
if (fn(*it, 0))
break;
}
if (it != a.end())
cout << "No" << endl;
else
cout << "Yes" << endl;
}
return 0;
} | delete | 31 | 32 | 31 | 31 | TLE | |
p01324 | C++ | Runtime Error | #include <algorithm>
#include <map>
#include <stdio.h>
#include <string>
using namespace std;
char in[30];
int UF[300];
int w[300];
pair<int, int> FIND(int a) {
if (UF[a] < 0)
return make_pair(a, 0);
pair<int, int> ret = FIND(UF[a]);
ret.second += w[a];
return ret;
}
bool ok = true;
void UNION(int a, int b, int c) {
pair<int, int> A = FIND(a);
pair<int, int> B = FIND(b);
int d = A.first;
int e = B.first;
int f = A.second + c - B.second;
if (d == e) {
if (B.second - c != A.second)
ok = false;
return;
}
UF[a] += UF[b];
UF[b] = a;
w[b] = f;
}
int main() {
int a;
while (scanf("%d", &a), a) {
map<string, int> m;
int sz = 0;
ok = true;
for (int i = 0; i < 300; i++) {
UF[i] = -1;
w[i] = 0;
}
for (int i = 0; i < a; i++) {
scanf("%s%s", in, in);
string A = in;
int C = 0;
scanf("%s%s", in, in);
if (in[1] == 0)
C = 0;
else {
sscanf(in, "%d^%d", &C, &C);
}
scanf("%s", in);
string B = in;
int p, q;
if (!m.count(A)) {
p = sz;
m[A] = sz++;
} else
p = m[A];
if (!m.count(B)) {
q = sz;
m[B] = sz++;
} else
q = m[B];
UNION(p, q, C);
// if(!ok)printf("%d\n",i);
}
if (ok)
printf("Yes\n");
else
printf("No\n");
}
} | #include <algorithm>
#include <map>
#include <stdio.h>
#include <string>
using namespace std;
char in[30];
int UF[300];
int w[300];
pair<int, int> FIND(int a) {
if (UF[a] < 0)
return make_pair(a, 0);
pair<int, int> ret = FIND(UF[a]);
ret.second += w[a];
return ret;
}
bool ok = true;
void UNION(int a, int b, int c) {
pair<int, int> A = FIND(a);
pair<int, int> B = FIND(b);
int d = A.first;
int e = B.first;
int f = A.second + c - B.second;
if (d == e) {
if (B.second - c != A.second)
ok = false;
return;
}
UF[d] += UF[d];
UF[e] = d;
w[e] = f;
}
int main() {
int a;
while (scanf("%d", &a), a) {
map<string, int> m;
int sz = 0;
ok = true;
for (int i = 0; i < 300; i++) {
UF[i] = -1;
w[i] = 0;
}
for (int i = 0; i < a; i++) {
scanf("%s%s", in, in);
string A = in;
int C = 0;
scanf("%s%s", in, in);
if (in[1] == 0)
C = 0;
else {
sscanf(in, "%d^%d", &C, &C);
}
scanf("%s", in);
string B = in;
int p, q;
if (!m.count(A)) {
p = sz;
m[A] = sz++;
} else
p = m[A];
if (!m.count(B)) {
q = sz;
m[B] = sz++;
} else
q = m[B];
UNION(p, q, C);
// if(!ok)printf("%d\n",i);
}
if (ok)
printf("Yes\n");
else
printf("No\n");
}
} | replace | 27 | 30 | 27 | 30 | 0 | |
p01324 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;
const int MAX_M = 101;
const int INF = 1 << 24;
map<string, int> id;
int G[MAX_M][MAX_M];
int M;
int getId(string s) {
if (id.find(s) == id.end()) {
id[s] = M++;
}
return id[s];
}
bool solve() {
for (int k = 0; k < M; ++k) {
for (int s = 0; s < M; ++s) {
for (int d = 0; d < M; ++d) {
if (G[s][k] == INF || G[k][d] == INF)
continue;
if (G[s][d] == INF) {
G[s][d] = G[s][k] + G[k][d];
} else if (G[s][d] != G[s][k] + G[k][d]) {
return false;
}
}
}
}
return true;
}
int main() {
int N;
while (cin >> N && N) {
M = 0;
id.clear();
fill(G[0], G[MAX_M], INF);
for (int i = 0; i < N; ++i) {
string in[5];
for (int j = 0; j < 5; ++j)
cin >> in[j];
int a = getId(in[1]);
int b = getId(in[4]);
int cost = atoi((in[3].substr(3)).c_str());
G[a][b] = cost;
G[b][a] = -cost;
}
cout << (solve() ? "Yes" : "No") << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;
const int MAX_M = 201;
const int INF = 1 << 24;
map<string, int> id;
int G[MAX_M][MAX_M];
int M;
int getId(string s) {
if (id.find(s) == id.end()) {
id[s] = M++;
}
return id[s];
}
bool solve() {
for (int k = 0; k < M; ++k) {
for (int s = 0; s < M; ++s) {
for (int d = 0; d < M; ++d) {
if (G[s][k] == INF || G[k][d] == INF)
continue;
if (G[s][d] == INF) {
G[s][d] = G[s][k] + G[k][d];
} else if (G[s][d] != G[s][k] + G[k][d]) {
return false;
}
}
}
}
return true;
}
int main() {
int N;
while (cin >> N && N) {
M = 0;
id.clear();
fill(G[0], G[MAX_M], INF);
for (int i = 0; i < N; ++i) {
string in[5];
for (int j = 0; j < 5; ++j)
cin >> in[j];
int a = getId(in[1]);
int b = getId(in[4]);
int cost = atoi((in[3].substr(3)).c_str());
G[a][b] = cost;
G[b][a] = -cost;
}
cout << (solve() ? "Yes" : "No") << endl;
}
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p01324 | C++ | Time Limit Exceeded | #include <iostream>
#include <list>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
int *find(
std::unordered_map<std::string, std::unordered_map<std::string, int>> *map,
const std::string &s1, const std::string &s2) {
auto it1 = map->find(s1);
if (it1 == map->end())
return NULL;
auto it2 = it1->second.find(s2);
return (it2 == it1->second.end()) ? NULL : &it2->second;
}
bool walk(
std::unordered_map<std::string, std::unordered_map<std::string, int>> *map,
const std::tuple<std::string, std::string, int> &t) {
const std::string &s0 = std::get<0>(t);
const std::string &s1 = std::get<1>(t);
int mag1 = std::get<2>(t);
int *old = find(map, s0, s1);
if (old == NULL) {
(*map)[s0][s1] = mag1;
(*map)[s1][s0] = -mag1;
} else if (*old != mag1)
return false;
std::list<std::tuple<std::string, std::string, int>> next;
for (auto x : map->at(s0)) {
const std::string &s2 = x.first;
int mag2 = x.second;
if (s1 != s2)
if (find(map, s1, s2) == NULL)
next.push_back(std::make_tuple(s1, s2, -mag1 + mag2));
}
for (auto x : map->at(s1)) {
const std::string &s2 = x.first;
int mag2 = x.second;
if (s0 != s2)
if (find(map, s0, s2) == NULL)
next.push_back(std::make_tuple(s0, s2, mag1 + mag2));
}
for (auto t : next)
if (!walk(map, t))
return false;
return true;
}
int main() {
char line[256];
while (not std::cin.eof()) {
int n;
std::cin >> n;
if (n == 0)
break;
std::list<std::tuple<std::string, std::string, int>> inputs;
for (int i = 0; i < n; ++i) {
int x;
std::string s1, s2;
std::cin.ignore(2);
std::cin >> s1;
std::cin.ignore(6);
std::cin >> x >> s2;
inputs.push_back(std::make_tuple(s1, s2, x));
}
std::unordered_map<std::string, std::unordered_map<std::string, int>> map;
bool is_valid(true);
for (auto x : inputs)
if (!walk(&map, x)) {
is_valid = false;
break;
}
std::cout << (is_valid ? "Yes" : "No") << std::endl;
}
return 0;
} | #include <iostream>
#include <list>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
int *find(
std::unordered_map<std::string, std::unordered_map<std::string, int>> *map,
const std::string &s1, const std::string &s2) {
auto it1 = map->find(s1);
if (it1 == map->end())
return NULL;
auto it2 = it1->second.find(s2);
return (it2 == it1->second.end()) ? NULL : &it2->second;
}
bool walk(
std::unordered_map<std::string, std::unordered_map<std::string, int>> *map,
const std::tuple<std::string, std::string, int> &t) {
const std::string &s0 = std::get<0>(t);
const std::string &s1 = std::get<1>(t);
int mag1 = std::get<2>(t);
int *old = find(map, s0, s1);
if (old == NULL) {
(*map)[s0][s1] = mag1;
(*map)[s1][s0] = -mag1;
} else
return (*old == mag1);
std::list<std::tuple<std::string, std::string, int>> next;
for (auto x : map->at(s0)) {
const std::string &s2 = x.first;
int mag2 = x.second;
if (s1 != s2)
if (find(map, s1, s2) == NULL)
next.push_back(std::make_tuple(s1, s2, -mag1 + mag2));
}
for (auto x : map->at(s1)) {
const std::string &s2 = x.first;
int mag2 = x.second;
if (s0 != s2)
if (find(map, s0, s2) == NULL)
next.push_back(std::make_tuple(s0, s2, mag1 + mag2));
}
for (auto t : next)
if (!walk(map, t))
return false;
return true;
}
int main() {
char line[256];
while (not std::cin.eof()) {
int n;
std::cin >> n;
if (n == 0)
break;
std::list<std::tuple<std::string, std::string, int>> inputs;
for (int i = 0; i < n; ++i) {
int x;
std::string s1, s2;
std::cin.ignore(2);
std::cin >> s1;
std::cin.ignore(6);
std::cin >> x >> s2;
inputs.push_back(std::make_tuple(s1, s2, x));
}
std::unordered_map<std::string, std::unordered_map<std::string, int>> map;
bool is_valid(true);
for (auto x : inputs)
if (!walk(&map, x)) {
is_valid = false;
break;
}
std::cout << (is_valid ? "Yes" : "No") << std::endl;
}
return 0;
} | replace | 28 | 30 | 28 | 30 | TLE | |
p01329 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define EQ(a, b) (abs((a) - (b)) < eps)
// Aho-Corasick
#define SIZE 256
struct Trie {
Trie *child[SIZE];
Trie *fail;
vector<int> matched;
Trie() {
for (int i = 0; i < SIZE; i++)
child[i] = NULL;
fail = NULL;
}
~Trie() {}
};
Trie pool[50000];
int pool_pos;
inline Trie *alloc() { return &(pool[pool_pos++] = Trie()); }
inline void destruct() { pool_pos = 0; }
vector<int> unite(const vector<int> &a, const vector<int> &b) {
vector<int> ret;
set_union(all(a), all(b), back_inserter(ret));
return ret;
}
Trie *build(vector<string> pattern) {
Trie *root = alloc();
Trie *now;
root->fail = root;
for (int i = 0; i < pattern.size(); i++) {
now = root;
for (int j = 0; j < pattern[i].size(); j++) {
int idx = (int)pattern[i][j];
if (now->child[idx] == NULL) {
now->child[idx] = alloc();
}
now = now->child[idx];
}
now->matched.pb(i);
}
queue<Trie *> q;
for (int i = 0; i < SIZE; i++) {
if ((root->child[i]) == NULL)
root->child[i] = root;
else {
root->child[i]->fail = root;
q.push(root->child[i]);
}
}
while (!q.empty()) {
now = q.front();
q.pop();
for (int i = 0; i < SIZE; i++) {
if (now->child[i]) {
Trie *next = now->fail;
while ((next->child[i]) == NULL)
next = next->fail;
now->child[i]->fail = next->child[i];
now->child[i]->matched =
unite(now->child[i]->matched, next->child[i]->matched);
q.push(now->child[i]);
}
}
}
return root;
}
// now???????????????????????????????????¨??§?¶?????????????????´¢??????
// ret???pattern???????´???°??§????????????????????¨???
// now????????§??????????????§????????????????????????????????¨???
void match(Trie *&now, const string s, vector<int> &ret) {
for (int i = 0; i < s.size(); i++) {
int idx = s[i];
while ((now->child[idx]) == NULL)
now = now->fail;
now = now->child[idx];
for (int j = 0; j < (now->matched.size()); j++) {
ret[now->matched[j]] = 1;
}
}
}
struct state {
P pos;
Trie *trie;
int turn;
state(P pos, Trie *trie, int turn) : pos(pos), trie(trie), turn(turn) {}
};
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
string dv[4] = {"D", "L", "U", "R"};
string f[55];
int main() {
while (1) {
int n, m, p;
P S, G;
set<Trie *> s[55][55];
scanf("%d %d", &n, &m);
if (n == 0 && m == 0)
break;
for (int i = 0; i < n; i++)
cin >> f[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (f[i][j] == 'S') {
S = P(j, i);
}
if (f[i][j] == 'G') {
G = P(j, i);
}
}
}
scanf("%d", &p);
vector<string> pat(p);
for (int i = 0; i < p; i++)
cin >> pat[i];
destruct();
Trie *root = build(pat);
/*while(1){
string s;
cin >> s;
vector<int> v(p,0);
Trie* r = root;
match(r,s,v);
int flag = 0;
for(int i=0;i<p;i++)flag|=v[i];
if(flag)printf("exist\n");
else printf("not exist\n");
}*/
queue<state> q;
q.push(state(S, root, 0));
int ans = -1;
while (!q.empty()) {
state now = q.front();
q.pop();
if (now.pos == G) {
ans = now.turn;
break;
}
int x = now.pos.fi, y = now.pos.sec;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= m || ny < 0 || ny >= n)
continue;
if (f[ny][nx] == '#')
continue;
vector<int> v(p, 0);
Trie *t = now.trie;
match(t, dv[i], v);
int flag = 0;
for (int j = 0; j < p; j++)
flag |= v[j];
if (flag)
continue;
if (s[nx][ny].find(t) == s[nx][ny].end()) {
s[nx][ny].insert(t);
q.push(state(P(nx, ny), t, now.turn + 1));
}
}
}
printf("%d\n", ans);
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define EQ(a, b) (abs((a) - (b)) < eps)
// Aho-Corasick
#define SIZE 256
struct Trie {
Trie *child[SIZE];
Trie *fail;
vector<int> matched;
Trie() {
for (int i = 0; i < SIZE; i++)
child[i] = NULL;
fail = NULL;
}
~Trie() {}
};
Trie pool[5000];
int pool_pos;
inline Trie *alloc() { return &(pool[pool_pos++] = Trie()); }
inline void destruct() { pool_pos = 0; }
vector<int> unite(const vector<int> &a, const vector<int> &b) {
vector<int> ret;
set_union(all(a), all(b), back_inserter(ret));
return ret;
}
Trie *build(vector<string> pattern) {
Trie *root = alloc();
Trie *now;
root->fail = root;
for (int i = 0; i < pattern.size(); i++) {
now = root;
for (int j = 0; j < pattern[i].size(); j++) {
int idx = (int)pattern[i][j];
if (now->child[idx] == NULL) {
now->child[idx] = alloc();
}
now = now->child[idx];
}
now->matched.pb(i);
}
queue<Trie *> q;
for (int i = 0; i < SIZE; i++) {
if ((root->child[i]) == NULL)
root->child[i] = root;
else {
root->child[i]->fail = root;
q.push(root->child[i]);
}
}
while (!q.empty()) {
now = q.front();
q.pop();
for (int i = 0; i < SIZE; i++) {
if (now->child[i]) {
Trie *next = now->fail;
while ((next->child[i]) == NULL)
next = next->fail;
now->child[i]->fail = next->child[i];
now->child[i]->matched =
unite(now->child[i]->matched, next->child[i]->matched);
q.push(now->child[i]);
}
}
}
return root;
}
// now???????????????????????????????????¨??§?¶?????????????????´¢??????
// ret???pattern???????´???°??§????????????????????¨???
// now????????§??????????????§????????????????????????????????¨???
void match(Trie *&now, const string s, vector<int> &ret) {
for (int i = 0; i < s.size(); i++) {
int idx = s[i];
while ((now->child[idx]) == NULL)
now = now->fail;
now = now->child[idx];
for (int j = 0; j < (now->matched.size()); j++) {
ret[now->matched[j]] = 1;
}
}
}
struct state {
P pos;
Trie *trie;
int turn;
state(P pos, Trie *trie, int turn) : pos(pos), trie(trie), turn(turn) {}
};
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
string dv[4] = {"D", "L", "U", "R"};
string f[55];
int main() {
while (1) {
int n, m, p;
P S, G;
set<Trie *> s[55][55];
scanf("%d %d", &n, &m);
if (n == 0 && m == 0)
break;
for (int i = 0; i < n; i++)
cin >> f[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (f[i][j] == 'S') {
S = P(j, i);
}
if (f[i][j] == 'G') {
G = P(j, i);
}
}
}
scanf("%d", &p);
vector<string> pat(p);
for (int i = 0; i < p; i++)
cin >> pat[i];
destruct();
Trie *root = build(pat);
/*while(1){
string s;
cin >> s;
vector<int> v(p,0);
Trie* r = root;
match(r,s,v);
int flag = 0;
for(int i=0;i<p;i++)flag|=v[i];
if(flag)printf("exist\n");
else printf("not exist\n");
}*/
queue<state> q;
q.push(state(S, root, 0));
int ans = -1;
while (!q.empty()) {
state now = q.front();
q.pop();
if (now.pos == G) {
ans = now.turn;
break;
}
int x = now.pos.fi, y = now.pos.sec;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= m || ny < 0 || ny >= n)
continue;
if (f[ny][nx] == '#')
continue;
vector<int> v(p, 0);
Trie *t = now.trie;
match(t, dv[i], v);
int flag = 0;
for (int j = 0; j < p; j++)
flag |= v[j];
if (flag)
continue;
if (s[nx][ny].find(t) == s[nx][ny].end()) {
s[nx][ny].insert(t);
q.push(state(P(nx, ny), t, now.turn + 1));
}
}
}
printf("%d\n", ans);
}
return 0;
} | replace | 51 | 52 | 51 | 52 | MLE | |
p01329 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define V(x, y, z) ((x)*1000000 + (y)*1000 + (z))
#define inf 1000000000
using namespace std;
int W, H;
char map[55][55];
int P;
string S[15];
int sx, sy, gx, gy;
vector<string> vec;
int Next[105][4];
bool ng[105][4];
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const char c[] = {'R', 'U', 'L', 'D'};
int dist[55][55][105];
int bfs() {
for (int x = 1; x <= W; x++) {
for (int y = 1; y <= H; y++) {
for (int k = 0; k < vec.size(); k++) {
dist[x][y][k] = inf;
}
}
}
dist[sx][sy][0] = 0;
queue<int> Q;
Q.push(V(sx, sy, 0));
int x, y, state;
while (Q.size()) {
state = Q.front() % 1000, Q.front() /= 1000;
y = Q.front() % 1000, Q.front() /= 1000;
x = Q.front();
Q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i], nstate = Next[state][i];
if (ng[state][i])
continue;
if (nx <= 0 || nx > W || ny <= 0 || ny > H)
continue;
if (map[nx][ny] == '#')
continue;
if (dist[nx][ny][nstate] < inf)
continue;
dist[nx][ny][nstate] = dist[x][y][state] + 1;
Q.push(V(nx, ny, nstate));
}
}
int ret = inf;
for (int i = 0; i < vec.size(); i++)
ret = min(ret, dist[gx][gy][i]);
if (ret == inf)
return -1;
return ret;
}
int main(void) {
while (1) {
cin >> H >> W;
if (H == 0 && W == 0)
break;
for (int y = 1; y <= H; y++) {
for (int x = 1; x <= W; x++) {
cin >> map[x][y];
if (map[x][y] == 'S')
sx = x, sy = y;
if (map[x][y] == 'G')
gx = x, gy = y;
}
}
cin >> P;
for (int i = 0; i < P; i++)
cin >> S[i];
vec.clear();
vec.push_back("");
for (int i = 0; i < P; i++) {
for (int j = 0; j < S[i].size(); j++) {
vec.push_back(S[i].substr(0, j));
}
}
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < 4; j++) {
ng[i][j] = false;
}
}
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < 4; j++) {
string s = vec[i] + c[j];
for (int k = 0; k < P; k++) {
string t = s;
while (t.size()) {
if (t == S[k]) {
ng[i][j] = true;
goto end;
}
t = t.substr(1);
}
}
end:;
while (1) {
auto p = lower_bound(vec.begin(), vec.end(), s);
if (*p == s) {
Next[i][j] = p - vec.begin();
break;
}
s = s.substr(1);
}
}
}
cout << bfs() << endl;
}
return 0;
}
| #include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define V(x, y, z) ((x)*1000000 + (y)*1000 + (z))
#define inf 1000000000
using namespace std;
int W, H;
char map[55][55];
int P;
string S[15];
int sx, sy, gx, gy;
vector<string> vec;
int Next[105][4];
bool ng[105][4];
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const char c[] = {'R', 'U', 'L', 'D'};
int dist[55][55][105];
int bfs() {
for (int x = 1; x <= W; x++) {
for (int y = 1; y <= H; y++) {
for (int k = 0; k < vec.size(); k++) {
dist[x][y][k] = inf;
}
}
}
dist[sx][sy][0] = 0;
queue<int> Q;
Q.push(V(sx, sy, 0));
int x, y, state;
while (Q.size()) {
state = Q.front() % 1000, Q.front() /= 1000;
y = Q.front() % 1000, Q.front() /= 1000;
x = Q.front();
Q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i], nstate = Next[state][i];
if (ng[state][i])
continue;
if (nx <= 0 || nx > W || ny <= 0 || ny > H)
continue;
if (map[nx][ny] == '#')
continue;
if (dist[nx][ny][nstate] < inf)
continue;
dist[nx][ny][nstate] = dist[x][y][state] + 1;
Q.push(V(nx, ny, nstate));
}
}
int ret = inf;
for (int i = 0; i < vec.size(); i++)
ret = min(ret, dist[gx][gy][i]);
if (ret == inf)
return -1;
return ret;
}
int main(void) {
while (1) {
cin >> H >> W;
if (H == 0 && W == 0)
break;
for (int y = 1; y <= H; y++) {
for (int x = 1; x <= W; x++) {
cin >> map[x][y];
if (map[x][y] == 'S')
sx = x, sy = y;
if (map[x][y] == 'G')
gx = x, gy = y;
}
}
cin >> P;
for (int i = 0; i < P; i++)
cin >> S[i];
vec.clear();
vec.push_back("");
for (int i = 0; i < P; i++) {
for (int j = 0; j < S[i].size(); j++) {
vec.push_back(S[i].substr(0, j));
}
}
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < 4; j++) {
ng[i][j] = false;
}
}
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < 4; j++) {
string s = vec[i] + c[j];
for (int k = 0; k < P; k++) {
string t = s;
while (t.size()) {
if (t == S[k]) {
ng[i][j] = true;
goto end;
}
t = t.substr(1);
}
}
end:;
while (1) {
auto p = lower_bound(vec.begin(), vec.end(), s);
if (p != vec.end() && *p == s) {
Next[i][j] = p - vec.begin();
break;
}
s = s.substr(1);
}
}
}
cout << bfs() << endl;
}
return 0;
}
| replace | 118 | 119 | 118 | 119 | 0 | |
p01329 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e8;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
string D = "DRUL";
const int var = 4;
int trans(char c) {
switch (c) {
case 'R':
return 0;
case 'D':
return 1;
case 'L':
return 2;
case 'U':
return 3;
default:
assert(false);
}
}
struct ac_node {
ac_node *fail;
ac_node *next[var];
vector<int> ok;
ac_node() : fail(nullptr), next{} {}
};
ac_node *new_ac_node() {
static const int pmax = 1e3;
static ac_node pool[pmax];
static int it = 0;
assert(it < pmax);
return &pool[it++];
}
ac_node *getnext(ac_node *p, char c) {
while (p->next[trans(c)] == nullptr)
p = p->fail;
return p->next[trans(c)];
}
class aho_corasick {
vector<int> unite(const vector<int> &a, const vector<int> &b) {
vector<int> res;
set_union(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res));
return res;
}
int K;
ac_node *root;
public:
aho_corasick(const vector<string> &Ts) : K(Ts.size()), root(new_ac_node()) {
ac_node *now;
root->fail = root;
for (int i = 0; i < K; i++) {
auto &T = Ts[i];
now = root;
for (auto c : T) {
if (now->next[trans(c)] == nullptr) {
now->next[trans(c)] = new_ac_node();
}
now = now->next[trans(c)];
}
now->ok.push_back(i);
}
queue<ac_node *> q;
for (int i = 0; i < var; i++) {
if (root->next[i] == nullptr) {
root->next[i] = root;
} else {
root->next[i]->fail = root;
q.push(root->next[i]);
}
}
while (!q.empty()) {
now = q.front();
q.pop();
for (int i = 0; i < var; i++) {
if (now->next[i] != nullptr) {
ac_node *nx = now->fail;
while (nx->next[i] == nullptr) {
nx = nx->fail;
}
now->next[i]->fail = nx->next[i];
now->next[i]->ok = unite(now->next[i]->ok, nx->next[i]->ok);
q.push(now->next[i]);
}
}
}
}
ac_node *getroot() const { return root; }
int get_node_id(ac_node *nd) const { return (int)(nd - root); }
vector<int> count(const string &S) const {
vector<int> res(K);
ac_node *now = root;
for (auto c : S) {
now = getnext(now, c);
for (auto k : now->ok)
res[k]++;
}
return res;
}
};
using T = tuple<int, int, ac_node *>;
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, m, p;
while (cin >> n >> m, n | m) {
vector<string> S(n);
int sx = 0, sy = 0, gx = 0, gy = 0;
for (int i = 0; i < n; i++) {
cin >> S[i];
for (int j = 0; j < (int)S[i].size(); j++) {
if (S[i][j] == 'S') {
sx = i;
sy = j;
} else if (S[i][j] == 'G') {
gx = i;
gy = j;
}
}
}
cin >> p;
vector<string> pts(p);
for (int i = 0; i < p; i++) {
cin >> pts[i];
}
aho_corasick aho(pts);
vector<vector<vector<int>>> f(
n, vector<vector<int>>(m, vector<int>(200, INF)));
queue<T> q;
q.push(make_tuple(sx, sy, aho.getroot()));
f[sx][sy][aho.get_node_id(aho.getroot())] = 0;
while (!q.empty()) {
auto tup = q.front();
q.pop();
int x = get<0>(tup), y = get<1>(tup);
ac_node *nd = get<2>(tup);
int id = aho.get_node_id(nd);
for (int i = 0; i < 4; i++) {
int tx = x + dx[i], ty = y + dy[i];
ac_node *tnd = getnext(nd, D[i]);
if (0 <= tx && tx < n && 0 <= ty && ty < m && S[tx][ty] != '#' &&
tnd->ok.empty() && f[tx][ty][aho.get_node_id(tnd)] == INF) {
q.push(make_tuple(tx, ty, tnd));
f[tx][ty][aho.get_node_id(tnd)] = f[x][y][id] + 1;
}
}
}
int res = INF;
for (auto val : f[gx][gy]) {
res = min(res, val);
}
cout << (res != INF ? res : -1) << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int INF = 1e8;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
string D = "DRUL";
const int var = 4;
int trans(char c) {
switch (c) {
case 'R':
return 0;
case 'D':
return 1;
case 'L':
return 2;
case 'U':
return 3;
default:
assert(false);
}
}
struct ac_node {
ac_node *fail;
ac_node *next[var];
vector<int> ok;
ac_node() : fail(nullptr), next{} {}
};
ac_node *new_ac_node() {
static const int pmax = 1e5;
static ac_node pool[pmax];
static int it = 0;
assert(it < pmax);
return &pool[it++];
}
ac_node *getnext(ac_node *p, char c) {
while (p->next[trans(c)] == nullptr)
p = p->fail;
return p->next[trans(c)];
}
class aho_corasick {
vector<int> unite(const vector<int> &a, const vector<int> &b) {
vector<int> res;
set_union(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res));
return res;
}
int K;
ac_node *root;
public:
aho_corasick(const vector<string> &Ts) : K(Ts.size()), root(new_ac_node()) {
ac_node *now;
root->fail = root;
for (int i = 0; i < K; i++) {
auto &T = Ts[i];
now = root;
for (auto c : T) {
if (now->next[trans(c)] == nullptr) {
now->next[trans(c)] = new_ac_node();
}
now = now->next[trans(c)];
}
now->ok.push_back(i);
}
queue<ac_node *> q;
for (int i = 0; i < var; i++) {
if (root->next[i] == nullptr) {
root->next[i] = root;
} else {
root->next[i]->fail = root;
q.push(root->next[i]);
}
}
while (!q.empty()) {
now = q.front();
q.pop();
for (int i = 0; i < var; i++) {
if (now->next[i] != nullptr) {
ac_node *nx = now->fail;
while (nx->next[i] == nullptr) {
nx = nx->fail;
}
now->next[i]->fail = nx->next[i];
now->next[i]->ok = unite(now->next[i]->ok, nx->next[i]->ok);
q.push(now->next[i]);
}
}
}
}
ac_node *getroot() const { return root; }
int get_node_id(ac_node *nd) const { return (int)(nd - root); }
vector<int> count(const string &S) const {
vector<int> res(K);
ac_node *now = root;
for (auto c : S) {
now = getnext(now, c);
for (auto k : now->ok)
res[k]++;
}
return res;
}
};
using T = tuple<int, int, ac_node *>;
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, m, p;
while (cin >> n >> m, n | m) {
vector<string> S(n);
int sx = 0, sy = 0, gx = 0, gy = 0;
for (int i = 0; i < n; i++) {
cin >> S[i];
for (int j = 0; j < (int)S[i].size(); j++) {
if (S[i][j] == 'S') {
sx = i;
sy = j;
} else if (S[i][j] == 'G') {
gx = i;
gy = j;
}
}
}
cin >> p;
vector<string> pts(p);
for (int i = 0; i < p; i++) {
cin >> pts[i];
}
aho_corasick aho(pts);
vector<vector<vector<int>>> f(
n, vector<vector<int>>(m, vector<int>(200, INF)));
queue<T> q;
q.push(make_tuple(sx, sy, aho.getroot()));
f[sx][sy][aho.get_node_id(aho.getroot())] = 0;
while (!q.empty()) {
auto tup = q.front();
q.pop();
int x = get<0>(tup), y = get<1>(tup);
ac_node *nd = get<2>(tup);
int id = aho.get_node_id(nd);
for (int i = 0; i < 4; i++) {
int tx = x + dx[i], ty = y + dy[i];
ac_node *tnd = getnext(nd, D[i]);
if (0 <= tx && tx < n && 0 <= ty && ty < m && S[tx][ty] != '#' &&
tnd->ok.empty() && f[tx][ty][aho.get_node_id(tnd)] == INF) {
q.push(make_tuple(tx, ty, tnd));
f[tx][ty][aho.get_node_id(tnd)] = f[x][y][id] + 1;
}
}
}
int res = INF;
for (auto val : f[gx][gy]) {
res = min(res, val);
}
cout << (res != INF ? res : -1) << endl;
}
return 0;
}
| replace | 35 | 36 | 35 | 36 | 0 | |
p01334 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#ifdef Debug
#define dump(x) cerr << #x << " = " << (x) << endl
#else
#define dump(x)
#endif
int par[100];
int Rank[100];
void init(int n) {
rep(i, n) {
par[i] = i;
Rank[i] = 0;
}
}
int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (Rank[x] < Rank[y]) {
par[x] = y;
} else {
par[y] = x;
if (Rank[x] == Rank[y])
Rank[x]++;
}
}
int main() {
int n;
while (cin >> n) {
if (n == 0)
break;
pii data[110][110] = {};
rep(i, n) rep(j, n) cin >> data[i][j].first >> data[i][j].second;
init(n * n);
rep(i, n) {
rep(j, n) { unite(n * i + j, n * data[i][j].second + data[i][j].first); }
}
bool used[10000] = {};
int sum = 0;
rep(i, n * n) {
int aa = find(i);
if (used[aa] == false) {
sum++;
used[aa] = true;
}
}
cout << sum << endl;
}
return 0;
} | #include "bits/stdc++.h"
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#ifdef Debug
#define dump(x) cerr << #x << " = " << (x) << endl
#else
#define dump(x)
#endif
int par[10000];
int Rank[10000];
void init(int n) {
rep(i, n) {
par[i] = i;
Rank[i] = 0;
}
}
int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (Rank[x] < Rank[y]) {
par[x] = y;
} else {
par[y] = x;
if (Rank[x] == Rank[y])
Rank[x]++;
}
}
int main() {
int n;
while (cin >> n) {
if (n == 0)
break;
pii data[110][110] = {};
rep(i, n) rep(j, n) cin >> data[i][j].first >> data[i][j].second;
init(n * n);
rep(i, n) {
rep(j, n) { unite(n * i + j, n * data[i][j].second + data[i][j].first); }
}
bool used[10000] = {};
int sum = 0;
rep(i, n * n) {
int aa = find(i);
if (used[aa] == false) {
sum++;
used[aa] = true;
}
}
cout << sum << endl;
}
return 0;
} | replace | 17 | 19 | 17 | 19 | 0 | |
p01335 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vi;
int main() {
int buckets[14];
int buckets2[5];
bool firsttime = true;
while (1) {
int n;
cin >> n;
if (!n)
break;
if (!firsttime)
cout << endl;
if (firsttime)
firsttime = false;
int cardscore[14][5];
int handscore[9];
for (int j = 1; j <= 4; j++) {
for (int k = 1; k <= 13; k++) {
cin >> cardscore[k][j];
}
}
for (int j = 0; j < 9; j++) {
cin >> handscore[j];
}
for (int i = 0; i < n; i++) {
int times = 0;
long long score = 0;
vector<string> card(5);
vector<pair<int, int>> tmpc(5);
for (int j = 0; j < 5; j++) {
cin >> card[j];
if (card[j][0] == 'A') {
tmpc[j].first = 1;
} else if (card[j][0] == 'T') {
tmpc[j].first = 10;
} else if (card[j][0] == 'J') {
tmpc[j].first = 11;
} else if (card[j][0] == 'Q') {
tmpc[j].first = 12;
} else if (card[j][0] == 'K') {
tmpc[j].first = 13;
} else {
tmpc[j].first = (int)(card[j][0] - '0');
}
if (card[j][1] == 'S') {
tmpc[j].second = 1;
} else if (card[j][1] == 'C') {
tmpc[j].second = 2;
} else if (card[j][1] == 'H') {
tmpc[j].second = 3;
} else if (card[j][1] == 'D') {
tmpc[j].second = 4;
}
score += cardscore[tmpc[j].first][tmpc[j].second];
}
int onep = 0, threec = 0, four = 0;
bool flash = false;
bool straight = true;
sort(tmpc.begin(), tmpc.end());
memset(buckets, 0, sizeof(buckets));
memset(buckets2, 0, sizeof(buckets2));
for (int j = 0; j < 5; j++) {
buckets[tmpc[j].first]++;
buckets2[tmpc[j].second]++;
}
for (int j = 1; j <= 13; j++) {
if (buckets[j] == 2) {
onep++;
} else if (buckets[j] == 3) {
threec++;
} else if (buckets[j] == 4) {
four++;
}
}
for (int j = 1; j <= 4; j++) {
if (buckets2[j] == 5) {
flash = true;
times = handscore[4];
}
}
if (onep == 1 && threec == 0) {
times = handscore[0];
} else if (onep == 2) {
times = handscore[1];
} else if (threec == 1 && onep == 0) {
times = handscore[2];
} else if (threec == 1 && onep == 1) {
times = handscore[5];
} else if (four == 1) {
times = handscore[6];
}
if (tmpc[0].first == 1 && tmpc[1].first == 10 && tmpc[2].first == 11 &&
tmpc[3].first == 12 && tmpc[4].first == 13) {
if (flash)
times = handscore[8];
else
times = handscore[3];
} else {
for (int j = 0; j < 4; j++) {
if (tmpc[j + 1].first != (tmpc[j].first + 1))
straight = false;
}
if (straight) {
if (flash)
times = handscore[7];
else
times = handscore[3];
}
}
cout << score * times << endl;
}
}
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vi;
int main() {
int buckets[14];
int buckets2[5];
bool firsttime = true;
int n;
while (cin >> n) {
if (!n)
break;
if (!firsttime)
cout << endl;
if (firsttime)
firsttime = false;
int cardscore[14][5];
int handscore[9];
for (int j = 1; j <= 4; j++) {
for (int k = 1; k <= 13; k++) {
cin >> cardscore[k][j];
}
}
for (int j = 0; j < 9; j++) {
cin >> handscore[j];
}
for (int i = 0; i < n; i++) {
int times = 0;
long long score = 0;
vector<string> card(5);
vector<pair<int, int>> tmpc(5);
for (int j = 0; j < 5; j++) {
cin >> card[j];
if (card[j][0] == 'A') {
tmpc[j].first = 1;
} else if (card[j][0] == 'T') {
tmpc[j].first = 10;
} else if (card[j][0] == 'J') {
tmpc[j].first = 11;
} else if (card[j][0] == 'Q') {
tmpc[j].first = 12;
} else if (card[j][0] == 'K') {
tmpc[j].first = 13;
} else {
tmpc[j].first = (int)(card[j][0] - '0');
}
if (card[j][1] == 'S') {
tmpc[j].second = 1;
} else if (card[j][1] == 'C') {
tmpc[j].second = 2;
} else if (card[j][1] == 'H') {
tmpc[j].second = 3;
} else if (card[j][1] == 'D') {
tmpc[j].second = 4;
}
score += cardscore[tmpc[j].first][tmpc[j].second];
}
int onep = 0, threec = 0, four = 0;
bool flash = false;
bool straight = true;
sort(tmpc.begin(), tmpc.end());
memset(buckets, 0, sizeof(buckets));
memset(buckets2, 0, sizeof(buckets2));
for (int j = 0; j < 5; j++) {
buckets[tmpc[j].first]++;
buckets2[tmpc[j].second]++;
}
for (int j = 1; j <= 13; j++) {
if (buckets[j] == 2) {
onep++;
} else if (buckets[j] == 3) {
threec++;
} else if (buckets[j] == 4) {
four++;
}
}
for (int j = 1; j <= 4; j++) {
if (buckets2[j] == 5) {
flash = true;
times = handscore[4];
}
}
if (onep == 1 && threec == 0) {
times = handscore[0];
} else if (onep == 2) {
times = handscore[1];
} else if (threec == 1 && onep == 0) {
times = handscore[2];
} else if (threec == 1 && onep == 1) {
times = handscore[5];
} else if (four == 1) {
times = handscore[6];
}
if (tmpc[0].first == 1 && tmpc[1].first == 10 && tmpc[2].first == 11 &&
tmpc[3].first == 12 && tmpc[4].first == 13) {
if (flash)
times = handscore[8];
else
times = handscore[3];
} else {
for (int j = 0; j < 4; j++) {
if (tmpc[j + 1].first != (tmpc[j].first + 1))
straight = false;
}
if (straight) {
if (flash)
times = handscore[7];
else
times = handscore[3];
}
}
cout << score * times << endl;
}
}
return 0;
}
| replace | 14 | 17 | 14 | 16 | TLE | |
p01335 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
using namespace std;
int h[5], s[5];
int v[13][4];
int m[128];
int k[10];
int H() {
for (int i = 1; i < 5; ++i)
if (s[i] - s[i - 1])
return 0;
return 1;
}
int X() { return h[1] + 1 == h[2] && h[2] + 1 == h[3] && h[3] + 1 == h[4]; }
int S() { return X() && h[0] + 1 == h[1]; }
int R() { return X() && h[0] == 1 && h[4] == 13; }
int F() {
if (H()) {
if (R())
return 9;
if (S())
return 8;
return 5;
} else {
if (h[0] == h[3] || h[1] == h[4])
return 7;
if (h[0] == h[1] && h[2] == h[4] || h[0] == h[2] && h[3] == h[4])
return 6;
if (S() || R())
return 4;
if (h[0] == h[2] || h[1] == h[3] || h[2] == h[4])
return 3;
if (h[0] == h[1])
if (h[2] == h[3] || h[3] == h[4])
return 2;
if (h[1] == h[2] && h[3] == h[4])
return 2;
if (h[0] == h[1] || h[1] == h[2] || h[2] == h[3] || h[3] == h[4])
return 1;
return 0;
}
}
int main() {
int N, i, j, r, x = 0;
char c, d;
m['A'] = 1;
for (i = 0; i < 10; ++i)
m[i + '0'] = i;
for (i = 0; i < 4; ++i)
m["TJQK"[i]] = i + 10, m["SCHD"[i]] = i;
while (scanf("%d", &N), N) {
if (x++)
puts("");
for (i = 0; i < 4; ++i)
for (j = 0; j < 13; ++j)
scanf("%d", &v[j][i]);
for (i = 1; i < 10; ++i)
scanf("%d", &k[i]);
while (N--) {
for (r = i = 0; i < 5; ++i) {
scanf(" %c %c", &c, &d);
h[i] = m[c];
s[i] = m[d];
r += v[h[i] - 1][s[i]];
}
sort(h, h + 5);
printf("%d\n", r * k[F()]);
}
}
return 0;
} | #include <algorithm>
#include <cstdio>
using namespace std;
int h[5], s[5];
int v[13][4];
int m[128];
int k[10];
int H() {
for (int i = 1; i < 5; ++i)
if (s[i] - s[i - 1])
return 0;
return 1;
}
int X() { return h[1] + 1 == h[2] && h[2] + 1 == h[3] && h[3] + 1 == h[4]; }
int S() { return X() && h[0] + 1 == h[1]; }
int R() { return X() && h[0] == 1 && h[4] == 13; }
int F() {
if (H()) {
if (R())
return 9;
if (S())
return 8;
return 5;
} else {
if (h[0] == h[3] || h[1] == h[4])
return 7;
if (h[0] == h[1] && h[2] == h[4] || h[0] == h[2] && h[3] == h[4])
return 6;
if (S() || R())
return 4;
if (h[0] == h[2] || h[1] == h[3] || h[2] == h[4])
return 3;
if (h[0] == h[1])
if (h[2] == h[3] || h[3] == h[4])
return 2;
if (h[1] == h[2] && h[3] == h[4])
return 2;
if (h[0] == h[1] || h[1] == h[2] || h[2] == h[3] || h[3] == h[4])
return 1;
return 0;
}
}
int main() {
int N, i, j, r, x = 0;
char c, d;
m['A'] = 1;
for (i = 0; i < 10; ++i)
m[i + '0'] = i;
for (i = 0; i < 4; ++i)
m["TJQK"[i]] = i + 10, m["SCHD"[i]] = i;
while (scanf("%d", &N) >= 0) {
if (x++)
puts("");
for (i = 0; i < 4; ++i)
for (j = 0; j < 13; ++j)
scanf("%d", &v[j][i]);
for (i = 1; i < 10; ++i)
scanf("%d", &k[i]);
while (N--) {
for (r = i = 0; i < 5; ++i) {
scanf(" %c %c", &c, &d);
h[i] = m[c];
s[i] = m[d];
r += v[h[i] - 1][s[i]];
}
sort(h, h + 5);
printf("%d\n", r * k[F()]);
}
}
return 0;
} | replace | 50 | 51 | 50 | 51 | TLE | |
p01340 | C++ | Time Limit Exceeded | #include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
int H, W;
char c[10][10];
int gr[10][10];
string dir = "LURD";
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int usd[32];
int getIndex(int x, int y, int dx, int dy) {
x += dx;
y += dy;
while (!(x < 0 || y < 0 || x >= W || y >= H)) {
if (gr[y][x] != -1 && usd[gr[y][x]] == 0) {
return gr[y][x];
}
x += dx;
y += dy;
}
// cout << x << " " << y << "(" << dx << " " << dy << ")" << endl;
return -1;
}
int kx[32];
int ky[32];
int done2[32];
int test(int g, int d) {
if (done2[g]++)
return 0;
int ans = 1;
for (int i = 0; i < 4; i++) {
if (i == 2)
continue;
int p = getIndex(kx[g], ky[g], dx[(d + i) & 3], dy[(d + i) & 3]);
// cout << g << " " << dir[(d+i)&3] << " " << p << endl;
if (p == -1)
continue;
ans += test(p, (d + i) & 3);
}
return ans;
}
char doit[32];
int cnt = 0;
int dfs(int g, int d, int r) {
if (r == 1) {
cout << doit << endl;
exit(0);
}
memset(done2, 0, sizeof(done2));
int w = test(g, d);
if (w != r) {
return 0;
}
usd[g] = 1;
for (int i = 0; i < 4; i++) {
doit[cnt - r] = dir[(d + i) & 3];
if (i == 2)
continue;
int p = getIndex(kx[g], ky[g], dx[(d + i) & 3], dy[(d + i) & 3]);
if (p == -1)
continue;
dfs(p, (d + i) & 3, r - 1);
}
usd[g] = 0;
}
int main() {
memset(gr, -1, sizeof(gr));
cin >> H >> W;
int sd, sp;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> c[i][j];
if (c[i][j] != '.') {
kx[cnt] = j;
ky[cnt] = i;
gr[i][j] = cnt++;
if (c[i][j] != 'o') {
sd = dir.find(c[i][j]);
sp = cnt - 1;
}
}
}
}
dfs(sp, sd, cnt);
while (1) {
}
} | #include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
int H, W;
char c[10][10];
int gr[10][10];
string dir = "LURD";
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int usd[32];
int getIndex(int x, int y, int dx, int dy) {
x += dx;
y += dy;
while (!(x < 0 || y < 0 || x >= W || y >= H)) {
if (gr[y][x] != -1 && usd[gr[y][x]] == 0) {
return gr[y][x];
}
x += dx;
y += dy;
}
// cout << x << " " << y << "(" << dx << " " << dy << ")" << endl;
return -1;
}
int kx[32];
int ky[32];
int done2[32];
int test(int g, int d) {
if (done2[g]++)
return 0;
int ans = 1;
for (int i = 0; i < 4; i++) {
if (i == 2)
continue;
int p = getIndex(kx[g], ky[g], dx[(d + i) & 3], dy[(d + i) & 3]);
// cout << g << " " << dir[(d+i)&3] << " " << p << endl;
if (p == -1)
continue;
ans += test(p, (d + i) & 3);
}
return ans;
}
char doit[32];
int cnt = 0;
int dfs(int g, int d, int r) {
if (r == 1) {
cout << doit << endl;
exit(0);
}
memset(done2, 0, sizeof(done2));
int w = test(g, d);
/*if( w != r ){
return 0;
}*/
usd[g] = 1;
for (int i = 0; i < 4; i++) {
doit[cnt - r] = dir[(d + i) & 3];
if (i == 2)
continue;
int p = getIndex(kx[g], ky[g], dx[(d + i) & 3], dy[(d + i) & 3]);
if (p == -1)
continue;
dfs(p, (d + i) & 3, r - 1);
}
usd[g] = 0;
}
int main() {
memset(gr, -1, sizeof(gr));
cin >> H >> W;
int sd, sp;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> c[i][j];
if (c[i][j] != '.') {
kx[cnt] = j;
ky[cnt] = i;
gr[i][j] = cnt++;
if (c[i][j] != 'o') {
sd = dir.find(c[i][j]);
sp = cnt - 1;
}
}
}
}
dfs(sp, sd, cnt);
while (1) {
}
} | replace | 56 | 59 | 56 | 59 | TLE | |
p01341 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <stdio.h>
#include <vector>
#define MAX_N 1005
#define MAX_R 20005
using namespace std;
typedef struct edge {
int from, to, cost;
} edge;
bool mcmp(edge x, edge y) { return x.cost < y.cost; }
int uft[MAX_N];
vector<edge> e;
void init(int n) {
int i;
for (i = 0; i <= n; i++)
uft[i] = i;
}
int parent(int n) {
if (uft[n] == n)
return n;
else
return uft[n] = parent(uft[n]);
}
void unit(int p, int q) { uft[parent(p)] = parent(q); }
int same(int p, int q) { return parent(p) == parent(q); }
int kruskal(int n, int r) {
int res = 0;
sort(e.begin(), e.end(), mcmp);
init(n);
for (int i = 0; i < r; i++) {
edge tmp = e[i];
if (!same(tmp.from, tmp.to)) {
res += tmp.cost;
unit(tmp.from, tmp.to);
e[i].cost = 0;
}
}
return res;
}
int piles[10001][2];
int m_distance(int f, int t) {
return pow((double)piles[f][0] - piles[t][0], 2) +
pow((double)piles[f][1] - piles[t][1], 2);
}
int main() {
int N, M;
int all_cost = 0;
scanf("%d%d", &N, &M);
for (int i = 1; i <= N; i++)
scanf("%d%d", &piles[i][0], &piles[i][1]);
for (int j = 0; j < M; j++) {
edge tmp;
scanf("%d%d", &tmp.from, &tmp.to);
tmp.cost = -m_distance(tmp.from, tmp.to);
e.push_back(tmp);
}
kruskal(N, M);
double res = 0.0;
for (int i = 0; i < M; i++)
res += sqrt(-(double)e[i].cost);
printf("%.3lf\n", res);
e.clear();
return 0;
} | #include <algorithm>
#include <cmath>
#include <stdio.h>
#include <vector>
#define MAX_N 10005
using namespace std;
typedef struct edge {
int from, to, cost;
} edge;
bool mcmp(edge x, edge y) { return x.cost < y.cost; }
int uft[MAX_N];
vector<edge> e;
void init(int n) {
int i;
for (i = 0; i <= n; i++)
uft[i] = i;
}
int parent(int n) {
if (uft[n] == n)
return n;
else
return uft[n] = parent(uft[n]);
}
void unit(int p, int q) { uft[parent(p)] = parent(q); }
int same(int p, int q) { return parent(p) == parent(q); }
int kruskal(int n, int r) {
int res = 0;
sort(e.begin(), e.end(), mcmp);
init(n);
for (int i = 0; i < r; i++) {
edge tmp = e[i];
if (!same(tmp.from, tmp.to)) {
res += tmp.cost;
unit(tmp.from, tmp.to);
e[i].cost = 0;
}
}
return res;
}
int piles[10001][2];
int m_distance(int f, int t) {
return pow((double)piles[f][0] - piles[t][0], 2) +
pow((double)piles[f][1] - piles[t][1], 2);
}
int main() {
int N, M;
int all_cost = 0;
scanf("%d%d", &N, &M);
for (int i = 1; i <= N; i++)
scanf("%d%d", &piles[i][0], &piles[i][1]);
for (int j = 0; j < M; j++) {
edge tmp;
scanf("%d%d", &tmp.from, &tmp.to);
tmp.cost = -m_distance(tmp.from, tmp.to);
e.push_back(tmp);
}
kruskal(N, M);
double res = 0.0;
for (int i = 0; i < M; i++)
res += sqrt(-(double)e[i].cost);
printf("%.3lf\n", res);
e.clear();
return 0;
} | replace | 4 | 6 | 4 | 5 | 0 | |
p01341 | C++ | Runtime Error | // kruskal tree
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
#define M 10000
int parent[M], a[M], b[M];
pair<double, int> node[M];
int root(int a) { return parent[a] == a ? a : parent[a] = root(parent[a]); }
int unite(int a, int b) {
int x = root(a), y = root(b);
if (x == y)
return 0;
parent[x] = y;
return 1;
}
int main() {
double s = 0;
int i, n, m;
scanf("%d%d", &n, &m);
vector<pair<double, double>> v(n);
for (i = 0; i < n; i++)
scanf("%lf%lf", &v[i].first, &v[i].second);
for (i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y), x--, y--;
a[i] = x, b[i] = y;
node[i].first = -hypot(v[x].first - v[y].first, v[x].second - v[y].second);
s -= node[i].first;
node[i].second = i;
}
sort(node, node + m);
for (i = 0; i < n; i++)
parent[i] = i;
for (i = 0; i < m; i++)
if (unite(a[node[i].second], b[node[i].second]))
s += node[i].first;
printf("%f\n", s);
} | // kruskal tree
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
#define M 99999
int parent[M], a[M], b[M];
pair<double, int> node[M];
int root(int a) { return parent[a] == a ? a : parent[a] = root(parent[a]); }
int unite(int a, int b) {
int x = root(a), y = root(b);
if (x == y)
return 0;
parent[x] = y;
return 1;
}
int main() {
double s = 0;
int i, n, m;
scanf("%d%d", &n, &m);
vector<pair<double, double>> v(n);
for (i = 0; i < n; i++)
scanf("%lf%lf", &v[i].first, &v[i].second);
for (i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y), x--, y--;
a[i] = x, b[i] = y;
node[i].first = -hypot(v[x].first - v[y].first, v[x].second - v[y].second);
s -= node[i].first;
node[i].second = i;
}
sort(node, node + m);
for (i = 0; i < n; i++)
parent[i] = i;
for (i = 0; i < m; i++)
if (unite(a[node[i].second], b[node[i].second]))
s += node[i].first;
printf("%f\n", s);
} | replace | 6 | 7 | 6 | 7 | 0 | |
p01341 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct edge {
int u, v;
double w;
edge() { u = v = w = 0; }
edge(int uu, int vv, double ww) {
u = uu;
v = vv;
w = ww;
}
bool operator<(const edge &b) const { return w > b.w; }
};
struct point {
int first, second;
point() { first = second = 0; }
point(int xx, int yy) {
first = xx;
second = yy;
}
};
const int MAX_N = 10001;
// pair<int, int> points[MAX_N];
const int MAX_M = MAX_N * (MAX_N - 1) / 2;
edge edges[MAX_M];
point points[MAX_N]; // start from point_1
int par[MAX_N];
int val[MAX_N];
void init(int n) {
for (int i = 1; i <= n; i++) {
par[i] = i;
val[i] = 0;
}
}
int find(int x) {
int root = x;
while (root != par[root])
root = par[root];
while (x != root) {
int t = par[x];
par[x] = root;
x = t;
}
return root;
}
bool same(int x, int y) {
x = find(x);
y = find(y);
return x == y;
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (val[x] < val[y])
par[x] = y;
else
par[y] = x;
if (val[x] == val[y])
val[x]++;
}
int main() {
int N, M;
cin >> N >> M;
point *points = new point[2 * N];
edge *edges = new edge[M];
for (int i = 1; i <= N; i++) {
cin >> points[i].first >> points[i].second;
}
int p, q;
int x1, x2, y1, y2;
for (int i = 0; i < M; i++) {
cin >> p >> q;
edges[i].u = p;
edges[i].v = q;
x1 = points[p].first;
x2 = points[q].first;
y1 = points[p].second;
y2 = points[q].second;
edges[i].w = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
sort(edges, edges + M);
double res = 0;
init(N);
for (int i = 0; i < M; i++) {
int p = edges[i].u, q = edges[i].v;
if (same(p, q)) {
res += edges[i].w;
} else {
unite(p, q);
}
}
printf("%.3lf", res);
delete[] points;
delete[] edges;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct edge {
int u, v;
double w;
edge() { u = v = w = 0; }
edge(int uu, int vv, double ww) {
u = uu;
v = vv;
w = ww;
}
bool operator<(const edge &b) const { return w > b.w; }
};
struct point {
int first, second;
point() { first = second = 0; }
point(int xx, int yy) {
first = xx;
second = yy;
}
};
const int MAX_N = 10001;
// pair<int, int> points[MAX_N];
const int MAX_M = MAX_N * (MAX_N - 1) / 2;
// edge edges[MAX_M];
////point points[MAX_N];//start from point_1
int par[MAX_N];
int val[MAX_N];
void init(int n) {
for (int i = 1; i <= n; i++) {
par[i] = i;
val[i] = 0;
}
}
int find(int x) {
int root = x;
while (root != par[root])
root = par[root];
while (x != root) {
int t = par[x];
par[x] = root;
x = t;
}
return root;
}
bool same(int x, int y) {
x = find(x);
y = find(y);
return x == y;
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (val[x] < val[y])
par[x] = y;
else
par[y] = x;
if (val[x] == val[y])
val[x]++;
}
int main() {
int N, M;
cin >> N >> M;
point *points = new point[2 * N];
edge *edges = new edge[M];
for (int i = 1; i <= N; i++) {
cin >> points[i].first >> points[i].second;
}
int p, q;
int x1, x2, y1, y2;
for (int i = 0; i < M; i++) {
cin >> p >> q;
edges[i].u = p;
edges[i].v = q;
x1 = points[p].first;
x2 = points[q].first;
y1 = points[p].second;
y2 = points[q].second;
edges[i].w = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
sort(edges, edges + M);
double res = 0;
init(N);
for (int i = 0; i < M; i++) {
int p = edges[i].u, q = edges[i].v;
if (same(p, q)) {
res += edges[i].w;
} else {
unite(p, q);
}
}
printf("%.3lf", res);
delete[] points;
delete[] edges;
}
| replace | 30 | 32 | 30 | 32 | MLE | |
p01341 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#define N 100005
#define P pair<int, int>
#define ll long long
#define mk(a, b) make_pair(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
int inf = 0x3f3f3f3f;
int n, m, pre[N];
struct ac {
int v;
double c;
};
void init() {
for (int i = 1; i < N; ++i) {
pre[i] = i;
}
}
int find(int x) {
if (x == pre[x])
return x;
else
return pre[x] = find(pre[x]);
}
void join(int x, int y) {
int fx = find(x);
int fy = find(y);
if (fx == fy)
return;
else if (fx < fy)
pre[fy] = fx;
else
pre[fx] = fy;
}
double sum;
bool vis[N];
double dis[N];
vector<P> a(N);
vector<ac> g[N];
void prime(int x) {
mem(dis, 0);
mem(vis, false);
dis[x] = 1;
for (int i = 1; i <= n; ++i) {
double MAX = 0;
int u = -1;
for (int j = 1; j <= n; ++j) {
if (vis[j])
continue;
if (dis[j] > MAX) {
MAX = dis[j];
u = j;
}
}
if (u == -1)
return;
vis[u] = true;
if (u == x)
MAX = 0;
sum += MAX;
// cout << "u = " << u << " " << MAX << endl;
for (int j = 0; j < g[u].size(); ++j) {
ac t = g[u][j];
if (vis[t.v])
continue;
if (dis[t.v] < t.c) {
dis[t.v] = t.c;
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
while (cin >> n >> m) {
init();
for (int i = 1; i <= n; ++i) {
cin >> a[i].first >> a[i].second;
}
double ans = 0;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
int dx = a[u].first - a[v].first;
int dy = a[u].second - a[v].second;
double temp = sqrt(dx * dx + dy * dy);
// cout << "temp = " << temp << endl;
ans += temp;
g[u].push_back((ac){v, temp});
g[v].push_back((ac){u, temp});
join(u, v);
}
// cout << ans << endl;
for (int i = 1; i <= n; ++i) {
if (pre[i] == i) {
sum = 0;
prime(i);
ans -= sum;
// cout << i << "::" << sum << endl;
}
}
// ans += 1;
// cout << ans << endl;
printf("%.3lf\n", ans);
for (int i = 1; i <= n; ++i) {
g[i].clear();
}
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#define N 100005
#define P pair<int, int>
#define ll long long
#define mk(a, b) make_pair(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
int inf = 0x3f3f3f3f;
int n, m, pre[N];
struct ac {
int v;
double c;
};
void init() {
for (int i = 1; i < N; ++i) {
pre[i] = i;
}
}
int find(int x) {
if (x == pre[x])
return x;
else
return pre[x] = find(pre[x]);
}
void join(int x, int y) {
int fx = find(x);
int fy = find(y);
if (fx == fy)
return;
else if (fx < fy)
pre[fy] = fx;
else
pre[fx] = fy;
}
double sum;
bool vis[N];
double dis[N];
vector<P> a(N);
vector<ac> g[N];
void prime(int x) {
mem(dis, 0);
mem(vis, false);
dis[x] = 1;
for (int i = 1; i <= n; ++i) {
double MAX = 0;
int u = -1;
for (int j = 1; j <= n; ++j) {
if (vis[j])
continue;
if (dis[j] > MAX) {
MAX = dis[j];
u = j;
}
}
if (u == -1)
return;
vis[u] = true;
if (u == x)
MAX = 0;
sum += MAX;
// cout << "u = " << u << " " << MAX << endl;
for (int j = 0; j < g[u].size(); ++j) {
ac t = g[u][j];
if (vis[t.v])
continue;
if (dis[t.v] < t.c) {
dis[t.v] = t.c;
}
}
}
}
int main() {
// #ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// #endif
ios::sync_with_stdio(false);
while (cin >> n >> m) {
init();
for (int i = 1; i <= n; ++i) {
cin >> a[i].first >> a[i].second;
}
double ans = 0;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
int dx = a[u].first - a[v].first;
int dy = a[u].second - a[v].second;
double temp = sqrt(dx * dx + dy * dy);
// cout << "temp = " << temp << endl;
ans += temp;
g[u].push_back((ac){v, temp});
g[v].push_back((ac){u, temp});
join(u, v);
}
// cout << ans << endl;
for (int i = 1; i <= n; ++i) {
if (pre[i] == i) {
sum = 0;
prime(i);
ans -= sum;
// cout << i << "::" << sum << endl;
}
}
// ans += 1;
// cout << ans << endl;
printf("%.3lf\n", ans);
for (int i = 1; i <= n; ++i) {
g[i].clear();
}
}
return 0;
}
| replace | 81 | 84 | 81 | 84 | 0 | |
p01341 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
struct Way {
int p, q, cost;
Way() {}
Way(int _p, int _q, int _cost) {
p = _p;
q = _q;
cost = _cost;
}
bool operator<(const Way &a) const { return cost > a.cost; }
};
struct Node {
int x, y;
};
int par[10000], _rank[10000];
void init(int n) {
for (int i = 0; i < n; i++)
par[i] = i, _rank[i] = 1;
}
int find(int a) {
if (par[a] == a)
return a;
else
return par[a] = find(par[a]);
}
void unite(int _a, int _b) {
int a = find(_a);
int b = find(_b);
if (a == b)
return;
if (_rank[a] > _rank[b]) {
par[b] = a;
} else {
par[a] = b;
if (_rank[a] == _rank[b])
_rank[b]++;
}
}
int main() {
int N, M;
Node node[10000];
Way way[10000000];
cin >> N >> M;
for (int i = 0; i < N; i++) {
cin >> node[i].x >> node[i].y;
}
for (int i = 0; i < M; i++) {
int a, b;
cin >> way[i].p >> way[i].q;
a = node[--way[i].p].x - node[--way[i].q].x;
b = node[way[i].p].y - node[way[i].q].y;
way[i].cost = a * a + b * b;
// printf("\t%d\n",way[i].cost);
}
double ans = 0;
sort(way, way + M);
init(N);
for (int i = 0; i < M; i++) {
int a = find(way[i].p), b = find(way[i].q);
if (a != b)
unite(a, b);
else
ans += sqrt(way[i].cost);
}
printf("%lf\n", ans);
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
struct Way {
int p, q, cost;
Way() {}
Way(int _p, int _q, int _cost) {
p = _p;
q = _q;
cost = _cost;
}
bool operator<(const Way &a) const { return cost > a.cost; }
};
struct Node {
int x, y;
};
int par[10000], _rank[10000];
void init(int n) {
for (int i = 0; i < n; i++)
par[i] = i, _rank[i] = 1;
}
int find(int a) {
if (par[a] == a)
return a;
else
return par[a] = find(par[a]);
}
void unite(int _a, int _b) {
int a = find(_a);
int b = find(_b);
if (a == b)
return;
if (_rank[a] > _rank[b]) {
par[b] = a;
} else {
par[a] = b;
if (_rank[a] == _rank[b])
_rank[b]++;
}
}
int main() {
int N, M;
Node node[10000];
Way way[100000];
cin >> N >> M;
for (int i = 0; i < N; i++) {
cin >> node[i].x >> node[i].y;
}
for (int i = 0; i < M; i++) {
int a, b;
cin >> way[i].p >> way[i].q;
a = node[--way[i].p].x - node[--way[i].q].x;
b = node[way[i].p].y - node[way[i].q].y;
way[i].cost = a * a + b * b;
// printf("\t%d\n",way[i].cost);
}
double ans = 0;
sort(way, way + M);
init(N);
for (int i = 0; i < M; i++) {
int a = find(way[i].p), b = find(way[i].q);
if (a != b)
unite(a, b);
else
ans += sqrt(way[i].cost);
}
printf("%lf\n", ans);
} | replace | 53 | 54 | 53 | 54 | -11 | |
p01341 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int n, m;
struct edge {
int u, v;
double w;
edge(int _u, int _v, double _w) : u(_u), v(_v), w(_w) {}
bool operator<(const edge &b) const { return w > b.w; }
};
vector<edge> ve;
int parent[1005];
int findSet(int u) {
if (parent[u] == u)
return u;
return parent[u] = findSet(parent[u]);
}
void Union(int x, int y) {
int px = findSet(x);
int py = findSet(y);
if (px != py)
parent[px] = py;
}
bool isSameSet(int x, int y) { return findSet(x) == findSet(y); }
vector<pii> vp;
int main() {
// freopen("in","r",stdin);
while (~scanf("%d%d", &n, &m)) {
for (int i = 0; i <= n; i++)
parent[i] = i;
ve.clear();
vp.clear();
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
vp.push_back(make_pair(x, y));
}
double ori = 0;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
pii p1 = vp[u];
pii p2 = vp[v];
double w = sqrt((p1.first - p2.first) * (p1.first - p2.first) +
(p1.second - p2.second) * (p1.second - p2.second));
ori += w;
ve.push_back(edge(u, v, w));
}
sort(ve.begin(), ve.end());
double MaxST = 0;
for (int i = 0; i < ve.size(); i++) {
edge e = ve[i];
if (!isSameSet(e.u, e.v)) {
Union(e.u, e.v);
MaxST += e.w;
}
}
printf("%.3lf\n", ori - MaxST);
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int n, m;
struct edge {
int u, v;
double w;
edge(int _u, int _v, double _w) : u(_u), v(_v), w(_w) {}
bool operator<(const edge &b) const { return w > b.w; }
};
vector<edge> ve;
int parent[10005];
int findSet(int u) {
if (parent[u] == u)
return u;
return parent[u] = findSet(parent[u]);
}
void Union(int x, int y) {
int px = findSet(x);
int py = findSet(y);
if (px != py)
parent[px] = py;
}
bool isSameSet(int x, int y) { return findSet(x) == findSet(y); }
vector<pii> vp;
int main() {
// freopen("in","r",stdin);
while (~scanf("%d%d", &n, &m)) {
for (int i = 0; i <= n; i++)
parent[i] = i;
ve.clear();
vp.clear();
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
vp.push_back(make_pair(x, y));
}
double ori = 0;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
pii p1 = vp[u];
pii p2 = vp[v];
double w = sqrt((p1.first - p2.first) * (p1.first - p2.first) +
(p1.second - p2.second) * (p1.second - p2.second));
ori += w;
ve.push_back(edge(u, v, w));
}
sort(ve.begin(), ve.end());
double MaxST = 0;
for (int i = 0; i < ve.size(); i++) {
edge e = ve[i];
if (!isSameSet(e.u, e.v)) {
Union(e.u, e.v);
MaxST += e.w;
}
}
printf("%.3lf\n", ori - MaxST);
}
return 0;
} | replace | 19 | 20 | 19 | 20 | 0 | |
p01341 | C++ | Runtime Error | /*
Á·Ï°Ì⣺Save your cats_AOJ 2224
¼õÈ¥×îÉÙ¡¢Ð¡µÄ±ß£¬Ê¹µÃÕû¸öÆ½ÃæÁ¬Í¨£»
¼´ÎªÇó×î´óÉú³ÉÊ÷ÎÊÌ⣬µ«×¢ÒâÇóµÄÊDzÃÈ¥µÄ·¾¶¡£
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const int MAX_E = 10007, MAX_V = 10007;
int mark[MAX_E];
pair<int, int> ver[MAX_V];
struct edge {
int u, v, cost;
};
bool cmp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }
edge es[MAX_E];
int V, E;
// DUS start line
int par[MAX_V]; // ¸¸Ç×
int hight[MAX_V]; // Ê÷µÄ¸ß¶È
// ³õʼ»¯n¸öÔªËØ
void init(int n) {
for (int i = 0; i < n; i++) {
par[i] = i;
hight[i] = 0;
}
}
// ²éѯÊ÷µÄ¸ù
int find(int x) {
if (par[x] == x)
return x;
else
return par[x] = find(par[x]);
}
// ºÏ²¢xºÍyËùÊôµÄ¼¯ºÏ
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (hight[x] < hight[y])
par[x] = y;
else {
par[y] = x;
if (hight[x] == hight[y])
hight[x]++;
}
}
// ÅжÏxºÍyÊÇ·ñÊôÓÚͬһ¸ö¼¯ºÏ
bool same(int x, int y) { return find(x) == find(y); }
// DUS end line
double kruskal() {
sort(es, es + E, cmp);
init(V);
double res = 0;
for (int i = 0; i < E; i++) {
edge e = es[i];
if (!same(e.u, e.v)) {
unite(e.u, e.v);
mark[i] = 1;
}
}
for (int i = 0; i < E; i++) {
if (mark[i] == 0)
res += sqrt(-es[i].cost);
}
return res;
}
int main() {
scanf("%d%d", &V, &E);
for (int i = 0; i < V; i++) {
scanf("%d%d", &ver[i].first, &ver[i].second);
}
for (int i = 0; i < E; i++) {
edge ed;
scanf("%d%d", &ed.u, &ed.v);
ed.u--;
ed.v--;
ed.cost = -((ver[ed.u].first - ver[ed.v].first) *
(ver[ed.u].first - ver[ed.v].first) +
(ver[ed.u].second - ver[ed.v].second) *
(ver[ed.u].second - ver[ed.v].second));
es[i] = ed;
}
double ans = kruskal();
printf("%.3lf\n", ans);
return 0;
}
| /*
Á·Ï°Ì⣺Save your cats_AOJ 2224
¼õÈ¥×îÉÙ¡¢Ð¡µÄ±ß£¬Ê¹µÃÕû¸öÆ½ÃæÁ¬Í¨£»
¼´ÎªÇó×î´óÉú³ÉÊ÷ÎÊÌ⣬µ«×¢ÒâÇóµÄÊDzÃÈ¥µÄ·¾¶¡£
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const int MAX_E = 49995007, MAX_V = 10007;
int mark[MAX_E];
pair<int, int> ver[MAX_V];
struct edge {
int u, v, cost;
};
bool cmp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }
edge es[MAX_E];
int V, E;
// DUS start line
int par[MAX_V]; // ¸¸Ç×
int hight[MAX_V]; // Ê÷µÄ¸ß¶È
// ³õʼ»¯n¸öÔªËØ
void init(int n) {
for (int i = 0; i < n; i++) {
par[i] = i;
hight[i] = 0;
}
}
// ²éѯÊ÷µÄ¸ù
int find(int x) {
if (par[x] == x)
return x;
else
return par[x] = find(par[x]);
}
// ºÏ²¢xºÍyËùÊôµÄ¼¯ºÏ
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (hight[x] < hight[y])
par[x] = y;
else {
par[y] = x;
if (hight[x] == hight[y])
hight[x]++;
}
}
// ÅжÏxºÍyÊÇ·ñÊôÓÚͬһ¸ö¼¯ºÏ
bool same(int x, int y) { return find(x) == find(y); }
// DUS end line
double kruskal() {
sort(es, es + E, cmp);
init(V);
double res = 0;
for (int i = 0; i < E; i++) {
edge e = es[i];
if (!same(e.u, e.v)) {
unite(e.u, e.v);
mark[i] = 1;
}
}
for (int i = 0; i < E; i++) {
if (mark[i] == 0)
res += sqrt(-es[i].cost);
}
return res;
}
int main() {
scanf("%d%d", &V, &E);
for (int i = 0; i < V; i++) {
scanf("%d%d", &ver[i].first, &ver[i].second);
}
for (int i = 0; i < E; i++) {
edge ed;
scanf("%d%d", &ed.u, &ed.v);
ed.u--;
ed.v--;
ed.cost = -((ver[ed.u].first - ver[ed.v].first) *
(ver[ed.u].first - ver[ed.v].first) +
(ver[ed.u].second - ver[ed.v].second) *
(ver[ed.u].second - ver[ed.v].second));
es[i] = ed;
}
double ans = kruskal();
printf("%.3lf\n", ans);
return 0;
}
| replace | 12 | 13 | 12 | 13 | 0 | |
p01341 | C++ | Runtime Error | /*
* @Author: 王文宇
* @Date: 2018-03-14 23:59:01
* @Last Modified by: 王文宇
* @Last Modified time: 2018-03-15 00:12:07
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10007;
#define _for(i, a, b) for (int i = a; i <= b; i++)
int n, m, fa[maxn];
struct node {
int x, y;
double length;
} a[maxn];
struct point {
int x, y;
} b[maxn];
bool cmp(node a, node b) { return a.length > b.length; }
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
double get(int x1, int x2, int y1, int y2) {
double xx = (double)(x1 - x2) * (double)(x1 - x2);
double yy = (double)(y1 - y2) * (double)(y1 - y2);
return sqrt(xx + yy);
}
int main(int argc, char const *argv[]) {
cin >> n >> m;
_for(i, 1, n) {
fa[i] = i;
int x, y;
cin >> b[i].x >> b[i].y;
}
double sum = 0.0;
_for(i, 1, m) {
int x, y;
cin >> x >> y;
a[i].x = x;
a[i].y = y;
a[i].length = get(b[x].x, b[y].x, b[x].y, b[y].y);
sum += a[i].length;
}
sort(a + 1, a + 1 + m, cmp);
_for(i, 1, m) {
int p = find(a[i].x);
int q = find(a[i].y);
if (p != q) {
sum -= a[i].length;
fa[p] = q;
}
}
printf("%.3f\n", sum);
return 0;
}
| /*
* @Author: 王文宇
* @Date: 2018-03-14 23:59:01
* @Last Modified by: 王文宇
* @Last Modified time: 2018-03-15 00:12:07
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000007;
#define _for(i, a, b) for (int i = a; i <= b; i++)
int n, m, fa[maxn];
struct node {
int x, y;
double length;
} a[maxn];
struct point {
int x, y;
} b[maxn];
bool cmp(node a, node b) { return a.length > b.length; }
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
double get(int x1, int x2, int y1, int y2) {
double xx = (double)(x1 - x2) * (double)(x1 - x2);
double yy = (double)(y1 - y2) * (double)(y1 - y2);
return sqrt(xx + yy);
}
int main(int argc, char const *argv[]) {
cin >> n >> m;
_for(i, 1, n) {
fa[i] = i;
int x, y;
cin >> b[i].x >> b[i].y;
}
double sum = 0.0;
_for(i, 1, m) {
int x, y;
cin >> x >> y;
a[i].x = x;
a[i].y = y;
a[i].length = get(b[x].x, b[y].x, b[x].y, b[y].y);
sum += a[i].length;
}
sort(a + 1, a + 1 + m, cmp);
_for(i, 1, m) {
int p = find(a[i].x);
int q = find(a[i].y);
if (p != q) {
sum -= a[i].length;
fa[p] = q;
}
}
printf("%.3f\n", sum);
return 0;
}
| replace | 8 | 9 | 8 | 9 | 0 | |
p01342 | C++ | Runtime Error | #include <cstdio>
#include <queue>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
inline int getInt() {
int s;
scanf("%d", &s);
return s;
}
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
using namespace std;
int wall[50][50][4];
const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};
enum Dir { DOWN = 0, RIGHT = 1, LEFT = 3, UP = 2 };
int ix, iy;
int ox, oy;
int h, w;
inline int mkid(int x, int y) { return w * y + x; }
inline int getX(int id) { return id % w; }
inline int getY(int id) { return id / w; }
int bfs() {
queue<int> q;
vector<int> memo(h * w, -1);
memo[mkid(ix, iy)] = 0;
q.push(mkid(ix, iy));
while (q.size()) {
int id = q.front();
q.pop();
int x = getX(id);
int y = getY(id);
if (x == ox && y == oy)
return memo[id];
REP(i, 4) {
int xx = x + _dx[i];
int yy = y + _dy[i];
int ii = mkid(xx, yy);
if (ISIN(xx, yy, w, h) && memo[ii] == -1 && wall[y][x][i] == 0) {
memo[ii] = memo[id] + 1;
q.push(ii);
}
}
}
return -1;
}
int main() {
w = getInt();
h = getInt();
int n = getInt();
REP(i, n) {
int sx = getInt();
int sy = getInt();
int dx = getInt();
int dy = getInt();
if (sx > dx)
swap(sx, dx);
if (sy > dy)
swap(sy, dy);
if (sx == dx) {
for (int y = sy; y < dy; y++) {
wall[y][sx - 1][RIGHT] = 1;
wall[y][sx][LEFT] = 1;
}
} else {
for (int x = sx; x < dx; x++) {
wall[sy - 1][x][UP] = 1;
wall[sy][x][DOWN] = 1;
}
}
}
ix = getInt();
iy = getInt();
ox = getInt();
oy = getInt();
int d = bfs();
int ans = 0;
REP(i, h) REP(j, w) {
if (i + 1 < h && wall[i][j][UP] == 0) {
wall[i][j][UP] = 1;
wall[i + 1][j][DOWN] = 1;
ans = max(ans, bfs() - d);
wall[i][j][UP] = 0;
wall[i + 1][j][DOWN] = 0;
}
if (j + 1 < w && wall[i][j][RIGHT] == 0) {
wall[i][j][RIGHT] = 1;
wall[i][j + 1][LEFT] = 1;
ans = max(ans, bfs() - d);
wall[i][j][RIGHT] = 0;
wall[i][j + 1][LEFT] = 0;
}
}
printf("%d\n", ans);
return 0;
} | #include <cstdio>
#include <queue>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
inline int getInt() {
int s;
scanf("%d", &s);
return s;
}
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
using namespace std;
int wall[64][64][4];
const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};
enum Dir { DOWN = 0, RIGHT = 1, LEFT = 3, UP = 2 };
int ix, iy;
int ox, oy;
int h, w;
inline int mkid(int x, int y) { return w * y + x; }
inline int getX(int id) { return id % w; }
inline int getY(int id) { return id / w; }
int bfs() {
queue<int> q;
vector<int> memo(h * w, -1);
memo[mkid(ix, iy)] = 0;
q.push(mkid(ix, iy));
while (q.size()) {
int id = q.front();
q.pop();
int x = getX(id);
int y = getY(id);
if (x == ox && y == oy)
return memo[id];
REP(i, 4) {
int xx = x + _dx[i];
int yy = y + _dy[i];
int ii = mkid(xx, yy);
if (ISIN(xx, yy, w, h) && memo[ii] == -1 && wall[y][x][i] == 0) {
memo[ii] = memo[id] + 1;
q.push(ii);
}
}
}
return -1;
}
int main() {
w = getInt();
h = getInt();
int n = getInt();
REP(i, n) {
int sx = getInt();
int sy = getInt();
int dx = getInt();
int dy = getInt();
if (sx > dx)
swap(sx, dx);
if (sy > dy)
swap(sy, dy);
if (sx == dx) {
for (int y = sy; y < dy; y++) {
wall[y][sx - 1][RIGHT] = 1;
wall[y][sx][LEFT] = 1;
}
} else {
for (int x = sx; x < dx; x++) {
wall[sy - 1][x][UP] = 1;
wall[sy][x][DOWN] = 1;
}
}
}
ix = getInt();
iy = getInt();
ox = getInt();
oy = getInt();
int d = bfs();
int ans = 0;
REP(i, h) REP(j, w) {
if (i + 1 < h && wall[i][j][UP] == 0) {
wall[i][j][UP] = 1;
wall[i + 1][j][DOWN] = 1;
ans = max(ans, bfs() - d);
wall[i][j][UP] = 0;
wall[i + 1][j][DOWN] = 0;
}
if (j + 1 < w && wall[i][j][RIGHT] == 0) {
wall[i][j][RIGHT] = 1;
wall[i][j + 1][LEFT] = 1;
ans = max(ans, bfs() - d);
wall[i][j][RIGHT] = 0;
wall[i][j + 1][LEFT] = 0;
}
}
printf("%d\n", ans);
return 0;
} | replace | 16 | 17 | 16 | 17 | 0 | |
p01342 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
#define pb push_back
#define sz(x) (int)x.size()
#define scf scanf
#define ptf printf
#define forp(i, j, k) for (int i = j; i < k; i++)
#define form(i, j, k) for (int i = j; i > k; i--)
using namespace std;
typedef long long LL;
const int N = 51, dx[5] = {0, -1, 1, 0, 0}, dy[5] = {0, 0, 0, -1, 1};
struct node {
int x, y;
} q[N * N];
int u[N][N], d[N][N], wl[N][N][5], ix, iy, ox, oy, w, h;
int bfs() {
memset(u, 0, sizeof(u));
q[0].x = ix, q[0].y = iy;
u[ix][iy] = 1;
d[ix][iy] = 0;
for (int l = 0, r = 1; l < r; l++) {
int x = q[l].x, y = q[l].y;
if (x == ox && y == oy)
return d[ox][oy];
forp(i, 1, 5) {
int xx = x + dx[i], yy = y + dy[i];
if (!wl[x][y][i] && xx >= 0 && xx < w && yy >= 0 && yy < h &&
!u[xx][yy]) {
q[r].x = xx;
q[r].y = yy;
r++;
d[xx][yy] = d[x][y] + 1;
u[xx][yy] = 1;
}
}
}
return -1;
}
int main() {
int x1, y1, x2, y2, n;
scf("%d%d%d", &w, &h, &n);
forp(i, 0, n) {
scf("%d%d%d%d", &x1, &y1, &x2, &y2);
if (x1 == x2)
forp(j, min(y1, y2), max(y1, y2)) wl[x1 - 1][j][2] = wl[x1][j][1] = 1;
if (y1 == y2)
forp(j, min(x1, x2), max(x1, x2)) wl[j][y1 - 1][4] = wl[j][y1][3] = 1;
}
scf("%d%d%d%d", &ix, &iy, &ox, &oy);
int tans, ans;
tans = ans = bfs();
forp(i, 0, w) forp(j, 0, h) {
if (i != w - 1 && !wl[i][j][2]) {
wl[i][j][2] = wl[i + 1][j][1] = 1;
tans = max(tans, bfs());
wl[i][j][2] = wl[i + 1][j][1] = 0;
}
if (j != h - 1 && !wl[i][j][4]) {
wl[i][j][4] = wl[i][j + 1][3] = 1;
tans = max(tans, bfs());
wl[i][j][4] = wl[i][j + 1][3] = 0;
}
}
ptf("%d\n", tans - ans);
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
#define pb push_back
#define sz(x) (int)x.size()
#define scf scanf
#define ptf printf
#define forp(i, j, k) for (int i = j; i < k; i++)
#define form(i, j, k) for (int i = j; i > k; i--)
using namespace std;
typedef long long LL;
const int N = 51, dx[5] = {0, -1, 1, 0, 0}, dy[5] = {0, 0, 0, -1, 1};
struct node {
int x, y;
} q[N * N];
int u[N][N], d[N][N], wl[N][N][5], ix, iy, ox, oy, w, h;
int bfs() {
memset(u, 0, sizeof(u));
q[0].x = ix, q[0].y = iy;
u[ix][iy] = 1;
d[ix][iy] = 0;
for (int l = 0, r = 1; l < r; l++) {
int x = q[l].x, y = q[l].y;
if (x == ox && y == oy)
return d[ox][oy];
forp(i, 1, 5) {
int xx = x + dx[i], yy = y + dy[i];
if (!wl[x][y][i] && xx >= 0 && xx < w && yy >= 0 && yy < h &&
!u[xx][yy]) {
q[r].x = xx;
q[r].y = yy;
r++;
d[xx][yy] = d[x][y] + 1;
u[xx][yy] = 1;
}
}
}
return -1;
}
int main() {
int x1, y1, x2, y2, n;
scf("%d%d%d", &w, &h, &n);
forp(i, 0, n) {
scf("%d%d%d%d", &x1, &y1, &x2, &y2);
if (x1 == x2 && (x1 == 0 || x1 == w))
continue;
if (y1 == y2 && (y1 == 0 || y1 == h))
continue;
if (x1 == x2)
forp(j, min(y1, y2), max(y1, y2)) wl[x1 - 1][j][2] = wl[x1][j][1] = 1;
if (y1 == y2)
forp(j, min(x1, x2), max(x1, x2)) wl[j][y1 - 1][4] = wl[j][y1][3] = 1;
}
scf("%d%d%d%d", &ix, &iy, &ox, &oy);
int tans, ans;
tans = ans = bfs();
forp(i, 0, w) forp(j, 0, h) {
if (i != w - 1 && !wl[i][j][2]) {
wl[i][j][2] = wl[i + 1][j][1] = 1;
tans = max(tans, bfs());
wl[i][j][2] = wl[i + 1][j][1] = 0;
}
if (j != h - 1 && !wl[i][j][4]) {
wl[i][j][4] = wl[i][j + 1][3] = 1;
tans = max(tans, bfs());
wl[i][j][4] = wl[i][j + 1][3] = 0;
}
}
ptf("%d\n", tans - ans);
return 0;
} | insert | 54 | 54 | 54 | 58 | 0 | |
p01342 | C++ | Runtime Error | #include <cstdio>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
const int INF = 1 << 29;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int bfs(int h, int w, const bool B[99][99], int sy, int sx, int ty, int tx) {
int d[99][99];
rep(i, h) rep(j, w) d[i][j] = INF;
d[sy][sx] = 0;
queue<int> Q;
Q.push(sy * w + sx);
while (!Q.empty()) {
int y = Q.front() / w, x = Q.front() % w;
Q.pop();
rep(k, 4) {
int yy = y + dy[k], xx = x + dx[k];
if (0 <= yy && yy < h && 0 <= xx && xx < w && !B[yy][xx] &&
d[yy][xx] > d[y][x] + 1) {
d[yy][xx] = d[y][x] + 1;
Q.push(yy * w + xx);
}
}
}
return d[ty][tx];
}
int main() {
int w, h, n;
scanf("%d%d%d", &w, &h, &n);
int wall[1000][4];
rep(i, n) rep(j, 4) scanf("%d", wall[i] + j);
int sx, sy, tx, ty;
scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
sx *= 2;
sy *= 2;
tx *= 2;
ty *= 2;
bool B[99][99] = {};
rep(i, h - 1) rep(j, w - 1) B[2 * i + 1][2 * j + 1] = true;
rep(i, n) {
if (wall[i][0] == wall[i][2]) { // ツ縦ツづ個陛?
for (int y = min(wall[i][1], wall[i][3]); y < max(wall[i][1], wall[i][3]);
y++) {
if (0 < wall[i][0] && wall[i][0] < w) {
B[2 * y][2 * wall[i][0] - 1] = true;
}
}
} else { // ツ可。ツづ個陛?
for (int x = min(wall[i][0], wall[i][2]); x < max(wall[i][0], wall[i][2]);
x++) {
if (0 < wall[i][0] && wall[i][1] < h) {
B[2 * wall[i][1] - 1][2 * x] = true;
}
}
}
}
h = 2 * h - 1;
w = 2 * w - 1;
int d0 = bfs(h, w, B, sy, sx, ty, tx), d = d0;
rep(i, h) rep(j, w) if ((i % 2 == 1 || j % 2 == 1) && !B[i][j]) {
B[i][j] = true;
int tmp = bfs(h, w, B, sy, sx, ty, tx);
if (tmp < INF)
d = max(d, tmp);
B[i][j] = false;
}
printf("%d\n", (d - d0) / 2);
return 0;
} | #include <cstdio>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
const int INF = 1 << 29;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int bfs(int h, int w, const bool B[99][99], int sy, int sx, int ty, int tx) {
int d[99][99];
rep(i, h) rep(j, w) d[i][j] = INF;
d[sy][sx] = 0;
queue<int> Q;
Q.push(sy * w + sx);
while (!Q.empty()) {
int y = Q.front() / w, x = Q.front() % w;
Q.pop();
rep(k, 4) {
int yy = y + dy[k], xx = x + dx[k];
if (0 <= yy && yy < h && 0 <= xx && xx < w && !B[yy][xx] &&
d[yy][xx] > d[y][x] + 1) {
d[yy][xx] = d[y][x] + 1;
Q.push(yy * w + xx);
}
}
}
return d[ty][tx];
}
int main() {
int w, h, n;
scanf("%d%d%d", &w, &h, &n);
int wall[1000][4];
rep(i, n) rep(j, 4) scanf("%d", wall[i] + j);
int sx, sy, tx, ty;
scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
sx *= 2;
sy *= 2;
tx *= 2;
ty *= 2;
bool B[99][99] = {};
rep(i, h - 1) rep(j, w - 1) B[2 * i + 1][2 * j + 1] = true;
rep(i, n) {
if (wall[i][0] == wall[i][2]) { // ツ縦ツづ個陛?
for (int y = min(wall[i][1], wall[i][3]); y < max(wall[i][1], wall[i][3]);
y++) {
if (0 < wall[i][0] && wall[i][0] < w) {
B[2 * y][2 * wall[i][0] - 1] = true;
}
}
} else { // ツ可。ツづ個陛?
for (int x = min(wall[i][0], wall[i][2]); x < max(wall[i][0], wall[i][2]);
x++) {
if (0 < wall[i][1] && wall[i][1] < h) {
B[2 * wall[i][1] - 1][2 * x] = true;
}
}
}
}
h = 2 * h - 1;
w = 2 * w - 1;
int d0 = bfs(h, w, B, sy, sx, ty, tx), d = d0;
rep(i, h) rep(j, w) if ((i % 2 == 1 || j % 2 == 1) && !B[i][j]) {
B[i][j] = true;
int tmp = bfs(h, w, B, sy, sx, ty, tx);
if (tmp < INF)
d = max(d, tmp);
B[i][j] = false;
}
printf("%d\n", (d - d0) / 2);
return 0;
} | replace | 57 | 58 | 57 | 58 | 0 | |
p01347 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define all(x) (x).begin(), (x).end()
#define size(x) ((int)(x).size())
#define FIRST first
#define SECOND second
#define FRONT front
#define PUSH_BACK push_back
#define MAKE_PAIR make_pair
#define EMPTY empty
#define PRIORITY_QUEUE priority_queue
using namespace std;
typedef long long LL;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
// head
const int maxn = 123;
int vin[maxn], vout[maxn];
const int inf = 1e8;
int len[maxn];
struct edge {
int v, r, w, c;
};
std::vector<edge> G[maxn];
int h[maxn], dist[maxn];
int pv[maxn], pe[maxn];
int V;
std::vector<pii> d[maxn];
int DAG_longest_path(int u, int t) {
if (u == t || len[u])
return len[u];
for (auto e : d[u]) {
len[u] = max(len[u], e.second + DAG_longest_path(e.first, t));
}
return len[u];
}
void add(int u, int v, int w, int cost) {
G[u].push_back((edge){v, size(G[v]), w, cost});
G[v].push_back((edge){u, size(G[u]) - 1, 0, -cost});
}
int min_cost_flow(int s, int t, int flow) {
int res = 0;
fill(h, h + V, 0);
while (flow > 0) {
fill(dist, dist + V, inf);
dist[s] = 0;
priority_queue<pii, std::vector<pii>, greater<pii>> q;
q.push(pii(0, s));
while (!q.empty()) {
pii x = q.top();
q.pop();
int u = x.second;
if (dist[u] < x.first)
continue;
for (int i = 0; i < size(G[u]); i++) {
edge &e = G[u][i];
int k = h[u] + dist[u] + e.c - h[e.v];
if (k < dist[e.v] && e.w > 0) {
dist[e.v] = k;
q.push(pii(k, e.v));
pv[e.v] = u;
pe[e.v] = i;
}
}
}
if (dist[t] == inf) {
return -1; // 不能再增广
}
for (int i = 0; i < V; i++) {
h[i] += dist[i];
}
int tf = flow;
for (int v = t; v != s; v = pv[v]) {
tf = min(tf, G[pv[v]][pe[v]].w);
}
if (tf) {
for (int v = t; v != s; v = pv[v]) {
edge &e = G[pv[v]][pe[v]];
e.w -= tf;
G[v][e.r].w += tf;
}
res += tf * h[t];
flow -= tf;
}
}
return res;
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
int tot = 0;
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
vin[v]++, vout[u]++;
d[u].push_back(pii(v, w));
add(u, v, inf, -w);
tot += w;
}
DAG_longest_path(0, n - 1);
int s = n, t = n + 1;
V = n + 2;
add(n - 1, 0, inf, len[0]);
for (int i = 0; i < n; i++) {
if (vin[i] > vout[i]) {
add(s, i, vin[i] - vout[i], 0);
vout[s] += vin[i] - vout[i];
} else if (vout[i] > vin[i]) {
add(i, t, vout[i] - vin[i], 0);
}
}
cout << min_cost_flow(s, t, vout[s]) - tot << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define all(x) (x).begin(), (x).end()
#define size(x) ((int)(x).size())
#define FIRST first
#define SECOND second
#define FRONT front
#define PUSH_BACK push_back
#define MAKE_PAIR make_pair
#define EMPTY empty
#define PRIORITY_QUEUE priority_queue
using namespace std;
typedef long long LL;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
// head
const int maxn = 123;
int vin[maxn], vout[maxn];
const int inf = 1e8;
int len[maxn];
struct edge {
int v, r, w, c;
};
std::vector<edge> G[maxn];
int h[maxn], dist[maxn];
int pv[maxn], pe[maxn];
int V;
std::vector<pii> d[maxn];
int DAG_longest_path(int u, int t) {
if (u == t || len[u])
return len[u];
for (auto e : d[u]) {
len[u] = max(len[u], e.second + DAG_longest_path(e.first, t));
}
return len[u];
}
void add(int u, int v, int w, int cost) {
G[u].push_back((edge){v, size(G[v]), w, cost});
G[v].push_back((edge){u, size(G[u]) - 1, 0, -cost});
}
int min_cost_flow(int s, int t, int flow) {
int res = 0;
fill(h, h + V, 0);
while (flow > 0) {
fill(dist, dist + V, inf);
dist[s] = 0;
priority_queue<pii, std::vector<pii>, greater<pii>> q;
q.push(pii(0, s));
while (!q.empty()) {
pii x = q.top();
q.pop();
int u = x.second;
if (dist[u] < x.first)
continue;
for (int i = 0; i < size(G[u]); i++) {
edge &e = G[u][i];
int k = h[u] + dist[u] + e.c - h[e.v];
if (k < dist[e.v] && e.w > 0) {
dist[e.v] = k;
q.push(pii(k, e.v));
pv[e.v] = u;
pe[e.v] = i;
}
}
}
if (dist[t] == inf) {
return -1; // 不能再增广
}
for (int i = 0; i < V; i++) {
h[i] += dist[i];
}
int tf = flow;
for (int v = t; v != s; v = pv[v]) {
tf = min(tf, G[pv[v]][pe[v]].w);
}
if (tf) {
for (int v = t; v != s; v = pv[v]) {
edge &e = G[pv[v]][pe[v]];
e.w -= tf;
G[v][e.r].w += tf;
}
res += tf * h[t];
flow -= tf;
}
}
return res;
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
int tot = 0;
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
vin[v]++, vout[u]++;
d[u].push_back(pii(v, w));
add(u, v, inf, -w);
tot += w;
}
DAG_longest_path(0, n - 1);
int s = n, t = n + 1;
V = n + 2;
add(n - 1, 0, inf, len[0]);
for (int i = 0; i < n; i++) {
if (vin[i] > vout[i]) {
add(s, i, vin[i] - vout[i], 0);
vout[s] += vin[i] - vout[i];
} else if (vout[i] > vin[i]) {
add(i, t, vout[i] - vin[i], 0);
}
}
cout << min_cost_flow(s, t, vout[s]) - tot << endl;
return 0;
}
| replace | 99 | 101 | 99 | 101 | -11 | |
p01349 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#define N 32
using namespace std;
int Erase();
int Cnt(int, int, string);
int h, w, n;
string cp[N];
int main() {
int r, flag, i, j, k;
string in[N];
cin >> h >> w >> n;
for (i = 0; i <= w + 1; i++)
in[0] += 'x', in[h + 1] += 'x';
for (i = 1; i <= h; i++)
cin >> in[i], in[i] = 'x' + in[i] + 'x';
flag = 0;
for (k = 0; k <= h + 1; k++)
cp[k] = in[k];
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (cp[i][j] != cp[i][j + 1]) {
swap(cp[i][j], cp[i][j + 1]);
r = Erase();
if (r == 0) {
flag = 1;
break;
}
for (k = 0; k <= h + 1; k++)
cp[k] = in[k];
}
}
if (flag == 1)
break;
}
if (flag == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
int Erase() {
int i, j, x, y, cnt, flag, flag2, flag3, mark[h + 2][w + 2];
while (1) {
while (1) {
flag2 = 0;
for (i = h - 1; i >= 1; i--) {
for (j = 1; j <= w; j++) {
if ('A' <= cp[i][j] && cp[i][j] <= 'Z' && cp[i + 1][j] == '.') {
swap(cp[i][j], cp[i + 1][j]);
flag2 = 1;
}
}
}
if (flag2 == 0)
break;
}
flag = 0;
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
mark[i][j] = 0;
}
}
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (cp[i][j] != '.') {
if (cp[i][j] == cp[i][j + 1]) {
cnt = Cnt(i, j, "Right");
if (cnt >= n) {
flag = 1;
y = i;
x = j;
while (cnt) {
mark[y][x] = 1;
x++;
cnt--;
}
}
}
if (cp[i][j] == cp[i + 1][j]) {
cnt = Cnt(i, j, "Down");
if (cnt >= n) {
flag = 1;
y = i;
x = j;
while (cnt) {
mark[y][x] = 1;
y++;
cnt--;
}
}
}
}
}
}
if (flag == 0)
break;
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (mark[i][j] == 1)
cp[i][j] = '.';
}
}
}
flag3 = 0;
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (cp[i][j] != '.') {
flag3 = 1;
break;
}
}
}
if (flag3 == 0)
return 0;
return 1;
}
int Cnt(int y, int x, string d) {
int cnt = 0, py, px;
char t = cp[y][x];
if (d == "Right")
py = 0, px = 1;
else
py = 1, px = 0;
while (1) {
if (cp[y][x] == t) {
cnt++;
} else
break;
y += py, x += px;
}
return cnt;
} | #include <algorithm>
#include <iostream>
#define N 35
using namespace std;
int Erase();
int Cnt(int, int, string);
int h, w, n;
string cp[N];
int main() {
int r, flag, i, j, k;
string in[N];
cin >> h >> w >> n;
for (i = 0; i <= w + 1; i++)
in[0] += 'x', in[h + 1] += 'x';
for (i = 1; i <= h; i++)
cin >> in[i], in[i] = 'x' + in[i] + 'x';
flag = 0;
for (k = 0; k <= h + 1; k++)
cp[k] = in[k];
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (cp[i][j] != cp[i][j + 1]) {
swap(cp[i][j], cp[i][j + 1]);
r = Erase();
if (r == 0) {
flag = 1;
break;
}
for (k = 0; k <= h + 1; k++)
cp[k] = in[k];
}
}
if (flag == 1)
break;
}
if (flag == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
int Erase() {
int i, j, x, y, cnt, flag, flag2, flag3, mark[h + 2][w + 2];
while (1) {
while (1) {
flag2 = 0;
for (i = h - 1; i >= 1; i--) {
for (j = 1; j <= w; j++) {
if ('A' <= cp[i][j] && cp[i][j] <= 'Z' && cp[i + 1][j] == '.') {
swap(cp[i][j], cp[i + 1][j]);
flag2 = 1;
}
}
}
if (flag2 == 0)
break;
}
flag = 0;
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
mark[i][j] = 0;
}
}
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (cp[i][j] != '.') {
if (cp[i][j] == cp[i][j + 1]) {
cnt = Cnt(i, j, "Right");
if (cnt >= n) {
flag = 1;
y = i;
x = j;
while (cnt) {
mark[y][x] = 1;
x++;
cnt--;
}
}
}
if (cp[i][j] == cp[i + 1][j]) {
cnt = Cnt(i, j, "Down");
if (cnt >= n) {
flag = 1;
y = i;
x = j;
while (cnt) {
mark[y][x] = 1;
y++;
cnt--;
}
}
}
}
}
}
if (flag == 0)
break;
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (mark[i][j] == 1)
cp[i][j] = '.';
}
}
}
flag3 = 0;
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (cp[i][j] != '.') {
flag3 = 1;
break;
}
}
}
if (flag3 == 0)
return 0;
return 1;
}
int Cnt(int y, int x, string d) {
int cnt = 0, py, px;
char t = cp[y][x];
if (d == "Right")
py = 0, px = 1;
else
py = 1, px = 0;
while (1) {
if (cp[y][x] == t) {
cnt++;
} else
break;
y += py, x += px;
}
return cnt;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p01349 | C++ | Time Limit Exceeded | #include <stdio.h>
#include <string.h>
void swap(char *a, char *b) {
char tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
int h, w;
char t[30][31];
void fall(void) {
int i, j, k;
for (i = h - 2; i >= 0; i--) {
for (j = 0; j < w; j++) {
if (t[i + 1][j] == '.') {
for (k = i; k < h && t[k + 1][j] == '.'; k++)
;
t[k][j] = t[i][j];
t[i][j] = '.';
}
}
}
}
int range(int a, int b) { return a >= 0 && a < h && b >= 0 && b < w; }
int main(void) {
int i, j, k, l, n, m, p, f, ans, del[30][30], dx[4] = {0, 1, 0, -1},
dy[4] = {-1, 0, 1, 0};
char v[30][31];
scanf("%d%d%d%*c", &h, &w, &n);
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++)
v[i][j] = getchar();
getchar();
}
ans = 0;
for (i = 0; i < h; i++) {
for (j = 0; j < w - 1; j++) {
memcpy(t, v, sizeof(v));
if (t[i][j] == t[i][j + 1] && t[i][j] == '.')
continue;
swap(&t[i][j], &t[i][j + 1]);
fall();
while (1) {
f = 1;
memset(del, 0, sizeof(del));
for (k = 0; k < h; k++) {
for (l = 0; l < w; l++) {
if (t[k][l] != '.') {
for (m = 0; m < 4; m++) {
for (p = 0; range(k + dy[m] * p, l + dx[m] * p) &&
t[k + dy[m] * p][l + dx[m] * p] == t[k][l];
p++)
;
if (p >= n) {
f = 0;
for (p = 0; range(k + dy[m] * p, l + dx[m] * p) &&
t[k + dy[m] * p][l + dx[m] * p] == t[k][l];
p++)
del[k + dy[m] * p][l + dx[m] * p] = 1;
}
}
}
}
}
if (f)
break;
while (getchar() != 'e')
;
for (k = 0; k < h; k++)
for (l = 0; l < w; l++)
if (del[k][l])
t[k][l] = '.';
fall();
}
for (k = 0; k < h; k++) {
for (l = 0; l < w; l++)
if (t[k][l] != '.')
break;
if (l != w)
break;
}
if (k == h)
ans = 1;
}
}
if (ans)
printf("YES\n");
else
printf("NO\n");
return 0;
} | #include <stdio.h>
#include <string.h>
void swap(char *a, char *b) {
char tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
int h, w;
char t[30][31];
void fall(void) {
int i, j, k;
for (i = h - 2; i >= 0; i--) {
for (j = 0; j < w; j++) {
if (t[i + 1][j] == '.') {
for (k = i; k < h && t[k + 1][j] == '.'; k++)
;
t[k][j] = t[i][j];
t[i][j] = '.';
}
}
}
}
int range(int a, int b) { return a >= 0 && a < h && b >= 0 && b < w; }
int main(void) {
int i, j, k, l, n, m, p, f, ans, del[30][30], dx[4] = {0, 1, 0, -1},
dy[4] = {-1, 0, 1, 0};
char v[30][31];
scanf("%d%d%d%*c", &h, &w, &n);
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++)
v[i][j] = getchar();
getchar();
}
ans = 0;
for (i = 0; i < h; i++) {
for (j = 0; j < w - 1; j++) {
memcpy(t, v, sizeof(v));
if (t[i][j] == t[i][j + 1] && t[i][j] == '.')
continue;
swap(&t[i][j], &t[i][j + 1]);
fall();
while (1) {
f = 1;
memset(del, 0, sizeof(del));
for (k = 0; k < h; k++) {
for (l = 0; l < w; l++) {
if (t[k][l] != '.') {
for (m = 0; m < 4; m++) {
for (p = 0; range(k + dy[m] * p, l + dx[m] * p) &&
t[k + dy[m] * p][l + dx[m] * p] == t[k][l];
p++)
;
if (p >= n) {
f = 0;
for (p = 0; range(k + dy[m] * p, l + dx[m] * p) &&
t[k + dy[m] * p][l + dx[m] * p] == t[k][l];
p++)
del[k + dy[m] * p][l + dx[m] * p] = 1;
}
}
}
}
}
if (f)
break;
for (k = 0; k < h; k++)
for (l = 0; l < w; l++)
if (del[k][l])
t[k][l] = '.';
fall();
}
for (k = 0; k < h; k++) {
for (l = 0; l < w; l++)
if (t[k][l] != '.')
break;
if (l != w)
break;
}
if (k == h)
ans = 1;
}
}
if (ans)
printf("YES\n");
else
printf("NO\n");
return 0;
} | delete | 71 | 73 | 71 | 71 | TLE | |
p01350 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 1e5 + 1;
const double EPS = 1e-6;
const double INF = 1e18;
const double PI = acos(-1);
typedef complex<double> P;
double angle(P a, P b) { return abs(arg(a / b)); }
int main() {
int n;
double r, theta;
cin >> n >> r >> theta;
theta = theta / 180 * PI;
vector<P> xy(n);
for (int i = 0; i < n; i++) {
double x, y;
cin >> x >> y;
xy[i] = P(x, y);
}
vector<vector<vector<double>>> dp(
n + 1, vector<vector<double>>(n, vector<double>(MAX, INF)));
dp[n][0][0] = 0;
vector<vector<vector<bool>>> cango(
n + 1, vector<vector<bool>>(n, vector<bool>(n, true)));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (i == j || j == k ||
angle(xy[j] - xy[i], xy[k] - xy[j]) > theta + EPS) {
cango[i][j][k] = false;
}
}
}
}
int ans = 0;
for (int k = 0; k < MAX - 1; k++) {
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < n; j++) {
if (dp[i][j][k] == INF)
continue;
for (int p = 0; p < n; p++) {
if (p == j)
continue;
if (!cango[i][j][p])
continue;
double dist = dp[i][j][k] + abs(xy[p] - xy[j]);
if (dist < dp[j][p][k + 1] && dist < r + EPS) {
dp[j][p][k + 1] = dist;
ans = k + 1;
}
}
}
}
if (ans != k + 1)
break;
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 1e4 + 1;
const double EPS = 1e-6;
const double INF = 1e18;
const double PI = acos(-1);
typedef complex<double> P;
double angle(P a, P b) { return abs(arg(a / b)); }
int main() {
int n;
double r, theta;
cin >> n >> r >> theta;
theta = theta / 180 * PI;
vector<P> xy(n);
for (int i = 0; i < n; i++) {
double x, y;
cin >> x >> y;
xy[i] = P(x, y);
}
vector<vector<vector<double>>> dp(
n + 1, vector<vector<double>>(n, vector<double>(MAX, INF)));
dp[n][0][0] = 0;
vector<vector<vector<bool>>> cango(
n + 1, vector<vector<bool>>(n, vector<bool>(n, true)));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (i == j || j == k ||
angle(xy[j] - xy[i], xy[k] - xy[j]) > theta + EPS) {
cango[i][j][k] = false;
}
}
}
}
int ans = 0;
for (int k = 0; k < MAX - 1; k++) {
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < n; j++) {
if (dp[i][j][k] == INF)
continue;
for (int p = 0; p < n; p++) {
if (p == j)
continue;
if (!cango[i][j][p])
continue;
double dist = dp[i][j][k] + abs(xy[p] - xy[j]);
if (dist < dp[j][p][k + 1] && dist < r + EPS) {
dp[j][p][k + 1] = dist;
ans = k + 1;
}
}
}
}
if (ans != k + 1)
break;
}
cout << ans << endl;
return 0;
}
| replace | 6 | 7 | 6 | 7 | MLE | |
p01350 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount
typedef long double D;
typedef complex<D> P;
typedef pair<int, D> edge;
#define X real()
#define Y imag()
const D eps = 1e-8;
const D inf = 1e12;
const D PI = acos(-1);
namespace std {
bool operator<(const P &a, const P &b) {
return a.X == b.X ? a.Y < b.Y : a.X < b.X;
}
} // namespace std
bool cmp_x(const P &a, const P &b) {
return a.X == b.X ? a.Y < b.Y : a.X < b.X;
}
bool cmp_y(const P &a, const P &b) {
return a.Y == b.Y ? a.X < b.X : a.Y < b.Y;
}
bool cmp_a(const P &a, const P &b) {
return arg(a) == arg(b) ? norm(a) < norm(b) : arg(a) < arg(b);
}
D cross(P a, P b) { return (conj(a) * b).Y; }
D dot(P a, P b) { return (conj(a) * b).X; }
D toRag(D deg) { return deg * PI / 180.0; }
P rot(P base, P a, D theta) {
return base + polar(abs(a - base), arg(a - base) + theta);
}
struct L : public vector<P> { // line and segment
L(const P &a, const P &b) {
push_back(a);
push_back(b);
}
};
D f(P a, P b) { return acos(dot(a, b) / abs(a) / abs(b)) / PI * 180; }
int n;
D r, theta;
D dp[22][22][10010];
vector<edge> es[22][22];
P ps[22];
struct state {
int pre, v, cnt;
D d;
};
bool operator<(const state &a, const state &b) { return a.d > b.d; }
int main() {
cin.sync_with_stdio(false);
cin >> n;
cin >> r >> theta;
rep(i, n) {
D x, y;
cin >> x >> y;
ps[i] = P(x, y);
}
rep(i, n) rep(j, n) rep(k, 10010) dp[i][j][k] = inf;
rep(i, n) rep(j, n) {
if (i == j)
continue;
rep(k, n) { // (i,j) -> k
if (i == k || j == k)
continue;
P v1 = ps[j] - ps[i];
P v2 = ps[k] - ps[j];
if (abs(f(v1, v2)) < theta) {
// dbg(i); dbg(j); dbg(k);
es[i][j].push_back(edge(k, abs(v2)));
}
}
}
priority_queue<state> que;
repl(i, 1, n) { que.push((state){0, i, 1, abs(ps[i] - ps[0])}); }
int res = 0;
while (que.size()) {
int pre = que.top().pre, v = que.top().v, cnt = que.top().cnt;
D d = que.top().d;
que.pop();
if (dp[pre][v][cnt] != inf)
continue;
if (d < r)
maxch(res, cnt);
else
continue;
dp[pre][v][cnt] = d;
for (edge nxt : es[pre][v]) {
que.push((state){v, nxt.fi, cnt + 1, d + nxt.se});
}
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount
typedef long double D;
typedef complex<D> P;
typedef pair<int, D> edge;
#define X real()
#define Y imag()
const D eps = 1e-8;
const D inf = 1e12;
const D PI = acos(-1);
namespace std {
bool operator<(const P &a, const P &b) {
return a.X == b.X ? a.Y < b.Y : a.X < b.X;
}
} // namespace std
bool cmp_x(const P &a, const P &b) {
return a.X == b.X ? a.Y < b.Y : a.X < b.X;
}
bool cmp_y(const P &a, const P &b) {
return a.Y == b.Y ? a.X < b.X : a.Y < b.Y;
}
bool cmp_a(const P &a, const P &b) {
return arg(a) == arg(b) ? norm(a) < norm(b) : arg(a) < arg(b);
}
D cross(P a, P b) { return (conj(a) * b).Y; }
D dot(P a, P b) { return (conj(a) * b).X; }
D toRag(D deg) { return deg * PI / 180.0; }
P rot(P base, P a, D theta) {
return base + polar(abs(a - base), arg(a - base) + theta);
}
struct L : public vector<P> { // line and segment
L(const P &a, const P &b) {
push_back(a);
push_back(b);
}
};
D f(P a, P b) { return acos(dot(a, b) / abs(a) / abs(b)) / PI * 180; }
int n;
D r, theta;
double dp[22][22][10010];
vector<edge> es[22][22];
P ps[22];
struct state {
int pre, v, cnt;
D d;
};
bool operator<(const state &a, const state &b) { return a.d > b.d; }
int main() {
cin.sync_with_stdio(false);
cin >> n;
cin >> r >> theta;
rep(i, n) {
D x, y;
cin >> x >> y;
ps[i] = P(x, y);
}
rep(i, n) rep(j, n) rep(k, 10010) dp[i][j][k] = inf;
rep(i, n) rep(j, n) {
if (i == j)
continue;
rep(k, n) { // (i,j) -> k
if (i == k || j == k)
continue;
P v1 = ps[j] - ps[i];
P v2 = ps[k] - ps[j];
if (abs(f(v1, v2)) < theta) {
// dbg(i); dbg(j); dbg(k);
es[i][j].push_back(edge(k, abs(v2)));
}
}
}
priority_queue<state> que;
repl(i, 1, n) { que.push((state){0, i, 1, abs(ps[i] - ps[0])}); }
int res = 0;
while (que.size()) {
int pre = que.top().pre, v = que.top().v, cnt = que.top().cnt;
D d = que.top().d;
que.pop();
if (dp[pre][v][cnt] != inf)
continue;
if (d < r)
maxch(res, cnt);
else
continue;
dp[pre][v][cnt] = d;
for (edge nxt : es[pre][v]) {
que.push((state){v, nxt.fi, cnt + 1, d + nxt.se});
}
}
cout << res << endl;
return 0;
} | replace | 64 | 65 | 64 | 65 | MLE | |
p01350 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double R;
typedef complex<R> P;
const R EPS = 1e-10;
const R INF = 1e9;
const R PI = acos((R)(-1));
//?§???????[0, 2*PI)???
R radNorP(R x) { return fmod(fmod(x, 2 * PI) + 2 * PI, 2 * PI); }
//?§???????[-PI, PI)???
R radNorN(R x) {
x = radNorP(x);
if (x >= PI)
x -= 2 * PI;
return x;
}
R deg2rad(R x) { return x / 180 * PI; }
int n;
R r, th;
P p[22];
R dp[22][22][10100];
R g[22][22][22];
int main() {
cin >> n;
cin >> r >> th;
th = deg2rad(th);
for (int i = 0; i < n; i++) {
R x, y;
cin >> x >> y;
p[i] = P(x, y);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
g[i][j][k] = INF;
if (i == j || j == k)
continue;
R u = arg(p[j] - p[i]) - arg(p[k] - p[j]);
u = abs(radNorN(u));
if (u >= th)
continue;
g[i][j][k] = abs(p[k] - p[j]);
// printf("%d %d %d %Lf %Lf\n", i, j, k, u, g[i][j][k]);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j][1] = INF;
if (i != 0)
continue;
if (j == 0)
continue;
dp[0][j][1] = abs(p[j] - p[i]);
}
}
for (int s = 2; s < 10100; s++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j][s] = INF;
for (int k = 0; k < n; k++) {
dp[i][j][s] = min(dp[i][j][s], dp[k][i][s - 1] + g[k][i][j]);
}
}
}
}
int res = 0;
for (int s = 1; s < 10100; s++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dp[i][j][s] < r)
res = max(res, s);
}
}
}
cout << res << endl;
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef double R;
typedef complex<R> P;
const R EPS = 1e-10;
const R INF = 1e9;
const R PI = acos((R)(-1));
//?§???????[0, 2*PI)???
R radNorP(R x) { return fmod(fmod(x, 2 * PI) + 2 * PI, 2 * PI); }
//?§???????[-PI, PI)???
R radNorN(R x) {
x = radNorP(x);
if (x >= PI)
x -= 2 * PI;
return x;
}
R deg2rad(R x) { return x / 180 * PI; }
int n;
R r, th;
P p[22];
R dp[22][22][10100];
R g[22][22][22];
int main() {
cin >> n;
cin >> r >> th;
th = deg2rad(th);
for (int i = 0; i < n; i++) {
R x, y;
cin >> x >> y;
p[i] = P(x, y);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
g[i][j][k] = INF;
if (i == j || j == k)
continue;
R u = arg(p[j] - p[i]) - arg(p[k] - p[j]);
u = abs(radNorN(u));
if (u >= th)
continue;
g[i][j][k] = abs(p[k] - p[j]);
// printf("%d %d %d %Lf %Lf\n", i, j, k, u, g[i][j][k]);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j][1] = INF;
if (i != 0)
continue;
if (j == 0)
continue;
dp[0][j][1] = abs(p[j] - p[i]);
}
}
for (int s = 2; s < 10100; s++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j][s] = INF;
for (int k = 0; k < n; k++) {
dp[i][j][s] = min(dp[i][j][s], dp[k][i][s - 1] + g[k][i][j]);
}
}
}
}
int res = 0;
for (int s = 1; s < 10100; s++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dp[i][j][s] < r)
res = max(res, s);
}
}
}
cout << res << endl;
return 0;
} | replace | 14 | 15 | 14 | 15 | MLE | |
p01350 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define INF 1e9
using namespace std;
struct P {
double cost;
int pos, pre, num;
P(double cost, int pos, int pre, int num)
: cost(cost), pos(pos), pre(pre), num(num) {}
bool operator<(const P &a) const { return cost < a.cost; }
};
typedef complex<double> point;
point z[21];
int n;
double r, x;
vector<P> e[21][21];
bool check(point a, point b) {
a *= conj(b);
return 180.0 * (abs(arg(a)) / M_PI) <= x;
}
double D[10001][21][21];
int dijkstra() {
int res = 0;
for (int i = 0; i < 10001; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
D[i][j][k] = INF;
priority_queue<P> Q;
Q.push(P(0, 0, 0, 0));
D[0][0][0] = 0;
while (!Q.empty()) {
P t = Q.top();
Q.pop();
double cost = t.cost;
int pos = t.pos, pre = t.pre, num = t.num;
if (cost > r)
continue;
if (D[num][pos][pre] < cost)
continue;
res = max(res, num);
for (int i = 0; i < e[pos][pre].size(); i++) {
int nx = e[pos][pre][i].pos;
double ncost = cost + e[pos][pre][i].cost;
double &a = D[num + 1][nx][pos];
if (a > ncost) {
Q.push(P(ncost, nx, pos, num + 1));
a = ncost;
}
}
}
return res;
}
int main() {
cin >> n >> r >> x;
for (int i = 0; i < n; i++) {
double a, b;
cin >> a >> b;
z[i] = point(a, b);
}
for (int i = 0; i < n; i++) // pos
for (int j = 0; j < n; j++) // pre
for (int k = 0; k < n; k++) { // to
double cost = abs(z[i] - z[k]);
if (i != k && i != j && check(z[i] - z[j], z[k] - z[i]))
e[i][j].push_back(P(cost, k, 0, 0));
}
for (int i = 1; i < n; i++)
e[0][0].push_back(P(abs(z[0] - z[i]), i, 0, 0));
cout << dijkstra() << endl;
return 0;
} | #include <bits/stdc++.h>
#define INF 1e9
using namespace std;
struct P {
double cost;
int pos, pre, num;
P(double cost, int pos, int pre, int num)
: cost(cost), pos(pos), pre(pre), num(num) {}
bool operator<(const P &a) const { return cost > a.cost; }
};
typedef complex<double> point;
point z[21];
int n;
double r, x;
vector<P> e[21][21];
bool check(point a, point b) {
a *= conj(b);
return 180.0 * (abs(arg(a)) / M_PI) <= x;
}
double D[10001][21][21];
int dijkstra() {
int res = 0;
for (int i = 0; i < 10001; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
D[i][j][k] = INF;
priority_queue<P> Q;
Q.push(P(0, 0, 0, 0));
D[0][0][0] = 0;
while (!Q.empty()) {
P t = Q.top();
Q.pop();
double cost = t.cost;
int pos = t.pos, pre = t.pre, num = t.num;
if (cost > r)
continue;
if (D[num][pos][pre] < cost)
continue;
res = max(res, num);
for (int i = 0; i < e[pos][pre].size(); i++) {
int nx = e[pos][pre][i].pos;
double ncost = cost + e[pos][pre][i].cost;
double &a = D[num + 1][nx][pos];
if (a > ncost) {
Q.push(P(ncost, nx, pos, num + 1));
a = ncost;
}
}
}
return res;
}
int main() {
cin >> n >> r >> x;
for (int i = 0; i < n; i++) {
double a, b;
cin >> a >> b;
z[i] = point(a, b);
}
for (int i = 0; i < n; i++) // pos
for (int j = 0; j < n; j++) // pre
for (int k = 0; k < n; k++) { // to
double cost = abs(z[i] - z[k]);
if (i != k && i != j && check(z[i] - z[j], z[k] - z[i]))
e[i][j].push_back(P(cost, k, 0, 0));
}
for (int i = 1; i < n; i++)
e[0][0].push_back(P(abs(z[0] - z[i]), i, 0, 0));
cout << dijkstra() << endl;
return 0;
} | replace | 8 | 9 | 8 | 9 | TLE | |
p01351 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <numeric>
#include <queue>
#include <set>
inline int getInt() {
int s;
scanf("%d", &s);
return s;
}
typedef long long ll;
using namespace std;
int check(int a, int m) {
vector<int> visit(m);
int now = 0;
while (!visit[now]) {
visit[now] = 1;
now = (now + a) % m;
}
return accumulate(visit.begin(), visit.end(), 0);
}
int main() {
int m = getInt();
int n = getInt();
int a = getInt();
int b = getInt();
int c = getInt();
int d = getInt();
a %= m;
b %= n;
c %= m;
d %= n;
int acnt = check(a, m);
int bcnt = check(b, n);
ll ans1 = ((ll)acnt * bcnt) / __gcd(acnt, bcnt);
ll ans2 = 0;
vector<int> h(m, -1);
int hh = 0;
int xx = 0;
int yy = 0;
while (h[xx] == -1) {
h[xx] = yy;
xx = (xx + a) % m;
yy = (yy + b) % n;
hh++;
}
int ym = ans1 / hh;
int yd = n / ym;
xx = c;
yy = d;
while (true) {
ans2++;
if (h[xx] != -1) {
if ((yy - h[xx] + n) % yd == 0)
break;
}
xx = (xx + c) % m;
yy = (yy + d) % n;
}
printf("%lld\n", ans1 * ans2 - 1);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <numeric>
#include <queue>
#include <set>
inline int getInt() {
int s;
scanf("%d", &s);
return s;
}
typedef long long ll;
using namespace std;
int check(int a, int m) {
vector<int> visit(m);
int now = 0;
while (!visit[now]) {
visit[now] = 1;
now = (now + a) % m;
}
return accumulate(visit.begin(), visit.end(), 0);
}
int main() {
int m = getInt();
int n = getInt();
int a = getInt();
int b = getInt();
int c = getInt();
int d = getInt();
a %= m;
b %= n;
c %= m;
d %= n;
int acnt = check(a, m);
int bcnt = check(b, n);
ll ans1 = ((ll)acnt * bcnt) / __gcd(acnt, bcnt);
ll ans2 = 0;
vector<int> h(m, -1);
int hh = 0;
int xx = 0;
int yy = 0;
while (h[xx] == -1) {
h[xx] = yy;
xx = (xx + a) % m;
yy = (yy + b) % n;
hh++;
}
int ym = ans1 / hh;
int yd = n / ym;
xx = c;
yy = d;
while (true) {
ans2++;
if (ans2 == 1000 * n) {
int ccnt = check(c, m);
int dcnt = check(d, n);
ans2 = ((ll)ccnt * dcnt) / __gcd(ccnt, dcnt);
break;
}
if (h[xx] != -1) {
if ((yy - h[xx] + n) % yd == 0)
break;
}
xx = (xx + c) % m;
yy = (yy + d) % n;
}
printf("%lld\n", ans1 * ans2 - 1);
return 0;
} | insert | 68 | 68 | 68 | 75 | TLE | |
p01352 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount
#define INF 1e16
#define mod 1000000007
#define B 10
struct UnionFind {
vector<int> v;
vector<int> sav;
vector<int> hist;
UnionFind(int n) : v(n, -1) {}
void init() {
for (int i = 0; i < v.size(); i++)
v[i] = -1;
}
int find(int x) { return v[x] < 0 ? x : find(v[x]); }
void rollback() {
while (hist.size()) {
v[hist.back()] = sav[hist.back()];
hist.pop_back();
}
}
void snapshot() {
sav = v;
hist.clear();
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (-v[x] < -v[y])
swap(x, y);
hist.push_back(x);
hist.push_back(y);
v[x] += v[y];
v[y] = x;
return true;
}
bool root(int x) { return v[x] < 0; }
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -v[find(x)]; }
};
struct query {
int typ, u, v;
};
int N, Q;
vector<query> qs;
set<P> es;
int main() {
cin >> N >> Q;
rep(i, Q) {
int t, u, v;
cin >> t >> u >> v;
qs.push_back((query){t, u, v});
}
for (int sq = 0; sq < Q; sq += B) {
map<P, bool> qes;
set<P> est;
UnionFind uf(N);
for (int i = sq; i < min(sq + B, Q); i++) {
query q = qs[i];
if (q.typ <= 2)
qes[P(q.u, q.v)] = true;
}
for (P e : es) {
if (qes[P(e.fi, e.se)]) {
est.insert(e);
} else {
uf.unite(e.fi, e.se);
}
}
uf.snapshot();
for (int i = sq; i < min(sq + B, Q); i++) {
query q = qs[i];
if (q.typ == 1) {
est.insert(P(q.u, q.v));
es.insert(P(q.u, q.v));
} else if (q.typ == 2) {
est.erase(P(q.u, q.v));
es.erase(P(q.u, q.v));
} else {
uf.rollback();
for (P e : est) {
uf.unite(e.fi, e.se);
}
if (uf.same(q.u, q.v))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount
#define INF 1e16
#define mod 1000000007
#define B 200
struct UnionFind {
vector<int> v;
vector<int> sav;
vector<int> hist;
UnionFind(int n) : v(n, -1) {}
void init() {
for (int i = 0; i < v.size(); i++)
v[i] = -1;
}
int find(int x) { return v[x] < 0 ? x : find(v[x]); }
void rollback() {
while (hist.size()) {
v[hist.back()] = sav[hist.back()];
hist.pop_back();
}
}
void snapshot() {
sav = v;
hist.clear();
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (-v[x] < -v[y])
swap(x, y);
hist.push_back(x);
hist.push_back(y);
v[x] += v[y];
v[y] = x;
return true;
}
bool root(int x) { return v[x] < 0; }
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -v[find(x)]; }
};
struct query {
int typ, u, v;
};
int N, Q;
vector<query> qs;
set<P> es;
int main() {
cin >> N >> Q;
rep(i, Q) {
int t, u, v;
cin >> t >> u >> v;
qs.push_back((query){t, u, v});
}
for (int sq = 0; sq < Q; sq += B) {
map<P, bool> qes;
set<P> est;
UnionFind uf(N);
for (int i = sq; i < min(sq + B, Q); i++) {
query q = qs[i];
if (q.typ <= 2)
qes[P(q.u, q.v)] = true;
}
for (P e : es) {
if (qes[P(e.fi, e.se)]) {
est.insert(e);
} else {
uf.unite(e.fi, e.se);
}
}
uf.snapshot();
for (int i = sq; i < min(sq + B, Q); i++) {
query q = qs[i];
if (q.typ == 1) {
est.insert(P(q.u, q.v));
es.insert(P(q.u, q.v));
} else if (q.typ == 2) {
est.erase(P(q.u, q.v));
es.erase(P(q.u, q.v));
} else {
uf.rollback();
for (P e : est) {
uf.unite(e.fi, e.se);
}
if (uf.same(q.u, q.v))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
}
return 0;
}
| replace | 24 | 25 | 24 | 25 | TLE | |
p01352 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define for_(i, a, b) for (int i = (a); i < (b); ++i)
#define allof(a) (a).begin(), (a).end()
typedef pair<int, int> pii;
struct Edge : pii {
Edge(int u, int v) : pii(u, v) {}
int u() { return first; }
int v() { return second; }
};
class UnionFind {
private:
vector<int> data;
int num_of_g;
public:
UnionFind(int n) : data(n, -1), num_of_g(n) {}
bool unionSet(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return 0;
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
--num_of_g;
return 1;
}
bool sameSet(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
int getNumOfG() { return num_of_g; }
};
#define BUCKET_SIZE 50
int n, k, id[40010], t[40010], u[40010], v[40010];
vector<multiset<int>> eset;
vector<int> vis;
bool dfs(int v, int tar, int col) {
if (v == tar)
return true;
if (vis[v] == col)
return false;
vis[v] = col;
for (int u : eset[v])
if (dfs(u, tar, col))
return true;
return false;
}
void solve() {
set<Edge> edges;
vis.assign(n, -1);
int cur_col = 0;
for (int i = 0; i < k; i += BUCKET_SIZE) {
// vertex setting
vector<int> vset;
for (int rep = 0; i + rep < k && rep < BUCKET_SIZE; ++rep) {
int q = i + rep;
vset.push_back(u[q]);
vset.push_back(v[q]);
}
sort(allof(vset));
vset.erase(unique(allof(vset)), vset.end());
// graph construction
int V = vset.size();
for_(i, 0, n) id[i] = -1;
for_(i, 0, V) id[vset[i]] = i;
eset.assign(V, multiset<int>());
UnionFind uf(n);
for (Edge e : edges) {
if (id[e.u()] != -1 && id[e.v()] != -1) {
eset[id[e.u()]].insert(id[e.v()]);
eset[id[e.v()]].insert(id[e.u()]);
} else {
uf.unionSet(e.u(), e.v());
}
}
for_(i, 0, V) for_(j, i + 1, V) {
int u = vset[i], v = vset[j];
if (uf.sameSet(u, v)) {
eset[i].insert(j);
eset[j].insert(i);
}
}
// query
for (int rep = 0; i + rep < k && rep < BUCKET_SIZE; ++rep) {
int q = i + rep;
if (t[q] == 1) {
eset[id[u[q]]].insert(id[v[q]]);
eset[id[v[q]]].insert(id[u[q]]);
edges.insert(Edge(u[q], v[q]));
}
if (t[q] == 2) {
eset[id[u[q]]].erase(id[v[q]]);
eset[id[v[q]]].erase(id[u[q]]);
edges.erase(Edge(u[q], v[q]));
}
if (t[q] == 3) {
cout << (dfs(id[u[q]], id[v[q]], cur_col) ? "YES" : "NO") << endl;
++cur_col;
}
}
}
}
int main() {
cin >> n >> k;
for_(i, 0, k) cin >> t[i] >> u[i] >> v[i];
solve();
} | #include <bits/stdc++.h>
using namespace std;
#define for_(i, a, b) for (int i = (a); i < (b); ++i)
#define allof(a) (a).begin(), (a).end()
typedef pair<int, int> pii;
struct Edge : pii {
Edge(int u, int v) : pii(u, v) {}
int u() { return first; }
int v() { return second; }
};
class UnionFind {
private:
vector<int> data;
int num_of_g;
public:
UnionFind(int n) : data(n, -1), num_of_g(n) {}
bool unionSet(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return 0;
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
--num_of_g;
return 1;
}
bool sameSet(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
int getNumOfG() { return num_of_g; }
};
#define BUCKET_SIZE 25
int n, k, id[40010], t[40010], u[40010], v[40010];
vector<multiset<int>> eset;
vector<int> vis;
bool dfs(int v, int tar, int col) {
if (v == tar)
return true;
if (vis[v] == col)
return false;
vis[v] = col;
for (int u : eset[v])
if (dfs(u, tar, col))
return true;
return false;
}
void solve() {
set<Edge> edges;
vis.assign(n, -1);
int cur_col = 0;
for (int i = 0; i < k; i += BUCKET_SIZE) {
// vertex setting
vector<int> vset;
for (int rep = 0; i + rep < k && rep < BUCKET_SIZE; ++rep) {
int q = i + rep;
vset.push_back(u[q]);
vset.push_back(v[q]);
}
sort(allof(vset));
vset.erase(unique(allof(vset)), vset.end());
// graph construction
int V = vset.size();
for_(i, 0, n) id[i] = -1;
for_(i, 0, V) id[vset[i]] = i;
eset.assign(V, multiset<int>());
UnionFind uf(n);
for (Edge e : edges) {
if (id[e.u()] != -1 && id[e.v()] != -1) {
eset[id[e.u()]].insert(id[e.v()]);
eset[id[e.v()]].insert(id[e.u()]);
} else {
uf.unionSet(e.u(), e.v());
}
}
for_(i, 0, V) for_(j, i + 1, V) {
int u = vset[i], v = vset[j];
if (uf.sameSet(u, v)) {
eset[i].insert(j);
eset[j].insert(i);
}
}
// query
for (int rep = 0; i + rep < k && rep < BUCKET_SIZE; ++rep) {
int q = i + rep;
if (t[q] == 1) {
eset[id[u[q]]].insert(id[v[q]]);
eset[id[v[q]]].insert(id[u[q]]);
edges.insert(Edge(u[q], v[q]));
}
if (t[q] == 2) {
eset[id[u[q]]].erase(id[v[q]]);
eset[id[v[q]]].erase(id[u[q]]);
edges.erase(Edge(u[q], v[q]));
}
if (t[q] == 3) {
cout << (dfs(id[u[q]], id[v[q]], cur_col) ? "YES" : "NO") << endl;
++cur_col;
}
}
}
}
int main() {
cin >> n >> k;
for_(i, 0, k) cin >> t[i] >> u[i] >> v[i];
solve();
} | replace | 43 | 44 | 43 | 44 | TLE | |
p01352 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
if (t > f)
t = f;
}
template <class T, class U> void chmax(T &t, U f) {
if (t < f)
t = f;
}
const int X = 200;
int N, Q;
int T[44444], A[44444], B[44444], id[44444];
int ans[44444];
int col[44444];
vpint G[44444];
bool ex[44444];
vpint GG[44444];
bool used[44444];
bool rel[44444];
bool dfs(int v, int t, vint &lis) {
used[v] = true;
lis.pb(v);
if (v == t)
return true;
for (auto &e : GG[v]) {
if (!ex[e.se])
continue;
if (used[e.fi])
continue;
if (dfs(e.fi, t, lis))
return true;
}
return false;
}
void q(int ql, int qr) {
memset(col, -1, sizeof(col));
for (int i = ql; i < qr; i++)
if (T[i] != 3)
rel[id[i]] = true;
int n = 0;
for (int i = 0; i < N; i++) {
if (col[i] != -1)
continue;
queue<int> que;
col[i] = n++;
que.push(i);
while (que.size()) {
int v = que.front();
que.pop();
for (auto &e : G[v]) {
if (ex[e.se] && col[e.fi] == -1 && !rel[e.se]) {
col[e.fi] = col[v];
que.push(e.fi);
}
}
}
}
rep(i, n) GG[i].clear();
for (int i = ql; i < qr; i++)
if (T[i] != 3) {
GG[col[A[i]]].pb({col[B[i]], id[i]});
GG[col[B[i]]].pb({col[A[i]], id[i]});
}
for (int i = ql; i < qr; i++) {
if (T[i] != 3) {
ex[id[i]] ^= 1;
} else {
vint lis;
ans[i] = dfs(col[A[i]], col[B[i]], lis);
for (auto u : lis)
used[u] = 0;
}
}
for (int i = ql; i < qr; i++)
if (T[i] != 3)
rel[id[i]] = false;
}
signed main() {
scanf("%lld%lld", &N, &Q);
rep(i, Q) scanf("%lld%lld%lld", &T[i], &A[i], &B[i]);
vector<pint> v;
rep(i, Q) if (T[i] != 3) v.pb({A[i], B[i]});
sort(all(v));
v.erase(unique(all(v)), v.end());
rep(i, Q) if (T[i] != 3) id[i] =
lower_bound(all(v), pint(A[i], B[i])) - v.begin();
rep(i, Q) if (T[i] != 3) {
G[A[i]].pb({B[i], id[i]});
G[B[i]].pb({A[i], id[i]});
}
for (int i = 0; i * X < Q; i++) {
q(i * X, min(Q, (i + 1) * X));
}
rep(i, Q) if (T[i] == 3) puts(ans[i] ? "YES" : "NO");
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
if (t > f)
t = f;
}
template <class T, class U> void chmax(T &t, U f) {
if (t < f)
t = f;
}
const int X = 400;
int N, Q;
int T[44444], A[44444], B[44444], id[44444];
int ans[44444];
int col[44444];
vpint G[44444];
bool ex[44444];
vpint GG[44444];
bool used[44444];
bool rel[44444];
bool dfs(int v, int t, vint &lis) {
used[v] = true;
lis.pb(v);
if (v == t)
return true;
for (auto &e : GG[v]) {
if (!ex[e.se])
continue;
if (used[e.fi])
continue;
if (dfs(e.fi, t, lis))
return true;
}
return false;
}
void q(int ql, int qr) {
memset(col, -1, sizeof(col));
for (int i = ql; i < qr; i++)
if (T[i] != 3)
rel[id[i]] = true;
int n = 0;
for (int i = 0; i < N; i++) {
if (col[i] != -1)
continue;
queue<int> que;
col[i] = n++;
que.push(i);
while (que.size()) {
int v = que.front();
que.pop();
for (auto &e : G[v]) {
if (ex[e.se] && col[e.fi] == -1 && !rel[e.se]) {
col[e.fi] = col[v];
que.push(e.fi);
}
}
}
}
rep(i, n) GG[i].clear();
for (int i = ql; i < qr; i++)
if (T[i] != 3) {
GG[col[A[i]]].pb({col[B[i]], id[i]});
GG[col[B[i]]].pb({col[A[i]], id[i]});
}
for (int i = ql; i < qr; i++) {
if (T[i] != 3) {
ex[id[i]] ^= 1;
} else {
vint lis;
ans[i] = dfs(col[A[i]], col[B[i]], lis);
for (auto u : lis)
used[u] = 0;
}
}
for (int i = ql; i < qr; i++)
if (T[i] != 3)
rel[id[i]] = false;
}
signed main() {
scanf("%lld%lld", &N, &Q);
rep(i, Q) scanf("%lld%lld%lld", &T[i], &A[i], &B[i]);
vector<pint> v;
rep(i, Q) if (T[i] != 3) v.pb({A[i], B[i]});
sort(all(v));
v.erase(unique(all(v)), v.end());
rep(i, Q) if (T[i] != 3) id[i] =
lower_bound(all(v), pint(A[i], B[i])) - v.begin();
rep(i, Q) if (T[i] != 3) {
G[A[i]].pb({B[i], id[i]});
G[B[i]].pb({A[i], id[i]});
}
for (int i = 0; i * X < Q; i++) {
q(i * X, min(Q, (i + 1) * X));
}
rep(i, Q) if (T[i] == 3) puts(ans[i] ? "YES" : "NO");
return 0;
} | replace | 26 | 27 | 26 | 27 | TLE | |
p01352 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define set unordered_set
#define map unordered_map
using namespace std;
class UnionFind {
private:
int size;
int parent[40000];
int rank[40000];
public:
UnionFind() {}
UnionFind(int sz) { init(sz); }
void init(int sz) {
size = sz;
fill(rank, rank + sz, 0);
for (int i = 0; i < sz; ++i) {
parent[i] = i;
}
}
int root(int n) {
if (parent[n] == n)
return n;
return parent[n] = root(parent[n]);
}
void unite(int n, int m) {
int x = root(n);
int y = root(m);
if (x == y)
return;
if (rank[x] < rank[y]) {
parent[x] = y;
} else if (rank[x] > rank[y]) {
parent[y] = x;
} else {
parent[x] = y;
++rank[y];
}
}
bool isUnited(int n, int m) { return root(n) == root(m); }
};
struct Query {
int type, u, v;
};
typedef vector<map<int, int>> Graph;
const int bucket = 60;
map<int, int> make_table(set<int> &use) {
map<int, int> res;
int n = 0;
for (set<int>::iterator it = use.begin(); it != use.end(); ++it, ++n) {
res[*it] = n;
}
return res;
}
Graph trim(Graph &G, set<int> &use) {
int i = 0;
Graph res(use.size());
UnionFind uf(G.size());
for (int i = 0; i < G.size(); ++i) {
for (map<int, int>::iterator it = G[i].begin(); it != G[i].end(); ++it) {
if (it->second) {
int v = it->first;
if (!use.count(i) || !use.count(v)) {
uf.unite(i, v);
}
}
}
}
for (set<int>::iterator it1 = use.begin(); it1 != use.end(); ++it1, ++i) {
int j = 0;
for (set<int>::iterator it2 = use.begin(); it2 != use.end(); ++it2, ++j) {
if (uf.isUnited(*it1, *it2)) {
res[i][j]++;
}
if (G[*it1].find(*it2) != G[*it1].end() && G[*it1].find(*it2)->second) {
res[i][j]++;
}
}
}
return res;
}
void unite(Graph &G, int u, int v) {
G[u][v]++;
G[v][u]++;
}
void remove(Graph &G, int u, int v) {
// --G[u][v];
// --G[v][u];
if (--G[u][v] == 0)
G[u].erase(v);
if (--G[v][u] == 0)
G[v].erase(u);
}
bool judge(Graph &G, int v, int t, vector<bool> &reached) {
if (v == t)
return true;
reached[v] = true;
for (map<int, int>::iterator it = G[v].begin(); it != G[v].end(); ++it) {
int to = it->first;
if (!reached[to]) {
if (judge(G, to, t, reached))
return true;
}
}
return false;
}
bool judge(Graph &G, int s, int t) {
vector<bool> reached(G.size(), false);
return judge(G, s, t, reached);
}
/*
bool judge(Graph& G, int s, int t) {
assert(G.size() <= bucket*2);
bool reached[bucket*2] = {0};
queue<int> Q; Q.push(s); reached[s] = true;
while ( !Q.empty() ) {
int v = Q.front(); Q.pop();
for (map<int, int>::iterator it = G[v].begin(); it !=
G[v].end(); ++it) { int to = it->first; if ( !reached[to] ) { reached[to] =
true; Q.push(to);
}
}
}
return reached[t];
}
*/
int main() {
int N, M;
cin >> N >> M;
Query Q[40000];
for (int i = 0; i < M; ++i) {
int t, u, v;
scanf("%d%d%d", &t, &u, &v);
Q[i] = (Query){t, u, v};
}
set<int> use[40000 / bucket + 1];
for (int i = 0; i < M; ++i) {
int type = Q[i].type, u = Q[i].u, v = Q[i].v;
use[i / bucket].insert(u);
use[i / bucket].insert(v);
}
Graph G(N);
Graph g;
map<int, int> tbl;
for (int i = 0; i < M; ++i) {
int type = Q[i].type, u = Q[i].u, v = Q[i].v;
if (i % bucket == 0) {
tbl = make_table(use[i / bucket]);
g = trim(G, use[i / bucket]);
}
switch (type) {
case 1: // unite
unite(G, u, v);
unite(g, tbl[u], tbl[v]);
break;
case 2: // remove
remove(G, u, v);
remove(g, tbl[u], tbl[v]);
break;
case 3: // judge
if (judge(g, tbl[u], tbl[v])) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
break;
}
}
} | #include <bits/stdc++.h>
#define set unordered_set
#define map unordered_map
using namespace std;
class UnionFind {
private:
int size;
int parent[40000];
int rank[40000];
public:
UnionFind() {}
UnionFind(int sz) { init(sz); }
void init(int sz) {
size = sz;
fill(rank, rank + sz, 0);
for (int i = 0; i < sz; ++i) {
parent[i] = i;
}
}
int root(int n) {
if (parent[n] == n)
return n;
return parent[n] = root(parent[n]);
}
void unite(int n, int m) {
int x = root(n);
int y = root(m);
if (x == y)
return;
if (rank[x] < rank[y]) {
parent[x] = y;
} else if (rank[x] > rank[y]) {
parent[y] = x;
} else {
parent[x] = y;
++rank[y];
}
}
bool isUnited(int n, int m) { return root(n) == root(m); }
};
struct Query {
int type, u, v;
};
typedef vector<map<int, int>> Graph;
const int bucket = 50;
map<int, int> make_table(set<int> &use) {
map<int, int> res;
int n = 0;
for (set<int>::iterator it = use.begin(); it != use.end(); ++it, ++n) {
res[*it] = n;
}
return res;
}
Graph trim(Graph &G, set<int> &use) {
int i = 0;
Graph res(use.size());
UnionFind uf(G.size());
for (int i = 0; i < G.size(); ++i) {
for (map<int, int>::iterator it = G[i].begin(); it != G[i].end(); ++it) {
if (it->second) {
int v = it->first;
if (!use.count(i) || !use.count(v)) {
uf.unite(i, v);
}
}
}
}
for (set<int>::iterator it1 = use.begin(); it1 != use.end(); ++it1, ++i) {
int j = 0;
for (set<int>::iterator it2 = use.begin(); it2 != use.end(); ++it2, ++j) {
if (uf.isUnited(*it1, *it2)) {
res[i][j]++;
}
if (G[*it1].find(*it2) != G[*it1].end() && G[*it1].find(*it2)->second) {
res[i][j]++;
}
}
}
return res;
}
void unite(Graph &G, int u, int v) {
G[u][v]++;
G[v][u]++;
}
void remove(Graph &G, int u, int v) {
// --G[u][v];
// --G[v][u];
if (--G[u][v] == 0)
G[u].erase(v);
if (--G[v][u] == 0)
G[v].erase(u);
}
bool judge(Graph &G, int v, int t, vector<bool> &reached) {
if (v == t)
return true;
reached[v] = true;
for (map<int, int>::iterator it = G[v].begin(); it != G[v].end(); ++it) {
int to = it->first;
if (!reached[to]) {
if (judge(G, to, t, reached))
return true;
}
}
return false;
}
bool judge(Graph &G, int s, int t) {
vector<bool> reached(G.size(), false);
return judge(G, s, t, reached);
}
/*
bool judge(Graph& G, int s, int t) {
assert(G.size() <= bucket*2);
bool reached[bucket*2] = {0};
queue<int> Q; Q.push(s); reached[s] = true;
while ( !Q.empty() ) {
int v = Q.front(); Q.pop();
for (map<int, int>::iterator it = G[v].begin(); it !=
G[v].end(); ++it) { int to = it->first; if ( !reached[to] ) { reached[to] =
true; Q.push(to);
}
}
}
return reached[t];
}
*/
int main() {
int N, M;
cin >> N >> M;
Query Q[40000];
for (int i = 0; i < M; ++i) {
int t, u, v;
scanf("%d%d%d", &t, &u, &v);
Q[i] = (Query){t, u, v};
}
set<int> use[40000 / bucket + 1];
for (int i = 0; i < M; ++i) {
int type = Q[i].type, u = Q[i].u, v = Q[i].v;
use[i / bucket].insert(u);
use[i / bucket].insert(v);
}
Graph G(N);
Graph g;
map<int, int> tbl;
for (int i = 0; i < M; ++i) {
int type = Q[i].type, u = Q[i].u, v = Q[i].v;
if (i % bucket == 0) {
tbl = make_table(use[i / bucket]);
g = trim(G, use[i / bucket]);
}
switch (type) {
case 1: // unite
unite(G, u, v);
unite(g, tbl[u], tbl[v]);
break;
case 2: // remove
remove(G, u, v);
remove(g, tbl[u], tbl[v]);
break;
case 3: // judge
if (judge(g, tbl[u], tbl[v])) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
break;
}
}
} | replace | 52 | 53 | 52 | 53 | TLE | |
p01352 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define set unordered_set
#define map unordered_map
using namespace std;
class UnionFind {
private:
int size;
int parent[40000];
int rank[40000];
public:
UnionFind() {}
UnionFind(int sz) { init(sz); }
void init(int sz) {
size = sz;
fill(rank, rank + sz, 0);
for (int i = 0; i < sz; ++i) {
parent[i] = i;
}
}
int root(int n) {
if (parent[n] == n)
return n;
return parent[n] = root(parent[n]);
}
void unite(int n, int m) {
int x = root(n);
int y = root(m);
if (x == y)
return;
if (rank[x] < rank[y]) {
parent[x] = y;
} else if (rank[x] > rank[y]) {
parent[y] = x;
} else {
parent[x] = y;
++rank[y];
}
}
bool isUnited(int n, int m) { return root(n) == root(m); }
};
struct Query {
int type, u, v;
};
typedef vector<map<int, int>> Graph;
const int bucket = 28;
map<int, int> make_table(set<int> &use) {
map<int, int> res;
int n = 0;
for (set<int>::iterator it = use.begin(); it != use.end(); ++it, ++n) {
res[*it] = n;
}
return res;
}
Graph trim(Graph &G, set<int> &use) {
int i = 0;
Graph res(use.size());
UnionFind uf(G.size());
for (int i = 0; i < G.size(); ++i) {
for (map<int, int>::iterator it = G[i].begin(); it != G[i].end(); ++it) {
if (it->second) {
int v = it->first;
if (!use.count(i) || !use.count(v)) {
uf.unite(i, v);
}
}
}
}
for (set<int>::iterator it1 = use.begin(); it1 != use.end(); ++it1, ++i) {
int j = 0;
for (set<int>::iterator it2 = use.begin(); it2 != use.end(); ++it2, ++j) {
if (uf.isUnited(*it1, *it2)) {
res[i][j]++;
}
if (G[*it1].find(*it2) != G[*it1].end() && G[*it1].find(*it2)->second) {
res[i][j]++;
}
}
}
return res;
}
void unite(Graph &G, int u, int v) {
G[u][v]++;
G[v][u]++;
}
void remove(Graph &G, int u, int v) {
// --G[u][v];
// --G[v][u];
if (--G[u][v] == 0)
G[u].erase(v);
if (--G[v][u] == 0)
G[v].erase(u);
}
bool judge(Graph &G, int v, int t, vector<bool> &reached) {
if (v == t)
return true;
reached[v] = true;
for (map<int, int>::iterator it = G[v].begin(); it != G[v].end(); ++it) {
int to = it->first;
if (!reached[to]) {
if (judge(G, to, t, reached))
return true;
}
}
return false;
}
bool judge(Graph &G, int s, int t) {
vector<bool> reached(G.size(), false);
return judge(G, s, t, reached);
}
/*
bool judge(Graph& G, int s, int t) {
assert(G.size() <= bucket*2);
bool reached[bucket*2] = {0};
queue<int> Q; Q.push(s); reached[s] = true;
while ( !Q.empty() ) {
int v = Q.front(); Q.pop();
for (map<int, int>::iterator it = G[v].begin(); it !=
G[v].end(); ++it) { int to = it->first; if ( !reached[to] ) { reached[to] =
true; Q.push(to);
}
}
}
return reached[t];
}
*/
int main() {
int N, M;
cin >> N >> M;
Query Q[40000];
for (int i = 0; i < M; ++i) {
int t, u, v;
scanf("%d%d%d", &t, &u, &v);
Q[i] = (Query){t, u, v};
}
set<int> use[40000 / bucket + 1];
for (int i = 0; i < M; ++i) {
int type = Q[i].type, u = Q[i].u, v = Q[i].v;
use[i / bucket].insert(u);
use[i / bucket].insert(v);
}
Graph G(N);
Graph g;
map<int, int> tbl;
for (int i = 0; i < M; ++i) {
int type = Q[i].type, u = Q[i].u, v = Q[i].v;
if (i % bucket == 0) {
tbl = make_table(use[i / bucket]);
g = trim(G, use[i / bucket]);
}
switch (type) {
case 1: // unite
unite(G, u, v);
unite(g, tbl[u], tbl[v]);
break;
case 2: // remove
remove(G, u, v);
remove(g, tbl[u], tbl[v]);
break;
case 3: // judge
if (judge(g, tbl[u], tbl[v])) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
break;
}
}
} | #include <bits/stdc++.h>
#define set unordered_set
#define map unordered_map
using namespace std;
class UnionFind {
private:
int size;
int parent[40000];
int rank[40000];
public:
UnionFind() {}
UnionFind(int sz) { init(sz); }
void init(int sz) {
size = sz;
fill(rank, rank + sz, 0);
for (int i = 0; i < sz; ++i) {
parent[i] = i;
}
}
int root(int n) {
if (parent[n] == n)
return n;
return parent[n] = root(parent[n]);
}
void unite(int n, int m) {
int x = root(n);
int y = root(m);
if (x == y)
return;
if (rank[x] < rank[y]) {
parent[x] = y;
} else if (rank[x] > rank[y]) {
parent[y] = x;
} else {
parent[x] = y;
++rank[y];
}
}
bool isUnited(int n, int m) { return root(n) == root(m); }
};
struct Query {
int type, u, v;
};
typedef vector<map<int, int>> Graph;
const int bucket = 50;
map<int, int> make_table(set<int> &use) {
map<int, int> res;
int n = 0;
for (set<int>::iterator it = use.begin(); it != use.end(); ++it, ++n) {
res[*it] = n;
}
return res;
}
Graph trim(Graph &G, set<int> &use) {
int i = 0;
Graph res(use.size());
UnionFind uf(G.size());
for (int i = 0; i < G.size(); ++i) {
for (map<int, int>::iterator it = G[i].begin(); it != G[i].end(); ++it) {
if (it->second) {
int v = it->first;
if (!use.count(i) || !use.count(v)) {
uf.unite(i, v);
}
}
}
}
for (set<int>::iterator it1 = use.begin(); it1 != use.end(); ++it1, ++i) {
int j = 0;
for (set<int>::iterator it2 = use.begin(); it2 != use.end(); ++it2, ++j) {
if (uf.isUnited(*it1, *it2)) {
res[i][j]++;
}
if (G[*it1].find(*it2) != G[*it1].end() && G[*it1].find(*it2)->second) {
res[i][j]++;
}
}
}
return res;
}
void unite(Graph &G, int u, int v) {
G[u][v]++;
G[v][u]++;
}
void remove(Graph &G, int u, int v) {
// --G[u][v];
// --G[v][u];
if (--G[u][v] == 0)
G[u].erase(v);
if (--G[v][u] == 0)
G[v].erase(u);
}
bool judge(Graph &G, int v, int t, vector<bool> &reached) {
if (v == t)
return true;
reached[v] = true;
for (map<int, int>::iterator it = G[v].begin(); it != G[v].end(); ++it) {
int to = it->first;
if (!reached[to]) {
if (judge(G, to, t, reached))
return true;
}
}
return false;
}
bool judge(Graph &G, int s, int t) {
vector<bool> reached(G.size(), false);
return judge(G, s, t, reached);
}
/*
bool judge(Graph& G, int s, int t) {
assert(G.size() <= bucket*2);
bool reached[bucket*2] = {0};
queue<int> Q; Q.push(s); reached[s] = true;
while ( !Q.empty() ) {
int v = Q.front(); Q.pop();
for (map<int, int>::iterator it = G[v].begin(); it !=
G[v].end(); ++it) { int to = it->first; if ( !reached[to] ) { reached[to] =
true; Q.push(to);
}
}
}
return reached[t];
}
*/
int main() {
int N, M;
cin >> N >> M;
Query Q[40000];
for (int i = 0; i < M; ++i) {
int t, u, v;
scanf("%d%d%d", &t, &u, &v);
Q[i] = (Query){t, u, v};
}
set<int> use[40000 / bucket + 1];
for (int i = 0; i < M; ++i) {
int type = Q[i].type, u = Q[i].u, v = Q[i].v;
use[i / bucket].insert(u);
use[i / bucket].insert(v);
}
Graph G(N);
Graph g;
map<int, int> tbl;
for (int i = 0; i < M; ++i) {
int type = Q[i].type, u = Q[i].u, v = Q[i].v;
if (i % bucket == 0) {
tbl = make_table(use[i / bucket]);
g = trim(G, use[i / bucket]);
}
switch (type) {
case 1: // unite
unite(G, u, v);
unite(g, tbl[u], tbl[v]);
break;
case 2: // remove
remove(G, u, v);
remove(g, tbl[u], tbl[v]);
break;
case 3: // judge
if (judge(g, tbl[u], tbl[v])) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
break;
}
}
} | replace | 52 | 53 | 52 | 53 | TLE | |
p01352 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
// const int B = 200;
const int B = 3;
int N, Q;
int U[40000], R[40000];
int find(int x) {
if (U[x] == x)
return x;
return U[x] = find(U[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return;
if (R[x] < R[y])
swap(x, y);
U[x] = y;
}
int QT[40000], QU[40000], QV[40000];
bool used[40000];
set<int> G[40000];
void dfs(int x) {
used[x] = true;
for (int t : G[x])
if (!used[t])
dfs(t);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> Q;
rep(i, Q) { cin >> QT[i] >> QU[i] >> QV[i]; }
set<P> edges;
vector<int> vs;
vector<P> dyn;
rep(q, Q) {
if (q % B == 0) {
rep(i, N) U[i] = i, R[i] = 1, G[i].clear();
rep(i, N) used[i] = true;
vs.clear();
dyn.clear();
for (int i = q; i < min(q + B, Q); i++) {
vs.pb(QU[i]);
vs.pb(QV[i]);
dyn.pb(P(QU[i], QV[i]));
}
sort(all(vs));
uniq(vs);
sort(all(dyn));
uniq(dyn);
for (P p : edges)
if (!binary_search(all(dyn), p))
unite(p._1, p._2);
for (P p : edges)
if (binary_search(all(dyn), p)) {
int u = find(p._1), v = find(p._2);
G[u].insert(v);
G[v].insert(u);
}
}
int u = find(QU[q]), v = find(QV[q]);
if (QT[q] == 1) {
edges.insert(P(QU[q], QV[q]));
G[u].insert(v);
G[v].insert(u);
} else if (QT[q] == 2) {
edges.erase(P(QU[q], QV[q]));
G[u].erase(v);
G[v].erase(u);
} else {
for (int x : vs)
used[find(x)] = false;
dfs(u);
if (used[v])
cout << "YES\n";
else
cout << "NO\n";
}
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
const int B = 200;
int N, Q;
int U[40000], R[40000];
int find(int x) {
if (U[x] == x)
return x;
return U[x] = find(U[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return;
if (R[x] < R[y])
swap(x, y);
U[x] = y;
}
int QT[40000], QU[40000], QV[40000];
bool used[40000];
set<int> G[40000];
void dfs(int x) {
used[x] = true;
for (int t : G[x])
if (!used[t])
dfs(t);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> Q;
rep(i, Q) { cin >> QT[i] >> QU[i] >> QV[i]; }
set<P> edges;
vector<int> vs;
vector<P> dyn;
rep(q, Q) {
if (q % B == 0) {
rep(i, N) U[i] = i, R[i] = 1, G[i].clear();
rep(i, N) used[i] = true;
vs.clear();
dyn.clear();
for (int i = q; i < min(q + B, Q); i++) {
vs.pb(QU[i]);
vs.pb(QV[i]);
dyn.pb(P(QU[i], QV[i]));
}
sort(all(vs));
uniq(vs);
sort(all(dyn));
uniq(dyn);
for (P p : edges)
if (!binary_search(all(dyn), p))
unite(p._1, p._2);
for (P p : edges)
if (binary_search(all(dyn), p)) {
int u = find(p._1), v = find(p._2);
G[u].insert(v);
G[v].insert(u);
}
}
int u = find(QU[q]), v = find(QV[q]);
if (QT[q] == 1) {
edges.insert(P(QU[q], QV[q]));
G[u].insert(v);
G[v].insert(u);
} else if (QT[q] == 2) {
edges.erase(P(QU[q], QV[q]));
G[u].erase(v);
G[v].erase(u);
} else {
for (int x : vs)
used[find(x)] = false;
dfs(u);
if (used[v])
cout << "YES\n";
else
cout << "NO\n";
}
}
return 0;
}
| replace | 25 | 27 | 25 | 26 | TLE | |
p01353 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
o << "(" << p.fi << "," << p.se << ")";
return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << "[";
for (T t : v) {
o << t << ",";
}
o << "]";
return o;
}
#define INF 2147483600
long solve() {
int n;
cin >> n;
long ret = 0;
vector<long> h(n + 1), a(n + 1), d(n + 1), s(n + 1), t(n + 1);
rep(i, n + 1) cin >> h[i] >> a[i] >> d[i] >> s[i];
repl(i, 1, n + 1) {
long tmp = a[0] - d[i];
if (tmp < 0) {
if (a[i] > d[0])
return -1;
else
tmp = 1;
}
t[i] = (h[i] + tmp - 1) / tmp;
}
// ?????´?????? ????????¬??????????????????
repl(i, 1, n + 1) if (s[0] > s[i]) {
h[0] += max(a[i] - d[0], 0L);
ret -= max(a[i] - d[0], 0L);
}
vector<int> vec(n);
rep(i, n) vec[i] = i + 1;
sort(all(vec), [&](int i, int j) {
long ia = max(a[i] - d[0], 0L);
long ja = max(a[j] - d[0], 0L);
return ia * t[j] > ja * t[i];
});
long acctime = 0;
rep(i, n) {
int id = vec[i];
acctime += t[id];
h[0] -= max(a[id] - d[0], 0L) * acctime;
ret += max(a[id] - d[0], 0L) * acctime;
if (h[0] <= 0)
return -1;
}
return ret;
}
int main() {
cout << solve() << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
o << "(" << p.fi << "," << p.se << ")";
return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << "[";
for (T t : v) {
o << t << ",";
}
o << "]";
return o;
}
#define INF 2147483600
long solve() {
int n;
cin >> n;
long ret = 0;
vector<long> h(n + 1), a(n + 1), d(n + 1), s(n + 1), t(n + 1);
rep(i, n + 1) cin >> h[i] >> a[i] >> d[i] >> s[i];
repl(i, 1, n + 1) {
long tmp = a[0] - d[i];
if (tmp <= 0) {
if (a[i] > d[0])
return -1;
else
tmp = 1;
}
t[i] = (h[i] + tmp - 1) / tmp;
}
// ?????´?????? ????????¬??????????????????
repl(i, 1, n + 1) if (s[0] > s[i]) {
h[0] += max(a[i] - d[0], 0L);
ret -= max(a[i] - d[0], 0L);
}
vector<int> vec(n);
rep(i, n) vec[i] = i + 1;
sort(all(vec), [&](int i, int j) {
long ia = max(a[i] - d[0], 0L);
long ja = max(a[j] - d[0], 0L);
return ia * t[j] > ja * t[i];
});
long acctime = 0;
rep(i, n) {
int id = vec[i];
acctime += t[id];
h[0] -= max(a[id] - d[0], 0L) * acctime;
ret += max(a[id] - d[0], 0L) * acctime;
if (h[0] <= 0)
return -1;
}
return ret;
}
int main() {
cout << solve() << endl;
return 0;
} | replace | 36 | 37 | 36 | 37 | 0 | |
p01353 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)
int n;
i64 h[40404];
i64 a[40404];
i64 d[40404];
i64 s[40404];
i64 A[40404];
i64 B[40404];
i64 ceil(i64 A, i64 B) { return A / B + min(1LL, A % B); }
int main() {
cin >> n;
vector<int> vec;
rep(i, 0, n) {
cin >> h[i] >> a[i] >> d[i] >> s[i];
if (i == 0)
continue;
// cant
if (a[0] <= d[i] && a[i] > d[0]) {
cout << -1 << endl;
return 0;
}
if (a[0] > d[i]) {
vec.push_back(i);
A[i] = (ceil(h[i], a[0] - d[i]));
B[i] = (max(0LL, a[i] - d[0]));
}
}
auto ope = [&](int i, int j) { return A[i] * B[j] < A[j] * B[i]; };
sort(vec.begin(), vec.end(), ope);
i64 HP = 0;
i64 cou = 0;
rep(i, 0, vec.size() - 1) {
int index = vec[i];
cou += A[index];
HP += cou * B[index];
if (s[index] < s[0])
HP -= B[index];
if (h[0] <= HP) {
cout << -1 << endl;
return 0;
}
}
cout << HP << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)
int n;
i64 h[40404];
i64 a[40404];
i64 d[40404];
i64 s[40404];
i64 A[40404];
i64 B[40404];
i64 ceil(i64 A, i64 B) { return A / B + min(1LL, A % B); }
int main() {
cin >> n;
vector<int> vec;
rep(i, 0, n) {
cin >> h[i] >> a[i] >> d[i] >> s[i];
if (i == 0)
continue;
// cant
if (a[0] <= d[i] && a[i] > d[0]) {
cout << -1 << endl;
return 0;
}
if (a[0] > d[i]) {
vec.push_back(i);
A[i] = (ceil(h[i], a[0] - d[i]));
B[i] = (max(0LL, a[i] - d[0]));
}
}
auto ope = [&](int i, int j) { return A[i] * B[j] < A[j] * B[i]; };
sort(vec.begin(), vec.end(), ope);
i64 HP = 0;
i64 cou = 0;
rep(i, 0, (int)vec.size() - 1) {
int index = vec[i];
cou += A[index];
HP += cou * B[index];
if (s[index] < s[0])
HP -= B[index];
if (h[0] <= HP) {
cout << -1 << endl;
return 0;
}
}
cout << HP << endl;
return 0;
}
| replace | 51 | 52 | 51 | 52 | 0 | |
p01353 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;
struct info {
int h, a;
double rate;
info(int h, int a, double r) : h(h), a(a), rate(r) {}
info() {}
};
bool comp(const info &a, const info &b) { return a.rate > b.rate; }
int main() {
int n;
cin >> n;
lli h, a, d, s;
cin >> h >> a >> d >> s;
vector<info> e(n);
lli sum = 0, damage = 0;
for (int i = 0; i < n; i++) {
int eh, ea, ed, es;
cin >> eh >> ea >> ed >> es;
ea -= d;
if (ea <= 0) {
e[i] = info(1, 0, 0);
continue;
}
if (es > s) {
damage += ea;
}
if (a - ed <= 0) {
h = 0;
continue;
}
eh = (eh + (a - ed) - 1) / (a - ed);
e[i] = info(eh, ea, (double)ea / eh);
sum += ea;
}
if (h == 0) {
cout << -1 << endl;
return 0;
}
sort(e.begin(), e.end(), comp);
for (int i = 0; i < (int)e.size(); i++) {
int limit = (h - damage) / sum;
if (e[i].h > limit) {
h = 0;
break;
}
damage += sum * (e[i].h - 1);
sum -= e[i].a;
damage += sum;
}
if (h <= damage) {
cout << -1 << endl;
} else {
cout << damage << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;
struct info {
int h, a;
double rate;
info(int h, int a, double r) : h(h), a(a), rate(r) {}
info() {}
};
bool comp(const info &a, const info &b) { return a.rate > b.rate; }
int main() {
int n;
cin >> n;
lli h, a, d, s;
cin >> h >> a >> d >> s;
vector<info> e(n);
lli sum = 0, damage = 0;
for (int i = 0; i < n; i++) {
int eh, ea, ed, es;
cin >> eh >> ea >> ed >> es;
ea -= d;
if (ea <= 0) {
e[i] = info(1, 0, 0);
continue;
}
if (es > s) {
damage += ea;
}
if (a - ed <= 0) {
h = 0;
continue;
}
eh = (eh + (a - ed) - 1) / (a - ed);
e[i] = info(eh, ea, (double)ea / eh);
sum += ea;
}
if (h == 0) {
cout << -1 << endl;
return 0;
}
sort(e.begin(), e.end(), comp);
for (int i = 0; sum > 0; i++) {
int limit = (h - damage) / sum;
if (e[i].h > limit) {
h = 0;
break;
}
damage += sum * (e[i].h - 1);
sum -= e[i].a;
damage += sum;
}
if (h <= damage) {
cout << -1 << endl;
} else {
cout << damage << endl;
}
return 0;
} | replace | 47 | 48 | 47 | 48 | 0 | |
p01354 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef double real;
real dp[2][17][1 << 16];
int n, m;
real p[17][17];
int main() {
cin >> m >> n;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> p[i][j];
for (int j = 0; j < 17; j++)
for (int k = 0; k < (1 << 16); k++)
dp[n % 2][j][k] = 1;
for (int i = n - 1; i >= 0; i--) {
int id = i % 2;
for (int k = (1 << m); k >= 0; k--) {
for (int j = 0; j < m; j++) {
dp[id][j][k] = 0;
if ((1 << j) & k)
continue;
dp[id][j][k] =
dp[!id][j][k] * p[j][i] + dp[id][16][k | (1 << j)] * (1 - p[j][i]);
}
dp[id][16][k] = 0;
for (int j = 0; j < m; j++) {
dp[id][16][k] = max(dp[id][16][k], dp[id][j][k]);
}
}
}
printf("%.10lf\n", dp[0][16][0]);
return 0;
}
| #include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef double real;
real dp[2][17][1 << 16];
int n, m;
real p[17][17];
int main() {
cin >> m >> n;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> p[i][j];
for (int j = 0; j < 17; j++)
for (int k = 0; k < (1 << 16); k++)
dp[n % 2][j][k] = 1;
for (int i = n - 1; i >= 0; i--) {
int id = i % 2;
for (int k = (1 << m) - 1; k >= 0; k--) {
for (int j = 0; j < m; j++) {
dp[id][j][k] = 0;
if ((1 << j) & k)
continue;
dp[id][j][k] =
dp[!id][j][k] * p[j][i] + dp[id][16][k | (1 << j)] * (1 - p[j][i]);
}
dp[id][16][k] = 0;
for (int j = 0; j < m; j++) {
dp[id][16][k] = max(dp[id][16][k], dp[id][j][k]);
}
}
}
printf("%.10lf\n", dp[0][16][0]);
return 0;
}
| replace | 22 | 23 | 22 | 23 | 0 | |
p01354 | C++ | Runtime Error | #include <cstdio>
#include <iostream>
using namespace std;
int m, n;
double prob[16][16];
double dp[16][1 << 16];
double search(int pos, int cat) {
if (dp[pos][cat] > -0.5)
return dp[pos][cat];
double &res = dp[pos][cat];
res = 0.0;
for (int i = 0; i < m; i++) {
if (cat & (1 << i))
continue;
double pSum = 0.0;
double pCur = 1.0;
for (int j = pos; j < n; j++) {
pSum += (1 - prob[i][j]) * pCur * search(j, cat | (1 << i));
pCur *= prob[i][j];
}
pSum += pCur;
res = max(res, pSum);
}
return res;
}
int main() {
while (cin >> m >> n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
cin >> prob[i][j];
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j < (1 << m); j++)
dp[i][j] = -1.0;
}
printf("%.10lf\n", search(0, 0));
}
} | #include <cstdio>
#include <iostream>
using namespace std;
int m, n;
double prob[16][16];
double dp[16][1 << 16];
double search(int pos, int cat) {
if (dp[pos][cat] > -0.5)
return dp[pos][cat];
double &res = dp[pos][cat];
res = 0.0;
for (int i = 0; i < m; i++) {
if (cat & (1 << i))
continue;
double pSum = 0.0;
double pCur = 1.0;
for (int j = pos; j < n; j++) {
pSum += (1 - prob[i][j]) * pCur * search(j, cat | (1 << i));
pCur *= prob[i][j];
}
pSum += pCur;
res = max(res, pSum);
}
return res;
}
int main() {
while (cin >> m >> n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
cin >> prob[i][j];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < (1 << m); j++)
dp[i][j] = -1.0;
}
printf("%.10lf\n", search(0, 0));
}
} | replace | 35 | 36 | 35 | 36 | 0 | |
p01354 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
for (int W = 0; W < (n); W++) \
cerr << v[W] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, i, j) \
{ \
for (int aaa = 0; aaa < i; aaa++) { \
for (int bbb = 0; bbb < j; bbb++) \
cerr << v[aaa][bbb] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define MOD 1000000007
typedef long long ll;
typedef pair<ll, ll> P;
int n, m;
double v[22][22];
double dp[1 << 16][22];
double dfs(int state, int num) {
if (dp[state][num] != -1)
return dp[state][num];
double ret = 0;
REP(i, n) {
if (!(state & (1 << i))) {
double tmp = 0;
double p = 1;
for (int k = num; k < m; k++) {
tmp += p * (1 - v[i][k]) * dfs(state | (1 << i), k);
p *= v[i][k];
}
ret = max(ret, tmp + p);
}
}
return ret;
}
int main() {
cin >> n >> m;
REP(i, 1 << 16) REP(j, 22) dp[i][j] = -1;
REP(i, n) {
REP(j, m) { cin >> v[i][j]; }
}
cout << Decimal << dfs(0, 0) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
for (int W = 0; W < (n); W++) \
cerr << v[W] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, i, j) \
{ \
for (int aaa = 0; aaa < i; aaa++) { \
for (int bbb = 0; bbb < j; bbb++) \
cerr << v[aaa][bbb] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define MOD 1000000007
typedef long long ll;
typedef pair<ll, ll> P;
int n, m;
double v[22][22];
double dp[1 << 16][22];
double dfs(int state, int num) {
if (dp[state][num] != -1)
return dp[state][num];
double ret = 0;
REP(i, n) {
if (!(state & (1 << i))) {
double tmp = 0;
double p = 1;
for (int k = num; k < m; k++) {
tmp += p * (1 - v[i][k]) * dfs(state | (1 << i), k);
p *= v[i][k];
}
ret = max(ret, tmp + p);
}
}
return dp[state][num] = ret;
}
int main() {
cin >> n >> m;
REP(i, 1 << 16) REP(j, 22) dp[i][j] = -1;
REP(i, n) {
REP(j, m) { cin >> v[i][j]; }
}
cout << Decimal << dfs(0, 0) << endl;
return 0;
}
| replace | 61 | 62 | 61 | 62 | TLE | |
p01354 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
using namespace std;
long double x[20][20], m, n, neko[20][20][20], dp[20][100000];
long double solve(int pos, int ret) {
if (pos == n)
return 1.0l;
if (dp[pos][ret] >= -0.1l)
return dp[pos][ret];
long double maxn = 0.0l;
for (int i = 0; i < m; i++) {
if ((ret / (1 << i)) & 1 == 1)
continue;
long double cnt = 0.0;
for (int j = pos; j <= n; j++) {
long double w1 = solve(j, ret + (1 << i)) * neko[i][pos][j];
cnt += w1;
}
maxn = max(maxn, cnt);
}
dp[pos][ret] = maxn;
return maxn;
}
int main() {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 100000; j++)
dp[i][j] = -1.0l;
}
cin >> m >> n;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
cin >> x[i][j];
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
neko[i][j][j] = 1.0l;
long double cnt = 1.0l;
for (int k = j; k < n; k++) {
cnt *= x[i][k];
neko[i][j][k + 1] = cnt;
}
for (int k = j; k <= n; k++) {
neko[i][j][k] = neko[i][j][k] - neko[i][j][k + 1];
}
}
}
printf("%.12Lf\n", solve(0, 0));
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
long double x[20][20], m, n, neko[20][20][20], dp[20][100000];
inline long double solve(int pos, int ret) {
if (pos == n)
return 1.0l;
if (dp[pos][ret] >= -0.1l)
return dp[pos][ret];
long double maxn = 0.0l;
for (int i = 0; i < m; i++) {
if ((ret / (1 << i)) & 1 == 1)
continue;
long double cnt = 0.0;
for (int j = pos; j <= n; j++) {
long double w1 = solve(j, ret + (1 << i)) * neko[i][pos][j];
cnt += w1;
}
maxn = max(maxn, cnt);
}
dp[pos][ret] = maxn;
return maxn;
}
int main() {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 100000; j++)
dp[i][j] = -1.0l;
}
cin >> m >> n;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
cin >> x[i][j];
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
neko[i][j][j] = 1.0l;
long double cnt = 1.0l;
for (int k = j; k < n; k++) {
cnt *= x[i][k];
neko[i][j][k + 1] = cnt;
}
for (int k = j; k <= n; k++) {
neko[i][j][k] = neko[i][j][k] - neko[i][j][k + 1];
}
}
}
printf("%.12Lf\n", solve(0, 0));
return 0;
} | replace | 4 | 5 | 4 | 5 | TLE | |
p01354 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
int m, n;
vector<vector<ld>> rate;
ld dp[17][(1 << 16) + 1];
ld solve(int room, int rest) {
if (room == n)
return 1.0;
if (rest == (1 << m) - 1)
return 0;
if (dp[room][rest] >= 0)
return dp[room][rest];
ld res = 0;
REP(i, m) {
if ((rest >> i) & 1)
continue;
ld exp = 1.0;
ld tres = 0;
FOR(j, room, n) {
ld win = rate[i][j];
tres += exp * (1.0 - win) * solve(j, rest | (1 << i));
exp *= win;
}
tres += exp;
chmax(res, tres);
}
return dp[room][rest] = res;
}
int main() {
cin.sync_with_stdio(false);
REP(i, 17) REP(j, (1 << 16) + 1) dp[i][j] = -1;
cin >> m >> n;
rate.resize(m, vector<ld>(n));
REP(i, m) REP(j, n) cin >> rate[i][j];
cout << D10 << solve(0, 0) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
int m, n;
vector<vector<ld>> rate;
ld dp[17][(1 << 16) + 1];
ld solve(int room, int rest) {
if (room == n)
return 1.0;
if (rest == (1 << m) - 1)
return 0;
if (dp[room][rest] >= 0)
return dp[room][rest];
ld res = 0;
REP(i, m) {
if ((rest >> i) & 1)
continue;
ld exp = 1.0;
ld tres = 0;
FOR(j, room, n) {
ld win = rate[i][j];
tres += exp * (1.0 - win) * solve(j, rest | (1 << i));
exp *= win;
}
tres += exp;
chmax(res, tres);
}
return dp[room][rest] = res;
}
int main() {
cin.sync_with_stdio(false);
REP(i, 17) REP(j, (1 << 16) + 1) dp[i][j] = -1;
cin >> m >> n;
rate.resize(m, vector<ld>(n));
REP(i, m) REP(j, n) cin >> rate[i][j];
cout << D10 << solve(0, 0) << endl;
return 0;
} | replace | 8 | 9 | 8 | 9 | TLE | |
p01354 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
// ld dp[17][1 << 16] = {0}; // dp[next enemy][used cat]
int N, M;
ld P[16][16];
bool m1[16][1 << 16] = {0};
ld m2[16][1 << 16];
ld dp(int n, int rest) {
if (n >= N)
return (ld)1;
if (m1[n][rest])
return m2[n][rest];
ld ret = 0;
for (int i = 0; i < M; ++i) {
ld p = 0;
if (rest & (1 << i)) {
int nr = rest & ~(1 << i);
ld per = 1;
for (int j = n; j < N; ++j) {
p += per * (1 - P[i][j]) * dp(j, nr);
per *= P[i][j];
}
p += per;
}
ret = max(ret, p);
}
return m1[n][rest] = true, m2[n][rest] = ret;
}
int main() {
cin >> M >> N;
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
cin >> P[i][j];
}
}
printf("%.12f\n", (double)dp(0, (1 << M) - 1));
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef double ld;
// ld dp[17][1 << 16] = {0}; // dp[next enemy][used cat]
int N, M;
ld P[16][16];
bool m1[16][1 << 16] = {0};
ld m2[16][1 << 16];
ld dp(int n, int rest) {
if (n >= N)
return (ld)1;
if (m1[n][rest])
return m2[n][rest];
ld ret = 0;
for (int i = 0; i < M; ++i) {
ld p = 0;
if (rest & (1 << i)) {
int nr = rest & ~(1 << i);
ld per = 1;
for (int j = n; j < N; ++j) {
p += per * (1 - P[i][j]) * dp(j, nr);
per *= P[i][j];
}
p += per;
}
ret = max(ret, p);
}
return m1[n][rest] = true, m2[n][rest] = ret;
}
int main() {
cin >> M >> N;
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
cin >> P[i][j];
}
}
printf("%.12f\n", (double)dp(0, (1 << M) - 1));
return 0;
} | replace | 4 | 6 | 4 | 5 | TLE | |
p01354 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)
const int N = 4;
const int M = 16;
const int T = (1 << N);
const double INF = (1 << 30);
int n, m;
double cat[N][M];
double memo[2][T][N];
void input() {
cin >> n >> m;
rep(i, n) rep(j, m) cin >> cat[i][j];
}
bool has(int state, int i) { return (state & (1 << i)) > 0; }
string bitstr(int state) {
string ret = "";
rep(i, n) ret += has(state, i) ? "1" : "0";
return ret;
}
void setEdge(int state, int e[N], int *esum) {
(*esum) = 0;
rep(i, n) {
if (has(state, i)) {
e[(*esum)] = i;
(*esum)++;
}
}
}
double solve_dp() {
rep(i, 2) rep(j, T) rep(k, N) memo[i][j][k] = 0;
int t = (1 << n);
for (int i = m - 1; i >= 0; i--) {
rep(j, T) rep(k, N) memo[i % 2][j][k] = 0;
rep(j, t) {
int edge[N];
int esum = 0;
setEdge(j, edge, &esum);
rep(k, n) {
if (!has(j, k))
continue;
double maxi = 0;
double val1 = 0;
double winrate = cat[k][i];
if (i != m - 1)
val1 = winrate * memo[(i + 1) % 2][j][k];
else
val1 = winrate;
maxi = max(maxi, val1);
int dstate = j ^ (1 << k);
rep(p, esum) {
int e = edge[p];
if (!has(dstate, e))
continue;
maxi = max(maxi, val1 + (1 - winrate) * memo[i % 2][dstate][e]);
}
memo[i % 2][j][k] = maxi;
}
}
}
double ans = 0;
rep(i, n) ans = max(ans, memo[0][t - 1][i]);
return ans;
}
double solve() { return solve_dp(); }
int main() {
input();
printf("%.12f\n", solve());
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)
const int N = 16;
const int M = 16;
const int T = (1 << N);
const double INF = (1 << 30);
int n, m;
double cat[N][M];
double memo[2][T][N];
void input() {
cin >> n >> m;
rep(i, n) rep(j, m) cin >> cat[i][j];
}
bool has(int state, int i) { return (state & (1 << i)) > 0; }
string bitstr(int state) {
string ret = "";
rep(i, n) ret += has(state, i) ? "1" : "0";
return ret;
}
void setEdge(int state, int e[N], int *esum) {
(*esum) = 0;
rep(i, n) {
if (has(state, i)) {
e[(*esum)] = i;
(*esum)++;
}
}
}
double solve_dp() {
rep(i, 2) rep(j, T) rep(k, N) memo[i][j][k] = 0;
int t = (1 << n);
for (int i = m - 1; i >= 0; i--) {
rep(j, T) rep(k, N) memo[i % 2][j][k] = 0;
rep(j, t) {
int edge[N];
int esum = 0;
setEdge(j, edge, &esum);
rep(k, n) {
if (!has(j, k))
continue;
double maxi = 0;
double val1 = 0;
double winrate = cat[k][i];
if (i != m - 1)
val1 = winrate * memo[(i + 1) % 2][j][k];
else
val1 = winrate;
maxi = max(maxi, val1);
int dstate = j ^ (1 << k);
rep(p, esum) {
int e = edge[p];
if (!has(dstate, e))
continue;
maxi = max(maxi, val1 + (1 - winrate) * memo[i % 2][dstate][e]);
}
memo[i % 2][j][k] = maxi;
}
}
}
double ans = 0;
rep(i, n) ans = max(ans, memo[0][t - 1][i]);
return ans;
}
double solve() { return solve_dp(); }
int main() {
input();
printf("%.12f\n", solve());
} | replace | 11 | 12 | 11 | 12 | 0 | |
p01354 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
vector<vector<ld>> pers;
ld memo[16][1 << 16];
bool ok[16][1 << 16];
int M, N;
ld check(const int now, bitset<16> &lives) {
const int a = lives.to_ulong();
if (!memo[now][a] < -1) {
return memo[now][a];
} else {
ld amax = 0;
for (int i = 0; i < M; ++i) {
if (lives[i]) {
lives[i] = false;
ld per = 0;
ld living = 1;
for (int enemy = now; enemy < N; ++enemy) {
per += living * (1 - pers[i][enemy]) * check(enemy, lives);
living *= pers[i][enemy];
}
per += living;
amax = max(amax, per);
lives[i] = true;
}
}
return memo[now][a] = amax;
}
}
int main() {
cin >> M >> N;
pers.resize(M);
for (int i = 0; i < 16; ++i) {
for (int j = 0; j < 1 << 16; ++j) {
memo[i][j] = -2;
}
}
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
ld p;
cin >> p;
pers[i].push_back(p);
}
}
bitset<16> bs((1 << 16) - 1);
ld ans = check(0, bs);
cout << fixed << setprecision(22) << ans << endl;
return 0;
} | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
vector<vector<ld>> pers;
ld memo[16][1 << 16];
bool ok[16][1 << 16];
int M, N;
ld check(const int now, bitset<16> &lives) {
const int a = lives.to_ulong();
if (!(memo[now][a] < -1)) {
return memo[now][a];
} else {
ld amax = 0;
for (int i = 0; i < M; ++i) {
if (lives[i]) {
lives[i] = false;
ld per = 0;
ld living = 1;
for (int enemy = now; enemy < N; ++enemy) {
per += living * (1 - pers[i][enemy]) * check(enemy, lives);
living *= pers[i][enemy];
}
per += living;
amax = max(amax, per);
lives[i] = true;
}
}
return memo[now][a] = amax;
}
}
int main() {
cin >> M >> N;
pers.resize(M);
for (int i = 0; i < 16; ++i) {
for (int j = 0; j < 1 << 16; ++j) {
memo[i][j] = -2;
}
}
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
ld p;
cin >> p;
pers[i].push_back(p);
}
}
bitset<16> bs((1 << 16) - 1);
ld ans = check(0, bs);
cout << fixed << setprecision(22) << ans << endl;
return 0;
} | replace | 14 | 15 | 14 | 15 | TLE | |
p01356 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
#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
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
template <typename A, typename B> inline void chmin(A &a, B b) {
if (a > b)
a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
if (a < b)
a = b;
}
int N, M, A, B, P, Q;
void dfs(int k, int n, int s, vint &v, vint &latte) {
if (k == n) {
latte.pb(s);
return;
}
dfs(k + 1, n, s, v, latte);
dfs(k + 1, n, s + v[k], v, latte);
}
signed main() {
cin >> N >> M >> A >> B >> P >> Q;
if (A == 1 && B == 1) {
P += Q;
int ans = M;
if (M / P > N)
chmin(ans, abs(M - P * N));
else {
chmin(ans, abs(M - M / P * P));
if (M / P + 1 <= N)
chmin(ans, abs(M - M / P * P - P));
}
cout << ans << endl;
return 0;
}
vector<int> v;
int x = P, y = Q;
for (int i = 0; i < N; i++) {
v.pb(x + y);
if (1.0 * x * A > 1e15 || 1.0 * y * B > 1e15)
break;
x *= A;
y *= B;
}
int n = v.size() / 2;
vint latte, malta;
dfs(0, n, 0, v, latte);
dfs(n, v.size(), 0, v, malta);
sort(all(latte));
sort(all(malta));
int ans = M;
int cur = latte.size() - 1;
for (int i = 0; i < malta.size(); i++) {
while (cur && latte[cur] + malta[i] > M)
cur--;
chmin(ans, abs(M - latte[cur] - malta[i]));
}
cur = 0;
for (int i = (int)malta.size() - 1; i >= 0; i--) {
while (cur + 1 != latte.size() && latte[cur] + malta[i] < M)
cur++;
chmin(ans, abs(M - latte[cur] - malta[i]));
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#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
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
template <typename A, typename B> inline void chmin(A &a, B b) {
if (a > b)
a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
if (a < b)
a = b;
}
int N, M, A, B, P, Q;
void dfs(int k, int n, int s, vint &v, vint &latte) {
if (k == n) {
latte.pb(s);
return;
}
dfs(k + 1, n, s, v, latte);
dfs(k + 1, n, s + v[k], v, latte);
}
signed main() {
cin >> N >> M >> A >> B >> P >> Q;
if (A == 1 && B == 1) {
P += Q;
int ans = M;
if (M / P > N)
chmin(ans, abs(M - P * N));
else {
chmin(ans, abs(M - M / P * P));
if (M / P + 1 <= N)
chmin(ans, abs(M - M / P * P - P));
}
cout << ans << endl;
return 0;
}
vector<int> v;
int x = P, y = Q;
for (int i = 0; i < N; i++) {
v.pb(x + y);
if (1.0 * x * A > 1e15 || 1.0 * y * B > 1e15 || x + y > M)
break;
x *= A;
y *= B;
}
int n = v.size() / 2;
vint latte, malta;
dfs(0, n, 0, v, latte);
dfs(n, v.size(), 0, v, malta);
sort(all(latte));
sort(all(malta));
int ans = M;
int cur = latte.size() - 1;
for (int i = 0; i < malta.size(); i++) {
while (cur && latte[cur] + malta[i] > M)
cur--;
chmin(ans, abs(M - latte[cur] - malta[i]));
}
cur = 0;
for (int i = (int)malta.size() - 1; i >= 0; i--) {
while (cur + 1 != latte.size() && latte[cur] + malta[i] < M)
cur++;
chmin(ans, abs(M - latte[cur] - malta[i]));
}
cout << ans << endl;
return 0;
} | replace | 54 | 55 | 54 | 55 | MLE | |
p01358 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define REV(v) rbegin(v), rend(v)
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int N = 510, M = 1e5 + 10;
P pos[2][M];
int x[M];
int n, u, v, m;
int board[N][N];
bool check(int turn, P *pos, int uv) {
MEMSET(board, 0);
for (int i = 0; i < turn; ++i) {
board[pos[x[i]].Y][pos[x[i]].X] = 1;
}
int cnt[2] = {};
FOR(i, 1, n + 1) {
if (board[i][i])
++cnt[0];
if (board[i][n + 1 - i])
++cnt[1];
}
rep(i, n + 1) rep(j, n + 1) {
board[i + 1][j] += board[i][j];
board[i][j + 1] += board[i][j];
board[i + 1][j + 1] -= board[i][j];
}
int res = (cnt[0] == n) + (cnt[1] == n);
FOR(i, 1, n + 1) {
if (board[i][n + 1] == n)
++res;
if (board[n + 1][i] == n)
++res;
}
if (n == 1)
res = !!res;
return res >= uv;
}
int main() {
cin >> n >> u >> v >> m;
rep(i, 2) rep(j, n) rep(k, n) {
int xx;
cin >> xx;
pos[i][xx] = MP(k + 1, j + 1);
}
rep(i, m) cin >> x[i];
bool end = false;
int lb = 0, ub = m + 1;
while (ub - lb > 1) {
int mid = (ub + lb) / 2;
bool usa = check(mid, pos[0], u);
bool neko = check(mid, pos[1], v);
if (usa != neko) {
if (usa)
cout << "USAGI" << endl;
else
cout << "NEKO" << endl;
end = true;
break;
}
if (usa)
ub = mid;
else
lb = mid;
}
if (!end)
cout << "DRAW" << endl;
return 0;
} | #include <algorithm>
#include <array>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define REV(v) rbegin(v), rend(v)
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int N = 510, M = 1e5 + 10;
P pos[2][M * 10];
int x[M];
int n, u, v, m;
int board[N][N];
bool check(int turn, P *pos, int uv) {
MEMSET(board, 0);
for (int i = 0; i < turn; ++i) {
board[pos[x[i]].Y][pos[x[i]].X] = 1;
}
int cnt[2] = {};
FOR(i, 1, n + 1) {
if (board[i][i])
++cnt[0];
if (board[i][n + 1 - i])
++cnt[1];
}
rep(i, n + 1) rep(j, n + 1) {
board[i + 1][j] += board[i][j];
board[i][j + 1] += board[i][j];
board[i + 1][j + 1] -= board[i][j];
}
int res = (cnt[0] == n) + (cnt[1] == n);
FOR(i, 1, n + 1) {
if (board[i][n + 1] == n)
++res;
if (board[n + 1][i] == n)
++res;
}
if (n == 1)
res = !!res;
return res >= uv;
}
int main() {
cin >> n >> u >> v >> m;
rep(i, 2) rep(j, n) rep(k, n) {
int xx;
cin >> xx;
pos[i][xx] = MP(k + 1, j + 1);
}
rep(i, m) cin >> x[i];
bool end = false;
int lb = 0, ub = m + 1;
while (ub - lb > 1) {
int mid = (ub + lb) / 2;
bool usa = check(mid, pos[0], u);
bool neko = check(mid, pos[1], v);
if (usa != neko) {
if (usa)
cout << "USAGI" << endl;
else
cout << "NEKO" << endl;
end = true;
break;
}
if (usa)
ub = mid;
else
lb = mid;
}
if (!end)
cout << "DRAW" << endl;
return 0;
} | replace | 40 | 41 | 40 | 41 | 0 | |
p01358 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, u, v, m, in, ru, cu, rn, cn, countu = 0, countn = 0;
bool uw = false, nw = false;
cin >> n >> u >> v >> m;
vector<int> rowu(n, 0), colu(n, 0), crsu(2, 0), rown(n, 0), coln(n, 0),
crsn(2, 0);
vector<pair<int, int>> dicu(100000, make_pair(-1, -1)),
dicn(100000, make_pair(-1, -1));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> in;
dicu[in - 1] = make_pair(i, j);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> in;
dicn[in - 1] = make_pair(i, j);
}
}
for (int i = 0; i < m; i++) {
cin >> in;
ru = dicu[in - 1].first;
cu = dicu[in - 1].second;
if (!(ru == -1 || cu == -1)) {
rowu[ru]++;
if (rowu[ru] == n)
countu++;
colu[cu]++;
if (colu[cu] == n && n != 1)
countu++;
if (ru == cu) {
crsu[0]++;
if (crsu[0] == n && n != 1)
countu++;
}
if (n - 1 == ru + cu) {
crsu[1]++;
if (crsu[1] == n && n != 1)
countu++;
}
}
rn = dicn[in - 1].first;
cn = dicn[in - 1].second;
if (!(rn == -1 || cn == -1)) {
rown[rn]++;
if (rown[rn] == n)
countn++;
coln[cn]++;
if (coln[cn] == n && n != 1)
countn++;
if (rn == cn) {
crsn[0]++;
if (crsn[0] == n && n != 1)
countn++;
}
if (n - 1 == rn + cn) {
crsn[1]++;
if (crsn[1] == n && n != 1)
countn++;
}
}
if (countu >= u)
uw = true;
if (countn >= v)
nw = true;
if (uw && nw) {
cout << "DRAW" << endl;
return 0;
}
if (uw) {
cout << "USAGI" << endl;
return 0;
}
if (nw) {
cout << "NEKO" << endl;
return 0;
}
}
cout << "DRAW" << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int n, u, v, m, in, ru, cu, rn, cn, countu = 0, countn = 0;
bool uw = false, nw = false;
cin >> n >> u >> v >> m;
vector<int> rowu(n, 0), colu(n, 0), crsu(2, 0), rown(n, 0), coln(n, 0),
crsn(2, 0);
vector<pair<int, int>> dicu(1000000, make_pair(-1, -1)),
dicn(1000000, make_pair(-1, -1));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> in;
dicu[in - 1] = make_pair(i, j);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> in;
dicn[in - 1] = make_pair(i, j);
}
}
for (int i = 0; i < m; i++) {
cin >> in;
ru = dicu[in - 1].first;
cu = dicu[in - 1].second;
if (!(ru == -1 || cu == -1)) {
rowu[ru]++;
if (rowu[ru] == n)
countu++;
colu[cu]++;
if (colu[cu] == n && n != 1)
countu++;
if (ru == cu) {
crsu[0]++;
if (crsu[0] == n && n != 1)
countu++;
}
if (n - 1 == ru + cu) {
crsu[1]++;
if (crsu[1] == n && n != 1)
countu++;
}
}
rn = dicn[in - 1].first;
cn = dicn[in - 1].second;
if (!(rn == -1 || cn == -1)) {
rown[rn]++;
if (rown[rn] == n)
countn++;
coln[cn]++;
if (coln[cn] == n && n != 1)
countn++;
if (rn == cn) {
crsn[0]++;
if (crsn[0] == n && n != 1)
countn++;
}
if (n - 1 == rn + cn) {
crsn[1]++;
if (crsn[1] == n && n != 1)
countn++;
}
}
if (countu >= u)
uw = true;
if (countn >= v)
nw = true;
if (uw && nw) {
cout << "DRAW" << endl;
return 0;
}
if (uw) {
cout << "USAGI" << endl;
return 0;
}
if (nw) {
cout << "NEKO" << endl;
return 0;
}
}
cout << "DRAW" << endl;
return 0;
}
| replace | 8 | 10 | 8 | 10 | 0 | |
p01358 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi M_PI
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
const int N = 100010;
int main() {
int n, u, v, m;
cin >> n >> u >> v >> m;
vector<P> pu(N, make_pair(-1, -1)), pn(N, make_pair(-1, -1));
REP(i, n) REP(j, n) {
int a;
cin >> a;
pu[a] = make_pair(i, j);
}
REP(i, n) REP(j, n) {
int a;
cin >> a;
pn[a] = make_pair(i, j);
}
VI xu(n), yu(n), ou(2), xn(n), yn(n), on(2);
while (m-- > 0 && u > 0 && v > 0) {
int a, i, j;
cin >> a;
i = pu[a].first;
j = pu[a].second;
if (i != -1) {
if (++xu[i] == n)
u--;
if (++yu[j] == n)
u--;
if (i == j)
if (++ou[0] == n)
u--;
if (i + j == n - 1)
if (++ou[1] == n)
u--;
}
i = pn[a].first;
j = pn[a].second;
if (i != -1) {
if (++xn[i] == n)
v--;
if (++yn[j] == n)
v--;
if (i == j)
if (++on[0] == n)
v--;
if (i + j == n - 1)
if (++on[1] == n)
v--;
}
}
if (n == 1) {
u += 3;
v += 3;
}
string ans;
if (u <= 0 && v > 0)
ans = "USAGI";
else if (v <= 0 && u > 0)
ans = "NEKO";
else
ans = "DRAW";
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi M_PI
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
const int N = 1000010;
int main() {
int n, u, v, m;
cin >> n >> u >> v >> m;
vector<P> pu(N, make_pair(-1, -1)), pn(N, make_pair(-1, -1));
REP(i, n) REP(j, n) {
int a;
cin >> a;
pu[a] = make_pair(i, j);
}
REP(i, n) REP(j, n) {
int a;
cin >> a;
pn[a] = make_pair(i, j);
}
VI xu(n), yu(n), ou(2), xn(n), yn(n), on(2);
while (m-- > 0 && u > 0 && v > 0) {
int a, i, j;
cin >> a;
i = pu[a].first;
j = pu[a].second;
if (i != -1) {
if (++xu[i] == n)
u--;
if (++yu[j] == n)
u--;
if (i == j)
if (++ou[0] == n)
u--;
if (i + j == n - 1)
if (++ou[1] == n)
u--;
}
i = pn[a].first;
j = pn[a].second;
if (i != -1) {
if (++xn[i] == n)
v--;
if (++yn[j] == n)
v--;
if (i == j)
if (++on[0] == n)
v--;
if (i + j == n - 1)
if (++on[1] == n)
v--;
}
}
if (n == 1) {
u += 3;
v += 3;
}
string ans;
if (u <= 0 && v > 0)
ans = "USAGI";
else if (v <= 0 && u > 0)
ans = "NEKO";
else
ans = "DRAW";
cout << ans << endl;
return 0;
} | replace | 26 | 27 | 26 | 27 | 0 | |
p01359 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int main() {
while (1) {
int n, q;
cin >> n >> q;
if (!n)
break;
vector<tuple<int, int, string>> v;
REP(i, n) {
string name;
int era, western;
cin >> name >> era >> western;
v.emplace_back(western, era, name);
}
sort(begin(v), end(v));
REP(i, q) {
int year;
cin >> year;
int ue, uw;
string un;
tie(uw, ue, un) =
*upper_bound(begin(v), end(v), make_tuple(year, 0, string()));
if (year <= uw - ue)
cout << "Unknown" << endl;
else
cout << un << ' ' << ue - (uw - year) << endl;
}
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int main() {
while (1) {
int n, q;
cin >> n >> q;
if (!n)
break;
vector<tuple<int, int, string>> v;
REP(i, n) {
string name;
int era, western;
cin >> name >> era >> western;
v.emplace_back(western, era, name);
}
sort(begin(v), end(v));
REP(i, q) {
int year;
cin >> year;
int ue, uw;
string un;
auto itr = upper_bound(begin(v), end(v), make_tuple(year, 0, string()));
if (itr == end(v)) {
cout << "Unknown" << endl;
continue;
}
tie(uw, ue, un) = *itr;
if (year <= uw - ue)
cout << "Unknown" << endl;
else
cout << un << ' ' << ue - (uw - year) << endl;
}
}
return 0;
} | replace | 29 | 31 | 29 | 35 | 0 | |
p01359 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
#define INF 1000000000000LL
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<long, long> pll;
typedef pair<pll, string> pls;
int N, Q, Y, W;
string Name;
pls E[1000];
int main(void) {
while (cin >> N >> Q, N || Q) {
REP(i, 0, N) {
cin >> Name >> Y >> W;
E[i] = pls(pll(W - Y + 1, Y), Name);
}
E[N] = pls(pll(INF, 0), "");
sort(E, E + N);
REP(i, 0, Q) {
int q;
cin >> q;
if (q < E[0].fi.fi) {
cout << "Unknown" << endl;
continue;
}
REP(j, 0, N) {
if (E[j].fi.fi <= q && q < E[j + 1].fi.fi) {
int ans = q - E[j].fi.fi + 1;
if (ans <= E[j].fi.se) {
cout << E[j].se << " " << ans << endl;
goto END;
}
}
}
cout << "Unknown" << endl;
END:;
}
}
return 0;
} | #include <algorithm>
#include <iostream>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
#define INF 1000000000000LL
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<long, long> pll;
typedef pair<pll, string> pls;
int N, Q, Y, W;
string Name;
pls E[1001];
int main(void) {
while (cin >> N >> Q, N || Q) {
REP(i, 0, N) {
cin >> Name >> Y >> W;
E[i] = pls(pll(W - Y + 1, Y), Name);
}
E[N] = pls(pll(INF, 0), "");
sort(E, E + N);
REP(i, 0, Q) {
int q;
cin >> q;
if (q < E[0].fi.fi) {
cout << "Unknown" << endl;
continue;
}
REP(j, 0, N) {
if (E[j].fi.fi <= q && q < E[j + 1].fi.fi) {
int ans = q - E[j].fi.fi + 1;
if (ans <= E[j].fi.se) {
cout << E[j].se << " " << ans << endl;
goto END;
}
}
}
cout << "Unknown" << endl;
END:;
}
}
return 0;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p01360 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define pi 3.141592653589793
void solve(string s) {
int stepnum[10001][2];
// stepnum[i][0] : (0から数えて)iばんめの数字の移動のときに左足を使った
// stepnum[i][1] : 右足を使った
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < 2; j++) {
stepnum[i][j] = 0;
}
}
int s1 = (s[0] - '0') % 3, s2;
for (int i = 1; i < s.length(); i++) {
s2 = (s[i] - '0') % 3;
if ((s1 == 0 && s2 == 1) || (s1 == 0 && s2 == 2) || (s1 == 2 && s2 == 1)) {
// 右左、右右、左左はok
stepnum[i][0] = min(stepnum[i - 1][1], stepnum[i - 1][0] + 1);
stepnum[i][1] = stepnum[i - 1][1] + 1;
} else if ((s1 == 1 && s2 == 0) || (s1 == 1 && s2 == 2) ||
(s1 == 2 && s2 == 0)) {
// 左右、左左、右右はok
stepnum[i][0] = stepnum[i - 1][0] + 1;
stepnum[i][1] = min(stepnum[i - 1][0], stepnum[i - 1][1] + 1);
} else {
// どの足でもok
stepnum[i][0] = min(stepnum[i - 1][0] + 1, stepnum[i - 1][1]);
stepnum[i][1] = min(stepnum[i - 1][1] + 1, stepnum[i - 1][0]);
}
s1 = s2;
}
cout << min(stepnum[s.length() - 1][0], stepnum[s.length() - 1][1]) << endl;
}
int main() {
string s;
while (1) {
cin >> s;
if (s == "#")
break;
solve(s);
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define pi 3.141592653589793
void solve(string s) {
int stepnum[100001][2];
// stepnum[i][0] : (0から数えて)iばんめの数字の移動のときに左足を使った
// stepnum[i][1] : 右足を使った
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < 2; j++) {
stepnum[i][j] = 0;
}
}
int s1 = (s[0] - '0') % 3, s2;
for (int i = 1; i < s.length(); i++) {
s2 = (s[i] - '0') % 3;
if ((s1 == 0 && s2 == 1) || (s1 == 0 && s2 == 2) || (s1 == 2 && s2 == 1)) {
// 右左、右右、左左はok
stepnum[i][0] = min(stepnum[i - 1][1], stepnum[i - 1][0] + 1);
stepnum[i][1] = stepnum[i - 1][1] + 1;
} else if ((s1 == 1 && s2 == 0) || (s1 == 1 && s2 == 2) ||
(s1 == 2 && s2 == 0)) {
// 左右、左左、右右はok
stepnum[i][0] = stepnum[i - 1][0] + 1;
stepnum[i][1] = min(stepnum[i - 1][0], stepnum[i - 1][1] + 1);
} else {
// どの足でもok
stepnum[i][0] = min(stepnum[i - 1][0] + 1, stepnum[i - 1][1]);
stepnum[i][1] = min(stepnum[i - 1][1] + 1, stepnum[i - 1][0]);
}
s1 = s2;
}
cout << min(stepnum[s.length() - 1][0], stepnum[s.length() - 1][1]) << endl;
}
int main() {
string s;
while (1) {
cin >> s;
if (s == "#")
break;
solve(s);
}
return 0;
} | replace | 22 | 23 | 22 | 23 | 0 | |
p01360 | C++ | Runtime Error | #include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
int dp[2][2][9];
bool valid(int left, int right) {
if (left == right)
return false;
left %= 3;
right %= 3;
return left <= right;
}
int main() {
string str;
while (true) {
cin >> str;
if (str == "#")
break;
rep(i, 2) rep(j, 9) {
if (i == 0) {
if (valid(str[0] - '1', j))
dp[0][i][j] = 0;
else
dp[0][i][j] = 1e9;
} else {
if (valid(j, str[0] - '1'))
dp[0][i][j] = 0;
else
dp[0][i][j] = 1e9;
}
}
rep(i, str.size() - 1) {
rep(j, 2) rep(k, 9) dp[1][i][j] = 1e9;
rep(j, 2) rep(k, 9) {
if (j == 0) {
if (valid(str[i + 1] - '1', k))
dp[1][j][k] = dp[0][j][k] + 1;
else
dp[1][j][k] = 1e9;
if (valid(str[i + 1] - '1', str[i] - '1')) {
rep(l, 9) dp[1][j][k] = min(dp[1][j][k], dp[0][j ^ 1][l]);
}
} else {
if (valid(k, str[i + 1] - '1'))
dp[1][j][k] = dp[0][j][k] + 1;
else
dp[1][j][k] = 1e9;
if (valid(str[i] - '1', str[i + 1] - '1')) {
rep(l, 9) dp[1][j][k] = min(dp[1][j][k], dp[0][j ^ 1][l]);
}
}
}
rep(j, 2) rep(k, 9) dp[0][j][k] = dp[1][j][k];
}
int res = 1e9;
rep(i, 2) rep(j, 9) res = min(dp[0][i][j], res);
cout << res << endl;
}
return 0;
} | #include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
int dp[2][2][9];
bool valid(int left, int right) {
if (left == right)
return false;
left %= 3;
right %= 3;
return left <= right;
}
int main() {
string str;
while (true) {
cin >> str;
if (str == "#")
break;
rep(i, 2) rep(j, 9) {
if (i == 0) {
if (valid(str[0] - '1', j))
dp[0][i][j] = 0;
else
dp[0][i][j] = 1e9;
} else {
if (valid(j, str[0] - '1'))
dp[0][i][j] = 0;
else
dp[0][i][j] = 1e9;
}
}
rep(i, str.size() - 1) {
rep(j, 2) rep(k, 9) dp[1][j][k] = 1e9;
rep(j, 2) rep(k, 9) {
if (j == 0) {
if (valid(str[i + 1] - '1', k))
dp[1][j][k] = dp[0][j][k] + 1;
else
dp[1][j][k] = 1e9;
if (valid(str[i + 1] - '1', str[i] - '1')) {
rep(l, 9) dp[1][j][k] = min(dp[1][j][k], dp[0][j ^ 1][l]);
}
} else {
if (valid(k, str[i + 1] - '1'))
dp[1][j][k] = dp[0][j][k] + 1;
else
dp[1][j][k] = 1e9;
if (valid(str[i] - '1', str[i + 1] - '1')) {
rep(l, 9) dp[1][j][k] = min(dp[1][j][k], dp[0][j ^ 1][l]);
}
}
}
rep(j, 2) rep(k, 9) dp[0][j][k] = dp[1][j][k];
}
int res = 1e9;
rep(i, 2) rep(j, 9) res = min(dp[0][i][j], res);
cout << res << endl;
}
return 0;
} | replace | 36 | 37 | 36 | 37 | 0 | |
p01361 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <agents.h>
#endif
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int potion[13];
int dmg[256];
int dp[1010][1 << 12];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int dx[256] = {};
int dy[256] = {};
rep(i, 4) {
dx["UDRL"[i]] = vector<int>({-1, 1, 0, 0})[i];
dy["UDRL"[i]] = vector<int>({0, 0, 1, -1})[i];
}
int mhp, hp;
while (cin >> hp >> mhp, hp) {
int r, c;
cin >> r >> c;
vector<string> dangeon(r);
rep(i, r) cin >> dangeon[i];
int t;
cin >> t;
rep(i, t) {
char c;
int d;
cin >> c >> d;
dmg[c] = d;
}
int x, y;
x = y = 0;
string mv;
mv += dangeon[y][x];
int s;
cin >> s;
while (s--) {
char c;
int k;
cin >> c >> k;
while (k--) {
x += dx[c];
y += dy[c];
mv += dangeon[y][x];
}
}
int p;
cin >> p;
rep(i, p) {
int x;
cin >> x;
potion[i] = x;
}
MEMSET(dp, 0);
dp[0][0] = hp;
int sz = mv.size();
rep(i, sz) {
if (i)
rep(j, 1 << p) { dp[i][j] = dp[i - 1][j] - dmg[mv[i]]; }
rep(j, 1 << p) {
rep(k, p) {
int x = 1 << k;
if (~j & x)
continue;
if (dp[i][j & ~x] <= 0)
continue;
dp[i][j] = max(dp[i][j], min(mhp, dp[i][j & ~x] + potion[k]));
}
}
// rep(j, 1 << p){
// bitset<2> b(j);
// cout << b.to_string() << ' ' << dp[i][j] << endl;
// }
}
bool b = *max_element(dp[sz - 1], dp[sz - 1] + (1 << p)) >= 1;
cout << (b ? "YES" : "NO") << endl;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <agents.h>
#endif
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int potion[13];
int dmg[256];
int dp[1010][1 << 12];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int dx[256] = {};
int dy[256] = {};
rep(i, 4) {
dx["UDRL"[i]] = vector<int>({0, 0, 1, -1})[i];
dy["UDRL"[i]] = vector<int>({-1, 1, 0, 0})[i];
}
int mhp, hp;
while (cin >> hp >> mhp, hp) {
int r, c;
cin >> r >> c;
vector<string> dangeon(r);
rep(i, r) cin >> dangeon[i];
int t;
cin >> t;
rep(i, t) {
char c;
int d;
cin >> c >> d;
dmg[c] = d;
}
int x, y;
x = y = 0;
string mv;
mv += dangeon[y][x];
int s;
cin >> s;
while (s--) {
char c;
int k;
cin >> c >> k;
while (k--) {
x += dx[c];
y += dy[c];
mv += dangeon[y][x];
}
}
int p;
cin >> p;
rep(i, p) {
int x;
cin >> x;
potion[i] = x;
}
MEMSET(dp, 0);
dp[0][0] = hp;
int sz = mv.size();
rep(i, sz) {
if (i)
rep(j, 1 << p) { dp[i][j] = dp[i - 1][j] - dmg[mv[i]]; }
rep(j, 1 << p) {
rep(k, p) {
int x = 1 << k;
if (~j & x)
continue;
if (dp[i][j & ~x] <= 0)
continue;
dp[i][j] = max(dp[i][j], min(mhp, dp[i][j & ~x] + potion[k]));
}
}
// rep(j, 1 << p){
// bitset<2> b(j);
// cout << b.to_string() << ' ' << dp[i][j] << endl;
// }
}
bool b = *max_element(dp[sz - 1], dp[sz - 1] + (1 << p)) >= 1;
cout << (b ? "YES" : "NO") << endl;
}
return 0;
} | replace | 52 | 54 | 52 | 54 | 0 | |
p01361 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int vy[] = {-1, 1, 0, 0};
const int vx[] = {0, 0, -1, 1};
const string temp = "UDLR";
int main() {
vector<int> buff[13][1 << 12];
for (int k = 0; k < 13; k++) {
for (int i = 0; i < 1 << k; i++) {
for (int j = 0; j < 1 << k; j++) {
if (i & j)
continue;
buff[k][i].push_back(j);
}
}
}
int latte, malta, H, W, T, S, P;
string A[100];
int damage[256], B[1000], C[12];
while (cin >> latte >> malta, latte) {
cin >> H >> W;
for (int i = 0; i < H; i++)
cin >> A[i];
cin >> T;
for (int i = 0; i < T; i++) {
char c;
int d;
cin >> c >> d;
damage[c] = d;
}
cin >> S;
int curr = 0;
for (int i = 0; i < S; i++) {
char c;
int d;
cin >> c >> d;
int dir = (int)temp.find(c);
while (d--)
B[curr++] = dir;
}
cin >> P;
for (int i = 0; i < P; i++) {
cin >> C[i];
}
int yy = 0, xx = 0;
vector<int> add(1 << P, 0);
for (int i = 0; i < 1 << P; i++) {
for (int j = 0; j < P; j++) {
if ((i >> j) & 1)
add[i] += C[j];
}
}
vector<int> dp(1 << P, -1);
dp[0] = latte;
for (int i = 0; i < curr; i++) {
vector<int> nextdp(1 << P, -1);
yy += vy[B[i]], xx += vx[B[i]];
for (int j = 0; j < 1 << P; j++) {
if (dp[j] == -1)
continue;
if (dp[j] - damage[A[yy][xx]] >= 1) {
nextdp[j] = max(nextdp[j], dp[j] - damage[A[yy][xx]]);
}
for (int k : buff[P][j]) {
int get = min(malta, dp[j] + add[k]) - damage[A[yy][xx]];
if (get >= 1)
nextdp[j | k] = max(nextdp[j | k], get);
}
}
dp.swap(nextdp);
}
bool beet = false;
for (int k : dp)
if (k >= 1)
beet = true;
if (beet)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
const int vy[] = {-1, 1, 0, 0};
const int vx[] = {0, 0, -1, 1};
const string temp = "UDLR";
int main() {
vector<int> buff[13][1 << 12];
for (int k = 0; k < 13; k++) {
for (int i = 0; i < 1 << k; i++) {
for (int j = 0; j < 1 << k; j++) {
if (i & j)
continue;
buff[k][i].push_back(j);
}
}
}
int latte, malta, H, W, T, S, P;
string A[100];
int damage[256], B[1000], C[12];
while (cin >> latte >> malta, latte) {
cin >> H >> W;
for (int i = 0; i < H; i++)
cin >> A[i];
cin >> T;
for (int i = 0; i < T; i++) {
char c;
int d;
cin >> c >> d;
damage[c] = d;
}
cin >> S;
int curr = 0;
for (int i = 0; i < S; i++) {
char c;
int d;
cin >> c >> d;
int dir = (int)temp.find(c);
while (d--)
B[curr++] = dir;
}
cin >> P;
for (int i = 0; i < P; i++) {
cin >> C[i];
}
int yy = 0, xx = 0;
vector<int> add(1 << P, 0);
for (int i = 0; i < 1 << P; i++) {
for (int j = 0; j < P; j++) {
if ((i >> j) & 1)
add[i] += C[j];
}
}
vector<int> dp(1 << P, -1);
dp[0] = latte;
for (int i = 0; i < curr; i++) {
vector<int> nextdp(1 << P, -1);
yy += vy[B[i]], xx += vx[B[i]];
for (int j = 0; j < 1 << P; j++) {
if (dp[j] == -1)
continue;
if (dp[j] - damage[A[yy][xx]] >= 1) {
nextdp[j] = max(nextdp[j], dp[j] - damage[A[yy][xx]]);
continue;
}
for (int k : buff[P][j]) {
int get = min(malta, dp[j] + add[k]) - damage[A[yy][xx]];
if (get >= 1)
nextdp[j | k] = max(nextdp[j | k], get);
}
}
dp.swap(nextdp);
}
bool beet = false;
for (int k : dp)
if (k >= 1)
beet = true;
if (beet)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
} | insert | 70 | 70 | 70 | 71 | TLE | |
p01362 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;
template <class T> struct dice {
T t, b, n, s, e, w; // top bottom north south east west
int dir[6]; // directions of each face
dice() {}
dice(T t, T b, T n, T s, T e, T w) : t(t), b(b), n(n), s(s), e(e), w(w) {
REP(i, 6) dir[i] = 0;
}
void roll(T &a, T &b, T &c, T &d) {
swap(a, b);
swap(b, c);
swap(c, d);
}
void roll_x() {
roll(t, n, b, s);
int tmp[6];
tmp[0] = dir[4];
tmp[1] = (dir[1] + 1) % 4;
tmp[2] = (dir[5] + 2) % 4;
tmp[3] = (dir[3] + 3) % 4;
tmp[4] = (dir[2] + 2) % 4;
tmp[5] = dir[0];
REP(i, 6) dir[i] = tmp[i];
}
void roll_y() {
roll(t, w, b, e);
int tmp[6];
tmp[0] = (dir[0] + 3) % 4;
tmp[1] = (dir[4] + 3) % 4;
tmp[2] = (dir[2] + 1) % 4;
tmp[3] = (dir[5] + 3) % 4;
tmp[4] = (dir[3] + 3) % 4;
tmp[5] = (dir[1] + 3) % 4;
REP(i, 6) dir[i] = tmp[i];
}
void roll_z() { roll(s, e, n, w); }
vector<dice> all_rolls() {
vector<dice> ret;
for (int k = 0; k < 6; (k & 1 ? roll_y() : roll_x()), ++k)
for (int i = 0; i < 4; roll_z(), ++i)
ret.push_back(*this);
return ret;
}
void toTable(int *res) {
res[0] = t;
res[1] = b;
res[2] = n;
res[3] = s;
res[4] = e;
res[5] = w;
}
};
string face_str[6][3];
bool face[6][3][3];
dice<int> dicemp[6][6];
int dist[6][6];
bool func(int f, int d) {
if (d == 0) {
return face[f][2][0] || face[f][2][1] || face[f][2][2];
} else if (d == 1) {
return face[f][0][0] || face[f][1][0] || face[f][2][0];
} else if (d == 2) {
return face[f][0][0] || face[f][0][1] || face[f][0][2];
} else {
return face[f][0][2] || face[f][1][2] || face[f][2][2];
}
}
int main() {
dice<int> di(4, 5, 2, 0, 1, 3);
FOR(it, di.all_rolls()) { dicemp[it->t][it->s] = *it; }
while (cin >> face_str[0][0], face_str[0][0] != "#") {
REP(i, 2) cin >> face_str[0][i + 1];
REP(i, 5) REP(j, 3) cin >> face_str[i + 1][j];
REP(i, 6) REP(j, 3) REP(k, 3) {
face[i][j][k] = (face_str[i][j][k] == '*');
}
queue<pii> Q;
Q.push(pii(di.t, di.s));
memset(dist, -1, sizeof(dist));
dist[di.t][di.s] = 0;
int ans = -1;
while (!Q.empty()) {
pii p = Q.front();
Q.pop();
dice<int> now = dicemp[p.first][p.second];
int d = dist[now.t][now.s];
if (func(now.s, now.dir[0]) && func(now.n, now.dir[2])) {
ans = d;
break;
}
dice<int> tmp = now;
REP(i, 2) {
tmp.roll_x();
if (dist[tmp.t][tmp.s] == -1) {
dist[tmp.t][tmp.s] = d + 1;
Q.push(pii(tmp.t, tmp.s));
}
tmp.roll_x();
}
tmp = now;
REP(i, 2) {
tmp.roll_y();
if (dist[tmp.t][tmp.s] == -1) {
dist[tmp.t][tmp.s] = d + 1;
Q.push(pii(tmp.t, tmp.s));
}
tmp.roll_y();
}
}
assert(ans != -1);
cout << ans << endl;
}
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;
template <class T> struct dice {
T t, b, n, s, e, w; // top bottom north south east west
int dir[6]; // directions of each face
dice() {}
dice(T t, T b, T n, T s, T e, T w) : t(t), b(b), n(n), s(s), e(e), w(w) {
REP(i, 6) dir[i] = 0;
}
void roll(T &a, T &b, T &c, T &d) {
swap(a, b);
swap(b, c);
swap(c, d);
}
void roll_x() {
roll(t, n, b, s);
int tmp[6];
tmp[0] = dir[4];
tmp[1] = (dir[1] + 1) % 4;
tmp[2] = (dir[5] + 2) % 4;
tmp[3] = (dir[3] + 3) % 4;
tmp[4] = (dir[2] + 2) % 4;
tmp[5] = dir[0];
REP(i, 6) dir[i] = tmp[i];
}
void roll_y() {
roll(t, w, b, e);
int tmp[6];
tmp[0] = (dir[0] + 3) % 4;
tmp[1] = (dir[4] + 3) % 4;
tmp[2] = (dir[2] + 1) % 4;
tmp[3] = (dir[5] + 3) % 4;
tmp[4] = (dir[3] + 3) % 4;
tmp[5] = (dir[1] + 3) % 4;
REP(i, 6) dir[i] = tmp[i];
}
void roll_z() {
roll(s, e, n, w);
roll(dir[0], dir[1], dir[2], dir[3]);
dir[4] = (dir[4] + 3) % 4;
dir[5] = (dir[5] + 1) % 4;
}
vector<dice> all_rolls() {
vector<dice> ret;
for (int k = 0; k < 6; (k & 1 ? roll_y() : roll_x()), ++k)
for (int i = 0; i < 4; roll_z(), ++i)
ret.push_back(*this);
return ret;
}
void toTable(int *res) {
res[0] = t;
res[1] = b;
res[2] = n;
res[3] = s;
res[4] = e;
res[5] = w;
}
};
string face_str[6][3];
bool face[6][3][3];
dice<int> dicemp[6][6];
int dist[6][6];
bool func(int f, int d) {
if (d == 0) {
return face[f][2][0] || face[f][2][1] || face[f][2][2];
} else if (d == 1) {
return face[f][0][0] || face[f][1][0] || face[f][2][0];
} else if (d == 2) {
return face[f][0][0] || face[f][0][1] || face[f][0][2];
} else {
return face[f][0][2] || face[f][1][2] || face[f][2][2];
}
}
int main() {
dice<int> di(4, 5, 2, 0, 1, 3);
FOR(it, di.all_rolls()) { dicemp[it->t][it->s] = *it; }
while (cin >> face_str[0][0], face_str[0][0] != "#") {
REP(i, 2) cin >> face_str[0][i + 1];
REP(i, 5) REP(j, 3) cin >> face_str[i + 1][j];
REP(i, 6) REP(j, 3) REP(k, 3) {
face[i][j][k] = (face_str[i][j][k] == '*');
}
queue<pii> Q;
Q.push(pii(di.t, di.s));
memset(dist, -1, sizeof(dist));
dist[di.t][di.s] = 0;
int ans = -1;
while (!Q.empty()) {
pii p = Q.front();
Q.pop();
dice<int> now = dicemp[p.first][p.second];
int d = dist[now.t][now.s];
if (func(now.s, now.dir[0]) && func(now.n, now.dir[2])) {
ans = d;
break;
}
dice<int> tmp = now;
REP(i, 2) {
tmp.roll_x();
if (dist[tmp.t][tmp.s] == -1) {
dist[tmp.t][tmp.s] = d + 1;
Q.push(pii(tmp.t, tmp.s));
}
tmp.roll_x();
}
tmp = now;
REP(i, 2) {
tmp.roll_y();
if (dist[tmp.t][tmp.s] == -1) {
dist[tmp.t][tmp.s] = d + 1;
Q.push(pii(tmp.t, tmp.s));
}
tmp.roll_y();
}
}
assert(ans != -1);
cout << ans << endl;
}
} | replace | 65 | 66 | 65 | 71 | 0 | |
p01366 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
constexpr int maxN = 10005;
constexpr int maxM = 40005;
int N, M;
int adj[maxN], gto[maxM], gnxt[maxM], length[maxM], cost[maxM], e;
void add_edge(int u, int v, int d, int c) {
gto[e] = v;
length[e] = d;
cost[e] = c;
gnxt[e] = adj[u];
adj[u] = e++;
}
void print_edge(int s) {
printf("%d:", s);
for (int e = adj[s]; ~e; e = gnxt[e])
printf(" %d", gto[e]);
putchar('\n');
}
int dis[maxN], pre[maxN];
int qu[maxN], qh, qt;
bool inq[maxN];
void bellman_ford(int s) {
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
for (inq[qu[qh = qt = 0] = s] = 1; qh <= qt; inq[qu[qh++ % N]] = 0)
for (int u = qu[qh % N], e = adj[u], v = gto[e]; ~e; v = gto[e = gnxt[e]])
if (dis[v] > dis[u] + length[e]) {
dis[v] = dis[u] + length[e];
pre[v] = cost[e];
if (!inq[v])
inq[qu[++qt % N] = v] = 1;
} else if (dis[v] == dis[u] + length[e]) {
pre[v] = min(pre[v], cost[e]);
}
}
int main() {
while (~scanf("%d%d", &N, &M) && (N || M)) {
memset(adj, -1, sizeof adj);
for (int i = 0; i != M; ++i) {
int u, v, d, c;
scanf("%d%d%d%d", &u, &v, &d, &c);
add_edge(u, v, d, c);
add_edge(v, u, d, c);
}
bellman_ford(1);
long long ans = 0;
for (int i = 1; i <= N; ++i)
ans += pre[i];
printf("%lld\n", ans);
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
constexpr int maxN = 10005;
constexpr int maxM = 400005;
int N, M;
int adj[maxN], gto[maxM], gnxt[maxM], length[maxM], cost[maxM], e;
void add_edge(int u, int v, int d, int c) {
gto[e] = v;
length[e] = d;
cost[e] = c;
gnxt[e] = adj[u];
adj[u] = e++;
}
void print_edge(int s) {
printf("%d:", s);
for (int e = adj[s]; ~e; e = gnxt[e])
printf(" %d", gto[e]);
putchar('\n');
}
int dis[maxN], pre[maxN];
int qu[maxN], qh, qt;
bool inq[maxN];
void bellman_ford(int s) {
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
for (inq[qu[qh = qt = 0] = s] = 1; qh <= qt; inq[qu[qh++ % N]] = 0)
for (int u = qu[qh % N], e = adj[u], v = gto[e]; ~e; v = gto[e = gnxt[e]])
if (dis[v] > dis[u] + length[e]) {
dis[v] = dis[u] + length[e];
pre[v] = cost[e];
if (!inq[v])
inq[qu[++qt % N] = v] = 1;
} else if (dis[v] == dis[u] + length[e]) {
pre[v] = min(pre[v], cost[e]);
}
}
int main() {
while (~scanf("%d%d", &N, &M) && (N || M)) {
memset(adj, -1, sizeof adj);
for (int i = 0; i != M; ++i) {
int u, v, d, c;
scanf("%d%d%d%d", &u, &v, &d, &c);
add_edge(u, v, d, c);
add_edge(v, u, d, c);
}
bellman_ford(1);
long long ans = 0;
for (int i = 1; i <= N; ++i)
ans += pre[i];
printf("%lld\n", ans);
}
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p01366 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define MAXN 10005
#define MAXM 20005
struct Edge {
int id, len, cos;
int nxt;
Edge() {}
Edge(int i, int l, int c) {
id = i;
len = l;
cos = c;
}
} E[MAXM];
int V[MAXN], cur;
void add_edge(int u, int v, int length, int cost) {
E[cur] = Edge(v, length, cost);
E[cur].nxt = V[u];
V[u] = cur;
cur++;
E[cur] = Edge(u, length, cost);
E[cur].nxt = V[v];
V[v] = cur;
cur++;
}
int N, M;
int dis[MAXN], cost[MAXN];
bool inque[MAXN];
queue<int> Q;
int SPFA() {
memset(inque, 0, sizeof inque);
for (int i = 1; i <= N; i++)
dis[i] = cost[i] = 0x3FFFFFFF;
while (!Q.empty())
Q.pop();
Q.push(1);
dis[1] = 0;
inque[1] = 1;
cost[1] = 0;
while (!Q.empty()) {
int t = Q.front();
Q.pop();
inque[t] = 0;
for (int p = V[t]; p != -1; p = E[p].nxt) {
int to = E[p].id;
int len = E[p].len;
if (dis[to] > dis[t] + len ||
(dis[to] == dis[t] + len && cost[to] > E[p].cos)) {
dis[to] = dis[t] + len;
cost[to] = E[p].cos;
if (!inque[to]) {
Q.push(to);
inque[to] = 1;
}
}
}
}
int ret = 0;
for (int i = 1; i <= N; i++)
ret += cost[i];
return ret;
}
int main() {
int u, v, d, c;
while (~scanf("%d%d", &N, &M)) {
if (!N && !M)
break;
memset(V, -1, sizeof V);
cur = 0;
for (int i = 1; i <= M; i++) {
scanf("%d%d%d%d", &u, &v, &d, &c);
add_edge(u, v, d, c);
}
printf("%d\n", SPFA());
}
return 0;
} | #include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define MAXN 10005
#define MAXM 20005
struct Edge {
int id, len, cos;
int nxt;
Edge() {}
Edge(int i, int l, int c) {
id = i;
len = l;
cos = c;
}
} E[MAXM * 2];
int V[MAXN], cur;
void add_edge(int u, int v, int length, int cost) {
E[cur] = Edge(v, length, cost);
E[cur].nxt = V[u];
V[u] = cur;
cur++;
E[cur] = Edge(u, length, cost);
E[cur].nxt = V[v];
V[v] = cur;
cur++;
}
int N, M;
int dis[MAXN], cost[MAXN];
bool inque[MAXN];
queue<int> Q;
int SPFA() {
memset(inque, 0, sizeof inque);
for (int i = 1; i <= N; i++)
dis[i] = cost[i] = 0x3FFFFFFF;
while (!Q.empty())
Q.pop();
Q.push(1);
dis[1] = 0;
inque[1] = 1;
cost[1] = 0;
while (!Q.empty()) {
int t = Q.front();
Q.pop();
inque[t] = 0;
for (int p = V[t]; p != -1; p = E[p].nxt) {
int to = E[p].id;
int len = E[p].len;
if (dis[to] > dis[t] + len ||
(dis[to] == dis[t] + len && cost[to] > E[p].cos)) {
dis[to] = dis[t] + len;
cost[to] = E[p].cos;
if (!inque[to]) {
Q.push(to);
inque[to] = 1;
}
}
}
}
int ret = 0;
for (int i = 1; i <= N; i++)
ret += cost[i];
return ret;
}
int main() {
int u, v, d, c;
while (~scanf("%d%d", &N, &M)) {
if (!N && !M)
break;
memset(V, -1, sizeof V);
cur = 0;
for (int i = 1; i <= M; i++) {
scanf("%d%d%d%d", &u, &v, &d, &c);
add_edge(u, v, d, c);
}
printf("%d\n", SPFA());
}
return 0;
} | replace | 15 | 16 | 15 | 16 | 0 | |
p01366 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define MAXN 10005
#define MAXM 20005
struct Edge {
int id, len, cos;
int nxt;
Edge() {}
Edge(int i, int l, int c) {
id = i;
len = l;
cos = c;
}
} E[MAXM];
int V[MAXN], cur;
void add_edge(int u, int v, int length, int cost) {
E[cur] = Edge(v, length, cost);
E[cur].nxt = V[u];
V[u] = cur;
cur++;
E[cur] = Edge(u, length, cost);
E[cur].nxt = V[v];
V[v] = cur;
cur++;
}
int N, M;
int dis[MAXN], cost[MAXN];
bool inque[MAXN];
queue<int> Q;
int SPFA() {
memset(inque, 0, sizeof inque);
for (int i = 1; i <= N; i++)
dis[i] = cost[i] = 0x3FFFFFFF;
while (!Q.empty())
Q.pop();
Q.push(1);
dis[1] = 0;
inque[1] = 1;
cost[1] = 0;
while (!Q.empty()) {
int t = Q.front();
Q.pop();
inque[t] = 0;
for (int p = V[t]; p != -1; p = E[p].nxt) {
int to = E[p].id;
int len = E[p].len;
if (dis[to] > dis[t] + len ||
(dis[to] == dis[t] + len && cost[to] > E[p].cos)) {
dis[to] = dis[t] + len;
cost[to] = E[p].cos;
if (!inque[to]) {
Q.push(to);
inque[to] = 1;
}
}
}
}
int ret = 0;
for (int i = 1; i <= N; i++)
ret += cost[i];
return ret;
}
int main() {
int u, v, d, c;
while (~scanf("%d%d", &N, &M)) {
if (!N && !M)
break;
memset(V, -1, sizeof V);
cur = 0;
for (int i = 1; i <= M; i++) {
scanf("%d%d%d%d", &u, &v, &d, &c);
add_edge(u, v, d, c);
}
printf("%d\n", SPFA());
}
return 0;
} | #include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define MAXN 10005
#define MAXM 20005
struct Edge {
int id, len, cos;
int nxt;
Edge() {}
Edge(int i, int l, int c) {
id = i;
len = l;
cos = c;
}
} E[2 * MAXM];
int V[MAXN], cur;
void add_edge(int u, int v, int length, int cost) {
E[cur] = Edge(v, length, cost);
E[cur].nxt = V[u];
V[u] = cur;
cur++;
E[cur] = Edge(u, length, cost);
E[cur].nxt = V[v];
V[v] = cur;
cur++;
}
int N, M;
int dis[MAXN], cost[MAXN];
bool inque[MAXN];
queue<int> Q;
int SPFA() {
memset(inque, 0, sizeof inque);
for (int i = 1; i <= N; i++)
dis[i] = cost[i] = 0x3FFFFFFF;
while (!Q.empty())
Q.pop();
Q.push(1);
dis[1] = 0;
inque[1] = 1;
cost[1] = 0;
while (!Q.empty()) {
int t = Q.front();
Q.pop();
inque[t] = 0;
for (int p = V[t]; p != -1; p = E[p].nxt) {
int to = E[p].id;
int len = E[p].len;
if (dis[to] > dis[t] + len ||
(dis[to] == dis[t] + len && cost[to] > E[p].cos)) {
dis[to] = dis[t] + len;
cost[to] = E[p].cos;
if (!inque[to]) {
Q.push(to);
inque[to] = 1;
}
}
}
}
int ret = 0;
for (int i = 1; i <= N; i++)
ret += cost[i];
return ret;
}
int main() {
int u, v, d, c;
while (~scanf("%d%d", &N, &M)) {
if (!N && !M)
break;
memset(V, -1, sizeof V);
cur = 0;
for (int i = 1; i <= M; i++) {
scanf("%d%d%d%d", &u, &v, &d, &c);
add_edge(u, v, d, c);
}
printf("%d\n", SPFA());
}
return 0;
} | replace | 15 | 16 | 15 | 16 | 0 | |
p01366 | C++ | Runtime Error | #include <iostream>
#include <map>
#include <memory.h>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
const bool SUBMIT = 0;
struct Edge {
int v, d, c;
bool operator<(const Edge &b) const {
// return d < b.d || d == b.d && c < b.c ;
return (d == b.d) ? ((c == b.c) ? v < b.v : c < b.c) : d < b.d;
}
};
vector<Edge> g[10000];
set<Edge> mq; //这里tm的不能使用 SET,如果说只比较Edge d
//c,且d,c相等,就insert 不进去(小心啊小心)!!!
//用priority que 保险吶!!!
int dist[10000];
// int cost[10000];
int father[10000];
bool visited[10000];
int dijkstra(int s, int n) {
memset(father, -1, sizeof(int) * n);
memset(visited, 0, sizeof(bool) * n);
int i;
for (i = 0; i < n; ++i) {
dist[i] = 100000000;
// cost[i] = 100000000;
}
Edge edge;
edge.v = s;
edge.d = 0;
edge.c = 0;
mq.insert(edge);
father[s] = -1;
dist[s] = 0;
// cost[s] = 0;
int res = 0;
while (mq.size()) {
edge = *mq.begin();
int u = edge.v;
// printf("cur top: %d %d %d\n", u, edge.d, edge.c);
mq.erase(mq.begin());
if (visited[u])
continue;
visited[u] = 1;
res += edge.c;
for (i = 0; i < g[u].size(); ++i) {
Edge e = edge;
e.v = g[u][i].v;
e.d = g[u][i].d + edge.d;
e.c = g[u][i].c;
// printf(" insert e: %d %d %di\n", e.v, e.d, e.c);
mq.insert(e);
}
}
return res;
}
int main() {
int n, m;
int u, s;
if (!SUBMIT)
freopen("2249-input.txt", "r", stdin);
scanf("%d%d", &n, &m);
int i, j;
while (n != 0 || m != 0) {
for (i = 0; i < n; ++i) {
g[i].clear();
mq.clear();
}
Edge edge;
for (i = 0; i < m; ++i) {
scanf("%d%d%d%d", &u, &edge.v, &edge.d, &edge.c);
u--;
edge.v--;
if (i == 0)
s = u;
g[u].push_back(edge);
int temp = u;
u = edge.v;
edge.v = temp;
g[u].push_back(edge);
}
s = 0;
int cost = dijkstra(s, n);
/*int cost = 0;
for(i = 0; i < n; ++i){
if(father[i] >= 0){
for(j = 0; j < g[father[i]].size(); ++j){
int c = g[father[i]][j].c;
int v = g[father[i]][j].v;
if(v == i){
cost += c;
break;
}
}
}
}*/
printf("%d\n", cost);
scanf("%d%d", &n, &m);
}
return 0;
} | #include <iostream>
#include <map>
#include <memory.h>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
const bool SUBMIT = 1;
struct Edge {
int v, d, c;
bool operator<(const Edge &b) const {
// return d < b.d || d == b.d && c < b.c ;
return (d == b.d) ? ((c == b.c) ? v < b.v : c < b.c) : d < b.d;
}
};
vector<Edge> g[10000];
set<Edge> mq; //这里tm的不能使用 SET,如果说只比较Edge d
//c,且d,c相等,就insert 不进去(小心啊小心)!!!
//用priority que 保险吶!!!
int dist[10000];
// int cost[10000];
int father[10000];
bool visited[10000];
int dijkstra(int s, int n) {
memset(father, -1, sizeof(int) * n);
memset(visited, 0, sizeof(bool) * n);
int i;
for (i = 0; i < n; ++i) {
dist[i] = 100000000;
// cost[i] = 100000000;
}
Edge edge;
edge.v = s;
edge.d = 0;
edge.c = 0;
mq.insert(edge);
father[s] = -1;
dist[s] = 0;
// cost[s] = 0;
int res = 0;
while (mq.size()) {
edge = *mq.begin();
int u = edge.v;
// printf("cur top: %d %d %d\n", u, edge.d, edge.c);
mq.erase(mq.begin());
if (visited[u])
continue;
visited[u] = 1;
res += edge.c;
for (i = 0; i < g[u].size(); ++i) {
Edge e = edge;
e.v = g[u][i].v;
e.d = g[u][i].d + edge.d;
e.c = g[u][i].c;
// printf(" insert e: %d %d %di\n", e.v, e.d, e.c);
mq.insert(e);
}
}
return res;
}
int main() {
int n, m;
int u, s;
if (!SUBMIT)
freopen("2249-input.txt", "r", stdin);
scanf("%d%d", &n, &m);
int i, j;
while (n != 0 || m != 0) {
for (i = 0; i < n; ++i) {
g[i].clear();
mq.clear();
}
Edge edge;
for (i = 0; i < m; ++i) {
scanf("%d%d%d%d", &u, &edge.v, &edge.d, &edge.c);
u--;
edge.v--;
if (i == 0)
s = u;
g[u].push_back(edge);
int temp = u;
u = edge.v;
edge.v = temp;
g[u].push_back(edge);
}
s = 0;
int cost = dijkstra(s, n);
/*int cost = 0;
for(i = 0; i < n; ++i){
if(father[i] >= 0){
for(j = 0; j < g[father[i]].size(); ++j){
int c = g[father[i]][j].c;
int v = g[father[i]][j].v;
if(v == i){
cost += c;
break;
}
}
}
}*/
printf("%d\n", cost);
scanf("%d%d", &n, &m);
}
return 0;
} | replace | 7 | 8 | 7 | 8 | -11 | |
p01366 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;
struct st {
int a, b, c;
};
list<st> E[10000];
int d[10000], c[10000];
int main() {
int n, m;
while (scanf("%d%d", &n, &m), n) {
rep(i, n) E[i].clear();
rep(i, m) {
int u, v, d, c;
scanf("%d%d%d%d", &u, &v, &d, &c);
u--;
v--;
E[u].push_back({v, d, c});
E[v].push_back({u, d, c});
}
memset(d, 0x3f, sizeof(d));
priority_queue<P> que;
d[0] = 0;
que.push(P(0, 0));
while (!que.empty()) {
P p = que.top();
que.pop();
for (st &v : E[p.second]) {
if (d[v.a] > p.first + v.b) {
d[v.a] = p.first + v.b;
c[v.a] = v.c;
que.push(P(d[v.a], v.a));
} else if (d[v.a] == p.first + v.b && c[v.a] > v.c)
c[v.a] = v.c;
}
}
printf("%d\n", accumulate(c, c + n, 0));
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;
struct st {
int a, b, c;
};
vector<st> E[10000];
int d[10000], c[10000];
int main() {
int n, m;
while (scanf("%d%d", &n, &m), n) {
rep(i, n) E[i].clear();
rep(i, m) {
int u, v, d, c;
scanf("%d%d%d%d", &u, &v, &d, &c);
u--;
v--;
E[u].push_back({v, d, c});
E[v].push_back({u, d, c});
}
memset(d, 0x3f, sizeof(d));
priority_queue<P> que;
d[0] = 0;
que.push(P(0, 0));
while (!que.empty()) {
P p = que.top();
que.pop();
for (st &v : E[p.second]) {
if (d[v.a] > p.first + v.b) {
d[v.a] = p.first + v.b;
c[v.a] = v.c;
que.push(P(d[v.a], v.a));
} else if (d[v.a] == p.first + v.b && c[v.a] > v.c)
c[v.a] = v.c;
}
}
printf("%d\n", accumulate(c, c + n, 0));
}
} | replace | 8 | 9 | 8 | 9 | TLE | |
p01366 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 5, maxm = 4e4 + 5;
const ll inf = 1e18;
inline int fa(int u) { return u / 2; }
inline int ls(int u) { return u * 2; }
inline int rs(int u) { return u * 2 + 1; }
struct heap {
int hea[maxn], pos[maxn], siz;
ll *dis;
heap(ll a[], int n) {
dis = a;
siz = n;
int i;
for (i = 1; i <= n; i++) {
dis[i] = inf;
hea[i] = i;
pos[i] = i;
}
}
void hswap(int u, int v) {
swap(hea[u], hea[v]);
pos[hea[u]] = u;
pos[hea[v]] = v;
}
void update(int u) {
if (u == 1)
return;
if (dis[hea[fa(u)]] <= dis[hea[u]])
return;
hswap(u, fa(u));
update(fa(u));
}
void pushdown(int u) {
int v = u;
if (ls(u) <= siz && dis[hea[ls(u)]] < dis[hea[v]])
v = ls(u);
if (rs(u) <= siz && dis[hea[rs(u)]] < dis[hea[v]])
v = rs(u);
if (u == v)
return;
hswap(u, v);
pushdown(v);
}
void change(int u, ll val) {
dis[u] = val;
update(pos[u]);
pushdown(pos[u]);
}
int pop() {
hswap(1, siz);
siz--;
pushdown(1);
return hea[siz + 1];
}
};
struct gragh {
int head[maxn], next[maxm], to[maxm], tot;
ll wei[maxm], cost[maxn];
gragh() {
memset(head, -1, sizeof(head));
tot = 0;
}
void clear() {
memset(head, -1, sizeof(head));
tot = 0;
}
void add(int u, int v, ll w, ll c) {
next[tot] = head[u];
head[u] = tot;
to[tot] = v;
wei[tot] = w;
cost[tot] = c;
tot++;
}
void dijkstra(ll dis[], ll val[], int st, int n) {
heap hp(dis, n);
hp.change(st, 0);
int i, u, v, ed;
for (i = 1; i <= n; i++)
val[i] = inf;
val[st] = 0;
while (hp.siz) {
u = hp.pop();
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] > dis[u] + wei[ed])
hp.change(v, dis[u] + wei[ed]);
}
for (u = 1; u <= n; u++)
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] == dis[u] + wei[ed])
val[v] = min(val[v], cost[ed]);
}
};
gragh g;
ll dis[maxn], val[maxn];
int main() {
int n, m, i, u, v;
ll w, c, ans;
while (scanf("%d%d", &n, &m) && (n || m)) {
g.clear();
for (i = 0; i < m; i++) {
scanf("%d%d%lld%lld", &u, &v, &w, &c);
g.add(u, v, w, c);
g.add(v, u, w, c);
}
g.dijkstra(dis, val, 1, n);
ans = 0;
for (i = 1; i <= n; i++)
ans += val[i];
printf("%lld\n", ans);
}
return 0;
}
| #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5, maxm = 4e5 + 5;
const ll inf = 1e18;
inline int fa(int u) { return u / 2; }
inline int ls(int u) { return u * 2; }
inline int rs(int u) { return u * 2 + 1; }
struct heap {
int hea[maxn], pos[maxn], siz;
ll *dis;
heap(ll a[], int n) {
dis = a;
siz = n;
int i;
for (i = 1; i <= n; i++) {
dis[i] = inf;
hea[i] = i;
pos[i] = i;
}
}
void hswap(int u, int v) {
swap(hea[u], hea[v]);
pos[hea[u]] = u;
pos[hea[v]] = v;
}
void update(int u) {
if (u == 1)
return;
if (dis[hea[fa(u)]] <= dis[hea[u]])
return;
hswap(u, fa(u));
update(fa(u));
}
void pushdown(int u) {
int v = u;
if (ls(u) <= siz && dis[hea[ls(u)]] < dis[hea[v]])
v = ls(u);
if (rs(u) <= siz && dis[hea[rs(u)]] < dis[hea[v]])
v = rs(u);
if (u == v)
return;
hswap(u, v);
pushdown(v);
}
void change(int u, ll val) {
dis[u] = val;
update(pos[u]);
pushdown(pos[u]);
}
int pop() {
hswap(1, siz);
siz--;
pushdown(1);
return hea[siz + 1];
}
};
struct gragh {
int head[maxn], next[maxm], to[maxm], tot;
ll wei[maxm], cost[maxn];
gragh() {
memset(head, -1, sizeof(head));
tot = 0;
}
void clear() {
memset(head, -1, sizeof(head));
tot = 0;
}
void add(int u, int v, ll w, ll c) {
next[tot] = head[u];
head[u] = tot;
to[tot] = v;
wei[tot] = w;
cost[tot] = c;
tot++;
}
void dijkstra(ll dis[], ll val[], int st, int n) {
heap hp(dis, n);
hp.change(st, 0);
int i, u, v, ed;
for (i = 1; i <= n; i++)
val[i] = inf;
val[st] = 0;
while (hp.siz) {
u = hp.pop();
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] > dis[u] + wei[ed])
hp.change(v, dis[u] + wei[ed]);
}
for (u = 1; u <= n; u++)
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] == dis[u] + wei[ed])
val[v] = min(val[v], cost[ed]);
}
};
gragh g;
ll dis[maxn], val[maxn];
int main() {
int n, m, i, u, v;
ll w, c, ans;
while (scanf("%d%d", &n, &m) && (n || m)) {
g.clear();
for (i = 0; i < m; i++) {
scanf("%d%d%lld%lld", &u, &v, &w, &c);
g.add(u, v, w, c);
g.add(v, u, w, c);
}
g.dijkstra(dis, val, 1, n);
ans = 0;
for (i = 1; i <= n; i++)
ans += val[i];
printf("%lld\n", ans);
}
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p01366 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 2e4 + 5, maxm = 4e5 + 5;
const ll inf = 1e18;
inline int fa(int u) { return u / 2; }
inline int ls(int u) { return u * 2; }
inline int rs(int u) { return u * 2 + 1; }
struct heap {
int hea[maxn], pos[maxn], siz;
ll *dis;
heap(ll a[], int n) {
dis = a;
siz = n;
int i;
for (i = 1; i <= n; i++) {
dis[i] = inf;
hea[i] = i;
pos[i] = i;
}
}
void hswap(int u, int v) {
swap(hea[u], hea[v]);
pos[hea[u]] = u;
pos[hea[v]] = v;
}
void update(int u) {
if (u == 1)
return;
if (dis[hea[fa(u)]] <= dis[hea[u]])
return;
hswap(u, fa(u));
update(fa(u));
}
void pushdown(int u) {
int v = u;
if (ls(u) <= siz && dis[hea[ls(u)]] < dis[hea[v]])
v = ls(u);
if (rs(u) <= siz && dis[hea[rs(u)]] < dis[hea[v]])
v = rs(u);
if (u == v)
return;
hswap(u, v);
pushdown(v);
}
void change(int u, ll val) {
dis[u] = val;
update(pos[u]);
pushdown(pos[u]);
}
int pop() {
hswap(1, siz);
siz--;
pushdown(1);
return hea[siz + 1];
}
};
struct gragh {
int head[maxn], next[maxm], to[maxm], tot;
ll wei[maxm], cost[maxn];
gragh() {
memset(head, -1, sizeof(head));
tot = 0;
}
void clear() {
memset(head, -1, sizeof(head));
tot = 0;
}
void add(int u, int v, ll w, ll c) {
next[tot] = head[u];
head[u] = tot;
to[tot] = v;
wei[tot] = w;
cost[tot] = c;
tot++;
}
void dijkstra(ll dis[], ll val[], int st, int n) {
heap hp(dis, n);
hp.change(st, 0);
int i, u, v, ed;
for (i = 1; i <= n; i++)
val[i] = inf;
val[st] = 0;
while (hp.siz) {
u = hp.pop();
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] > dis[u] + wei[ed])
hp.change(v, dis[u] + wei[ed]);
}
for (u = 1; u <= n; u++)
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] == dis[u] + wei[ed])
val[v] = min(val[v], cost[ed]);
}
};
gragh g;
ll dis[maxn], val[maxn];
int main() {
int n, m, i, u, v;
ll w, c, ans;
while (scanf("%d%d", &n, &m) && (n || m)) {
g.clear();
for (i = 0; i < m; i++) {
scanf("%d%d%lld%lld", &u, &v, &w, &c);
g.add(u, v, w, c);
g.add(v, u, w, c);
}
g.dijkstra(dis, val, 1, n);
ans = 0;
for (i = 1; i <= n; i++)
ans += val[i];
printf("%lld\n", ans);
}
return 0;
}
| #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 5e4 + 5, maxm = 4e5 + 5;
const ll inf = 1e18;
inline int fa(int u) { return u / 2; }
inline int ls(int u) { return u * 2; }
inline int rs(int u) { return u * 2 + 1; }
struct heap {
int hea[maxn], pos[maxn], siz;
ll *dis;
heap(ll a[], int n) {
dis = a;
siz = n;
int i;
for (i = 1; i <= n; i++) {
dis[i] = inf;
hea[i] = i;
pos[i] = i;
}
}
void hswap(int u, int v) {
swap(hea[u], hea[v]);
pos[hea[u]] = u;
pos[hea[v]] = v;
}
void update(int u) {
if (u == 1)
return;
if (dis[hea[fa(u)]] <= dis[hea[u]])
return;
hswap(u, fa(u));
update(fa(u));
}
void pushdown(int u) {
int v = u;
if (ls(u) <= siz && dis[hea[ls(u)]] < dis[hea[v]])
v = ls(u);
if (rs(u) <= siz && dis[hea[rs(u)]] < dis[hea[v]])
v = rs(u);
if (u == v)
return;
hswap(u, v);
pushdown(v);
}
void change(int u, ll val) {
dis[u] = val;
update(pos[u]);
pushdown(pos[u]);
}
int pop() {
hswap(1, siz);
siz--;
pushdown(1);
return hea[siz + 1];
}
};
struct gragh {
int head[maxn], next[maxm], to[maxm], tot;
ll wei[maxm], cost[maxn];
gragh() {
memset(head, -1, sizeof(head));
tot = 0;
}
void clear() {
memset(head, -1, sizeof(head));
tot = 0;
}
void add(int u, int v, ll w, ll c) {
next[tot] = head[u];
head[u] = tot;
to[tot] = v;
wei[tot] = w;
cost[tot] = c;
tot++;
}
void dijkstra(ll dis[], ll val[], int st, int n) {
heap hp(dis, n);
hp.change(st, 0);
int i, u, v, ed;
for (i = 1; i <= n; i++)
val[i] = inf;
val[st] = 0;
while (hp.siz) {
u = hp.pop();
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] > dis[u] + wei[ed])
hp.change(v, dis[u] + wei[ed]);
}
for (u = 1; u <= n; u++)
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] == dis[u] + wei[ed])
val[v] = min(val[v], cost[ed]);
}
};
gragh g;
ll dis[maxn], val[maxn];
int main() {
int n, m, i, u, v;
ll w, c, ans;
while (scanf("%d%d", &n, &m) && (n || m)) {
g.clear();
for (i = 0; i < m; i++) {
scanf("%d%d%lld%lld", &u, &v, &w, &c);
g.add(u, v, w, c);
g.add(v, u, w, c);
}
g.dijkstra(dis, val, 1, n);
ans = 0;
for (i = 1; i <= n; i++)
ans += val[i];
printf("%lld\n", ans);
}
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p01366 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 3.5e4 + 5, maxm = 4e5 + 5;
const ll inf = 1e18;
inline int fa(int u) { return u / 2; }
inline int ls(int u) { return u * 2; }
inline int rs(int u) { return u * 2 + 1; }
struct heap {
int hea[maxn], pos[maxn], siz;
ll *dis;
heap(ll a[], int n) {
dis = a;
siz = n;
int i;
for (i = 1; i <= n; i++) {
dis[i] = inf;
hea[i] = i;
pos[i] = i;
}
}
void hswap(int u, int v) {
swap(hea[u], hea[v]);
pos[hea[u]] = u;
pos[hea[v]] = v;
}
void update(int u) {
if (u == 1)
return;
if (dis[hea[fa(u)]] <= dis[hea[u]])
return;
hswap(u, fa(u));
update(fa(u));
}
void pushdown(int u) {
int v = u;
if (ls(u) <= siz && dis[hea[ls(u)]] < dis[hea[v]])
v = ls(u);
if (rs(u) <= siz && dis[hea[rs(u)]] < dis[hea[v]])
v = rs(u);
if (u == v)
return;
hswap(u, v);
pushdown(v);
}
void change(int u, ll val) {
dis[u] = val;
update(pos[u]);
pushdown(pos[u]);
}
int pop() {
hswap(1, siz);
siz--;
pushdown(1);
return hea[siz + 1];
}
};
struct gragh {
int head[maxn], next[maxm], to[maxm], tot;
ll wei[maxm], cost[maxn];
gragh() {
memset(head, -1, sizeof(head));
tot = 0;
}
void clear() {
memset(head, -1, sizeof(head));
tot = 0;
}
void add(int u, int v, ll w, ll c) {
next[tot] = head[u];
head[u] = tot;
to[tot] = v;
wei[tot] = w;
cost[tot] = c;
tot++;
}
void dijkstra(ll dis[], ll val[], int st, int n) {
heap hp(dis, n);
hp.change(st, 0);
int i, u, v, ed;
for (i = 1; i <= n; i++)
val[i] = inf;
val[st] = 0;
while (hp.siz) {
u = hp.pop();
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] > dis[u] + wei[ed])
hp.change(v, dis[u] + wei[ed]);
}
for (u = 1; u <= n; u++)
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] == dis[u] + wei[ed])
val[v] = min(val[v], cost[ed]);
}
};
gragh g;
ll dis[maxn], val[maxn];
int main() {
int n, m, i, u, v;
ll w, c, ans;
while (scanf("%d%d", &n, &m) && (n || m)) {
g.clear();
for (i = 0; i < m; i++) {
scanf("%d%d%lld%lld", &u, &v, &w, &c);
g.add(u, v, w, c);
g.add(v, u, w, c);
}
g.dijkstra(dis, val, 1, n);
ans = 0;
for (i = 1; i <= n; i++)
ans += val[i];
printf("%lld\n", ans);
}
return 0;
}
| #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 4e4 + 5, maxm = 1e5 + 5;
const ll inf = 1e18;
inline int fa(int u) { return u / 2; }
inline int ls(int u) { return u * 2; }
inline int rs(int u) { return u * 2 + 1; }
struct heap {
int hea[maxn], pos[maxn], siz;
ll *dis;
heap(ll a[], int n) {
dis = a;
siz = n;
int i;
for (i = 1; i <= n; i++) {
dis[i] = inf;
hea[i] = i;
pos[i] = i;
}
}
void hswap(int u, int v) {
swap(hea[u], hea[v]);
pos[hea[u]] = u;
pos[hea[v]] = v;
}
void update(int u) {
if (u == 1)
return;
if (dis[hea[fa(u)]] <= dis[hea[u]])
return;
hswap(u, fa(u));
update(fa(u));
}
void pushdown(int u) {
int v = u;
if (ls(u) <= siz && dis[hea[ls(u)]] < dis[hea[v]])
v = ls(u);
if (rs(u) <= siz && dis[hea[rs(u)]] < dis[hea[v]])
v = rs(u);
if (u == v)
return;
hswap(u, v);
pushdown(v);
}
void change(int u, ll val) {
dis[u] = val;
update(pos[u]);
pushdown(pos[u]);
}
int pop() {
hswap(1, siz);
siz--;
pushdown(1);
return hea[siz + 1];
}
};
struct gragh {
int head[maxn], next[maxm], to[maxm], tot;
ll wei[maxm], cost[maxn];
gragh() {
memset(head, -1, sizeof(head));
tot = 0;
}
void clear() {
memset(head, -1, sizeof(head));
tot = 0;
}
void add(int u, int v, ll w, ll c) {
next[tot] = head[u];
head[u] = tot;
to[tot] = v;
wei[tot] = w;
cost[tot] = c;
tot++;
}
void dijkstra(ll dis[], ll val[], int st, int n) {
heap hp(dis, n);
hp.change(st, 0);
int i, u, v, ed;
for (i = 1; i <= n; i++)
val[i] = inf;
val[st] = 0;
while (hp.siz) {
u = hp.pop();
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] > dis[u] + wei[ed])
hp.change(v, dis[u] + wei[ed]);
}
for (u = 1; u <= n; u++)
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] == dis[u] + wei[ed])
val[v] = min(val[v], cost[ed]);
}
};
gragh g;
ll dis[maxn], val[maxn];
int main() {
int n, m, i, u, v;
ll w, c, ans;
while (scanf("%d%d", &n, &m) && (n || m)) {
g.clear();
for (i = 0; i < m; i++) {
scanf("%d%d%lld%lld", &u, &v, &w, &c);
g.add(u, v, w, c);
g.add(v, u, w, c);
}
g.dijkstra(dis, val, 1, n);
ans = 0;
for (i = 1; i <= n; i++)
ans += val[i];
printf("%lld\n", ans);
}
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p01366 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 5, maxm = 2e4 + 5;
const ll inf = 1e18;
inline int fa(int u) { return u / 2; }
inline int ls(int u) { return u * 2; }
inline int rs(int u) { return u * 2 + 1; }
struct heap {
int hea[maxn], pos[maxn], siz;
ll *dis;
heap(ll a[], int n) {
dis = a;
siz = n;
int i;
for (i = 1; i <= n; i++) {
dis[i] = inf;
hea[i] = i;
pos[i] = i;
}
}
void hswap(int u, int v) {
swap(hea[u], hea[v]);
pos[hea[u]] = u;
pos[hea[v]] = v;
}
void update(int u) {
if (u == 1)
return;
if (dis[hea[fa(u)]] <= dis[hea[u]])
return;
hswap(u, fa(u));
update(fa(u));
}
void pushdown(int u) {
int v = u;
if (ls(u) <= siz && dis[hea[ls(u)]] < dis[hea[v]])
v = ls(u);
if (rs(u) <= siz && dis[hea[rs(u)]] < dis[hea[v]])
v = rs(u);
if (u == v)
return;
hswap(u, v);
pushdown(v);
}
void change(int u, ll val) {
dis[u] = val;
update(pos[u]);
pushdown(pos[u]);
}
int pop() {
hswap(1, siz);
siz--;
pushdown(1);
return hea[siz + 1];
}
};
struct gragh {
int head[maxn], next[maxm], to[maxm], tot;
ll wei[maxm], cost[maxm];
gragh() {
memset(head, -1, sizeof(head));
tot = 0;
}
void clear() {
memset(head, -1, sizeof(head));
tot = 0;
}
void add(int u, int v, ll w, ll c) {
next[tot] = head[u];
head[u] = tot;
to[tot] = v;
wei[tot] = w;
cost[tot] = c;
tot++;
}
void dijkstra(ll dis[], ll val[], int st, int n) {
heap hp(dis, n);
hp.change(st, 0);
int i, u, v, ed;
for (i = 1; i <= n; i++)
val[i] = inf;
val[st] = 0;
while (hp.siz) {
u = hp.pop();
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] > dis[u] + wei[ed])
hp.change(v, dis[u] + wei[ed]);
}
for (u = 1; u <= n; u++)
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] == dis[u] + wei[ed])
val[v] = min(val[v], cost[ed]);
}
};
gragh g;
ll dis[maxn], val[maxn];
int main() {
int n, m, i, u, v;
ll w, c, ans;
while (scanf("%d%d", &n, &m) && (n || m)) {
g.clear();
for (i = 0; i < m; i++) {
scanf("%d%d%lld%lld", &u, &v, &w, &c);
g.add(u, v, w, c);
g.add(v, u, w, c);
}
g.dijkstra(dis, val, 1, n);
ans = 0;
for (i = 1; i <= n; i++)
ans += val[i];
printf("%lld\n", ans);
}
return 0;
}
| #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 5, maxm = 4e4 + 5;
const ll inf = 1e18;
inline int fa(int u) { return u / 2; }
inline int ls(int u) { return u * 2; }
inline int rs(int u) { return u * 2 + 1; }
struct heap {
int hea[maxn], pos[maxn], siz;
ll *dis;
heap(ll a[], int n) {
dis = a;
siz = n;
int i;
for (i = 1; i <= n; i++) {
dis[i] = inf;
hea[i] = i;
pos[i] = i;
}
}
void hswap(int u, int v) {
swap(hea[u], hea[v]);
pos[hea[u]] = u;
pos[hea[v]] = v;
}
void update(int u) {
if (u == 1)
return;
if (dis[hea[fa(u)]] <= dis[hea[u]])
return;
hswap(u, fa(u));
update(fa(u));
}
void pushdown(int u) {
int v = u;
if (ls(u) <= siz && dis[hea[ls(u)]] < dis[hea[v]])
v = ls(u);
if (rs(u) <= siz && dis[hea[rs(u)]] < dis[hea[v]])
v = rs(u);
if (u == v)
return;
hswap(u, v);
pushdown(v);
}
void change(int u, ll val) {
dis[u] = val;
update(pos[u]);
pushdown(pos[u]);
}
int pop() {
hswap(1, siz);
siz--;
pushdown(1);
return hea[siz + 1];
}
};
struct gragh {
int head[maxn], next[maxm], to[maxm], tot;
ll wei[maxm], cost[maxm];
gragh() {
memset(head, -1, sizeof(head));
tot = 0;
}
void clear() {
memset(head, -1, sizeof(head));
tot = 0;
}
void add(int u, int v, ll w, ll c) {
next[tot] = head[u];
head[u] = tot;
to[tot] = v;
wei[tot] = w;
cost[tot] = c;
tot++;
}
void dijkstra(ll dis[], ll val[], int st, int n) {
heap hp(dis, n);
hp.change(st, 0);
int i, u, v, ed;
for (i = 1; i <= n; i++)
val[i] = inf;
val[st] = 0;
while (hp.siz) {
u = hp.pop();
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] > dis[u] + wei[ed])
hp.change(v, dis[u] + wei[ed]);
}
for (u = 1; u <= n; u++)
for (ed = head[u]; ed != -1; ed = next[ed])
if (dis[v = to[ed]] == dis[u] + wei[ed])
val[v] = min(val[v], cost[ed]);
}
};
gragh g;
ll dis[maxn], val[maxn];
int main() {
int n, m, i, u, v;
ll w, c, ans;
while (scanf("%d%d", &n, &m) && (n || m)) {
g.clear();
for (i = 0; i < m; i++) {
scanf("%d%d%lld%lld", &u, &v, &w, &c);
g.add(u, v, w, c);
g.add(v, u, w, c);
}
g.dijkstra(dis, val, 1, n);
ans = 0;
for (i = 1; i <= n; i++)
ans += val[i];
printf("%lld\n", ans);
}
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p01366 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
#define MAX_N 1005
#define MAX_M 100005
#define INF 1e9 + 1
int n, m, x;
struct edge {
int to, dis, cost;
edge();
edge(int to, int dis, int cost) : to(to), dis(dis), cost(cost) {}
};
int sum;
int ans[MAX_N];
typedef pair<int, int> P; // first??????????????????second?????¶????????????
vector<edge> G[MAX_N];
int d[MAX_N]; //??¶?????°??????????????????
int dijkstra(int s) {
int res = 0;
priority_queue<P, vector<P>, greater<P>> que;
fill(d, d + MAX_N, INF);
fill(ans, ans + MAX_N, INF);
d[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.dis) {
ans[e.to] = e.cost;
d[e.to] = d[v] + e.dis;
que.push(P(d[e.to], e.to));
} else if (d[e.to] == d[v] + e.dis && ans[e.to] > e.cost) {
ans[e.to] = e.cost;
}
}
}
return accumulate(ans + 2, ans + n + 1, res);
}
int main() {
while (scanf("%d%d", &n, &m) && n != 0) {
int u, v, d, c;
for (int i = 0; i <= n; i++) {
G[i].clear();
}
for (int i = 0; i < m; i++) {
scanf("%d%d%d%d", &u, &v, &d, &c);
G[u].push_back(edge(v, d, c));
G[v].push_back(edge(u, d, c));
}
sum = dijkstra(1);
printf("%d\n", sum);
}
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
#define MAX_N 10006
#define MAX_M 20000
#define INF 1e9 + 1
int n, m, x;
struct edge {
int to, dis, cost;
edge();
edge(int to, int dis, int cost) : to(to), dis(dis), cost(cost) {}
};
int sum;
int ans[MAX_N];
typedef pair<int, int> P; // first??????????????????second?????¶????????????
vector<edge> G[MAX_N];
int d[MAX_N]; //??¶?????°??????????????????
int dijkstra(int s) {
int res = 0;
priority_queue<P, vector<P>, greater<P>> que;
fill(d, d + MAX_N, INF);
fill(ans, ans + MAX_N, INF);
d[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.dis) {
ans[e.to] = e.cost;
d[e.to] = d[v] + e.dis;
que.push(P(d[e.to], e.to));
} else if (d[e.to] == d[v] + e.dis && ans[e.to] > e.cost) {
ans[e.to] = e.cost;
}
}
}
return accumulate(ans + 2, ans + n + 1, res);
}
int main() {
while (scanf("%d%d", &n, &m) && n != 0) {
int u, v, d, c;
for (int i = 0; i <= n; i++) {
G[i].clear();
}
for (int i = 0; i < m; i++) {
scanf("%d%d%d%d", &u, &v, &d, &c);
G[u].push_back(edge(v, d, c));
G[v].push_back(edge(u, d, c));
}
sum = dijkstra(1);
printf("%d\n", sum);
}
} | replace | 9 | 11 | 9 | 11 | 0 | |
p01366 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define GET_ARG(a, b, c, F, ...) F
#define REP3(i, s, e) for (i = s; i <= e; i++)
#define REP2(i, n) REP3(i, 0, (int)(n)-1)
#define REP(...) GET_ARG(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define RREP3(i, s, e) for (i = s; i >= e; i--)
#define RREP2(i, n) RREP3(i, (int)(n)-1, 0)
#define RREP(...) GET_ARG(__VA_ARGS__, RREP3, RREP2)(__VA_ARGS__)
#define DEBUG(x) cerr << #x ": " << x << endl
const int INF = 1e8;
vector<pair<int, pair<int, int>>> e[10000];
int dist[1000];
int main(void) {
while (true) {
int i, n, m;
cin >> n >> m;
if (n == 0 && m == 0)
return 0;
REP(i, n) e[i].clear();
REP(i, m) {
int u, v, d, c;
cin >> u >> v >> d >> c;
u--;
v--;
e[u].push_back({v, {d, c}});
e[v].push_back({u, {d, c}});
}
priority_queue<pair<int, int>> q;
q.push({0, 0});
REP(i, n) dist[i] = INF;
dist[0] = 0;
while (!q.empty()) {
auto p = q.top();
q.pop();
int u = p.second;
for (auto x : e[u]) {
int v = x.first;
int d = x.second.first;
if (dist[v] > dist[u] + d) {
dist[v] = dist[u] + d;
q.push({-dist[v], v});
}
}
}
int ans = 0;
REP(i, 1, n - 1) {
int mn = INF;
for (auto p : e[i]) {
int v = p.first;
int d = p.second.first;
int c = p.second.second;
if (dist[i] == dist[v] + d)
mn = min(mn, c);
}
ans += mn;
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define GET_ARG(a, b, c, F, ...) F
#define REP3(i, s, e) for (i = s; i <= e; i++)
#define REP2(i, n) REP3(i, 0, (int)(n)-1)
#define REP(...) GET_ARG(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define RREP3(i, s, e) for (i = s; i >= e; i--)
#define RREP2(i, n) RREP3(i, (int)(n)-1, 0)
#define RREP(...) GET_ARG(__VA_ARGS__, RREP3, RREP2)(__VA_ARGS__)
#define DEBUG(x) cerr << #x ": " << x << endl
const int INF = 1e8;
vector<pair<int, pair<int, int>>> e[10000];
int dist[10000];
int main(void) {
while (true) {
int i, n, m;
cin >> n >> m;
if (n == 0 && m == 0)
return 0;
REP(i, n) e[i].clear();
REP(i, m) {
int u, v, d, c;
cin >> u >> v >> d >> c;
u--;
v--;
e[u].push_back({v, {d, c}});
e[v].push_back({u, {d, c}});
}
priority_queue<pair<int, int>> q;
q.push({0, 0});
REP(i, n) dist[i] = INF;
dist[0] = 0;
while (!q.empty()) {
auto p = q.top();
q.pop();
int u = p.second;
for (auto x : e[u]) {
int v = x.first;
int d = x.second.first;
if (dist[v] > dist[u] + d) {
dist[v] = dist[u] + d;
q.push({-dist[v], v});
}
}
}
int ans = 0;
REP(i, 1, n - 1) {
int mn = INF;
for (auto p : e[i]) {
int v = p.first;
int d = p.second.first;
int c = p.second.second;
if (dist[i] == dist[v] + d)
mn = min(mn, c);
}
ans += mn;
}
cout << ans << endl;
}
return 0;
} | replace | 22 | 23 | 22 | 23 | 0 | |
p01366 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef ll li;
typedef pair<int, int> PI;
#define EPS (1e-6)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, n) rep(i, n)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define min3(a, b, c) min((a), min((b), (c)))
#define min4(a, b, c, d) min((a), min3((b), (c), (d)))
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define FLL(a, b) memset((a), b, sizeof(a))
#define CLR(a) memset((a), 0, sizeof(a))
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &val) {
return out << "(" << val.F << ", " << val.S << ")";
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &val) {
out << "{";
rep(i, SZ(val)) out << (i ? ", " : "") << val[i];
return out << "}";
}
typedef double FP;
typedef complex<FP> pt;
typedef pt P;
typedef pair<pt, pt> line;
FP dot(P a, P b) { return real(conj(a) * b); }
FP crs(P a, P b) { return imag(conj(a) * b); }
P ortho(P a) { return conj(P(imag(a), real(a))); }
P ortho(line a) { return ortho(a.S - a.F); }
P crspt(P a, P b, P c, P d) {
b -= a, d -= c;
return a + b * crs(d, c - a) / crs(d, b);
}
P crspt(line a, line b) { return crspt(a.F, a.S, b.F, b.S); }
bool onl(P a1, P a2, P b) {
return abs(b - a1) + abs(b - a2) < abs(a1 - a2) + EPS;
}
bool onl(line a, P b) { return onl(a.F, a.S, b); }
bool iscrs(line a, line b) {
P c = crspt(a, b);
return onl(a, c) && onl(b, c);
}
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};
enum { TOP, BTM, LFT, RGT, FRT, BCK };
int dxdy2ce[] = {RGT, FRT, LFT, BCK};
template <class T> T shift(T a, int b, int c, int d, int e) {
__typeof(a[0]) t = a[b];
a[b] = a[c];
a[c] = a[d];
a[d] = a[e];
a[e] = t;
return a;
}
template <class T> T rgt(T a) { return shift(a, TOP, LFT, BTM, RGT); }
template <class T> T lft(T a) { return shift(a, TOP, RGT, BTM, LFT); }
template <class T> T frt(T a) { return shift(a, TOP, BCK, BTM, FRT); }
template <class T> T bck(T a) { return shift(a, TOP, FRT, BTM, BCK); }
line mkl(P a, P v) { return line(a, a + v); }
FP lpdist(line a, P b) { return abs(b - crspt(a, mkl(b, ortho(a)))); }
FP spdist(line a, P b) {
P c(crspt(a, mkl(b, ortho(a))));
return onl(a, c) ? abs(b - c) : min(abs(a.F - b), abs(a.S - b));
}
FP ssdist(line a, line b) {
return min4(spdist(a, b.F), spdist(a, b.S), spdist(b, a.F), spdist(b, a.S));
}
int n, m;
vector<PI> G[10000];
bool vis[10000];
int c[10000], d[10000];
void solve() {
CLR(vis);
rep(i, n) G[i].clear();
rep(i, m) {
int a, b;
cin >> a >> b >> d[i] >> c[i];
--a, --b;
G[a].pb(mp(b, i));
G[b].pb(mp(a, i));
}
priority_queue<pair<PI, int>> q;
FOR(it, G[0]) q.push(mp(mp(-d[it->S], -c[it->S]), it->F));
int ans = 0;
vis[0] = true;
while (!q.empty()) {
int cc = q.top().F.F;
int ec = -q.top().F.S;
int cv = q.top().S;
q.pop();
if (vis[cv])
continue;
// cout << cc << ' ' << ec << ' ' << cv << endl;
vis[cv] = true;
ans += ec;
FOR(it, G[cv])
q.push(mp(mp(cc - d[it->S], -c[it->S]), it->F));
}
cout << ans << endl;
}
int main(int argc, char *argv[]) {
while (cin >> n >> m && n)
solve();
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef ll li;
typedef pair<int, int> PI;
#define EPS (1e-6)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, n) rep(i, n)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define min3(a, b, c) min((a), min((b), (c)))
#define min4(a, b, c, d) min((a), min3((b), (c), (d)))
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define FLL(a, b) memset((a), b, sizeof(a))
#define CLR(a) memset((a), 0, sizeof(a))
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &val) {
return out << "(" << val.F << ", " << val.S << ")";
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &val) {
out << "{";
rep(i, SZ(val)) out << (i ? ", " : "") << val[i];
return out << "}";
}
typedef double FP;
typedef complex<FP> pt;
typedef pt P;
typedef pair<pt, pt> line;
FP dot(P a, P b) { return real(conj(a) * b); }
FP crs(P a, P b) { return imag(conj(a) * b); }
P ortho(P a) { return conj(P(imag(a), real(a))); }
P ortho(line a) { return ortho(a.S - a.F); }
P crspt(P a, P b, P c, P d) {
b -= a, d -= c;
return a + b * crs(d, c - a) / crs(d, b);
}
P crspt(line a, line b) { return crspt(a.F, a.S, b.F, b.S); }
bool onl(P a1, P a2, P b) {
return abs(b - a1) + abs(b - a2) < abs(a1 - a2) + EPS;
}
bool onl(line a, P b) { return onl(a.F, a.S, b); }
bool iscrs(line a, line b) {
P c = crspt(a, b);
return onl(a, c) && onl(b, c);
}
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};
enum { TOP, BTM, LFT, RGT, FRT, BCK };
int dxdy2ce[] = {RGT, FRT, LFT, BCK};
template <class T> T shift(T a, int b, int c, int d, int e) {
__typeof(a[0]) t = a[b];
a[b] = a[c];
a[c] = a[d];
a[d] = a[e];
a[e] = t;
return a;
}
template <class T> T rgt(T a) { return shift(a, TOP, LFT, BTM, RGT); }
template <class T> T lft(T a) { return shift(a, TOP, RGT, BTM, LFT); }
template <class T> T frt(T a) { return shift(a, TOP, BCK, BTM, FRT); }
template <class T> T bck(T a) { return shift(a, TOP, FRT, BTM, BCK); }
line mkl(P a, P v) { return line(a, a + v); }
FP lpdist(line a, P b) { return abs(b - crspt(a, mkl(b, ortho(a)))); }
FP spdist(line a, P b) {
P c(crspt(a, mkl(b, ortho(a))));
return onl(a, c) ? abs(b - c) : min(abs(a.F - b), abs(a.S - b));
}
FP ssdist(line a, line b) {
return min4(spdist(a, b.F), spdist(a, b.S), spdist(b, a.F), spdist(b, a.S));
}
int n, m;
vector<PI> G[10000];
bool vis[10000];
int c[30000], d[30000];
void solve() {
CLR(vis);
rep(i, n) G[i].clear();
rep(i, m) {
int a, b;
cin >> a >> b >> d[i] >> c[i];
--a, --b;
G[a].pb(mp(b, i));
G[b].pb(mp(a, i));
}
priority_queue<pair<PI, int>> q;
FOR(it, G[0]) q.push(mp(mp(-d[it->S], -c[it->S]), it->F));
int ans = 0;
vis[0] = true;
while (!q.empty()) {
int cc = q.top().F.F;
int ec = -q.top().F.S;
int cv = q.top().S;
q.pop();
if (vis[cv])
continue;
// cout << cc << ' ' << ec << ' ' << cv << endl;
vis[cv] = true;
ans += ec;
FOR(it, G[cv])
q.push(mp(mp(cc - d[it->S], -c[it->S]), it->F));
}
cout << ans << endl;
}
int main(int argc, char *argv[]) {
while (cin >> n >> m && n)
solve();
return 0;
} | replace | 97 | 98 | 97 | 98 | 0 | |
p01368 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int N = 100;
const int L = 1000;
const int INF = 1e+8 + 10;
const int MAX_V = L * 2 + 2;
struct data {
int p, t;
data(int p = 0, int t = 0) : p(p), t(t) {}
bool operator<(const data &d) const { return t < d.t; }
};
struct edge {
int to, cap, rev;
edge(int t = 0, int c = 0, int r = 0) : to(t), cap(c), rev(r) {}
};
void add_edge(const int &from, const int &to, const int &cap,
vector<edge> G[MAX_V]) {
G[from].push_back(edge(to, cap, G[to].size()));
G[to].push_back(edge(from, 0, G[from].size() - 1));
}
int dfs_f(const int &v, const int &t, const int &f, vector<edge> G[MAX_V],
bool used[MAX_V]) {
if (v == t)
return f;
used[v] = true;
for (int i = 0; i < G[v].size(); i++) {
edge &e = G[v][i];
if (!used[e.to] && e.cap > 0) {
int d = dfs_f(e.to, t, min(f, e.cap), G, used);
if (d > 0) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int max_flow(int s, int t, vector<edge> G[MAX_V]) {
int flow = 0;
while (1) {
bool used[MAX_V];
fill(used, used + MAX_V, false);
int f = dfs_f(s, t, INF, G, used);
if (f == 0)
return flow;
flow += f;
}
}
int n, m, l, G[N][N];
vector<data> v;
int solve() {
vector<edge> fG[MAX_V];
for (int i = 0; i < n; i++)
G[i][i] = 0;
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
}
}
}
sort(v.begin(), v.end());
for (int i = 0; i < l; i++) {
add_edge(l * 2, i, 1, fG);
add_edge(l + i, l * 2 + 1, 1, fG);
// add_edge(i, l+i, 1, fG);
for (int j = i + 1; j < l; j++) {
if (G[v[i].p][v[j].p] < INF && v[i].t + G[v[i].p][v[j].p] <= v[j].t) {
add_edge(j, l + i, 1, fG);
}
}
}
// ?????§???????????? = V - ????°?????????????
return l - max_flow(l * 2, l * 2 + 1, fG);
}
int main() {
int x, y, d;
while (cin >> n >> m >> l && (n | m | l)) {
fill(G[0], G[n], INF);
v.clear();
for (int i = 0; i < m; i++) {
cin >> x >> y >> d;
G[x][y] = min(G[x][y], d);
G[y][x] = min(G[y][x], d);
}
for (int i = 0; i < l; i++) {
cin >> x >> y;
v.push_back(data(x, y));
}
cout << solve() << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
const int N = 100;
const int L = 1000;
const int INF = 1e+8 + 10;
const int MAX_V = L * 2 + 2;
struct data {
int p, t;
data(int p = 0, int t = 0) : p(p), t(t) {}
bool operator<(const data &d) const { return t < d.t; }
};
struct edge {
int to, cap, rev;
edge(int t = 0, int c = 0, int r = 0) : to(t), cap(c), rev(r) {}
};
void add_edge(const int &from, const int &to, const int &cap,
vector<edge> G[MAX_V]) {
G[from].push_back(edge(to, cap, G[to].size()));
G[to].push_back(edge(from, 0, G[from].size() - 1));
}
int dfs_f(int v, int t, int f, vector<edge> G[MAX_V], bool used[MAX_V]) {
if (v == t)
return f;
used[v] = true;
for (int i = 0; i < G[v].size(); i++) {
edge &e = G[v][i];
if (!used[e.to] && e.cap > 0) {
int d = dfs_f(e.to, t, min(f, e.cap), G, used);
if (d > 0) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int max_flow(int s, int t, vector<edge> G[MAX_V]) {
int flow = 0;
while (1) {
bool used[MAX_V];
fill(used, used + MAX_V, false);
int f = dfs_f(s, t, INF, G, used);
if (f == 0)
return flow;
flow += f;
}
}
int n, m, l, G[N][N];
vector<data> v;
int solve() {
vector<edge> fG[MAX_V];
for (int i = 0; i < n; i++)
G[i][i] = 0;
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
}
}
}
sort(v.begin(), v.end());
for (int i = 0; i < l; i++) {
add_edge(l * 2, i, 1, fG);
add_edge(l + i, l * 2 + 1, 1, fG);
// add_edge(i, l+i, 1, fG);
for (int j = i + 1; j < l; j++) {
if (G[v[i].p][v[j].p] < INF && v[i].t + G[v[i].p][v[j].p] <= v[j].t) {
add_edge(j, l + i, 1, fG);
}
}
}
// ?????§???????????? = V - ????°?????????????
return l - max_flow(l * 2, l * 2 + 1, fG);
}
int main() {
int x, y, d;
while (cin >> n >> m >> l && (n | m | l)) {
fill(G[0], G[n], INF);
v.clear();
for (int i = 0; i < m; i++) {
cin >> x >> y >> d;
G[x][y] = min(G[x][y], d);
G[y][x] = min(G[y][x], d);
}
for (int i = 0; i < l; i++) {
cin >> x >> y;
v.push_back(data(x, y));
}
cout << solve() << endl;
}
} | replace | 25 | 27 | 25 | 26 | TLE | |
p01368 | C++ | Runtime Error | // include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
int N, M, L;
const int MAX_V = 300;
int V;
VI biG[MAX_V];
VI origG[MAX_V];
int match[MAX_V];
bool used[MAX_V];
void add_edge(int u, int v) {
biG[u].push_back(v);
biG[v].push_back(u);
}
bool dfs(int v) {
used[v] = true;
for (int i = 0; i < biG[v].size(); ++i) {
int u = biG[v][i], w = match[u];
if (w < 0 || !used[w] && dfs(w)) {
match[v] = u;
match[u] = v;
return true;
}
}
return false;
}
int bipartite_matching() {
int res = 0;
memset(match, -1, sizeof(match));
for (int v = 0; v < V; ++v) {
if (match[v] < 0) {
memset(used, false, sizeof(used));
if (dfs(v))
++res;
}
}
return res;
}
int findMinPathcover(int n) {
REP(i, MAX_V) biG[i].clear();
V = 2 * n;
REP(i, n) REP(j, SZ(origG[i])) { add_edge(i, n + origG[i][j]); }
int res = bipartite_matching();
return n - res;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
while (cin >> N >> M >> L, N) {
REP(i, MAX_V) origG[i].clear();
vector<vector<LL>> dist(N, vector<LL>(N, 1e+15));
REP(i, N) dist[i][i] = 0;
REP(i, M) {
int u, v, d;
cin >> u >> v >> d;
dist[u][v] = dist[v][u] = d;
}
REP(k, N)
REP(i, N) REP(j, N) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
vector<LL> p(L), t(L);
REP(i, L) cin >> p[i] >> t[i];
REP(i, L)
REP(j, L) if (i != j && t[i] + dist[p[i]][p[j]] <= t[j]) origG[i].PB(j);
cout << findMinPathcover(L) << endl;
}
return 0;
} | // include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
int N, M, L;
const int MAX_V = 2100;
int V;
VI biG[MAX_V];
VI origG[MAX_V];
int match[MAX_V];
bool used[MAX_V];
void add_edge(int u, int v) {
biG[u].push_back(v);
biG[v].push_back(u);
}
bool dfs(int v) {
used[v] = true;
for (int i = 0; i < biG[v].size(); ++i) {
int u = biG[v][i], w = match[u];
if (w < 0 || !used[w] && dfs(w)) {
match[v] = u;
match[u] = v;
return true;
}
}
return false;
}
int bipartite_matching() {
int res = 0;
memset(match, -1, sizeof(match));
for (int v = 0; v < V; ++v) {
if (match[v] < 0) {
memset(used, false, sizeof(used));
if (dfs(v))
++res;
}
}
return res;
}
int findMinPathcover(int n) {
REP(i, MAX_V) biG[i].clear();
V = 2 * n;
REP(i, n) REP(j, SZ(origG[i])) { add_edge(i, n + origG[i][j]); }
int res = bipartite_matching();
return n - res;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
while (cin >> N >> M >> L, N) {
REP(i, MAX_V) origG[i].clear();
vector<vector<LL>> dist(N, vector<LL>(N, 1e+15));
REP(i, N) dist[i][i] = 0;
REP(i, M) {
int u, v, d;
cin >> u >> v >> d;
dist[u][v] = dist[v][u] = d;
}
REP(k, N)
REP(i, N) REP(j, N) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
vector<LL> p(L), t(L);
REP(i, L) cin >> p[i] >> t[i];
REP(i, L)
REP(j, L) if (i != j && t[i] + dist[p[i]][p[j]] <= t[j]) origG[i].PB(j);
cout << findMinPathcover(L) << endl;
}
return 0;
} | replace | 59 | 60 | 59 | 60 | 0 | |
p01368 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
const int vmax = 1010;
int X, Y;
vector<int> graph[vmax];
int match[vmax];
bool used[vmax];
bool dfs(int v) {
used[v] = true;
for (auto &u : graph[v]) {
int w = match[u];
if (w < 0 || (!used[w] && dfs(w))) {
match[v] = u;
match[u] = v;
return true;
}
}
return false;
}
int bipartite_matching() {
int res = 0;
rep(i, vmax) match[i] = -1;
rep(v, X + Y) {
if (match[v] < 0) {
rep(i, vmax) used[i] = 0;
if (dfs(v))
res++;
}
}
return res;
}
int n, m, l;
int dist[110][110];
pair<int, int> info[1010];
int main(void) {
while (cin >> n >> m >> l) {
if (n == 0)
break;
rep(i, vmax) graph[i].clear();
rep(i, n) rep(j, n) dist[i][j] = ((i == j) ? 0 : 1 << 20);
rep(i, m) {
int u, v, d;
cin >> u >> v >> d;
dist[u][v] = min(dist[u][v], d);
dist[v][u] = min(dist[v][u], d);
}
rep(k, n) rep(i, n) rep(j, n) dist[i][j] =
min(dist[i][j], dist[i][k] + dist[k][j]);
rep(i, l) cin >> info[i].second >> info[i].first;
sort(info, info + l);
X = Y = l - 1;
rep(i, l - 1) rep(j, l - 1) {
if (i > j)
continue;
int a = i, b = j + 1;
if (dist[info[b].second][info[a].second] > info[b].first - info[a].first)
continue;
b += X - 1;
graph[a].push_back(b);
graph[b].push_back(a);
}
cout << l - bipartite_matching() << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
const int vmax = 2010;
int X, Y;
vector<int> graph[vmax];
int match[vmax];
bool used[vmax];
bool dfs(int v) {
used[v] = true;
for (auto &u : graph[v]) {
int w = match[u];
if (w < 0 || (!used[w] && dfs(w))) {
match[v] = u;
match[u] = v;
return true;
}
}
return false;
}
int bipartite_matching() {
int res = 0;
rep(i, vmax) match[i] = -1;
rep(v, X + Y) {
if (match[v] < 0) {
rep(i, vmax) used[i] = 0;
if (dfs(v))
res++;
}
}
return res;
}
int n, m, l;
int dist[110][110];
pair<int, int> info[1010];
int main(void) {
while (cin >> n >> m >> l) {
if (n == 0)
break;
rep(i, vmax) graph[i].clear();
rep(i, n) rep(j, n) dist[i][j] = ((i == j) ? 0 : 1 << 20);
rep(i, m) {
int u, v, d;
cin >> u >> v >> d;
dist[u][v] = min(dist[u][v], d);
dist[v][u] = min(dist[v][u], d);
}
rep(k, n) rep(i, n) rep(j, n) dist[i][j] =
min(dist[i][j], dist[i][k] + dist[k][j]);
rep(i, l) cin >> info[i].second >> info[i].first;
sort(info, info + l);
X = Y = l - 1;
rep(i, l - 1) rep(j, l - 1) {
if (i > j)
continue;
int a = i, b = j + 1;
if (dist[info[b].second][info[a].second] > info[b].first - info[a].first)
continue;
b += X - 1;
graph[a].push_back(b);
graph[b].push_back(a);
}
cout << l - bipartite_matching() << endl;
}
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p01368 | C++ | Runtime Error | #include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define LINF (1LL << 60)
#define INF (1 << 30)
#define MAX_N 2160
typedef long long Int;
struct E {
int to, rev;
Int lim;
E(int x, Int y, int z) {
to = x;
lim = y;
rev = z;
}
};
vector<E> edge[MAX_N];
int lev[MAX_N];
int iter[MAX_N];
void make_edge(int a, int b, Int l) {
edge[a].push_back(E(b, l, edge[b].size()));
edge[b].push_back(E(a, 0, edge[a].size() - 1));
}
Int dfs(int s, int g, Int f) {
if (s == g)
return f;
for (int &i = iter[s]; i < edge[s].size(); i++) {
E &e = edge[s][i];
if (lev[e.to] <= lev[s] || e.lim <= 0)
continue;
Int tmp = dfs(e.to, g, min(f, e.lim));
if (!tmp)
continue;
e.lim -= tmp;
edge[e.to][e.rev].lim += tmp;
return tmp;
}
return 0;
}
void bfs(int x) {
queue<int> q;
q.push(x);
int p = 0;
while (!q.empty()) {
for (int i = q.size(); i--;) {
int tmp = q.front();
q.pop();
if (lev[tmp] != INF)
continue;
lev[tmp] = p;
for (int j = 0; j < edge[tmp].size(); j++) {
if (edge[tmp][j].lim > 0 && lev[edge[tmp][j].to] == INF)
q.push(edge[tmp][j].to);
}
}
p++;
}
}
Int max_flow(int s, int g) {
Int res = 0;
bool fin = false;
while (!fin) {
fill(lev, lev + MAX_N, INF);
fill(iter, iter + MAX_N, 0);
bfs(s);
fin = true;
while (true) {
int p = dfs(s, g, INF);
if (p == 0)
break;
fin = false;
res += p;
}
}
return res;
}
int dist[108][108];
int p[108], t[108];
int main() {
int n, m, l, u, v, d;
while (cin >> n >> m >> l, n | m | l) {
for (int i = 0; i < 2160; i++)
edge[i].clear();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j)
dist[i][j] = 0;
else
dist[i][j] = INF - 1;
}
}
for (int i = 0; i < m; i++) {
cin >> u >> v >> d;
dist[u][v] = d;
dist[v][u] = d;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
dist[j][k] = min(dist[j][i] + dist[i][k], dist[j][k]);
for (int i = 0; i < l; i++) {
cin >> p[i] >> t[i];
}
for (int i = 0; i < l; i++) {
make_edge(l * 2, i, 1);
make_edge(i + l, l * 2 + 1, 1);
for (int j = 0; j < l; j++) {
if (i == j)
continue;
if (t[i] + dist[p[i]][p[j]] <= t[j]) {
make_edge(i, j + l, 1);
}
}
}
cout << l - max_flow(l * 2, l * 2 + 1) << endl;
}
return 0;
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define LINF (1LL << 60)
#define INF (1 << 30)
#define MAX_N 2160
typedef long long Int;
struct E {
int to, rev;
Int lim;
E(int x, Int y, int z) {
to = x;
lim = y;
rev = z;
}
};
vector<E> edge[MAX_N];
int lev[MAX_N];
int iter[MAX_N];
void make_edge(int a, int b, Int l) {
edge[a].push_back(E(b, l, edge[b].size()));
edge[b].push_back(E(a, 0, edge[a].size() - 1));
}
Int dfs(int s, int g, Int f) {
if (s == g)
return f;
for (int &i = iter[s]; i < edge[s].size(); i++) {
E &e = edge[s][i];
if (lev[e.to] <= lev[s] || e.lim <= 0)
continue;
Int tmp = dfs(e.to, g, min(f, e.lim));
if (!tmp)
continue;
e.lim -= tmp;
edge[e.to][e.rev].lim += tmp;
return tmp;
}
return 0;
}
void bfs(int x) {
queue<int> q;
q.push(x);
int p = 0;
while (!q.empty()) {
for (int i = q.size(); i--;) {
int tmp = q.front();
q.pop();
if (lev[tmp] != INF)
continue;
lev[tmp] = p;
for (int j = 0; j < edge[tmp].size(); j++) {
if (edge[tmp][j].lim > 0 && lev[edge[tmp][j].to] == INF)
q.push(edge[tmp][j].to);
}
}
p++;
}
}
Int max_flow(int s, int g) {
Int res = 0;
bool fin = false;
while (!fin) {
fill(lev, lev + MAX_N, INF);
fill(iter, iter + MAX_N, 0);
bfs(s);
fin = true;
while (true) {
int p = dfs(s, g, INF);
if (p == 0)
break;
fin = false;
res += p;
}
}
return res;
}
int dist[108][108];
int p[1080], t[1080];
int main() {
int n, m, l, u, v, d;
while (cin >> n >> m >> l, n | m | l) {
for (int i = 0; i < 2160; i++)
edge[i].clear();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j)
dist[i][j] = 0;
else
dist[i][j] = INF - 1;
}
}
for (int i = 0; i < m; i++) {
cin >> u >> v >> d;
dist[u][v] = d;
dist[v][u] = d;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
dist[j][k] = min(dist[j][i] + dist[i][k], dist[j][k]);
for (int i = 0; i < l; i++) {
cin >> p[i] >> t[i];
}
for (int i = 0; i < l; i++) {
make_edge(l * 2, i, 1);
make_edge(i + l, l * 2 + 1, 1);
for (int j = 0; j < l; j++) {
if (i == j)
continue;
if (t[i] + dist[p[i]][p[j]] <= t[j]) {
make_edge(i, j + l, 1);
}
}
}
cout << l - max_flow(l * 2, l * 2 + 1) << endl;
}
return 0;
} | replace | 86 | 87 | 86 | 87 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.