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
|
---|---|---|---|---|---|---|---|---|---|---|---|
p00170 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair
template <class T> void pv(T a, T b) {
for (T i = a; i != b; ++i)
cout << *i << " ";
cout << endl;
}
template <class T> void chmin(T &t, T f) {
if (t > f)
t = f;
}
template <class T> void chmax(T &t, T f) {
if (t < f)
t = f;
}
int in() {
int x;
scanf("%d", &x);
return x;
}
struct food {
string f;
int w;
int s;
};
int n;
vector<food> foods;
vector<string> res;
pair<int, vector<string>> result;
void f(int b, int w, int k) {
int i;
// cout<<b<<" "<<w<<endl<<" ";
// for(i=0;i<res.size();i++)cout<<res[i]<<endl;
// cout<<endl;
if (b == (1 << n) - 1) {
if (result.first > k) {
result.first = k;
result.second.clear();
for (i = 0; i < n; i++)
result.second.push_back(res[i]);
}
}
for (i = 0; i < n; i++) {
if (foods[i].s < w)
continue;
res.push_back(foods[i].f);
f(b | 1 << i, w + foods[i].w, k + w + foods[i].w);
res.pop_back();
}
}
int main() {
while (n = in()) {
int i;
foods.clear();
food buf;
result.first = 1000000000;
for (i = 0; i < n; i++) {
cin >> buf.f >> buf.w >> buf.s;
foods.push_back(buf);
}
res.clear();
f(0, 0, 0);
for (i = 0; i < n; i++) {
cout << result.second[n - i - 1] << endl;
}
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair
template <class T> void pv(T a, T b) {
for (T i = a; i != b; ++i)
cout << *i << " ";
cout << endl;
}
template <class T> void chmin(T &t, T f) {
if (t > f)
t = f;
}
template <class T> void chmax(T &t, T f) {
if (t < f)
t = f;
}
int in() {
int x;
scanf("%d", &x);
return x;
}
struct food {
string f;
int w;
int s;
};
int n;
vector<food> foods;
vector<string> res;
pair<int, vector<string>> result;
void f(int b, int w, int k) {
int i;
// cout<<b<<" "<<w<<endl<<" ";
// for(i=0;i<res.size();i++)cout<<res[i]<<endl;
// cout<<endl;
if (b == (1 << n) - 1) {
if (result.first > k) {
result.first = k;
result.second.clear();
for (i = 0; i < n; i++)
result.second.push_back(res[i]);
}
}
for (i = 0; i < n; i++) {
if (b & 1 << i)
continue;
if (foods[i].s < w)
continue;
res.push_back(foods[i].f);
f(b | 1 << i, w + foods[i].w, k + w + foods[i].w);
res.pop_back();
}
}
int main() {
while (n = in()) {
int i;
foods.clear();
food buf;
result.first = 1000000000;
for (i = 0; i < n; i++) {
cin >> buf.f >> buf.w >> buf.s;
foods.push_back(buf);
}
res.clear();
f(0, 0, 0);
for (i = 0; i < n; i++) {
cout << result.second[n - i - 1] << endl;
}
}
return 0;
} | insert | 69 | 69 | 69 | 71 | TLE | |
p00170 | C++ | Time Limit Exceeded |
// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#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 <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// conversion
//------------------------------------------
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }
// 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 MT make_tuple
#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);
const int DX[] = {0, 1, 0, -1};
const int DY[] = {-1, 0, 1, 0};
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))
// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
struct Food {
string name;
int w;
int s;
int id;
Food(string name, int w, int s, int id) : name(name), w(w), s(s), id(id){};
bool operator<(const Food &rhs) const { return id < rhs.id; }
};
double calc(vector<Food> foods) {
int length = foods.size();
int sum = 0;
for (int i = length - 1; i >= 0; --i) {
int s = foods[i].s;
if (s < sum)
return DBL_MAX;
sum += foods[i].w;
}
double d = 0, n = 0;
REP(i, length) {
n += (i + 1) * foods[i].w;
d += foods[i].w;
}
return n / d;
}
int main(int argc, char const *argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
vector<Food> ans;
while (cin >> n, n) {
vector<Food> foods;
foods.reserve(n);
string name;
int w, s;
REP(i, n) {
cin >> name >> w >> s;
foods.emplace_back(name, w, s, i);
}
double min = DBL_MAX;
do {
double value = calc(foods);
if (min > value) {
min = value;
ans = foods;
}
} while (next_permutation(foods.begin(), foods.end()));
REP(i, n)
cout << ans[i].name << endl;
};
return 0;
} |
// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#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 <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// conversion
//------------------------------------------
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }
// 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 MT make_tuple
#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);
const int DX[] = {0, 1, 0, -1};
const int DY[] = {-1, 0, 1, 0};
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))
// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
struct Food {
string name;
int w;
int s;
int id;
Food(string name, int w, int s, int id) : name(name), w(w), s(s), id(id){};
bool operator<(const Food &rhs) const { return id < rhs.id; }
};
double calc(vector<Food> &foods) {
int length = foods.size();
int sum = 0;
for (int i = length - 1; i >= 0; --i) {
int s = foods[i].s;
if (s < sum)
return DBL_MAX;
sum += foods[i].w;
}
double d = 0, n = 0;
REP(i, length) {
n += (i + 1) * foods[i].w;
d += foods[i].w;
}
return n / d;
}
int main(int argc, char const *argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
vector<Food> ans;
while (cin >> n, n) {
vector<Food> foods;
foods.reserve(n);
string name;
int w, s;
REP(i, n) {
cin >> name >> w >> s;
foods.emplace_back(name, w, s, i);
}
double min = DBL_MAX;
do {
double value = calc(foods);
if (min > value) {
min = value;
ans = foods;
}
} while (next_permutation(foods.begin(), foods.end()));
REP(i, n)
cout << ans[i].name << endl;
};
return 0;
} | replace | 98 | 99 | 98 | 99 | TLE | |
p00170 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef ostringstream OSS;
typedef istringstream ISS;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
#define X first
#define Y second
const double EPS = 1e-6;
int main(void) {
int N;
while (cin >> N, N) {
;
vector<string> F(N);
VI W(N), S(N);
for (int i = 0; i < N; i++) {
cin >> F[i] >> W[i] >> S[i];
}
VI p(N);
iota(p.begin(), p.end(), 0);
double ans = 1e+8;
VI best_p(p.begin(), p.end());
do {
double g = 0, gt = 0;
for (int i = 0; i < N; i++) {
g += (i + 1) * W[p[i]];
gt += W[p[i]];
}
g /= gt;
bool flg = true;
for (int i = N - 1; i >= 0; i--) {
int sum = 0;
for (int j = i + 1; j < N; j++) {
sum += W[p[j]];
}
flg &= sum <= S[p[i]];
}
if (flg && g < ans + EPS) {
ans = g;
swap(best_p, p);
}
} while (next_permutation(p.begin(), p.end()));
for (auto i : best_p) {
cout << F[i] << endl;
}
}
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef ostringstream OSS;
typedef istringstream ISS;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
#define X first
#define Y second
const double EPS = 1e-6;
int main(void) {
int N;
while (cin >> N, N) {
;
vector<string> F(N);
VI W(N), S(N);
for (int i = 0; i < N; i++) {
cin >> F[i] >> W[i] >> S[i];
}
VI p(N);
iota(p.begin(), p.end(), 0);
double ans = 1e+8;
VI best_p(p.begin(), p.end());
do {
double g = 0, gt = 0;
for (int i = 0; i < N; i++) {
g += (i + 1) * W[p[i]];
gt += W[p[i]];
}
g /= gt;
if (g - EPS > ans) {
continue;
}
bool flg = true;
for (int i = N - 1; i >= 0; i--) {
int sum = 0;
for (int j = i + 1; j < N; j++) {
sum += W[p[j]];
}
flg &= sum <= S[p[i]];
}
if (flg && g < ans + EPS) {
ans = g;
swap(best_p, p);
}
} while (next_permutation(p.begin(), p.end()));
for (auto i : best_p) {
cout << F[i] << endl;
}
}
return 0;
} | insert | 58 | 58 | 58 | 62 | TLE | |
p00173 | C++ | Runtime Error | #include <cstdio>
int main() {
char n[32];
int m, a;
for (int i = 0; i < 9; i++) {
scanf("%s%d%d", n, &m, &a);
printf("%s %d %d\n", m + a, m * 200 + a * 300);
}
return 0;
} | #include <cstdio>
int main() {
char n[32];
int m, a;
for (int i = 0; i < 9; i++) {
scanf("%s%d%d", n, &m, &a);
printf("%s %d %d\n", n, m + a, m * 200 + a * 300);
}
return 0;
} | replace | 7 | 8 | 7 | 8 | -11 | |
p00173 | C++ | Runtime Error | #include <stdio.h>
int main(void) {
char a;
int s, z, i, f, g;
while (scanf("%s %d %d", &a, &s, &z) != EOF) {
f = s + z;
g = (s * 200) + (z * 300);
printf("%c %d %d\n", a, f, g);
s = 0;
z = 0;
f = 0;
g = 0;
}
return 0;
} | #include <stdio.h>
int main(void) {
int a, s, d, f[10], h[10], i, j;
char g[16][10];
for (i = 1; i <= 9; i++) {
scanf("%s %d %d", g[i], &s, &d);
f[i] = s * 200 + d * 300;
h[i] = s + d;
printf("%s %d %d\n", g[i], h[i], f[i]);
}
return 0;
} | replace | 2 | 12 | 2 | 9 | 0 | |
p00174 | C++ | Runtime Error | #include <stdio.h>
#include <string.h>
int main() {
char ss[40];
int i, j, a, b;
while (~scanf(" %s ", ss)) {
if (ss[0] == '0')
break;
i = 1;
a = 0;
b = 0;
while (1) {
if (ss[i] == '\0')
break;
if (ss[i] == 'A')
++a;
if (ss[i] == 'B')
++b;
++i;
}
if (a > b) {
++a;
} else {
++b;
}
printf("%d %d\n", a, b);
}
return 0;
} | #include <stdio.h>
#include <string.h>
int main() {
char ss[110];
int i, j, a, b;
while (~scanf(" %s ", ss)) {
if (ss[0] == '0')
break;
i = 1;
a = 0;
b = 0;
while (1) {
if (ss[i] == '\0')
break;
if (ss[i] == 'A')
++a;
if (ss[i] == 'B')
++b;
++i;
}
if (a > b) {
++a;
} else {
++b;
}
printf("%d %d\n", a, b);
}
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00174 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
string s;
int a, b;
while (true) {
for (int i = 0; i < 3; i++) {
a = b = 0;
cin >> s;
if (s[0] == '0')
break;
for (int j = 1; j < s.length(); j++) {
if (s[j] == 'A')
a++;
else
b++;
}
if (a > b)
a++;
else
b++;
cout << a << " " << b << endl;
}
}
} | #include <iostream>
using namespace std;
int main() {
string s;
int a, b;
while (true) {
for (int i = 0; i < 3; i++) {
a = b = 0;
cin >> s;
if (s[0] == '0')
return 0;
for (int j = 1; j < s.length(); j++) {
if (s[j] == 'A')
a++;
else
b++;
}
if (a > b)
a++;
else
b++;
cout << a << " " << b << endl;
}
}
} | replace | 11 | 12 | 11 | 12 | TLE | |
p00175 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int i, j, k, cnt = 1, flg;
int n;
int fo[10] = {1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144};
while (1) {
cin >> n;
flg = 0;
if (n == -1)
break;
for (i = 10; i >= 0; i--) {
if (n == 0 && flg == 0) {
cout << "0";
break;
}
if (n / fo[i] > 0 && n != 0) {
cout << n / fo[i];
n = n % fo[i];
flg = 1;
} else if (flg == 1)
cout << "0";
}
cout << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int i, j, k, cnt = 1, flg;
int n;
int fo[10] = {1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144};
while (1) {
cin >> n;
flg = 0;
if (n == -1)
break;
for (i = 9; i >= 0; i--) {
if (n == 0 && flg == 0) {
cout << "0";
break;
}
if (n / fo[i] > 0 && n != 0) {
cout << n / fo[i];
n = n % fo[i];
flg = 1;
} else if (flg == 1)
cout << "0";
}
cout << endl;
}
return 0;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p00175 | C++ | Time Limit Exceeded | #include <stdio.h>
int main(void) {
int a[100];
int n;
int index;
while (1) {
scanf("%d", &n);
if (n == -1)
break;
index = 0;
do {
a[index++] = (n & 0x3);
n >> 2;
} while (n != 0);
index--;
while (index >= 0) {
printf("%d", a[index--]);
}
puts("");
}
} | #include <stdio.h>
int main(void) {
int a[100];
int n;
int index;
while (1) {
scanf("%d", &n);
if (n == -1)
break;
index = 0;
do {
a[index++] = (n & 0x3);
n >>= 2;
} while (n != 0);
index--;
while (index >= 0) {
printf("%d", a[index--]);
}
puts("");
}
} | replace | 14 | 15 | 14 | 15 | TLE | |
p00178 | C++ | Runtime Error | #include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MH 100
using namespace std;
bool ck(int *f) {
rep(i, 5) if (!f[i]) return 0;
return 1;
}
int main() {
int n;
while (cin >> n, n) {
int d, l, x, y, c = 0;
int field[MH][5] = {0};
rep(i, n) {
cin >> d >> l >> x, y = MH, x--, d--;
for (; y > 0; y--)
rep(j, (d ? 1 : l)) if (field[y - 1][x + j]) goto END;
END:
rep(j, l) field[y + d * j][x + (1 - d) * j] = 1;
rep(i, MH) while (ck(field[i])) {
rep(j, MH - i - 1) rep(k, 5) field[j + i][k] = field[j + i + 1][k];
rep(k, 5) field[MH - 1][k] = 0;
}
}
rep(i, MH) rep(j, 5) c += field[i][j];
cout << c << endl;
}
return 0;
} | #include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MH 5000
using namespace std;
bool ck(int *f) {
rep(i, 5) if (!f[i]) return 0;
return 1;
}
int main() {
int n;
while (cin >> n, n) {
int d, l, x, y, c = 0;
int field[MH][5] = {0};
rep(i, n) {
cin >> d >> l >> x, y = MH, x--, d--;
for (; y > 0; y--)
rep(j, (d ? 1 : l)) if (field[y - 1][x + j]) goto END;
END:
rep(j, l) field[y + d * j][x + (1 - d) * j] = 1;
rep(i, MH) while (ck(field[i])) {
rep(j, MH - i - 1) rep(k, 5) field[j + i][k] = field[j + i + 1][k];
rep(k, 5) field[MH - 1][k] = 0;
}
}
rep(i, MH) rep(j, 5) c += field[i][j];
cout << c << endl;
}
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p00178 | C++ | Runtime Error | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;
//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"
int fall(vector<vector<int>> &field, vector<vector<int>> &pino, int l) {
{}
int y = 0;
for (y = 0; y < field.size() - pino.size(); ++y) {
bool ok = true;
for (int i = 0; i < pino.size(); ++i) {
for (int j = 0; j < pino[i].size(); ++j) {
if (pino[i][j]) {
if (field[y + i + 1][l + j])
ok = false;
}
}
}
if (!ok) {
break;
}
}
for (int i = 0; i < pino.size(); ++i) {
for (int j = 0; j < pino[i].size(); ++j) {
if (pino[i][j]) {
assert(!field[y + i][l + j]);
field[y + i][l + j] = true;
}
}
}
int delline = 0;
for (int i = y + pino.size() - 1; i >= y; --i) {
while (all_of(field[i].begin(), field[i].end(),
[](const int a) { return a == 1; })) {
transform(field[i].begin(), field[i].end(), field[i].begin(),
[](const int a) { return false; });
for (int j = i - 1; j >= 0; --j) {
swap(field[j], field[j + 1]);
}
delline += 1;
}
}
return delline;
}
int main() {
while (1) {
int N;
cin >> N;
if (!N)
break;
int sum = 0;
vector<vector<int>> field(10, vector<int>(5));
for (int i = 0; i < N; ++i) {
int d, p, q;
cin >> d >> p >> q;
q--;
int w = d == 2 ? 1 : p;
int h = d == 1 ? 1 : p;
sum += p;
vector<vector<int>> pino(h, vector<int>(w, 1));
sum -= fall(field, pino, q) * 5;
}
cout << sum << endl;
}
return 0;
} | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;
//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"
int fall(vector<vector<int>> &field, vector<vector<int>> &pino, int l) {
{}
int y = 0;
for (y = 0; y < field.size() - pino.size(); ++y) {
bool ok = true;
for (int i = 0; i < pino.size(); ++i) {
for (int j = 0; j < pino[i].size(); ++j) {
if (pino[i][j]) {
if (field[y + i + 1][l + j])
ok = false;
}
}
}
if (!ok) {
break;
}
}
for (int i = 0; i < pino.size(); ++i) {
for (int j = 0; j < pino[i].size(); ++j) {
if (pino[i][j]) {
assert(!field[y + i][l + j]);
field[y + i][l + j] = true;
}
}
}
int delline = 0;
for (int i = y + pino.size() - 1; i >= y; --i) {
while (all_of(field[i].begin(), field[i].end(),
[](const int a) { return a == 1; })) {
transform(field[i].begin(), field[i].end(), field[i].begin(),
[](const int a) { return false; });
for (int j = i - 1; j >= 0; --j) {
swap(field[j], field[j + 1]);
}
delline += 1;
}
}
return delline;
}
int main() {
while (1) {
int N;
cin >> N;
if (!N)
break;
int sum = 0;
vector<vector<int>> field(5 * N + 10, vector<int>(5));
for (int i = 0; i < N; ++i) {
int d, p, q;
cin >> d >> p >> q;
q--;
int w = d == 2 ? 1 : p;
int h = d == 1 ? 1 : p;
sum += p;
vector<vector<int>> pino(h, vector<int>(w, 1));
sum -= fall(field, pino, q) * 5;
}
cout << sum << endl;
}
return 0;
} | replace | 59 | 60 | 59 | 60 | 0 | |
p00179 | C++ | Time Limit Exceeded | #include <iostream>
#include <queue>
#include <string>
using namespace std;
queue<int> q;
int t[59049];
int INF = 100000;
int encode(vector<int> vc) {
int p = 0;
for (int i = 0; i < vc.size(); i++) {
p = p * 3 + vc[i];
}
return p;
}
vector<int> decode(int p, int n) {
vector<int> vc(n);
for (int i = 0; i < n; i++) {
vc[n - i - 1] = p % 3;
p /= 3;
}
return vc;
}
int main() {
while (true) {
for (int i = 0; i < 59049; i++)
t[i] = INF;
string s;
cin >> s;
int n = s.size(), start = 0, goal = 1;
for (int i = 0; i < n; i++)
goal *= 3;
goal -= 1;
for (int i = 0; i < n; i++) {
if (s[i] == 'r')
start = start * 3 + 2;
if (s[i] == 'g')
start = start * 3 + 1;
if (s[i] == 'b')
start = start * 3;
}
q.push(start);
t[start] = 0;
int ok = -1;
while (q.size()) {
int p = q.front();
if (p == 0 || p == goal / 2 || p == goal) {
ok = p;
while (q.size())
q.pop();
break;
}
q.pop();
vector<int> vc = decode(p, n);
for (int i = 0; i < n - 1; i++) {
if (vc[i] == vc[i + 1])
continue;
vector<int> vc_c = vc;
vc_c[i] = vc_c[i + 1] = ((vc[i] + vc[i + 1]) * 2) % 3;
int hoge = encode(vc_c);
if (t[hoge] != INF)
continue;
q.push(hoge);
t[hoge] = t[p] + 1;
}
}
if (ok == -1)
cout << "NA" << endl;
else
cout << t[ok] << endl;
}
return 0;
} | #include <iostream>
#include <queue>
#include <string>
using namespace std;
queue<int> q;
int t[59049];
int INF = 100000;
int encode(vector<int> vc) {
int p = 0;
for (int i = 0; i < vc.size(); i++) {
p = p * 3 + vc[i];
}
return p;
}
vector<int> decode(int p, int n) {
vector<int> vc(n);
for (int i = 0; i < n; i++) {
vc[n - i - 1] = p % 3;
p /= 3;
}
return vc;
}
int main() {
while (true) {
for (int i = 0; i < 59049; i++)
t[i] = INF;
string s;
cin >> s;
if (s == "0")
break;
int n = s.size(), start = 0, goal = 1;
for (int i = 0; i < n; i++)
goal *= 3;
goal -= 1;
for (int i = 0; i < n; i++) {
if (s[i] == 'r')
start = start * 3 + 2;
if (s[i] == 'g')
start = start * 3 + 1;
if (s[i] == 'b')
start = start * 3;
}
q.push(start);
t[start] = 0;
int ok = -1;
while (q.size()) {
int p = q.front();
if (p == 0 || p == goal / 2 || p == goal) {
ok = p;
while (q.size())
q.pop();
break;
}
q.pop();
vector<int> vc = decode(p, n);
for (int i = 0; i < n - 1; i++) {
if (vc[i] == vc[i + 1])
continue;
vector<int> vc_c = vc;
vc_c[i] = vc_c[i + 1] = ((vc[i] + vc[i + 1]) * 2) % 3;
int hoge = encode(vc_c);
if (t[hoge] != INF)
continue;
q.push(hoge);
t[hoge] = t[p] + 1;
}
}
if (ok == -1)
cout << "NA" << endl;
else
cout << t[ok] << endl;
}
return 0;
} | insert | 28 | 28 | 28 | 30 | TLE | |
p00179 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
class Data {
public:
string bc;
int t;
Data(string bc, int t) : bc(bc), t(t) {}
Data() {}
};
bool judge(string s, int l) {
for (int i = 0; i < l - 1; ++i) {
if (s[i] != s[i + 1])
return false;
}
return true;
}
char cj(char a, char b) {
if (a == 'b' && b == 'g')
return 'r';
if (a == 'b' && b == 'r')
return 'g';
if (a == 'g' && b == 'r')
return 'b';
swap(a, b);
if (a == 'b' && b == 'g')
return 'r';
if (a == 'b' && b == 'r')
return 'g';
if (a == 'g' && b == 'r')
return 'b';
}
int main() {
string s;
while (cin >> s, s != "s") {
int l = s.length();
int res = -1;
queue<Data> Q;
set<string> visited;
Q.push(Data(s, 0));
while (!Q.empty()) {
Data t = Q.front();
Q.pop();
if (visited.count(t.bc))
continue;
if (judge(t.bc, l)) {
res = t.t;
break;
}
visited.insert(t.bc);
for (int i = 0; i < l - 1; ++i) {
if (t.bc[i] == t.bc[i + 1])
continue;
char nc = cj(t.bc[i], t.bc[i + 1]);
string nbc = t.bc;
nbc[i] = nbc[i + 1] = nc;
Q.push(Data(nbc, t.t + 1));
}
}
if (res == -1)
cout << "NA" << endl;
else
cout << res << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
class Data {
public:
string bc;
int t;
Data(string bc, int t) : bc(bc), t(t) {}
Data() {}
};
bool judge(string s, int l) {
for (int i = 0; i < l - 1; ++i) {
if (s[i] != s[i + 1])
return false;
}
return true;
}
char cj(char a, char b) {
if (a == 'b' && b == 'g')
return 'r';
if (a == 'b' && b == 'r')
return 'g';
if (a == 'g' && b == 'r')
return 'b';
swap(a, b);
if (a == 'b' && b == 'g')
return 'r';
if (a == 'b' && b == 'r')
return 'g';
if (a == 'g' && b == 'r')
return 'b';
}
int main() {
string s;
while (cin >> s, s != "0") {
int l = s.length();
int res = -1;
queue<Data> Q;
set<string> visited;
Q.push(Data(s, 0));
while (!Q.empty()) {
Data t = Q.front();
Q.pop();
if (visited.count(t.bc))
continue;
if (judge(t.bc, l)) {
res = t.t;
break;
}
visited.insert(t.bc);
for (int i = 0; i < l - 1; ++i) {
if (t.bc[i] == t.bc[i + 1])
continue;
char nc = cj(t.bc[i], t.bc[i + 1]);
string nbc = t.bc;
nbc[i] = nbc[i + 1] = nc;
Q.push(Data(nbc, t.t + 1));
}
}
if (res == -1)
cout << "NA" << endl;
else
cout << res << endl;
}
return 0;
} | replace | 37 | 38 | 37 | 38 | TLE | |
p00179 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef pair<string, int> P;
//????????????????????????????????????
bool judge(string str) {
for (int i = 0; i < str.size(); ++i) {
if (str[0] != str[i])
return false;
}
return true;
}
//??£?????????2?????????????????§?????????????????????
char color(char a, char b) {
if ((a == 'g' && b == 'r') || (a == 'r' && b == 'g'))
return 'b';
else if ((a == 'b' && b == 'r') || (a == 'r' && b == 'b'))
return 'g';
else if ((a == 'g' && b == 'b') || (a == 'b' && b == 'g'))
return 'r';
}
int solve(string s) {
queue<P> q;
map<string, int> m;
q.push(P(s, 0)); //?????\??????????????¶????????\??????
while (!q.empty()) { //?????\??????????????¶?????????????????§
P p = q.front();
q.pop();
if (m[p.first])
continue; //??????????????????
string now = p.first;
if (judge(now))
return p.second;
for (int i = 0; i < now.size() - 1; ++i) {
if (now[i] != now[i + 1]) { //??£?????????2????????°????????¨???
char tmp = color(now[i], now[i + 1]);
string D = now;
D[i] = tmp;
D[i + 1] = tmp;
q.push(P(D, p.second + 1));
}
}
}
return -1;
}
int main() {
string worm;
while (cin >> worm) {
if (worm == "0")
break;
int count = solve(worm);
if (count == -1)
cout << "NA" << endl;
else
cout << count << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef pair<string, int> P;
//????????????????????????????????????
bool judge(string str) {
for (int i = 0; i < str.size(); ++i) {
if (str[0] != str[i])
return false;
}
return true;
}
//??£?????????2?????????????????§?????????????????????
char color(char a, char b) {
if ((a == 'g' && b == 'r') || (a == 'r' && b == 'g'))
return 'b';
else if ((a == 'b' && b == 'r') || (a == 'r' && b == 'b'))
return 'g';
else if ((a == 'g' && b == 'b') || (a == 'b' && b == 'g'))
return 'r';
}
int solve(string s) {
queue<P> q;
map<string, int> m;
q.push(P(s, 0)); //?????\??????????????¶????????\??????
while (!q.empty()) { //?????\??????????????¶?????????????????§
P p = q.front();
q.pop();
if (m[p.first])
continue; //??????????????????
string now = p.first;
if (judge(now))
return p.second;
m[p.first] = p.second;
for (int i = 0; i < now.size() - 1; ++i) {
if (now[i] != now[i + 1]) { //??£?????????2????????°????????¨???
char tmp = color(now[i], now[i + 1]);
string D = now;
D[i] = tmp;
D[i + 1] = tmp;
q.push(P(D, p.second + 1));
}
}
}
return -1;
}
int main() {
string worm;
while (cin >> worm) {
if (worm == "0")
break;
int count = solve(worm);
if (count == -1)
cout << "NA" << endl;
else
cout << count << endl;
}
return 0;
} | insert | 39 | 39 | 39 | 40 | MLE | |
p00179 | C++ | Time Limit Exceeded | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define QUEUE_NUM (10192)
struct Data {
char color[15];
int cnt;
};
typedef struct {
int top, bottom;
Data data[QUEUE_NUM];
} Queue;
// QÉdataðÇÁ
void push(Queue *Q, Data data) {
Q->data[Q->bottom] = data;
Q->bottom = (Q->bottom + 1);
}
// QÌæªvfðæ¾
Data front(Queue *Q) { return Q->data[Q->top]; }
// QÌæªvfðí
void pop(Queue *Q) { Q->top = (Q->top + 1) % QUEUE_NUM; }
int check(char str[], int n) {
int i, number, tem = 1, temp;
for (i = 0; i < n; i++) {
if (str[i] == 'r') {
str[i] = '0';
} else if (str[i] == 'g') {
str[i] = '1';
} else if (str[i] == 'b') {
str[i] = '2';
} else {
str[i] = '\0';
}
}
number = atoi(str);
// puts("\n--in--");
// printf("%d ",number);
number = 0;
while (i != -1) {
temp = (str[i] - 48);
number += tem * temp;
tem *= 3;
i--;
}
// printf("%d\n",number);
// puts("--o--");
return number;
}
int main(void) {
while (1) {
int i, j, n, ans = -1;
char col[4] = {'r', 'g', 'b'}, str[16];
int temp;
bool visited[178000] = {0};
Queue Q = {0, 0, {0}};
Data bag;
scanf("%s", bag.color);
if (bag.color[0] == '0')
break;
n = strlen(bag.color);
bag.cnt = 0;
push(&Q, bag);
while (!(Q.bottom == Q.top)) {
int flg = 0, flag = 0;
Data d = front(&Q);
pop(&Q);
for (i = 0; i < n - 1; i++) {
if (d.color[i] != d.color[i + 1])
flg = 1;
}
if (flg == 0) {
ans = d.cnt;
break;
}
for (i = 0; i < n - 1; i++) {
if (d.color[i] != d.color[i + 1]) {
int used[4] = {0};
Data next;
flg = 0;
strcpy(next.color, d.color);
for (j = 0; j < 3; j++) {
if (col[j] == next.color[i])
used[j] = 1;
if (col[j] == next.color[i + 1])
used[j] = 1;
}
for (j = 0; j < 3; j++) {
if (used[j] == 0) {
next.color[i] = col[j];
next.color[i + 1] = col[j];
}
}
/**/
strcpy(str, next.color);
temp = check(str, n);
if (visited[temp] == 1) {
// printf("d¡\n");
continue;
} else {
visited[temp] = 1;
}
/**/
// printf("now%d ==
//%s\n",next.cnt,next.color); Sleep(100);
next.cnt = d.cnt + 1;
push(&Q, next);
}
}
}
if (ans == -1) {
puts("NA");
} else {
printf("%d\n", ans);
}
}
return 0;
} | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define QUEUE_NUM (20386)
struct Data {
char color[15];
int cnt;
};
typedef struct {
int top, bottom;
Data data[QUEUE_NUM];
} Queue;
// QÉdataðÇÁ
void push(Queue *Q, Data data) {
Q->data[Q->bottom] = data;
Q->bottom = (Q->bottom + 1);
}
// QÌæªvfðæ¾
Data front(Queue *Q) { return Q->data[Q->top]; }
// QÌæªvfðí
void pop(Queue *Q) { Q->top = (Q->top + 1) % QUEUE_NUM; }
int check(char str[], int n) {
int i, number, tem = 1, temp;
for (i = 0; i < n; i++) {
if (str[i] == 'r') {
str[i] = '0';
} else if (str[i] == 'g') {
str[i] = '1';
} else if (str[i] == 'b') {
str[i] = '2';
} else {
str[i] = '\0';
}
}
number = atoi(str);
// puts("\n--in--");
// printf("%d ",number);
number = 0;
while (i != -1) {
temp = (str[i] - 48);
number += tem * temp;
tem *= 3;
i--;
}
// printf("%d\n",number);
// puts("--o--");
return number;
}
int main(void) {
while (1) {
int i, j, n, ans = -1;
char col[4] = {'r', 'g', 'b'}, str[16];
int temp;
bool visited[178000] = {0};
Queue Q = {0, 0, {0}};
Data bag;
scanf("%s", bag.color);
if (bag.color[0] == '0')
break;
n = strlen(bag.color);
bag.cnt = 0;
push(&Q, bag);
while (!(Q.bottom == Q.top)) {
int flg = 0, flag = 0;
Data d = front(&Q);
pop(&Q);
for (i = 0; i < n - 1; i++) {
if (d.color[i] != d.color[i + 1])
flg = 1;
}
if (flg == 0) {
ans = d.cnt;
break;
}
for (i = 0; i < n - 1; i++) {
if (d.color[i] != d.color[i + 1]) {
int used[4] = {0};
Data next;
flg = 0;
strcpy(next.color, d.color);
for (j = 0; j < 3; j++) {
if (col[j] == next.color[i])
used[j] = 1;
if (col[j] == next.color[i + 1])
used[j] = 1;
}
for (j = 0; j < 3; j++) {
if (used[j] == 0) {
next.color[i] = col[j];
next.color[i + 1] = col[j];
}
}
/**/
strcpy(str, next.color);
temp = check(str, n);
if (visited[temp] == 1) {
// printf("d¡\n");
continue;
} else {
visited[temp] = 1;
}
/**/
// printf("now%d ==
//%s\n",next.cnt,next.color); Sleep(100);
next.cnt = d.cnt + 1;
push(&Q, next);
}
}
}
if (ans == -1) {
puts("NA");
} else {
printf("%d\n", ans);
}
}
return 0;
} | replace | 4 | 5 | 4 | 5 | TLE | |
p00180 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int n, m;
vector<set<pair<int, int>>> cost(100, set<pair<int, int>>());
int prim() {
int res = 0;
set<int> v;
v.insert(0);
while (v.size() < n) {
pair<int, int> mini(100000007, -1);
for (set<int>::iterator it = v.begin(); it != v.end(); it++) {
for (set<pair<int, int>>::iterator it2 = cost[*it].begin();
it2 != cost[*it].end(); it2++)
if (v.find((*it2).second) == v.end()) {
if (mini.first > (*it2).first)
mini = (*it2);
break;
}
}
v.insert(mini.second);
res += mini.first;
}
return res;
}
int main() {
while (1) {
cin >> n >> m;
if (n == 0 && m == 0)
break;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
cost[a].insert(pair<int, int>(c, b));
cost[b].insert(pair<int, int>(c, a));
}
cout << prim() << endl;
for (int i = 0; i < m; i++)
cost[i].clear();
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int n, m;
vector<set<pair<int, int>>> cost(200, set<pair<int, int>>());
int prim() {
int res = 0;
set<int> v;
v.insert(0);
while (v.size() < n) {
pair<int, int> mini(100000007, -1);
for (set<int>::iterator it = v.begin(); it != v.end(); it++) {
for (set<pair<int, int>>::iterator it2 = cost[*it].begin();
it2 != cost[*it].end(); it2++)
if (v.find((*it2).second) == v.end()) {
if (mini.first > (*it2).first)
mini = (*it2);
break;
}
}
v.insert(mini.second);
res += mini.first;
}
return res;
}
int main() {
while (1) {
cin >> n >> m;
if (n == 0 && m == 0)
break;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
cost[a].insert(pair<int, int>(c, b));
cost[b].insert(pair<int, int>(c, a));
}
cout << prim() << endl;
for (int i = 0; i < m; i++)
cost[i].clear();
}
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p00180 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
using namespace std;
// disjoint-set
int parent[100];
void init(int n) {
for (int i = 0; i < n; i++)
parent[i] = i;
}
int root(int a) {
if (parent[a] == a)
return a;
return (parent[a] = root(parent[a]));
}
bool same_set_p(int a, int b) { return root(a) == root(b); }
void unite(int a, int b) { parent[root(a)] = root(b); }
// graph
class Edge {
public:
int cost, a, b;
Edge(int c = 0, int d = 0, int e = 0) : cost(c), a(d), b(e) {}
bool operator<(const Edge &o) const { return cost < o.cost; }
};
// main
int n, m;
Edge graph[100];
int main() {
while (scanf("%d %d", &n, &m), n) {
for (int i = 0; i < m; i++) {
int a, b, cost;
scanf("%d %d %d", &a, &b, &cost);
graph[i] = Edge(cost, a, b);
}
sort(graph, graph + m);
int answer = 0;
init(n);
for (int i = 0; i < m; i++) {
Edge e = graph[i];
if (!same_set_p(e.a, e.b)) {
answer += e.cost;
unite(e.a, e.b);
}
}
printf("%d\n", answer);
}
} | #include <algorithm>
#include <cstdio>
using namespace std;
// disjoint-set
int parent[100];
void init(int n) {
for (int i = 0; i < n; i++)
parent[i] = i;
}
int root(int a) {
if (parent[a] == a)
return a;
return (parent[a] = root(parent[a]));
}
bool same_set_p(int a, int b) { return root(a) == root(b); }
void unite(int a, int b) { parent[root(a)] = root(b); }
// graph
class Edge {
public:
int cost, a, b;
Edge(int c = 0, int d = 0, int e = 0) : cost(c), a(d), b(e) {}
bool operator<(const Edge &o) const { return cost < o.cost; }
};
// main
int n, m;
Edge graph[10000];
int main() {
while (scanf("%d %d", &n, &m), n) {
for (int i = 0; i < m; i++) {
int a, b, cost;
scanf("%d %d %d", &a, &b, &cost);
graph[i] = Edge(cost, a, b);
}
sort(graph, graph + m);
int answer = 0;
init(n);
for (int i = 0; i < m; i++) {
Edge e = graph[i];
if (!same_set_p(e.a, e.b)) {
answer += e.cost;
unite(e.a, e.b);
}
}
printf("%d\n", answer);
}
} | replace | 33 | 34 | 33 | 34 | TLE | |
p00180 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
struct Dset {
Dset *parent;
int rank;
Dset() { clear(); }
void clear() {
parent = NULL;
rank = 0;
}
Dset *find() {
if (!parent) {
return this;
} else {
parent = parent->find();
return parent;
}
}
bool merge(Dset *a) {
Dset *myRoot = find();
Dset *yourRoot = a->find();
if (myRoot->rank < yourRoot->rank) {
myRoot->parent = yourRoot;
} else if (myRoot->rank > yourRoot->rank) {
yourRoot->parent = myRoot;
} else if (myRoot != yourRoot) {
yourRoot->parent = myRoot;
myRoot->rank++;
} else {
return false;
}
return true;
}
};
struct Bridge {
int a;
int b;
int cost;
bool operator<(const Bridge &obj) const { return (cost < obj.cost); }
};
int main() {
int n, m, ans;
Bridge bridge[6000];
Dset x[100];
while (1) {
cin >> n >> m;
if (!n && !m)
break;
for (int i = 0; i < m; i++) {
cin >> bridge[i].a >> bridge[i].b >> bridge[i].cost;
}
sort(bridge, bridge + m);
for (int i = 0; i < m; i++) {
x[i].clear();
}
ans = 0;
for (int i = 0; i < m; i++) {
if (x[bridge[i].a].merge(&x[bridge[i].b])) {
ans += bridge[i].cost;
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
struct Dset {
Dset *parent;
int rank;
Dset() { clear(); }
void clear() {
parent = NULL;
rank = 0;
}
Dset *find() {
if (!parent) {
return this;
} else {
parent = parent->find();
return parent;
}
}
bool merge(Dset *a) {
Dset *myRoot = find();
Dset *yourRoot = a->find();
if (myRoot->rank < yourRoot->rank) {
myRoot->parent = yourRoot;
} else if (myRoot->rank > yourRoot->rank) {
yourRoot->parent = myRoot;
} else if (myRoot != yourRoot) {
yourRoot->parent = myRoot;
myRoot->rank++;
} else {
return false;
}
return true;
}
};
struct Bridge {
int a;
int b;
int cost;
bool operator<(const Bridge &obj) const { return (cost < obj.cost); }
};
int main() {
int n, m, ans;
Bridge bridge[6000];
Dset x[100];
while (1) {
cin >> n >> m;
if (!n && !m)
break;
for (int i = 0; i < m; i++) {
cin >> bridge[i].a >> bridge[i].b >> bridge[i].cost;
}
sort(bridge, bridge + m);
for (int i = 0; i < n; i++) {
x[i].clear();
}
ans = 0;
for (int i = 0; i < m; i++) {
if (x[bridge[i].a].merge(&x[bridge[i].b])) {
ans += bridge[i].cost;
}
}
cout << ans << endl;
}
return 0;
} | replace | 61 | 62 | 61 | 62 | 0 | |
p00180 | C++ | Runtime Error | // kruskal tree
#include <algorithm>
#include <iostream>
using namespace std;
#define M 101
int parent[M], a[M], b[M];
pair<int, int> node[M];
int root(int a) {
if (parent[a] == a)
return a;
return parent[a] = root(parent[a]);
}
int unite(int a, int b) {
if (root(a) == root(b))
return 0;
parent[root(a)] = root(b);
return 1;
}
int main() {
int i, s, n, m;
for (; cin >> n >> m, n; cout << s << endl) {
for (i = 0; i < m; i++)
cin >> a[i] >> b[i] >> node[i].first, node[i].second = i;
sort(node, node + m);
for (i = 0; i < n; i++)
parent[i] = i;
for (s = i = 0; i < m; i++)
if (unite(a[node[i].second], b[node[i].second]))
s += node[i].first;
}
} | // kruskal tree
#include <algorithm>
#include <iostream>
using namespace std;
#define M 9999
int parent[M], a[M], b[M];
pair<int, int> node[M];
int root(int a) {
if (parent[a] == a)
return a;
return parent[a] = root(parent[a]);
}
int unite(int a, int b) {
if (root(a) == root(b))
return 0;
parent[root(a)] = root(b);
return 1;
}
int main() {
int i, s, n, m;
for (; cin >> n >> m, n; cout << s << endl) {
for (i = 0; i < m; i++)
cin >> a[i] >> b[i] >> node[i].first, node[i].second = i;
sort(node, node + m);
for (i = 0; i < n; i++)
parent[i] = i;
for (s = i = 0; i < m; i++)
if (unite(a[node[i].second], b[node[i].second]))
s += node[i].first;
}
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00180 | C++ | Time Limit Exceeded | #include <climits>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int br[100][100];
bool f[100];
int tr[100];
int n, m;
while (1) {
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
br[i][j] = -1;
tr[i] = INT_MAX;
f[i] = 0;
}
for (int k = 0; k < m; k++) {
int a, b, cost;
cin >> a >> b >> cost;
br[a][b] = cost;
br[b][a] = cost;
}
int comin = 0, r, trmin;
tr[0] = 0;
for (int k = 0; k < n; k++) {
trmin = INT_MAX;
for (int i = 0; i < n; i++)
if (f[i] == 0 && trmin > tr[i])
r = i, trmin = tr[i];
// cout << trmin<<endl;
comin += trmin;
f[r] = 1;
for (int i = 0; i < n; i++) {
if (f[i] == 0 && tr[i] > br[r][i] && br[r][i] != -1)
tr[i] = br[r][i];
}
}
cout << comin << endl;
}
return 0;
} | #include <climits>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int br[100][100];
bool f[100];
int tr[100];
int n, m;
while (1) {
cin >> n >> m;
if (n == 0 && m == 0)
break;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
br[i][j] = -1;
tr[i] = INT_MAX;
f[i] = 0;
}
for (int k = 0; k < m; k++) {
int a, b, cost;
cin >> a >> b >> cost;
br[a][b] = cost;
br[b][a] = cost;
}
int comin = 0, r, trmin;
tr[0] = 0;
for (int k = 0; k < n; k++) {
trmin = INT_MAX;
for (int i = 0; i < n; i++)
if (f[i] == 0 && trmin > tr[i])
r = i, trmin = tr[i];
// cout << trmin<<endl;
comin += trmin;
f[r] = 1;
for (int i = 0; i < n; i++) {
if (f[i] == 0 && tr[i] > br[r][i] && br[r][i] != -1)
tr[i] = br[r][i];
}
}
cout << comin << endl;
}
return 0;
} | insert | 13 | 13 | 13 | 15 | TLE | |
p00180 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
const int inf = 999999;
int prim(int a[101][101], int n) {
int r = 0, mcost[101];
bool vis[101];
fill(mcost, mcost + 101, inf);
fill(vis, vis + 101, false);
mcost[0] = 0;
while (1) {
int v = -1;
for (int i = 0; i < n; i++) {
if (!vis[i] && (v == -1 || mcost[v] > mcost[i])) {
v = i;
}
}
if (v == -1) {
break;
}
vis[v] = true;
r += mcost[v];
for (int i = 0; i < n; i++) {
mcost[i] = min(mcost[i], a[v][i]);
}
}
return r;
}
int main() {
int n, m, a[101][101], s, t, u;
while (1) {
cin >> n >> m;
if (n == 0 && m == 0) {
break;
}
fill(&a[0][0], &a[101][101], inf);
for (int i = 0; i <= n; i++) {
a[i][i] = 0;
}
while (m--) {
cin >> s >> t >> u;
a[s][t] = a[t][s] = u;
}
cout << prim(a, n) << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
const int inf = 999999;
int prim(int a[101][101], int n) {
int r = 0, mcost[101];
bool vis[101];
fill(mcost, mcost + 101, inf);
fill(vis, vis + 101, false);
mcost[0] = 0;
while (1) {
int v = -1;
for (int i = 0; i < n; i++) {
if (!vis[i] && (v == -1 || mcost[v] > mcost[i])) {
v = i;
}
}
if (v == -1) {
break;
}
vis[v] = true;
r += mcost[v];
for (int i = 0; i < n; i++) {
mcost[i] = min(mcost[i], a[v][i]);
}
}
return r;
}
int main() {
int n, m, a[101][101], s, t, u;
while (1) {
cin >> n >> m;
if (n == 0 && m == 0) {
break;
}
fill(&a[0][0], &a[100][100], inf);
for (int i = 0; i <= n; i++) {
a[i][i] = 0;
}
while (m--) {
cin >> s >> t >> u;
a[s][t] = a[t][s] = u;
}
cout << prim(a, n) << endl;
}
return 0;
} | replace | 47 | 48 | 47 | 48 | -6 | *** stack smashing detected ***: terminated
|
p00181 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <numeric>
#include <string.h>
#define INT_INF 2000000
int m, n;
int a[100 + 1];
int memo[20 + 1][100 + 1];
int search(int i, int j) {
if (memo[i][j] >= 0)
return memo[i][j];
if (i == 1)
return memo[i][j] = std::accumulate(a + j, a + n, 0);
int am = INT_INF;
for (int s = 0; j < n; s += a[j], j++)
am = std::min(std::max(search(i - 1, j), s), am);
return memo[i][j] = am;
}
int main(int argc, char **argv) {
while (1) {
std::cin >> m >> n;
if (m == 0)
break;
for (int i = 0; i < n; i++)
std::cin >> a[i];
memset(memo, -1, sizeof(memo));
std::cout << search(m, 0) << std::endl;
}
} | #include <algorithm>
#include <iostream>
#include <numeric>
#include <string.h>
#define INT_INF 2000000
int m, n;
int a[100 + 1];
int memo[20 + 1][100 + 1];
int search(int i, int j) {
if (memo[i][j] >= 0)
return memo[i][j];
if (i == 1)
return memo[i][j] = std::accumulate(a + j, a + n, 0);
int am = INT_INF;
for (int k = j, s = 0; k < n; s += a[k], k++)
am = std::min(std::max(search(i - 1, k), s), am);
return memo[i][j] = am;
}
int main(int argc, char **argv) {
while (1) {
std::cin >> m >> n;
if (m == 0)
break;
for (int i = 0; i < n; i++)
std::cin >> a[i];
memset(memo, -1, sizeof(memo));
std::cout << search(m, 0) << std::endl;
}
} | replace | 23 | 25 | 23 | 25 | TLE | |
p00181 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
using namespace std;
int n, m;
int hon[101];
int nibun() {
int R, L;
R = L = 0;
for (int i = 0; i < m; i++) {
L += hon[i];
}
while (1) {
if (R == L) {
return R;
}
double H = (double)(R + L) / 2;
int dan = 0;
int at = 0;
for (int i = 0; i < m; i++) {
if (hon[i] > H) {
dan = 10000;
break;
}
if (at + hon[i] <= H) {
at += hon[i];
} else {
at = 0;
dan++;
at += hon[i];
}
}
if (dan < n) {
L = H;
} else {
R = H + 0.5;
}
}
}
int main() {
int cont = 0;
while (1) {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d", &hon[i]);
}
printf("%d\n", nibun());
}
} | #include <algorithm>
#include <cstdio>
using namespace std;
int n, m;
int hon[101];
int nibun() {
int R, L;
R = L = 0;
for (int i = 0; i < m; i++) {
L += hon[i];
}
while (1) {
if (R == L) {
return R;
}
double H = (double)(R + L) / 2;
int dan = 0;
int at = 0;
for (int i = 0; i < m; i++) {
if (hon[i] > H) {
dan = 10000;
break;
}
if (at + hon[i] <= H) {
at += hon[i];
} else {
at = 0;
dan++;
at += hon[i];
}
}
if (dan < n) {
L = H;
} else {
R = H + 0.5;
}
}
}
int main() {
int cont = 0;
while (1) {
scanf("%d%d", &n, &m);
if (n == 0 && m == 0) {
break;
}
for (int i = 0; i < m; i++) {
scanf("%d", &hon[i]);
}
printf("%d\n", nibun());
}
} | insert | 46 | 46 | 46 | 49 | TLE | |
p00181 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int m, n, w[100];
int calc(int t) {
int sum = 0, ret = 1;
for (int i = 0; i < n; i++) {
if (w[i] > t)
return 1500001;
if (sum + w[i] > t) {
sum = 0;
ret++;
}
sum += w[i];
}
return ret;
}
int main() {
while (1) {
cin >> m >> n;
for (int i = 0; i < n; i++) {
cin >> w[i];
}
int l = 1, h = 1500000;
while (h - l > 1) {
int md = (l + h) / 2;
if (calc(md) > m)
l = md;
else
h = md;
}
cout << h << endl;
}
return 0;
} | #include <iostream>
using namespace std;
int m, n, w[100];
int calc(int t) {
int sum = 0, ret = 1;
for (int i = 0; i < n; i++) {
if (w[i] > t)
return 1500001;
if (sum + w[i] > t) {
sum = 0;
ret++;
}
sum += w[i];
}
return ret;
}
int main() {
while (1) {
cin >> m >> n;
if (m == 0 && n == 0)
break;
for (int i = 0; i < n; i++) {
cin >> w[i];
}
int l = 1, h = 1500000;
while (h - l > 1) {
int md = (l + h) / 2;
if (calc(md) > m)
l = md;
else
h = md;
}
cout << h << endl;
}
return 0;
} | insert | 23 | 23 | 23 | 25 | TLE | |
p00181 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
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 M, N;
int W[100];
bool C(int x) {
int cnt = 0;
int cur = 0;
while (cur < N) {
int next = cur;
int sum = 0;
while (next < N && sum + W[next] <= x)
sum += W[next++];
cur = next;
cnt++;
}
return cnt <= M;
}
signed main() {
while (cin >> M >> N, M || N) {
rep(i, N) cin >> W[i];
int lb = *min_element(W, W + N) - 1, ub = 1500000;
while (ub - lb > 1) {
int mid = (ub + lb) / 2;
if (C(mid))
ub = mid;
else
lb = mid;
}
cout << ub << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
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 M, N;
int W[100];
bool C(int x) {
int cnt = 0;
int cur = 0;
while (cur < N) {
int next = cur;
int sum = 0;
while (next < N && sum + W[next] <= x)
sum += W[next++];
cur = next;
cnt++;
}
return cnt <= M;
}
signed main() {
while (cin >> M >> N, M || N) {
rep(i, N) cin >> W[i];
int lb = *max_element(W, W + N) - 1, ub = 1500000;
while (ub - lb > 1) {
int mid = (ub + lb) / 2;
if (C(mid))
ub = mid;
else
lb = mid;
}
cout << ub << endl;
}
return 0;
} | replace | 45 | 46 | 45 | 46 | TLE | |
p00182 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
bool dfs(int i);
int n, a[50];
bool fil[50];
bool dfs2(int i, int j, int rem) {
if (rem == 0)
return dfs(i + 1);
if (j == n)
return false;
if (!fil[j] && a[j] <= rem) {
fil[j] = true;
if (dfs2(i, j + 1, rem - a[j]))
return true;
fil[j] = false;
}
return dfs2(i, j + 1, rem);
}
bool dfs(int i) {
if (i == n)
return true;
if (!fil[i])
return false;
return dfs2(i, i + 1, a[i]) || dfs(i + 1);
}
int main() {
for (; scanf("%d", &n), n;) {
rep(i, n) {
scanf("%d", a[i]);
fil[i] = false;
}
sort(a, a + n, greater<int>());
fil[0] = true;
puts(dfs(0) ? "YES" : "NO");
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
bool dfs(int i);
int n, a[50];
bool fil[50];
bool dfs2(int i, int j, int rem) {
if (rem == 0)
return dfs(i + 1);
if (j == n)
return false;
if (!fil[j] && a[j] <= rem) {
fil[j] = true;
if (dfs2(i, j + 1, rem - a[j]))
return true;
fil[j] = false;
}
return dfs2(i, j + 1, rem);
}
bool dfs(int i) {
if (i == n)
return true;
if (!fil[i])
return false;
return dfs2(i, i + 1, a[i]) || dfs(i + 1);
}
int main() {
for (; scanf("%d", &n), n;) {
rep(i, n) {
scanf("%d", a + i);
fil[i] = false;
}
sort(a, a + n, greater<int>());
fil[0] = true;
puts(dfs(0) ? "YES" : "NO");
}
return 0;
} | replace | 34 | 35 | 34 | 35 | -11 | |
p00182 | C++ | Time Limit Exceeded | #include <algorithm>
#include <stdio.h>
using namespace std;
int c[60];
int v[60];
int u[60];
int n;
int solve(int a, int b) {
if (b == 0) {
bool ok = true;
for (int i = 0; i < n; i++)
if (!u[i]) {
ok = false;
break;
}
if (ok)
return 1;
int at = 0;
for (int i = 0; i < n; i++) {
if (v[i]) {
v[i] = 0;
int res = solve(0, c[i]);
if (res)
return 1;
v[i] = 1;
}
}
return 0;
}
if (a == n)
return 0;
if (!u[a] && b >= c[a]) {
u[a] = v[a] = 1;
if (solve(a + 1, b - c[a]))
return 1;
u[a] = v[a] = 0;
}
int rem = 0;
for (int i = a + 1; i < n; i++)
if (b >= c[i] && !u[i])
rem += c[i];
if (rem < b)
return 0;
return solve(a + 1, b);
}
int main() {
int a;
while (scanf("%d", &a), a) {
n = a;
for (int i = 0; i < a; i++)
scanf("%d", c + i);
for (int i = 0; i < a; i++)
c[i] = -c[i];
std::sort(c, c + a);
for (int i = 0; i < a; i++)
c[i] = -c[i];
for (int i = 0; i < a; i++)
v[i] = u[i] = 0;
v[0] = u[0] = 1;
if (solve(0, 0))
printf("YES\n");
else
printf("NO\n");
}
} | #include <algorithm>
#include <stdio.h>
using namespace std;
int c[60];
int v[60];
int u[60];
int n;
int solve(int a, int b) {
if (b == 0) {
bool ok = true;
for (int i = 0; i < n; i++)
if (!u[i]) {
ok = false;
break;
}
if (ok)
return 1;
int at = 0;
for (int i = 0; i < n; i++) {
if (v[i]) {
v[i] = 0;
int res = solve(0, c[i]);
if (res)
return 1;
v[i] = 1;
}
if (!u[i])
break;
}
return 0;
}
if (a == n)
return 0;
if (!u[a] && b >= c[a]) {
u[a] = v[a] = 1;
if (solve(a + 1, b - c[a]))
return 1;
u[a] = v[a] = 0;
}
int rem = 0;
for (int i = a + 1; i < n; i++)
if (b >= c[i] && !u[i])
rem += c[i];
if (rem < b)
return 0;
return solve(a + 1, b);
}
int main() {
int a;
while (scanf("%d", &a), a) {
n = a;
for (int i = 0; i < a; i++)
scanf("%d", c + i);
for (int i = 0; i < a; i++)
c[i] = -c[i];
std::sort(c, c + a);
for (int i = 0; i < a; i++)
c[i] = -c[i];
for (int i = 0; i < a; i++)
v[i] = u[i] = 0;
v[0] = u[0] = 1;
if (solve(0, 0))
printf("YES\n");
else
printf("NO\n");
}
} | insert | 26 | 26 | 26 | 28 | TLE | |
p00182 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <set>
using namespace std;
typedef long long ll;
int n;
int lim[52], vol[52];
set<ll> no[52];
bool solve(int idx);
bool distribute(int idx, int next) {
if (idx == n) {
return false;
}
ll bit = 0;
for (int i = idx; i < n; i++) {
bit <<= 1;
if (vol[i] == lim[i]) {
bit++;
}
}
if (no[idx].find(bit) != no[idx].end()) {
return false;
}
no[idx].insert(bit);
for (int i = next; i < n; i++) {
if (vol[i] != 0)
continue;
if (vol[idx] < lim[i])
continue;
vol[idx] -= lim[i];
vol[i] = lim[i];
if (vol[idx] != 0 && distribute(idx, i + 1))
return true;
if (vol[idx] == 0 && solve(idx + 1))
return true;
vol[idx] += lim[i];
vol[i] = 0;
}
return false;
}
bool solve(int idx) {
if (idx == n)
return true;
if (vol[idx] == 0)
return false;
if (solve(idx + 1))
return true;
if (distribute(idx, idx + 1))
return true;
return false;
}
int main() {
while (cin >> n, n) {
for (int i = 0; i < n; i++) {
cin >> lim[i];
no[i].clear();
vol[i] = 0;
}
sort(lim, lim + n, greater<int>());
vol[0] = lim[0];
cout << (solve(0) ? "YES" : "NO") << endl;
}
} | #include <algorithm>
#include <iostream>
#include <set>
using namespace std;
typedef long long ll;
int n;
int lim[52], vol[52];
set<ll> no[52];
bool solve(int idx);
bool distribute(int idx, int next) {
if (idx == n) {
return false;
}
ll bit = 0;
for (int i = idx; i < n; i++) {
bit <<= 1;
if (vol[i] == lim[i]) {
bit++;
}
}
if (no[idx].find(bit) != no[idx].end()) {
return false;
}
no[idx].insert(bit);
for (int i = next; i < n; i++) {
if (vol[i] != 0)
continue;
if (vol[idx] < lim[i])
continue;
if (i - 1 != idx && lim[i - 1] == lim[i] && vol[i - 1] == 0)
continue;
vol[idx] -= lim[i];
vol[i] = lim[i];
if (vol[idx] != 0 && distribute(idx, i + 1))
return true;
if (vol[idx] == 0 && solve(idx + 1))
return true;
vol[idx] += lim[i];
vol[i] = 0;
}
return false;
}
bool solve(int idx) {
if (idx == n)
return true;
if (vol[idx] == 0)
return false;
if (solve(idx + 1))
return true;
if (distribute(idx, idx + 1))
return true;
return false;
}
int main() {
while (cin >> n, n) {
for (int i = 0; i < n; i++) {
cin >> lim[i];
no[i].clear();
vol[i] = 0;
}
sort(lim, lim + n, greater<int>());
vol[0] = lim[0];
cout << (solve(0) ? "YES" : "NO") << endl;
}
} | insert | 36 | 36 | 36 | 38 | TLE | |
p00182 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#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()
const int INF = 1 << 29;
typedef long long ll;
int a[50];
int n;
bool solve(int now, ll used, ll filled);
set<ll> st[50];
bool select(int now, int id, int vol, ll used, ll filled) {
if (vol == 0) {
if (used == (1LL << n) - 1)
return 1;
else
return solve(now + 1, used, filled);
}
if (st[now].count(filled >> now))
return 0;
st[now].insert(filled >> now);
for (int i = id; i < n; ++i) {
if (!(used >> i & 1) && vol - a[i] >= 0)
if (select(now, i + 1, vol - a[i], used | 1LL << i, filled | 1LL << i))
return 1;
}
return 0;
}
bool solve(int now, ll used, ll filled) {
for (int i = now; i < n; ++i) {
if (!(used >> i & 1))
return 0;
if ((filled >> i & 1) && select(i, i + 1, a[i], used, filled ^ 1LL << i))
return 1;
}
return 0;
}
int main() {
while (cin >> n, n) {
REP(i, n) st[i].clear();
REP(i, n) cin >> a[i];
if (n == 1) {
cout << "YES" << endl;
continue;
}
sort(a, a + n, greater<int>());
if (solve(0, 1, 1))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#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()
const int INF = 1 << 29;
typedef long long ll;
int a[50];
int n;
bool solve(int now, ll used, ll filled);
set<ll> st[50];
bool select(int now, int id, int vol, ll used, ll filled) {
if (vol == 0) {
if (used == (1LL << n) - 1)
return 1;
else
return solve(now + 1, used, filled);
}
int sum = 0;
for (int i = id; i < n; ++i)
if (!(used >> i & 1))
sum += a[i];
if (sum < vol)
return 0;
if (st[now].count(filled >> now))
return 0;
st[now].insert(filled >> now);
for (int i = id; i < n; ++i) {
if (!(used >> i & 1) && vol - a[i] >= 0)
if (select(now, i + 1, vol - a[i], used | 1LL << i, filled | 1LL << i))
return 1;
}
return 0;
}
bool solve(int now, ll used, ll filled) {
for (int i = now; i < n; ++i) {
if (!(used >> i & 1))
return 0;
if ((filled >> i & 1) && select(i, i + 1, a[i], used, filled ^ 1LL << i))
return 1;
}
return 0;
}
int main() {
while (cin >> n, n) {
REP(i, n) st[i].clear();
REP(i, n) cin >> a[i];
if (n == 1) {
cout << "YES" << endl;
continue;
}
sort(a, a + n, greater<int>());
if (solve(0, 1, 1))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
} | insert | 36 | 36 | 36 | 42 | MLE | |
p00182 | 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;
const ld eps = 1e-9;
//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"
bool getans(vector<int> &used, vector<int> &vs, int rest) {
if (rest) {
bool flag = false;
for (int i = 0; i < used.size(); ++i) {
if (used[i] == 0) {
used[i] = 2;
if (rest >= vs[i]) {
if (getans(used, vs, rest - vs[i])) {
used[i] = 0;
return true;
}
}
used[i] = 0;
// if (!flag)return false;
} else if (used[i] == 2) {
flag = true;
}
}
return false;
} else {
if (all_of(used.begin(), used.end(), [](const int n) { return n; })) {
return true;
}
bool flag = false;
for (int i = 0; i < used.size(); ++i) {
if (used[i] == 2) {
used[i] = 1;
bool ok = getans(used, vs, vs[i]);
used[i] = 2;
if (ok)
return true;
} else if (used[i] == 0) {
return false;
}
}
return false;
}
}
int main() {
while (1) {
int N;
cin >> N;
if (!N)
break;
vector<int> vs(N), used(N);
for (int i = 0; i < N; ++i) {
cin >> vs[i];
}
sort(vs.begin(), vs.end(), greater<int>());
used[0] = 2;
bool ans = getans(used, vs, 0);
if (ans)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
} | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;
//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"
bool getans(vector<int> &used, vector<int> &vs, int rest) {
if (rest) {
bool flag = false;
for (int i = 0; i < used.size(); ++i) {
if (used[i] == 0) {
used[i] = 2;
if (rest >= vs[i]) {
if (getans(used, vs, rest - vs[i])) {
used[i] = 0;
return true;
}
}
used[i] = 0;
if (!flag)
return false;
} else if (used[i] == 2) {
flag = true;
}
}
return false;
} else {
if (all_of(used.begin(), used.end(), [](const int n) { return n; })) {
return true;
}
bool flag = false;
for (int i = 0; i < used.size(); ++i) {
if (used[i] == 2) {
used[i] = 1;
bool ok = getans(used, vs, vs[i]);
used[i] = 2;
if (ok)
return true;
} else if (used[i] == 0) {
return false;
}
}
return false;
}
}
int main() {
while (1) {
int N;
cin >> N;
if (!N)
break;
vector<int> vs(N), used(N);
for (int i = 0; i < N; ++i) {
cin >> vs[i];
}
sort(vs.begin(), vs.end(), greater<int>());
used[0] = 2;
bool ans = getans(used, vs, 0);
if (ans)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
} | replace | 26 | 27 | 26 | 28 | TLE | |
p00184 | C++ | Runtime Error | #include <iostream>
using namespace std;
int judge(int);
int main() {
int n, age;
for (;;) {
int cnt[7] = {0};
cin >> n;
if (n == 0)
break;
for (int i = 0; i < n; i++) {
cin >> age;
cnt[judge(age)]++;
}
for (int i = 0; i < 7; i++)
cout << cnt[i] << endl;
}
return 0;
}
int judge(int age) { return age / 10; } | #include <iostream>
using namespace std;
int judge(int);
int main() {
int n, age;
for (;;) {
int cnt[7] = {0};
cin >> n;
if (n == 0)
break;
for (int i = 0; i < n; i++) {
cin >> age;
cnt[judge(age)]++;
}
for (int i = 0; i < 7; i++)
cout << cnt[i] << endl;
}
return 0;
}
int judge(int age) { return min(age / 10, 6); } | replace | 26 | 27 | 26 | 27 | 0 | |
p00185 | C++ | Runtime Error | #define SIZE 100001
#include <iostream>
using namespace std;
int main() {
int a, d, n;
bool prime[SIZE];
for (int i = 0; i < SIZE; i++) {
prime[i] = 1;
}
prime[0] = prime[1] = 0;
for (int i = 2; i < SIZE; i++) {
if (prime[i]) {
for (int j = 2; j * i < SIZE; j++) {
prime[j * i] = 0;
}
}
}
int N;
while (cin >> N, N) {
int cnt = 0;
for (int i = 0; i <= N / 2; i++) {
if (prime[i] && prime[N - i]) {
cnt++;
}
}
cout << cnt << endl;
}
} | #define SIZE 1000001
#include <iostream>
using namespace std;
int main() {
int a, d, n;
bool prime[SIZE];
for (int i = 0; i < SIZE; i++) {
prime[i] = 1;
}
prime[0] = prime[1] = 0;
for (int i = 2; i < SIZE; i++) {
if (prime[i]) {
for (int j = 2; j * i < SIZE; j++) {
prime[j * i] = 0;
}
}
}
int N;
while (cin >> N, N) {
int cnt = 0;
for (int i = 0; i <= N / 2; i++) {
if (prime[i] && prime[N - i]) {
cnt++;
}
}
cout << cnt << endl;
}
} | replace | 0 | 1 | 0 | 1 | 0 | |
p00185 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
bool prime[1000001] = {true, true};
vector<int> ps;
for (int i = 4; i <= 1000000; i += 2)
prime[i] = true;
for (int i = 3; i * i <= 1000000; i += 2) {
for (int j = i * 2; j <= 1000000; j += i)
prime[j] = true;
}
for (int i = 0; i <= 1000000; i++)
if (!prime[i])
ps.push_back(i);
int n;
while (cin >> n, n) {
int cnt = 0;
for (int i = 0; i < ps.size(); i++) {
if (ps[i] > n / 2)
break;
if (find(ps.begin(), ps.end(), n - ps[i]) != ps.end())
cnt++;
}
cout << cnt << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
bool prime[1000001] = {true, true};
vector<int> ps;
for (int i = 4; i <= 1000000; i += 2)
prime[i] = true;
for (int i = 3; i * i <= 1000000; i += 2) {
for (int j = i * 2; j <= 1000000; j += i)
prime[j] = true;
}
for (int i = 0; i <= 1000000; i++)
if (!prime[i])
ps.push_back(i);
int n;
while (cin >> n, n) {
int cnt = 0;
for (int i = 0; i < ps.size(); i++) {
if (ps[i] > n / 2)
break;
if (upper_bound(ps.begin(), ps.end(), n - ps[i]) -
lower_bound(ps.begin(), ps.end(), n - ps[i]))
cnt++;
}
cout << cnt << endl;
}
return 0;
} | replace | 23 | 24 | 23 | 25 | TLE | |
p00185 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
bool prime[1000010];
void sieve() {
for (int i = 1; i <= 1000000; i++) {
prime[i] = true;
}
prime[1] = false;
for (int i = 2; i * i <= 1000000; i++) {
if (prime[i]) {
for (int j = i * i; j <= 1000000; j += i) {
prime[j] = false;
}
}
}
}
int main() {
int n;
sieve();
while (cin >> n, n) {
int ans = 0;
for (int i = 2; i <= n / 2; i++) {
if (!prime[i])
continue;
for (int j = i; i + j <= n; j++) {
if (!prime[j])
continue;
if (i + j == n) {
ans++;
// cout <<i<<" "<<j<<endl;
}
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
bool prime[1000010];
void sieve() {
for (int i = 1; i <= 1000000; i++) {
prime[i] = true;
}
prime[1] = false;
for (int i = 2; i * i <= 1000000; i++) {
if (prime[i]) {
for (int j = i * i; j <= 1000000; j += i) {
prime[j] = false;
}
}
}
}
int main() {
int n;
sieve();
while (cin >> n, n) {
int ans = 0;
for (int i = 2; i <= n / 2; i++) {
if (prime[i] && prime[n - i]) {
ans++;
}
}
cout << ans << endl;
}
return 0;
} | replace | 58 | 67 | 58 | 60 | TLE | |
p00185 | C++ | Runtime Error | #include <iostream>
using namespace std;
#define MAX 50100
int prime[MAX - 4];
void is_prime() {
for (int i = 0; i < MAX; i++)
prime[i] = 0;
prime[0] = prime[1] = 1;
for (int i = 4; i < MAX; i += 2)
prime[i] = 1;
for (int i = 3; i * i < MAX; i++) {
if (prime[i])
continue;
for (int j = i * 2; j < MAX; j += i)
prime[j] = 1;
}
}
int main() {
int n;
is_prime();
while (true) {
cin >> n;
if (!n)
break;
int cnt = 0;
for (int i = 2; i <= n / 2; i++) {
if (!prime[i] && !prime[n - i])
cnt++;
}
cout << cnt << endl;
}
return 0;
} | #include <iostream>
using namespace std;
#define MAX 1000100
int prime[MAX - 4];
void is_prime() {
for (int i = 0; i < MAX; i++)
prime[i] = 0;
prime[0] = prime[1] = 1;
for (int i = 4; i < MAX; i += 2)
prime[i] = 1;
for (int i = 3; i * i < MAX; i++) {
if (prime[i])
continue;
for (int j = i * 2; j < MAX; j += i)
prime[j] = 1;
}
}
int main() {
int n;
is_prime();
while (true) {
cin >> n;
if (!n)
break;
int cnt = 0;
for (int i = 2; i <= n / 2; i++) {
if (!prime[i] && !prime[n - i])
cnt++;
}
cout << cnt << endl;
}
return 0;
} | replace | 2 | 3 | 2 | 3 | -11 | |
p00185 | C++ | Time Limit Exceeded | #include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int p[5000000];
bool prime(int x) {
if (x == 2)
return false;
if (x < 1 || x % 2 == 0)
return false;
int i = 3;
while (i <= sqrt(x)) {
if (x % i == 0)
return false;
i += 2;
}
return true;
}
int main() {
int n, ans;
int j = 0;
for (int i = 3; i <= 1000000; i++)
if (prime(i))
p[j++] = i;
while (1) {
ans = 0;
cin >> n;
if (n == 0)
break;
for (int i = 0; p[i] < n; i++) {
for (int j = i; p[j] < n; j++) {
if (n == (p[i] + p[j])) {
ans++;
break;
}
}
}
cout << ans << endl;
}
return 0;
} | #include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int p[5000000];
bool prime(int x) {
if (x == 2)
return false;
if (x < 1 || x % 2 == 0)
return false;
int i = 3;
while (i <= sqrt(x)) {
if (x % i == 0)
return false;
i += 2;
}
return true;
}
int main() {
int n, ans;
int j = 0;
for (int i = 3; i <= 1000000; i++)
if (prime(i))
p[j++] = i;
while (1) {
ans = 0;
cin >> n;
if (n == 0)
break;
for (int i = 0; p[i] <= (n + 1) / 2; i++) {
if (prime(n - p[i]))
ans++;
}
cout << ans << endl;
}
return 0;
} | replace | 35 | 43 | 35 | 38 | TLE | |
p00185 | C++ | Runtime Error | #include <iostream>
using namespace std;
#define MN 500001
bool pr[MN] = {0};
int main() {
for (int i = 2; i < MN; i++) {
pr[i] = 1;
}
for (int i = 2; i * i < MN; i++) {
if (pr[i])
for (int j = i * 2; j < MN; j += i) {
pr[j] = 0;
}
}
int n, c;
while (cin >> n, n) {
c = 0;
for (int i = 3; i < n / 2 + 1; i++) {
if (pr[i] && pr[n - i])
c++;
}
cout << c << endl;
}
} | #include <iostream>
using namespace std;
#define MN 1000001
bool pr[MN] = {0};
int main() {
for (int i = 2; i < MN; i++) {
pr[i] = 1;
}
for (int i = 2; i * i < MN; i++) {
if (pr[i])
for (int j = i * 2; j < MN; j += i) {
pr[j] = 0;
}
}
int n, c;
while (cin >> n, n) {
c = 0;
for (int i = 3; i < n / 2 + 1; i++) {
if (pr[i] && pr[n - i])
c++;
}
cout << c << endl;
}
} | replace | 3 | 4 | 3 | 4 | 0 | |
p00185 | C++ | Runtime Error | /*1000000ȺÌfÅü͵½nȺÌÅåÌfðoÍ·évO*/
#include <iostream>
const int MAX = 1000001;
const int P_NUM = 78498;
using namespace std;
bool p[MAX];
int a[79000] = {};
void f() {
for (int i = 0; i < MAX; i++) {
p[i] = (i <= 1) ? false : true;
}
for (int i = 2; i * i <= MAX + 1; i++) {
if (p[i]) {
for (int j = i * 2; j < MAX; j += i) {
p[j] = false;
}
}
}
for (int i = 2, j = 0; i < MAX; ++i) {
if (p[i])
a[j++] = i;
}
}
int main() {
int n;
f();
while (cin >> n, n) {
int ans = 0;
int pos;
for (pos = 0; n > a[pos]; ++pos)
;
for (int i = 0; i <= pos; ++i) {
int x = n - a[i];
if (p[x] && x >= a[i])
ans++; /*
for(int j=pos ; j>=i ; --j ){
if( a[i]+a[j] == n ){
ans++;
continue;
}
if( a[i]+a[j] < n ){
continue;
}
}*/
}
cout << ans << endl;
}
} | /*1000000ȺÌfÅü͵½nȺÌÅåÌfðoÍ·évO*/
#include <iostream>
const int MAX = 1000001;
const int P_NUM = 78498;
using namespace std;
bool p[MAX];
int a[79000] = {};
void f() {
for (int i = 0; i < MAX; i++) {
p[i] = (i <= 1) ? false : true;
}
for (int i = 2; i * i <= MAX + 1; i++) {
if (p[i]) {
for (int j = i * 2; j < MAX; j += i) {
p[j] = false;
}
}
}
for (int i = 2, j = 0; i < MAX; ++i) {
if (p[i])
a[j++] = i;
}
}
int main() {
int n;
f();
while (cin >> n, n) {
int ans = 0;
for (int i = 0; i < n; ++i)
if (p[i] && p[n - i] && (n - i) >= i)
ans++;
cout << ans << endl;
}
} | replace | 33 | 51 | 33 | 36 | 0 | |
p00185 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int prime[200];
int main() {
int count = 0;
for (int i = 3; i < 1000; i += 2) {
bool t = true;
for (int j = 0; j < count; j++)
if (i % prime[j] == 0)
t = false;
if (t == true) {
prime[count] = i;
count++;
}
}
int n;
while (true) {
cin >> n;
n /= 2;
int ans = 0, k = n * 2 - 3;
bool t;
for (int i = 3; i <= n; i += 2) {
t = true;
for (int j = 0; j < count; j++) {
if (i % prime[j] == 0) {
if (i != prime[j]) {
t = false;
break;
}
}
if (k % prime[j] == 0) {
if (k != prime[j]) {
t = false;
break;
}
}
}
if (t)
ans++;
k -= 2;
}
cout << ans << endl;
}
return 0;
} | #include <iostream>
using namespace std;
int prime[200];
int main() {
int count = 0;
for (int i = 3; i < 1000; i += 2) {
bool t = true;
for (int j = 0; j < count; j++)
if (i % prime[j] == 0)
t = false;
if (t == true) {
prime[count] = i;
count++;
}
}
int n;
while (true) {
cin >> n;
if (n == 0)
break;
n /= 2;
int ans = 0, k = n * 2 - 3;
bool t;
for (int i = 3; i <= n; i += 2) {
t = true;
for (int j = 0; j < count; j++) {
if (i % prime[j] == 0) {
if (i != prime[j]) {
t = false;
break;
}
}
if (k % prime[j] == 0) {
if (k != prime[j]) {
t = false;
break;
}
}
}
if (t)
ans++;
k -= 2;
}
cout << ans << endl;
}
return 0;
} | insert | 18 | 18 | 18 | 20 | TLE | |
p00185 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define CLR(a) memset(a, 0, sizeof(a))
typedef long long int ll;
using namespace std;
int main() {
vector<int> primes;
primes.PB(2);
for (int i = 3; i <= 1e7; i++) {
bool flg = true;
for (int j = 0; primes[j] * primes[j] <= i; j++) {
if (i % primes[j] == 0) {
flg = false;
break;
}
}
if (flg)
primes.PB(i);
}
while (true) {
int n;
cin >> n;
if (n == 0)
break;
int ans = 0;
for (int i = 0; primes[i] <= n / 2; i++) {
int j = lower_bound(ALL(primes), n - primes[i]) - primes.begin();
if (primes[j] + primes[i] == n) {
ans++;
}
}
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define CLR(a) memset(a, 0, sizeof(a))
typedef long long int ll;
using namespace std;
int main() {
vector<int> primes;
primes.PB(2);
for (int i = 3; i <= 999991; i++) {
bool flg = true;
for (int j = 0; primes[j] * primes[j] <= i; j++) {
if (i % primes[j] == 0) {
flg = false;
break;
}
}
if (flg)
primes.PB(i);
}
while (true) {
int n;
cin >> n;
if (n == 0)
break;
int ans = 0;
for (int i = 0; primes[i] <= n / 2; i++) {
int j = lower_bound(ALL(primes), n - primes[i]) - primes.begin();
if (primes[j] + primes[i] == n) {
ans++;
}
}
cout << ans << endl;
}
return 0;
} | replace | 18 | 19 | 18 | 19 | TLE | |
p00186 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
using namespace std;
void failed() { cout << "NA" << endl; }
int main() {
int Q1, B, C1, C2, Q2;
while (cin >> Q1) {
if (Q1 == 0)
break;
cin >> B >> C1 >> C2 >> Q2;
assert(Q2 >= 1);
/*
if(B/C1 == 0) {
failed(); continue;
}
*/
int AizuCh = min(B / C1, Q2);
if (AizuCh <= 0) {
failed();
continue;
}
int NCh = 0;
for (;;) {
NCh = (B - AizuCh * C1) / C2;
/*
if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
NCh = (B - AizuCh*C1)/C2;
}
*/
if (AizuCh + NCh >= Q1) {
cout << AizuCh << ' ' << NCh << endl;
break;
}
/*
else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
NCh ++, AizuCh --;
}
*/
else
AizuCh--;
if (AizuCh <= 0) {
failed();
break;
}
}
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
using namespace std;
void failed() { cout << "NA" << endl; }
int main() {
int Q1, B, C1, C2, Q2;
while (cin >> Q1) {
if (Q1 == 0)
break;
cin >> B >> C1 >> C2 >> Q2;
assert(Q2 >= 0);
/*
if(B/C1 == 0) {
failed(); continue;
}
*/
int AizuCh = min(B / C1, Q2);
if (AizuCh <= 0) {
failed();
continue;
}
int NCh = 0;
for (;;) {
NCh = (B - AizuCh * C1) / C2;
/*
if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
NCh = (B - AizuCh*C1)/C2;
}
*/
if (AizuCh + NCh >= Q1) {
cout << AizuCh << ' ' << NCh << endl;
break;
}
/*
else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
NCh ++, AizuCh --;
}
*/
else
AizuCh--;
if (AizuCh <= 0) {
failed();
break;
}
}
}
return 0;
} | replace | 20 | 21 | 20 | 21 | 0 | |
p00186 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <sstream>
using namespace std;
void failed() { cout << "NA" << endl; }
int main() {
int Q1, B, C1, C2, Q2;
while (cin >> Q1) {
if (Q1 == 0)
break;
cin >> B >> C1 >> C2 >> Q2;
stringstream ss;
ss << "Q: " << Q1 << " B: " << B << " C1: " << C1 << " C2: " << C2
<< " Q2: " << Q2;
assert(Q2 >= 1 && ss.str().c_str());
/*
if(B/C1 == 0) {
failed(); continue;
}
*/
int AizuCh = min(B / C1, Q2);
if (AizuCh <= 0) {
failed();
continue;
}
int NCh = 0;
for (;;) {
NCh = (B - AizuCh * C1) / C2;
/*
if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
NCh = (B - AizuCh*C1)/C2;
}
*/
if (AizuCh + NCh >= Q1) {
cout << AizuCh << ' ' << NCh << endl;
break;
}
/*
else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
NCh ++, AizuCh --;
}
*/
else
AizuCh--;
if (AizuCh <= 0) {
failed();
break;
}
}
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <sstream>
using namespace std;
void failed() { cout << "NA" << endl; }
int main() {
int Q1, B, C1, C2, Q2;
while (cin >> Q1) {
if (Q1 == 0)
break;
cin >> B >> C1 >> C2 >> Q2;
assert(Q2 >= 0);
/*
if(B/C1 == 0) {
failed(); continue;
}
*/
int AizuCh = min(B / C1, Q2);
if (AizuCh <= 0) {
failed();
continue;
}
int NCh = 0;
for (;;) {
NCh = (B - AizuCh * C1) / C2;
/*
if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
NCh = (B - AizuCh*C1)/C2;
}
*/
if (AizuCh + NCh >= Q1) {
cout << AizuCh << ' ' << NCh << endl;
break;
}
/*
else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
NCh ++, AizuCh --;
}
*/
else
AizuCh--;
if (AizuCh <= 0) {
failed();
break;
}
}
}
return 0;
} | replace | 21 | 26 | 21 | 22 | 0 | |
p00186 | C++ | Time Limit Exceeded | #include <cstdio>
int q1, b, c1, c2, q2;
int main() {
while (scanf("%d", &q1)) {
if (q1 == 0)
break;
scanf("%d%d%d%d", &b, &c1, &c2, &q2);
int jidori = b / c1;
jidori = (jidori < q2 ? jidori : q2);
b -= jidori * c1;
int normal = b / c2;
b -= normal * c2;
while (jidori + normal < q1) {
jidori--;
b += c1;
int temp = b / c2;
normal += temp;
b -= c2 * temp;
}
if (jidori <= 0)
puts("NA");
else
printf("%d %d\n", jidori, normal);
}
} | #include <cstdio>
int q1, b, c1, c2, q2;
int main() {
while (scanf("%d", &q1)) {
if (q1 == 0)
break;
scanf("%d%d%d%d", &b, &c1, &c2, &q2);
int jidori = b / c1;
jidori = (jidori < q2 ? jidori : q2);
b -= jidori * c1;
int normal = b / c2;
b -= normal * c2;
while (jidori + normal < q1) {
if (jidori <= 0)
break;
jidori--;
b += c1;
int temp = b / c2;
normal += temp;
b -= c2 * temp;
}
if (jidori <= 0)
puts("NA");
else
printf("%d %d\n", jidori, normal);
}
} | insert | 13 | 13 | 13 | 15 | TLE | |
p00188 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;
int main() {
while (true) {
int n;
cin >> n;
if (n == 0)
break;
vector<int> vec;
rep(i, n) {
int in;
cin >> in;
vec.pb(in);
}
int key, cnt = 0;
cin >> key;
int l, r, m;
l = 0, r = n - 1;
while (l <= r) {
m = (l + r) / 2;
cnt++;
if (vec[m] == key)
break;
if (vec[m] > key)
r = m;
else
l = m + 1;
}
cout << cnt << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;
int main() {
while (true) {
int n;
cin >> n;
if (n == 0)
break;
vector<int> vec;
rep(i, n) {
int in;
cin >> in;
vec.pb(in);
}
int key, cnt = 0;
cin >> key;
int l, r, m;
l = 0, r = n - 1;
while (l <= r) {
m = (l + r) / 2;
cnt++;
if (vec[m] == key)
break;
if (vec[m] > key)
r = m - 1;
else
l = m + 1;
}
cout << cnt << endl;
}
return 0;
} | replace | 60 | 61 | 60 | 61 | TLE | |
p00188 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
// #define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o) \
{ cerr << #o << " " << o << endl; }
#define dumpc(o) \
{ \
cerr << #o; \
for (auto &e : (o)) \
cerr << " " << e; \
cerr << endl; \
}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;
signed main() {
for (int n; cin >> n && n;) {
vector<int> a(n);
rep(i, 0, n) cin >> a[i];
int s;
cin >> s;
int l = 0, r = n - 1;
int cnt = 0;
while (true) {
cnt++;
if (r - l == 0)
break;
int m = (l + r) / 2;
// dump(m); dump(l); dump(r);
if (a[m] > s)
r = m - 1;
else if (a[m] < s)
l = m + 1;
else
break;
}
cout << cnt << endl;
}
return 0;
} | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
// #define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o) \
{ cerr << #o << " " << o << endl; }
#define dumpc(o) \
{ \
cerr << #o; \
for (auto &e : (o)) \
cerr << " " << e; \
cerr << endl; \
}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;
signed main() {
for (int n; cin >> n && n;) {
vector<int> a(n);
rep(i, 0, n) cin >> a[i];
int s;
cin >> s;
int l = 0, r = n - 1;
int cnt = 0;
while (true) {
if (r - l < 0)
break;
cnt++;
if (r - l == 0)
break;
int m = (l + r) / 2;
// dump(m); dump(l); dump(r);
if (a[m] > s)
r = m - 1;
else if (a[m] < s)
l = m + 1;
else
break;
}
cout << cnt << endl;
}
return 0;
} | insert | 54 | 54 | 54 | 56 | TLE | |
p00188 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
while (cin >> n) {
if (n == 0) {
break;
}
vector<long long int> data(n);
int left = 0;
int right = n - 1;
for (int i = 0; i < n; i++) {
cin >> data[i];
}
int counter = 0;
int tansa;
cin >> tansa;
while (true) {
int half = (left + right) / 2;
counter++;
if (left == right) {
break;
}
if (data[half] == tansa) {
break;
} else if (data[half] < tansa) {
left = half + 1;
} else {
right = half - 1;
}
}
cout << counter << endl;
}
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
while (cin >> n) {
if (n == 0) {
break;
}
vector<long long int> data(n);
int left = 0;
int right = n - 1;
for (int i = 0; i < n; i++) {
cin >> data[i];
}
int counter = 0;
int tansa;
cin >> tansa;
while (true) {
int half = (left + right) / 2;
counter++;
if (left == right) {
break;
}
if (data[half] == tansa) {
break;
} else if (data[half] < tansa) {
left = half + 1;
} else {
right = half - 1;
}
if (left > right) {
break;
}
}
cout << counter << endl;
}
return 0;
} | insert | 33 | 33 | 33 | 36 | TLE | |
p00188 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
int hi, lo, m, n, a, i, k, d[100];
while (cin >> n && n != 0) {
for (i = 0; i < n; i++)
cin >> d[i];
cin >> a;
hi = n - 1;
lo = k = 0;
while (true) {
k++;
m = (hi + lo) / 2;
if (d[m] < a)
lo = m + 1;
else if (d[m] > a)
hi = m - 1;
else
break;
if (hi == lo) {
k++;
break;
}
}
cout << k << endl;
}
return 0;
} | #include <iostream>
using namespace std;
int main() {
int hi, lo, m, n, a, i, k, d[100];
while (cin >> n && n != 0) {
for (i = 0; i < n; i++)
cin >> d[i];
cin >> a;
hi = n - 1;
lo = k = 0;
while (hi >= lo) {
k++;
m = (hi + lo) / 2;
if (d[m] < a)
lo = m + 1;
else if (d[m] > a)
hi = m - 1;
else
break;
if (hi == lo) {
k++;
break;
}
}
cout << k << endl;
}
return 0;
} | replace | 11 | 12 | 11 | 12 | TLE | |
p00188 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
std::vector<int> v;
int main() {
int n;
while (std::cin >> n, n) {
v.clear();
for (int i = 0; i != n; ++i) {
int a;
std::cin >> a;
v.push_back(a);
}
int b;
std::cin >> b;
int ans = 0;
int l = 0, r = v.size() - 1;
while (l <= r) {
int mid = (l + r) / 2;
++ans;
if (r <= l)
break;
if (v[mid] < b)
l = mid + 1;
else if (v[mid] > b)
r = mid - 1;
}
std::cout << ans << std::endl;
}
return 0;
} | #include <iostream>
#include <vector>
std::vector<int> v;
int main() {
int n;
while (std::cin >> n, n) {
v.clear();
for (int i = 0; i != n; ++i) {
int a;
std::cin >> a;
v.push_back(a);
}
int b;
std::cin >> b;
int ans = 0;
int l = 0, r = v.size() - 1;
while (l <= r) {
int mid = (l + r) / 2;
++ans;
if (r <= l)
break;
if (v[mid] < b)
l = mid + 1;
else if (v[mid] > b)
r = mid - 1;
else
break;
}
std::cout << ans << std::endl;
}
return 0;
} | insert | 33 | 33 | 33 | 35 | TLE | |
p00188 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <sstream>
#include <vector>
using namespace std;
int a[100000];
int lef;
int rig;
int ans;
bool binary_search_2(int num) {
if (lef == rig) {
ans++;
return false;
}
int middle = (lef + rig) / 2;
ans++;
if (a[middle] > num) {
rig = middle - 1;
binary_search_2(num);
} else if (a[middle] < num) {
lef = middle + 1;
binary_search_2(num);
} else if (a[middle] == num) {
return true;
}
}
bool binary_search(int num, int n) {
lef = 0;
rig = n - 1;
binary_search_2(num);
}
int main() {
int n, k;
while (true) {
cin >> n;
if (n == 0) {
break;
}
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cin >> k;
ans = 0;
binary_search(k, n);
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <sstream>
#include <vector>
using namespace std;
int a[100000];
int lef;
int rig;
int ans;
bool binary_search_2(int num) {
if (lef > rig) {
return false;
}
int middle = (lef + rig) / 2;
ans++;
if (a[middle] > num) {
rig = middle - 1;
binary_search_2(num);
} else if (a[middle] < num) {
lef = middle + 1;
binary_search_2(num);
} else if (a[middle] == num) {
return true;
}
}
bool binary_search(int num, int n) {
lef = 0;
rig = n - 1;
binary_search_2(num);
}
int main() {
int n, k;
while (true) {
cin >> n;
if (n == 0) {
break;
}
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cin >> k;
ans = 0;
binary_search(k, n);
cout << ans << endl;
}
return 0;
} | replace | 17 | 19 | 17 | 18 | TLE | |
p00188 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
int n;
while (cin >> n, n) {
int a[100], k;
for (int i = 0; i < n; i++)
cin >> a[i];
cin >> k;
int low = 0, up = n - 1, cnt = 0;
while (1) {
cnt++;
if (up == low)
break;
int mid = (up + low) / 2;
if (a[mid] == k)
break;
if (a[mid] < k)
low = mid + 1;
else
up = mid - 1;
}
cout << cnt << endl;
}
return 0;
} | #include <iostream>
using namespace std;
int main() {
int n;
while (cin >> n, n) {
int a[100], k;
for (int i = 0; i < n; i++)
cin >> a[i];
cin >> k;
int low = 0, up = n - 1, cnt = 0;
while (1) {
cnt++;
if (up == low)
break;
int mid = (up + low) / 2;
if (a[mid] == k)
break;
if (a[mid] < k)
low = mid + 1;
else
up = mid - 1;
if (up < low)
break;
}
cout << cnt << endl;
}
return 0;
} | insert | 22 | 22 | 22 | 24 | TLE | |
p00189 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#define N 1000015
#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;
bool vis[10];
int dis[10];
int g[10][10];
int inf = 0x3f3f3f3f;
int n, sum;
void Dijkstra(int x) {
mem(vis, false);
mem(dis, inf);
dis[x] = 0;
// dis[1] = inf;
for (int i = 0; i <= sum; ++i) {
int MIN = inf, u = -1;
for (int j = 0; j <= sum; ++j) {
if (vis[j])
continue;
if (dis[j] < MIN) {
MIN = dis[j];
u = j;
}
}
if (u == -1)
return;
vis[u] = true;
for (int j = 0; j <= sum; ++j) {
if (vis[j])
continue;
if (dis[j] > g[u][j] + MIN) {
dis[j] = g[u][j] + MIN;
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
while (cin >> n, n) {
int s, e, c;
mem(g, inf);
sum = -1;
for (int i = 0; i < n; ++i) {
cin >> s >> e >> c;
sum = max(s, sum);
sum = max(e, sum);
g[s][e] = g[e][s] = min(g[s][e], c);
}
int ans_n, ans_v = inf;
for (int i = 0; i <= sum; ++i) {
Dijkstra(i);
int sum_temp = 0;
for (int j = 0; j <= sum; ++j) {
if (dis[j] == inf) {
sum_temp = inf;
break;
}
sum_temp += dis[j];
}
if (sum_temp < ans_v) {
ans_v = sum_temp;
ans_n = i;
}
}
cout << ans_n << " " << ans_v << endl;
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#define N 1000015
#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;
bool vis[10];
int dis[10];
int g[10][10];
int inf = 0x3f3f3f3f;
int n, sum;
void Dijkstra(int x) {
mem(vis, false);
mem(dis, inf);
dis[x] = 0;
// dis[1] = inf;
for (int i = 0; i <= sum; ++i) {
int MIN = inf, u = -1;
for (int j = 0; j <= sum; ++j) {
if (vis[j])
continue;
if (dis[j] < MIN) {
MIN = dis[j];
u = j;
}
}
if (u == -1)
return;
vis[u] = true;
for (int j = 0; j <= sum; ++j) {
if (vis[j])
continue;
if (dis[j] > g[u][j] + MIN) {
dis[j] = g[u][j] + MIN;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
while (cin >> n, n) {
int s, e, c;
mem(g, inf);
sum = -1;
for (int i = 0; i < n; ++i) {
cin >> s >> e >> c;
sum = max(s, sum);
sum = max(e, sum);
g[s][e] = g[e][s] = min(g[s][e], c);
}
int ans_n, ans_v = inf;
for (int i = 0; i <= sum; ++i) {
Dijkstra(i);
int sum_temp = 0;
for (int j = 0; j <= sum; ++j) {
if (dis[j] == inf) {
sum_temp = inf;
break;
}
sum_temp += dis[j];
}
if (sum_temp < ans_v) {
ans_v = sum_temp;
ans_n = i;
}
}
cout << ans_n << " " << ans_v << endl;
}
return 0;
}
| replace | 48 | 51 | 48 | 49 | 0 | |
p00189 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define INF (1 << 20)
typedef pair<int, int> P;
vector<P> edge[10];
int dijkstre(int pos) {
int min_cost[10];
fill_n(min_cost, 10, INF);
priority_queue<P, vector<P>, greater<P>> que;
que.push(P(0, pos));
while (!que.empty()) {
P p = que.top();
que.pop();
int cost = p.first;
int now = p.second;
if (min_cost[now] < cost)
continue;
min_cost[now] = cost;
for (int i = 0; i < edge[now].size(); i++) {
int nc = edge[now][i].second;
int next = edge[now][i].first;
que.push(P(cost + nc, next));
}
}
int sum = 0;
for (int i = 0; i < 10; i++) {
if (min_cost[i] != INF) {
sum += min_cost[i];
}
}
return sum;
}
int main() {
int n;
while (1) {
cin >> n;
if (n == 0)
break;
int l = 100, r = 0;
for (int i = 0; i < 10; i++) {
edge[i].clear();
}
for (int i = 0; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
edge[a].push_back(P(b, c));
edge[b].push_back(P(a, c));
l = min(min(l, a), b);
r = max(max(r, a), b);
}
int ans = INF;
int pos = l;
for (int i = l; i <= r; i++) {
int tmp = dijkstre(i);
if (ans > tmp) {
pos = i;
ans = tmp;
}
}
cout << pos << " " << ans << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define INF (1 << 20)
typedef pair<int, int> P;
vector<P> edge[10];
int dijkstre(int pos) {
int min_cost[10];
fill_n(min_cost, 10, INF);
priority_queue<P, vector<P>, greater<P>> que;
que.push(P(0, pos));
while (!que.empty()) {
P p = que.top();
que.pop();
int cost = p.first;
int now = p.second;
if (min_cost[now] < cost)
continue;
min_cost[now] = cost;
for (int i = 0; i < edge[now].size(); i++) {
int nc = edge[now][i].second;
int next = edge[now][i].first;
if (cost + nc < min_cost[next]) {
que.push(P(cost + nc, next));
}
}
}
int sum = 0;
for (int i = 0; i < 10; i++) {
if (min_cost[i] != INF) {
sum += min_cost[i];
}
}
return sum;
}
int main() {
int n;
while (1) {
cin >> n;
if (n == 0)
break;
int l = 100, r = 0;
for (int i = 0; i < 10; i++) {
edge[i].clear();
}
for (int i = 0; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
edge[a].push_back(P(b, c));
edge[b].push_back(P(a, c));
l = min(min(l, a), b);
r = max(max(r, a), b);
}
int ans = INF;
int pos = l;
for (int i = l; i <= r; i++) {
int tmp = dijkstre(i);
if (ans > tmp) {
pos = i;
ans = tmp;
}
}
cout << pos << " " << ans << endl;
}
} | replace | 23 | 24 | 23 | 26 | MLE | |
p00189 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define tr(it, container) \
for (typeof(container.begin()) it = container.begin(); \
it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))
#define eq ==
typedef long long ll;
typedef complex<double> point;
// up right down left
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};
const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int NO = 1000000000;
int main() {
while (true) {
int n;
cin >> n;
if (n == 0)
break;
vector<pair<pair<int, int>, int>> R;
int m = 0;
rep(i, n) {
int a, b, c;
cin >> a >> b >> c;
m = max(m, max(a, b));
R.push_back(mp(mp(a, b), c));
}
m++;
vector<vector<int>> V(m, vector<int>(m, NO));
rep(i, m) { V[i][i] = 0; }
rep(i, n) {
int a = R[i].first.first;
int b = R[i].first.second;
int c = R[i].second;
V[a][b] = V[b][a] = c;
}
cerr << __LINE__ << endl;
for (int k = 0; k < m; k++) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
V[i][j] = min(V[i][j], V[i][k] + V[k][j]);
}
}
}
int ret = -1;
int mi = NO;
for (int i = 0; i < m; i++) {
int s = 0;
for (int j = 0; j < m; j++) {
s += V[i][j];
}
if (mi > s) {
mi = s;
ret = i;
}
}
cout << ret << " " << mi << endl;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define tr(it, container) \
for (typeof(container.begin()) it = container.begin(); \
it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))
#define eq ==
typedef long long ll;
typedef complex<double> point;
// up right down left
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};
const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int NO = 1000000000;
int main() {
while (true) {
int n;
cin >> n;
if (n == 0)
break;
vector<pair<pair<int, int>, int>> R;
int m = 0;
rep(i, n) {
int a, b, c;
cin >> a >> b >> c;
m = max(m, max(a, b));
R.push_back(mp(mp(a, b), c));
}
m++;
vector<vector<int>> V(m, vector<int>(m, NO));
rep(i, m) { V[i][i] = 0; }
rep(i, n) {
int a = R[i].first.first;
int b = R[i].first.second;
int c = R[i].second;
V[a][b] = V[b][a] = c;
}
for (int k = 0; k < m; k++) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
V[i][j] = min(V[i][j], V[i][k] + V[k][j]);
}
}
}
int ret = -1;
int mi = NO;
for (int i = 0; i < m; i++) {
int s = 0;
for (int j = 0; j < m; j++) {
s += V[i][j];
}
if (mi > s) {
mi = s;
ret = i;
}
}
cout << ret << " " << mi << endl;
}
return 0;
} | delete | 68 | 70 | 68 | 68 | 0 | 70
70
|
p00189 | C++ | Runtime Error | #include <bits/stdc++.h>
#define SIZE 100005
#define MAX_V 100001
#define INF (1e9 + 1)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int g[10][10];
int main() {
int a;
while (scanf("%d", &a), a) {
for (int i = 0; i, 10; i++) {
for (int j = 0; j < 10; j++) {
if (i == j)
g[i][j] = 0;
else
g[i][j] = 999999999;
}
}
int t = 0;
for (int i = 0; i < a; i++) {
int c, d, e;
scanf("%d%d%d", &c, &d, &e);
if (t < c)
t = c;
if (t < d)
t = d;
g[c][d] = e;
g[d][c] = e;
}
t++;
for (int k = 0; k < t; k++) {
for (int i = 0; i < t; i++) {
for (int j = 0; j < t; j++) {
if (g[i][k] + g[k][j] < g[i][j])
g[i][j] = g[i][k] + g[k][j];
}
}
}
int n = 0;
ll ans = 9999999999;
for (int i = 0; i < t; i++) {
ll now = 0;
for (int j = 0; j < t; j++)
now += g[i][j];
if (ans > now) {
ans = now;
n = i;
}
}
printf("%d %lld\n", n, ans);
}
}
| #include <bits/stdc++.h>
#define SIZE 100005
#define MAX_V 100001
#define INF (1e9 + 1)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int g[10][10];
int main() {
int a;
while (scanf("%d", &a), a) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (i == j)
g[i][j] = 0;
else
g[i][j] = 999999999;
}
}
int t = 0;
for (int i = 0; i < a; i++) {
int c, d, e;
scanf("%d%d%d", &c, &d, &e);
if (t < c)
t = c;
if (t < d)
t = d;
g[c][d] = e;
g[d][c] = e;
}
t++;
for (int k = 0; k < t; k++) {
for (int i = 0; i < t; i++) {
for (int j = 0; j < t; j++) {
if (g[i][k] + g[k][j] < g[i][j])
g[i][j] = g[i][k] + g[k][j];
}
}
}
int n = 0;
ll ans = 9999999999;
for (int i = 0; i < t; i++) {
ll now = 0;
for (int j = 0; j < t; j++)
now += g[i][j];
if (ans > now) {
ans = now;
n = i;
}
}
printf("%d %lld\n", n, ans);
}
}
| replace | 12 | 13 | 12 | 13 | -11 | |
p00189 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define SIZE 100
#define INF 10000000
typedef struct {
int data;
int pos;
} DAT;
using namespace std;
int main() {
DAT ans;
int i, j, k;
int a, b, c;
int n;
int time[SIZE][SIZE];
while (1) {
int sum[SIZE] = {0};
cin >> n;
for (i = 0; i < n + 1; i++) {
for (j = 0; j < n + 1; j++) {
if (i == j) {
time[i][j] = 0;
} else {
time[i][j] = INF;
}
}
}
int max = 0;
for (i = 0; i < n; i++) {
cin >> a >> b >> c;
time[a][b] = c;
time[b][a] = c;
if (max < a)
max = a;
if (max < b)
max = b;
}
for (i = 0; i < max + 1; i++) {
for (j = 0; j < max + 1; j++) {
for (k = 0; k < max + 1; k++) {
time[j][k] = min(time[j][k], time[j][i] + time[i][k]);
}
}
}
for (i = 0; i < max + 1; i++) {
for (j = 0; j < max + 1; j++) {
sum[i] += time[i][j];
}
}
ans.data = INF;
ans.pos = -1;
for (i = 0; i < max + 1; i++) {
if (sum[i] < ans.data) {
ans.data = sum[i];
ans.pos = i;
}
}
printf("%d %d\n", ans.pos, ans.data);
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define SIZE 100
#define INF 10000000
typedef struct {
int data;
int pos;
} DAT;
using namespace std;
int main() {
DAT ans;
int i, j, k;
int a, b, c;
int n;
int time[SIZE][SIZE];
while (1) {
int sum[SIZE] = {0};
cin >> n;
if (n == 0)
break;
for (i = 0; i < n + 1; i++) {
for (j = 0; j < n + 1; j++) {
if (i == j) {
time[i][j] = 0;
} else {
time[i][j] = INF;
}
}
}
int max = 0;
for (i = 0; i < n; i++) {
cin >> a >> b >> c;
time[a][b] = c;
time[b][a] = c;
if (max < a)
max = a;
if (max < b)
max = b;
}
for (i = 0; i < max + 1; i++) {
for (j = 0; j < max + 1; j++) {
for (k = 0; k < max + 1; k++) {
time[j][k] = min(time[j][k], time[j][i] + time[i][k]);
}
}
}
for (i = 0; i < max + 1; i++) {
for (j = 0; j < max + 1; j++) {
sum[i] += time[i][j];
}
}
ans.data = INF;
ans.pos = -1;
for (i = 0; i < max + 1; i++) {
if (sum[i] < ans.data) {
ans.data = sum[i];
ans.pos = i;
}
}
printf("%d %d\n", ans.pos, ans.data);
}
return 0;
} | insert | 28 | 28 | 28 | 30 | TLE | |
p00189 | C++ | Runtime Error | #include <algorithm>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define pb push_back
typedef long long ll;
using namespace std;
const int mod = 1e9 + 7;
const int INF = 1 << 25;
void solve(int n) {
int dp[11][11];
fill(dp[0], dp[10], INF);
rep(i, 9) dp[i][i] = 0;
int a, b, c;
int first = 10, last = 0;
int ans1;
ll ans2 = INF, temp;
rep(i, n) {
cin >> a >> b >> c;
first = min(first, min(a, b));
last = max(last, max(a, b));
dp[a][b] = dp[b][a] = c;
}
rep(k, n) rep(i, n) rep(j, n) {
dp[j][i] = dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}
REP(i, first, last + 1) {
temp = 0;
REP(j, first, last + 1) { temp += dp[i][j]; }
if (temp < ans2) {
ans2 = temp;
ans1 = i;
}
}
cout << ans1 << " " << ans2 << endl;
return;
}
int main() {
int n;
while (true) {
cin >> n;
if (!n)
break;
solve(n);
}
return 0;
} | #include <algorithm>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define pb push_back
typedef long long ll;
using namespace std;
const int mod = 1e9 + 7;
const int INF = 1 << 25;
void solve(int n) {
int dp[11][11];
fill(dp[0], dp[10], INF);
rep(i, 9) dp[i][i] = 0;
int a, b, c;
int first = 10, last = 0;
int ans1;
ll ans2 = INF, temp;
rep(i, n) {
cin >> a >> b >> c;
first = min(first, min(a, b));
last = max(last, max(a, b));
dp[a][b] = dp[b][a] = c;
}
REP(k, first, last + 1) REP(i, first, last + 1) REP(j, first, last + 1) {
dp[j][i] = dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}
REP(i, first, last + 1) {
temp = 0;
REP(j, first, last + 1) { temp += dp[i][j]; }
if (temp < ans2) {
ans2 = temp;
ans1 = i;
}
}
cout << ans1 << " " << ans2 << endl;
return;
}
int main() {
int n;
while (true) {
cin >> n;
if (!n)
break;
solve(n);
}
return 0;
} | replace | 39 | 40 | 39 | 40 | 0 | |
p00189 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
using namespace std;
#define MAXV 10
#define INF 100000000
int main() {
int n;
int v;
int a, b, c;
int d[MAXV][MAXV];
int sum, ans, ans_i;
while (1) {
cin >> n;
if (n == 0)
break;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j)
d[i][j] = 0;
else
d[i][j] = INF;
}
}
v = -1;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c;
v = max(v, max(a, b));
d[a][b] = c;
d[b][a] = c;
}
for (int k = 0; k <= v; k++) {
for (int i = 0; i <= v; i++) {
for (int j = 0; j <= v; j++) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
ans = INF, ans_i = -1;
for (int i = 0; i <= v; i++) {
sum = 0;
for (int j = 0; j <= v; j++) {
if (d[i][j] != INF)
sum += d[i][j];
}
if (ans > sum && sum != 0) {
ans = sum;
ans_i = i;
}
}
cout << ans_i << " " << ans << endl;
}
} | #include <algorithm>
#include <iostream>
using namespace std;
#define MAXV 10
#define INF 100000000
int main() {
int n;
int v;
int a, b, c;
int d[MAXV][MAXV];
int sum, ans, ans_i;
while (1) {
cin >> n;
if (n == 0)
break;
for (int i = 0; i < MAXV; i++) {
for (int j = 0; j < MAXV; j++) {
if (i == j)
d[i][j] = 0;
else
d[i][j] = INF;
}
}
v = -1;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c;
v = max(v, max(a, b));
d[a][b] = c;
d[b][a] = c;
}
for (int k = 0; k <= v; k++) {
for (int i = 0; i <= v; i++) {
for (int j = 0; j <= v; j++) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
ans = INF, ans_i = -1;
for (int i = 0; i <= v; i++) {
sum = 0;
for (int j = 0; j <= v; j++) {
if (d[i][j] != INF)
sum += d[i][j];
}
if (ans > sum && sum != 0) {
ans = sum;
ans_i = i;
}
}
cout << ans_i << " " << ans << endl;
}
} | replace | 21 | 23 | 21 | 23 | TLE | |
p00189 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
const int INF = 1 << 29;
const int N = 10;
int main() {
int n;
while (true) {
cin >> n;
if (!N)
break;
int dp[N][N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
dp[i][j] = INF;
int a, b, c;
while (n--) {
cin >> a >> b >> c;
dp[a][b] = c;
dp[b][a] = c;
}
for (int k = 0; k < N; ++k) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (dp[i][j] > dp[i][k] + dp[k][j])
dp[i][j] = dp[i][k] + dp[k][j];
}
}
}
int sum[N];
for (int i = 0; i < N; i++) {
sum[i] = 0;
for (int j = 0; j < N; j++) {
if (i != j && dp[i][j] != INF) {
sum[i] += dp[i][j];
}
}
}
int ans_i = 0;
for (int i = 0; i < N; i++) {
if (sum[i] < sum[ans_i] && sum[i] != 0) {
ans_i = i;
}
}
cout << ans_i << " " << sum[ans_i] << endl;
}
}
| #include <iostream>
using namespace std;
const int INF = 1 << 29;
const int N = 10;
int main() {
int n;
while (true) {
cin >> n;
if (!n)
break;
int dp[N][N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
dp[i][j] = INF;
int a, b, c;
while (n--) {
cin >> a >> b >> c;
dp[a][b] = c;
dp[b][a] = c;
}
for (int k = 0; k < N; ++k) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (dp[i][j] > dp[i][k] + dp[k][j])
dp[i][j] = dp[i][k] + dp[k][j];
}
}
}
int sum[N];
for (int i = 0; i < N; i++) {
sum[i] = 0;
for (int j = 0; j < N; j++) {
if (i != j && dp[i][j] != INF) {
sum[i] += dp[i][j];
}
}
}
int ans_i = 0;
for (int i = 0; i < N; i++) {
if (sum[i] < sum[ans_i] && sum[i] != 0) {
ans_i = i;
}
}
cout << ans_i << " " << sum[ans_i] << endl;
}
}
| replace | 11 | 12 | 11 | 12 | TLE | |
p00190 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
const int SIZE = 13;
struct Puzzle {
short c[SIZE];
void move(int d, int n) {
if (d == 0)
moveUp(n);
if (d == 1)
moveDown(n);
if (d == 2)
moveLeft(n);
if (d == 3)
moveRight(n);
}
// n = choosed blank number 0(first one) or 1(second one)
void moveUp(int n) {
int pos = findBlankPosition(n);
int tmp[] = {-1, -1, 0, -1, -1, 1, 2, 3, -1, 5, 6, 7, 10};
int npos = tmp[pos];
if (npos != -1)
swap(c[pos], c[npos]);
}
void moveDown(int n) {
int pos = findBlankPosition(n);
int tmp[] = {2, 5, 6, 7, -1, 9, 10, 11, -1, -1, 12, -1, -1};
int npos = tmp[pos];
if (npos != -1)
swap(c[pos], c[npos]);
}
void moveLeft(int n) {
int pos = findBlankPosition(n);
int tmp[] = {-1, -1, 1, 2, -1, 4, 5, 6, 7, -1, 9, 10, -1};
int npos = tmp[pos];
if (npos != -1)
swap(c[pos], c[npos]);
}
void moveRight(int n) {
int pos = findBlankPosition(n);
int tmp[] = {-1, 2, 3, -1, 5, 6, 7, 8, -1, 10, 11, -1, -1};
int npos = tmp[pos];
if (npos != -1)
swap(c[pos], c[npos]);
}
int findBlankPosition(int n) {
for (int i = 0; i < SIZE; i++) {
if (c[i] == 0)
n--;
if (n < 0)
return i;
}
return -1;
}
bool operator<(const Puzzle &p) const {
for (int i = 0; i < SIZE; i++)
if (c[i] != p.c[i])
return c[i] < p.c[i];
return false;
}
};
Puzzle puz;
bool input() {
cin >> puz.c[0];
if (puz.c[0] == -1)
return false;
for (int i = 1; i < SIZE; i++)
cin >> puz.c[i];
return true;
}
void solve() {
map<Puzzle, int> M;
M[puz] = 1;
queue<Puzzle> Q;
Q.push(puz);
while (!Q.empty()) {
Puzzle now = Q.front();
Q.pop();
for (int i = 0; i < 4; i++)
for (int j = 0; j < 2; j++) {
Puzzle nex = now;
nex.move(i, j);
if (M[nex] > M[now] + 1 || M[nex] == 0) {
M[nex] = M[now] + 1;
if (M[nex] <= 11)
Q.push(nex);
}
}
}
Puzzle tmp;
for (int i = 0; i < SIZE; i++)
tmp.c[i] = i % (SIZE - 1);
int ans = 25;
Q.push(tmp);
map<Puzzle, int> MG;
MG[tmp] = 1;
while (!Q.empty()) {
Puzzle now = Q.front();
Q.pop();
if (M[now] != 0)
ans = min(ans, M[now] + MG[now] - 2);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 2; j++) {
Puzzle nex = now;
nex.move(i, j);
if (MG[nex] > MG[now] + 1 || MG[nex] == 0) {
MG[nex] = MG[now] + 1;
if (MG[nex] <= 11)
Q.push(nex);
}
}
}
if (ans <= 20)
cout << ans << endl;
else
cout << "NA" << endl;
}
int main() {
while (input())
solve();
return 0;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
const int SIZE = 13;
struct Puzzle {
short c[SIZE];
void move(int d, int n) {
if (d == 0)
moveUp(n);
if (d == 1)
moveDown(n);
if (d == 2)
moveLeft(n);
if (d == 3)
moveRight(n);
}
// n = choosed blank number 0(first one) or 1(second one)
void moveUp(int n) {
int pos = findBlankPosition(n);
int tmp[] = {-1, -1, 0, -1, -1, 1, 2, 3, -1, 5, 6, 7, 10};
int npos = tmp[pos];
if (npos != -1)
swap(c[pos], c[npos]);
}
void moveDown(int n) {
int pos = findBlankPosition(n);
int tmp[] = {2, 5, 6, 7, -1, 9, 10, 11, -1, -1, 12, -1, -1};
int npos = tmp[pos];
if (npos != -1)
swap(c[pos], c[npos]);
}
void moveLeft(int n) {
int pos = findBlankPosition(n);
int tmp[] = {-1, -1, 1, 2, -1, 4, 5, 6, 7, -1, 9, 10, -1};
int npos = tmp[pos];
if (npos != -1)
swap(c[pos], c[npos]);
}
void moveRight(int n) {
int pos = findBlankPosition(n);
int tmp[] = {-1, 2, 3, -1, 5, 6, 7, 8, -1, 10, 11, -1, -1};
int npos = tmp[pos];
if (npos != -1)
swap(c[pos], c[npos]);
}
int findBlankPosition(int n) {
for (int i = 0; i < SIZE; i++) {
if (c[i] == 0)
n--;
if (n < 0)
return i;
}
return -1;
}
bool operator<(const Puzzle &p) const {
for (int i = 0; i < SIZE; i++)
if (c[i] != p.c[i])
return c[i] < p.c[i];
return false;
}
};
Puzzle puz;
bool input() {
cin >> puz.c[0];
if (puz.c[0] == -1)
return false;
for (int i = 1; i < SIZE; i++)
cin >> puz.c[i];
return true;
}
void solve() {
map<Puzzle, int> M;
M[puz] = 1;
queue<Puzzle> Q;
Q.push(puz);
while (!Q.empty()) {
Puzzle now = Q.front();
Q.pop();
for (int i = 0; i < 4; i++)
for (int j = 0; j < 2; j++) {
Puzzle nex = now;
nex.move(i, j);
if (M[nex] > M[now] + 1 || M[nex] == 0) {
M[nex] = M[now] + 1;
if (M[nex] < 11)
Q.push(nex);
}
}
}
Puzzle tmp;
for (int i = 0; i < SIZE; i++)
tmp.c[i] = i % (SIZE - 1);
int ans = 25;
Q.push(tmp);
map<Puzzle, int> MG;
MG[tmp] = 1;
while (!Q.empty()) {
Puzzle now = Q.front();
Q.pop();
if (M[now] != 0)
ans = min(ans, M[now] + MG[now] - 2);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 2; j++) {
Puzzle nex = now;
nex.move(i, j);
if (MG[nex] > MG[now] + 1 || MG[nex] == 0) {
MG[nex] = MG[now] + 1;
if (MG[nex] <= 11)
Q.push(nex);
}
}
}
if (ans <= 20)
cout << ans << endl;
else
cout << "NA" << endl;
}
int main() {
while (input())
solve();
return 0;
} | replace | 105 | 106 | 105 | 106 | TLE | |
p00190 | C++ | Time Limit Exceeded | #include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int to[13][5] = {{2, -1}, {2, 5, -1}, {0, 1, 3, 6, -1},
{2, 7, -1}, {5, -1}, {1, 4, 6, 9, -1},
{2, 5, 7, 10, -1}, {3, 6, 8, 11, -1}, {7, -1},
{5, 10, -1}, {6, 9, 11, 12}, {7, 10, -1},
{10, -1}};
int X[] = {2, 1, 2, 3, 0, 1, 2, 3, 4, 1, 2, 3, 2};
int Y[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4};
int solve(vector<int> data) {
queue<vector<int>> Q;
map<vector<int>, int> done;
Q.push(data);
done[Q.front()] = 0;
while (Q.size()) {
vector<int> q = Q.front();
Q.pop();
int dif = 0;
rep(i, 13) if (q[i]) dif += abs(X[q[i]] - X[i]) + abs(Y[q[i]] - Y[i]);
if (dif == 0)
return done[q];
;
if (dif / 2 + done[q] >= 20)
continue;
int curcost = done[q];
rep(x, 13) {
if (q[x] == 0) {
rep(i, 5) {
if (!~to[x][i])
break;
swap(q[x], q[to[x][i]]);
if (done.find(q) == done.end()) {
done[q] = curcost + 1;
Q.push(q);
}
swap(q[x], q[to[x][i]]);
}
}
}
}
return 21;
}
int main() {
vector<int> data(13);
while (1) {
rep(i, 13) if (!(cin >> data[i])) return 0;
int ans = solve(data);
if (ans > 20)
cout << "NA" << endl;
else
cout << ans << endl;
}
} | #include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int to[13][5] = {{2, -1}, {2, 5, -1}, {0, 1, 3, 6, -1},
{2, 7, -1}, {5, -1}, {1, 4, 6, 9, -1},
{2, 5, 7, 10, -1}, {3, 6, 8, 11, -1}, {7, -1},
{5, 10, -1}, {6, 9, 11, 12}, {7, 10, -1},
{10, -1}};
int X[] = {2, 1, 2, 3, 0, 1, 2, 3, 4, 1, 2, 3, 2};
int Y[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4};
int solve(vector<int> data) {
queue<vector<int>> Q;
map<vector<int>, int> done;
Q.push(data);
done[Q.front()] = 0;
while (Q.size()) {
vector<int> q = Q.front();
Q.pop();
int dif = 0;
rep(i, 13) if (q[i]) dif += abs(X[q[i]] - X[i]) + abs(Y[q[i]] - Y[i]);
if (dif == 0)
return done[q];
;
if (dif + done[q] > 20)
continue;
int curcost = done[q];
rep(x, 13) {
if (q[x] == 0) {
rep(i, 5) {
if (!~to[x][i])
break;
swap(q[x], q[to[x][i]]);
if (done.find(q) == done.end()) {
done[q] = curcost + 1;
Q.push(q);
}
swap(q[x], q[to[x][i]]);
}
}
}
}
return 21;
}
int main() {
vector<int> data(13);
while (1) {
rep(i, 13) if (!(cin >> data[i])) return 0;
int ans = solve(data);
if (ans > 20)
cout << "NA" << endl;
else
cout << ans << endl;
}
} | replace | 30 | 31 | 30 | 31 | TLE | |
p00190 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 20
#define WIDTH 100
int OK[5][5] = {{0, 0, 1, 0, 0},
{0, 1, 1, 1, 0},
{1, 1, 1, 1, 1},
{0, 1, 1, 1, 0},
{0, 0, 1, 0, 0}};
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
pair<int, vector<vector<int>>> dp[MAX_N + 2][WIDTH], E[WIDTH * 10];
int score(vector<vector<int>> p) {
int sum = 0;
pair<int, int> a[12], b[12];
int CNT = 0;
for (int i = 0; i < 25; i++) {
if (p[i / 5][i % 5] >= 1)
a[p[i / 5][i % 5]] = make_pair(i / 5, i % 5);
}
for (int i = 5; i < 20; i++) {
if (OK[i / 5][i % 5] == 1) {
CNT++;
b[CNT] = make_pair(i / 5, i % 5);
}
}
for (int i = 1; i <= 11; i++) {
sum += abs(a[i].first - b[i].first) + abs(a[i].second - b[i].second);
}
return sum;
}
vector<vector<int>> Move(vector<vector<int>> p, int x, int y, int dir) {
swap(p[x + dx[dir]][y + dy[dir]], p[x][y]);
return p;
}
int BeemSearch(vector<vector<int>> Z) {
vector<vector<int>> GOAL;
int CNT = 0;
for (int i = 0; i < 5; i++) {
vector<int> GOAL2(5, -1);
GOAL.push_back(GOAL2);
}
for (int i = 5; i < 20; i++) {
if (OK[i / 5][i % 5] == 1) {
CNT++;
GOAL[i / 5][i % 5] = CNT;
}
}
GOAL[0][2] = 0;
GOAL[4][2] = 0;
if (Z == GOAL)
return 0;
dp[0][0] = make_pair(score(Z), Z);
for (int i = 0; i < MAX_N; i++) {
int CNTS = 0;
for (int j = 0; j < WIDTH; j++) {
if (dp[i][j].second.size() == 0)
continue;
vector<vector<int>> D = dp[i][j].second;
for (int k = 0; k < 25; k++) {
if (D[k / 5][k % 5] != 0)
continue;
for (int l = 0; l < 4; l++) {
int cx = (k / 5) + dx[l], cy = (k % 5) + dy[l];
if (cx < 0 || cx >= 5 || cy < 0 || cy >= 5)
continue;
if (D[cx][cy] <= 0)
continue;
vector<vector<int>> D2 = Move(D, k / 5, k % 5, l);
E[CNTS] = make_pair(score(D2), D2);
CNTS++;
}
}
}
sort(E, E + CNTS);
dp[i + 1][0] = E[0];
int CNT2 = 1;
if (E[0].first == 0)
return i + 1;
for (int i = 1; i < CNTS; i++) {
if (E[i - 1] != E[i]) {
dp[i + 1][CNT2] = E[i];
CNT2++;
}
if (CNT2 == WIDTH)
break;
}
}
return -1;
}
int main() {
while (true) {
for (int i = 0; i < MAX_N + 2; i++) {
for (int j = 0; j < WIDTH; j++) {
vector<vector<int>> ZERO;
dp[i][j] = make_pair(0, ZERO);
}
}
for (int i = 0; i < WIDTH * 10; i++) {
vector<vector<int>> ZERO;
E[i] = make_pair(0, ZERO);
}
vector<vector<int>> C;
for (int i = 0; i < 5; i++) {
vector<int> C2(5, -1);
C.push_back(C2);
}
cin >> C[0][2];
if (C[0][2] == -1)
break;
for (int i = 1; i < 4; i++)
cin >> C[1][i];
for (int i = 0; i < 5; i++)
cin >> C[2][i];
for (int i = 1; i < 4; i++)
cin >> C[3][i];
cin >> C[4][2];
int ret = BeemSearch(C);
if (ret == -1)
cout << "NA" << endl;
else
cout << ret << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 20
#define WIDTH 100
int OK[5][5] = {{0, 0, 1, 0, 0},
{0, 1, 1, 1, 0},
{1, 1, 1, 1, 1},
{0, 1, 1, 1, 0},
{0, 0, 1, 0, 0}};
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
pair<int, vector<vector<int>>> dp[MAX_N + 2][WIDTH], E[WIDTH * 10];
int score(vector<vector<int>> p) {
int sum = 0;
pair<int, int> a[12], b[12];
int CNT = 0;
for (int i = 0; i < 25; i++) {
if (p[i / 5][i % 5] >= 1)
a[p[i / 5][i % 5]] = make_pair(i / 5, i % 5);
}
for (int i = 5; i < 20; i++) {
if (OK[i / 5][i % 5] == 1) {
CNT++;
b[CNT] = make_pair(i / 5, i % 5);
}
}
for (int i = 1; i <= 11; i++) {
sum += abs(a[i].first - b[i].first) + abs(a[i].second - b[i].second);
}
return sum;
}
vector<vector<int>> Move(vector<vector<int>> p, int x, int y, int dir) {
swap(p[x + dx[dir]][y + dy[dir]], p[x][y]);
return p;
}
int BeemSearch(vector<vector<int>> Z) {
vector<vector<int>> GOAL;
int CNT = 0;
for (int i = 0; i < 5; i++) {
vector<int> GOAL2(5, -1);
GOAL.push_back(GOAL2);
}
for (int i = 5; i < 20; i++) {
if (OK[i / 5][i % 5] == 1) {
CNT++;
GOAL[i / 5][i % 5] = CNT;
}
}
GOAL[0][2] = 0;
GOAL[4][2] = 0;
if (Z == GOAL)
return 0;
dp[0][0] = make_pair(score(Z), Z);
for (int i = 0; i < MAX_N; i++) {
int CNTS = 0;
for (int j = 0; j < WIDTH; j++) {
if (dp[i][j].second.size() == 0)
continue;
vector<vector<int>> D = dp[i][j].second;
for (int k = 0; k < 25; k++) {
if (D[k / 5][k % 5] != 0)
continue;
for (int l = 0; l < 4; l++) {
int cx = (k / 5) + dx[l], cy = (k % 5) + dy[l];
if (cx < 0 || cx >= 5 || cy < 0 || cy >= 5)
continue;
if (D[cx][cy] <= 0)
continue;
vector<vector<int>> D2 = Move(D, k / 5, k % 5, l);
E[CNTS] = make_pair(score(D2), D2);
CNTS++;
}
}
}
sort(E, E + CNTS);
dp[i + 1][0] = E[0];
int CNT2 = 1;
if (E[0].first == 0)
return i + 1;
for (int j = 1; j < CNTS; j++) {
if (E[j - 1] != E[j]) {
dp[i + 1][CNT2] = E[j];
CNT2++;
}
if (CNT2 == WIDTH)
break;
}
}
return -1;
}
int main() {
while (true) {
for (int i = 0; i < MAX_N + 2; i++) {
for (int j = 0; j < WIDTH; j++) {
vector<vector<int>> ZERO;
dp[i][j] = make_pair(0, ZERO);
}
}
for (int i = 0; i < WIDTH * 10; i++) {
vector<vector<int>> ZERO;
E[i] = make_pair(0, ZERO);
}
vector<vector<int>> C;
for (int i = 0; i < 5; i++) {
vector<int> C2(5, -1);
C.push_back(C2);
}
cin >> C[0][2];
if (C[0][2] == -1)
break;
for (int i = 1; i < 4; i++)
cin >> C[1][i];
for (int i = 0; i < 5; i++)
cin >> C[2][i];
for (int i = 1; i < 4; i++)
cin >> C[3][i];
cin >> C[4][2];
int ret = BeemSearch(C);
if (ret == -1)
cout << "NA" << endl;
else
cout << ret << endl;
}
return 0;
} | replace | 82 | 85 | 82 | 85 | 0 | |
p00191 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
double g[100][100], dp[100][100];
int main() {
int n, m;
while (scanf("%d%d", &n, &m), n || m) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%lf", &g[i][j]);
fill_n(dp[0], 100, 1.0);
fill_n(dp[1], 9900, 0.0);
for (int i = 1; i < m; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
dp[i][j] = max(dp[i][j], dp[i - 1][k] * g[k][j]);
printf("%.2f\n", *max_element(dp[m - 1], dp[m - 1] + n));
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
double g[100][100], dp[100][100];
int main() {
int n, m;
while (scanf("%d%d", &n, &m), n || m) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%lf", &g[i][j]);
fill_n(dp[0], 100, 1.0);
fill_n(dp[1], 9900, 0.0);
for (int i = 1; i < m; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
dp[i][j] = max(dp[i][j], dp[i - 1][k] * g[k][j]);
printf("%.2f\n", *max_element(dp[m - 1], dp[m - 1] + n));
}
return 0;
} | replace | 9 | 10 | 9 | 10 | TLE | |
p00191 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
double key[100][100];
double table[100][100];
void Init(int n, int m) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
table[i][j] = 0;
}
double memo(int cur, int count, int limit, int kind) {
if (table[cur][count])
return table[cur][count];
if (count == limit)
return 1.0;
double Max = 0;
int prev = 0;
for (int i = 0; i < kind; i++) {
double tmp = memo(i, count + 1, limit, kind);
if (Max <= tmp) {
// if(Max*key[prev][cur] < tmp*key[i][cur]){
Max = tmp;
prev = i;
//}
}
}
table[cur][count] = Max * key[prev][cur];
return table[cur][count];
}
int main() {
int n, m;
while (cin >> n >> m && ((n != 0) || (m != 0))) {
Init(n, m);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> key[i][j];
for (int i = 0; i < n; i++)
table[i][0] = memo(i, 0, m - 1, n);
double max = 0;
for (int i = 0; i < n; i++)
if (max < table[i][0])
max = table[i][0];
printf("%0.2f\n", max);
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
double key[100][100];
double table[100][100];
void Init(int n, int m) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
table[i][j] = 0;
}
double memo(int cur, int count, int limit, int kind) {
if (table[cur][count])
return table[cur][count];
if (count == limit)
return 1.0;
for (int i = 0; i < kind; i++)
table[cur][count] =
max(table[cur][count], memo(i, count + 1, limit, kind) * key[i][cur]);
return table[cur][count];
}
int main() {
int n, m;
while (cin >> n >> m && ((n != 0) || (m != 0))) {
Init(n, m);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> key[i][j];
for (int i = 0; i < n; i++)
table[i][0] = memo(i, 0, m - 1, n);
double max = 0;
for (int i = 0; i < n; i++)
if (max < table[i][0])
max = table[i][0];
printf("%0.2f\n", max);
}
return 0;
} | replace | 26 | 38 | 26 | 29 | TLE | |
p00191 | C++ | Runtime Error |
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << setprecision(2);
cout << setiosflags(ios::fixed);
int n, m, i, j, k;
long double M;
while (cin >> n >> m, n | m) {
vector<vector<long double>> manure(n, vector<long double>(n)),
dp(m, vector<long double>(n, 0.0));
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> manure[i][j];
for (j = 0; j < n; j++)
dp[0][j] = 1.0;
for (i = 1; i < m; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
dp[i][j] = max(dp[i][j], dp[i - 1][k] * manure[k][j]);
}
}
}
M = 0.0;
for (j = 0; j < n; j++)
M = max(M, dp[n - 1][j]);
cout << M << endl;
}
} |
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << setprecision(2);
cout << setiosflags(ios::fixed);
int n, m, i, j, k;
long double M;
while (cin >> n >> m, n | m) {
vector<vector<long double>> manure(n, vector<long double>(n)),
dp(m, vector<long double>(n, 0.0));
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> manure[i][j];
for (j = 0; j < n; j++)
dp[0][j] = 1.0;
for (i = 1; i < m; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
dp[i][j] = max(dp[i][j], dp[i - 1][k] * manure[k][j]);
}
}
}
M = 0.0;
for (j = 0; j < n; j++)
M = max(M, dp[m - 1][j]);
cout << M << endl;
}
} | replace | 28 | 29 | 28 | 29 | 0 | |
p00192 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
using namespace std;
int main() {
while (true) {
int m, n;
cin >> m >> n;
vector<int> C(n); // Ô
for (int i = 0; i < n; i++)
cin >> C[i];
vector<int> U(m, -1); // ã
vector<int> D(m, -1); // º
vector<int> S; // ÔÒ¿
vector<int> ans; // ¦
for (int t = 0; (int)ans.size() < n; t++) {
// oÉ
for (int i = 0; i < m; i++) {
if (D[i] >= 0)
if (--C[D[i]] <= 0)
ans.push_back(D[i]), D[i] = -1;
if (U[i] >= 0)
if (--C[U[i]] <= 0 && D[i] == -1)
ans.push_back(U[i]), U[i] = -1;
}
// üÉ
if (t % 10 == 0 && t / 10 < n)
S.push_back(t / 10);
if (S.size() > 0) {
bool f = false;
for (int i = 0; i < m && !f; i++)
if (U[i] == -1 && D[i] == -1)
U[i] = S[0], f = true;
if (!f) {
int mn = 99999999;
int mi;
for (int i = 0; i < m; i++)
if (D[i] == -1)
if (C[U[i]] >= C[S[0]])
if (C[U[i]] - C[S[0]] < mn)
mn = C[U[i]] - C[S[0]], mi = i;
if (mn < 99999999)
D[mi] = S[0], f = true;
}
if (!f) {
int mn = 99999999;
int mi;
for (int i = 0; i < m; i++)
if (D[i] == -1)
if (C[S[0]] - C[U[i]] < mn)
mn = C[S[0]] - C[U[i]], mi = i;
if (mn < 99999999)
D[mi] = S[0], f = true;
}
if (f)
S.erase(S.begin());
}
}
for (int i = 0; i < (int)ans.size(); i++)
cout << (i > 0 ? " " : "") << ans[i] + 1;
cout << endl;
}
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
int main() {
while (true) {
int m, n;
cin >> m >> n;
if (m == 0 && n == 0)
break;
vector<int> C(n); // Ô
for (int i = 0; i < n; i++)
cin >> C[i];
vector<int> U(m, -1); // ã
vector<int> D(m, -1); // º
vector<int> S; // ÔÒ¿
vector<int> ans; // ¦
for (int t = 0; (int)ans.size() < n; t++) {
// oÉ
for (int i = 0; i < m; i++) {
if (D[i] >= 0)
if (--C[D[i]] <= 0)
ans.push_back(D[i]), D[i] = -1;
if (U[i] >= 0)
if (--C[U[i]] <= 0 && D[i] == -1)
ans.push_back(U[i]), U[i] = -1;
}
// üÉ
if (t % 10 == 0 && t / 10 < n)
S.push_back(t / 10);
if (S.size() > 0) {
bool f = false;
for (int i = 0; i < m && !f; i++)
if (U[i] == -1 && D[i] == -1)
U[i] = S[0], f = true;
if (!f) {
int mn = 99999999;
int mi;
for (int i = 0; i < m; i++)
if (D[i] == -1)
if (C[U[i]] >= C[S[0]])
if (C[U[i]] - C[S[0]] < mn)
mn = C[U[i]] - C[S[0]], mi = i;
if (mn < 99999999)
D[mi] = S[0], f = true;
}
if (!f) {
int mn = 99999999;
int mi;
for (int i = 0; i < m; i++)
if (D[i] == -1)
if (C[S[0]] - C[U[i]] < mn)
mn = C[S[0]] - C[U[i]], mi = i;
if (mn < 99999999)
D[mi] = S[0], f = true;
}
if (f)
S.erase(S.begin());
}
}
for (int i = 0; i < (int)ans.size(); i++)
cout << (i > 0 ? " " : "") << ans[i] + 1;
cout << endl;
}
return 0;
} | insert | 8 | 8 | 8 | 10 | TLE | |
p00193 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
struct st {
int x, y, c;
};
int d[100][100], b[100][100], x[11], y[11];
int dx[][6]{{
0,
-1,
0,
1,
1,
1,
},
{-1, -1, -1, 0, 1, 0}};
int dy[]{-1, 0, 1, 1, 0, -1};
int main() {
int m, n, s;
while (scanf("%d%d", &m, &n), m) {
scanf("%d", &s);
rep(i, s) scanf("%d%d", &x[i], &y[i]), x[i]--, y[i]--;
int t;
scanf("%d", &t);
int Max = 0;
rep(i, t) {
scanf("%d%d", &x[s], &y[s]);
x[s]--;
y[s]--;
queue<st> que;
memset(d, -1, sizeof(d));
rep(j, s + 1) {
d[x[j]][y[j]] = 0;
b[x[j]][y[j]] = j;
que.push({x[j], y[j], j});
}
while (!que.empty()) {
st p = que.front();
que.pop();
rep(j, 6) {
int nx = p.x + dx[!(p.y & 1)][j], ny = p.y + dy[j];
if (0 <= nx && nx < m && 0 <= ny && ny < n) {
if (d[nx][ny] == -1) {
d[nx][ny] = d[p.x][p.y] + 1;
b[nx][ny] = p.c;
que.push({nx, ny, p.c});
} else if (d[nx][ny] == d[p.x][p.y] + 1) {
b[nx][ny] = -1;
que.push({nx, ny, p.c});
}
}
}
}
int cnt = 0;
rep(i, m) rep(j, n) {
if (b[i][j] == s)
cnt++;
}
Max = max(Max, cnt);
}
printf("%d\n", Max);
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
struct st {
int x, y, c;
};
int d[100][100], b[100][100], x[11], y[11];
int dx[][6]{{
0,
-1,
0,
1,
1,
1,
},
{-1, -1, -1, 0, 1, 0}};
int dy[]{-1, 0, 1, 1, 0, -1};
int main() {
int m, n, s;
while (scanf("%d%d", &m, &n), m) {
scanf("%d", &s);
rep(i, s) scanf("%d%d", &x[i], &y[i]), x[i]--, y[i]--;
int t;
scanf("%d", &t);
int Max = 0;
rep(i, t) {
scanf("%d%d", &x[s], &y[s]);
x[s]--;
y[s]--;
queue<st> que;
memset(d, -1, sizeof(d));
rep(j, s + 1) {
d[x[j]][y[j]] = 0;
b[x[j]][y[j]] = j;
que.push({x[j], y[j], j});
}
while (!que.empty()) {
st p = que.front();
que.pop();
rep(j, 6) {
int nx = p.x + dx[!(p.y & 1)][j], ny = p.y + dy[j];
if (0 <= nx && nx < m && 0 <= ny && ny < n) {
if (d[nx][ny] == -1) {
d[nx][ny] = d[p.x][p.y] + 1;
b[nx][ny] = p.c;
que.push({nx, ny, p.c});
} else if (d[nx][ny] == d[p.x][p.y] + 1 && b[nx][ny] != p.c) {
b[nx][ny] = -1;
que.push({nx, ny, p.c});
}
}
}
}
int cnt = 0;
rep(i, m) rep(j, n) {
if (b[i][j] == s)
cnt++;
}
Max = max(Max, cnt);
}
printf("%d\n", Max);
}
} | replace | 47 | 48 | 47 | 48 | MLE | |
p00193 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> grid;
const int INF = 1e9;
const int dx[6] = {0, 1, 1, 1, 0, -1}, dy[6] = {-1, -1, 0, 1, 1, 0};
int h, w;
grid bfs(int y, int x) {
grid res(h, vi(w, INF));
res[y][x] = 0;
queue<int> q;
q.push(y * w + x);
while (q.size()) {
y = q.front() / w;
x = q.front() % w;
q.pop();
int c = res[y][x];
for (int i = 0; i < 6; i++) {
int ny = y + dy[i],
nx = x + dx[i] + ((y % 2 == 0 && i % 3 != 2) ? -1 : 0);
if (ny < 0 || nx < 0 || ny >= h || nx >= w)
continue;
if (res[ny][nx] > c + 1) {
res[ny][nx] = c + 1;
q.push(ny * w + nx);
}
}
}
return res;
}
int main() {
while (cin >> h >> w, h) {
grid g(h, vi(w, INF));
int s;
cin >> s;
for (int z = 0; z < s; z++) {
int a, b;
cin >> a >> b;
a--;
b--;
grid tmp = bfs(b, a);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++)
g[i][j] = min(g[i][j], tmp[i][j]);
}
}
int t, res = 0;
cin >> t;
for (int z = 0; z < t; z++) {
int a, b;
cin >> a >> b;
a--;
b--;
grid tmp = bfs(b, a);
int cover = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (g[i][j] > tmp[i][j])
cover++;
}
}
res = max(res, cover);
}
cout << res << endl;
}
} | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> grid;
const int INF = 1e9;
const int dx[6] = {0, 1, 1, 1, 0, -1}, dy[6] = {-1, -1, 0, 1, 1, 0};
int h, w;
grid bfs(int y, int x) {
grid res(h, vi(w, INF));
res[y][x] = 0;
queue<int> q;
q.push(y * w + x);
while (q.size()) {
y = q.front() / w;
x = q.front() % w;
q.pop();
int c = res[y][x];
for (int i = 0; i < 6; i++) {
int ny = y + dy[i],
nx = x + dx[i] + ((y % 2 == 0 && i % 3 != 2) ? -1 : 0);
if (ny < 0 || nx < 0 || ny >= h || nx >= w)
continue;
if (res[ny][nx] > c + 1) {
res[ny][nx] = c + 1;
q.push(ny * w + nx);
}
}
}
return res;
}
int main() {
while (cin >> w >> h, w) {
grid g(h, vi(w, INF));
int s;
cin >> s;
for (int z = 0; z < s; z++) {
int a, b;
cin >> a >> b;
a--;
b--;
grid tmp = bfs(b, a);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++)
g[i][j] = min(g[i][j], tmp[i][j]);
}
}
int t, res = 0;
cin >> t;
for (int z = 0; z < t; z++) {
int a, b;
cin >> a >> b;
a--;
b--;
grid tmp = bfs(b, a);
int cover = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (g[i][j] > tmp[i][j])
cover++;
}
}
res = max(res, cover);
}
cout << res << endl;
}
} | replace | 38 | 39 | 38 | 39 | 0 | |
p00194 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int INF = 1 << 28;
inline pair<int, int> inputPos() {
string str;
cin >> str;
return pair<int, int>(str[0] - 'a', atoi(str.c_str() + 2) - 1);
}
int m, n;
int D;
int sign[20][20];
int cant[20][20][20][20];
int cst[20][20][20][20];
pair<int, int> sp, gp;
bool ok(int x, int y) { return x >= 0 && x < m && y >= 0 && y < n; }
void solve() {
static int dist[101][20][20][4];
fill_n(***dist, 101 * 20 * 20 * 4, INF);
dist[0][sp.first][sp.second][3] = 0;
for (int t = 0; t < 100; t++) {
for (int x = 0; x < m; x++) {
for (int y = 0; y < n; y++) {
for (int d = 0; d < 4; d++) {
if (dist[t][x][y][d] == INF)
continue;
for (int dd = 3; dd <= 5; dd++) {
int dir = (d + dd) % 4;
int nx = x + dx[dir];
int ny = y + dy[dir];
if (!ok(nx, ny))
continue;
if (cant[x][y][nx][ny])
continue;
int nt = t + D + cst[x][y][nx][ny];
if (sign[nx][ny]) {
if ((nt / sign[nx][ny] + dir) % 2 == 1) {
continue;
}
}
dist[nt][nx][ny][dir] = min(dist[nt][nx][ny][dir], nt);
}
}
}
}
}
for (int t = 0;; t++) {
for (int d = 0; d < 4; d++) {
if (dist[t][gp.first][gp.second][d] < INF) {
printf("%d\n", dist[t][gp.first][gp.second][d]);
return;
}
}
}
}
int main() {
while (scanf("%d %d", &m, &n), m) {
memset(sign, 0, sizeof(sign));
memset(cant, 0, sizeof(cant));
memset(cst, 0, sizeof(cst));
scanf("%d", &D);
int ns;
scanf("%d", &ns);
for (int i = 0; i < ns; i++) {
pair<int, int> pos = inputPos();
int k;
scanf("%d", &k);
sign[pos.first][pos.second] = k;
}
int nc;
scanf("%d", &nc);
for (int i = 0; i < nc; i++) {
pair<int, int> pos1 = inputPos();
pair<int, int> pos2 = inputPos();
cant[pos1.first][pos1.second][pos2.first][pos2.second] = true;
cant[pos2.first][pos2.second][pos1.first][pos1.second] = true;
}
int nj;
scanf("%d", &nj);
for (int i = 0; i < nj; i++) {
pair<int, int> pos1 = inputPos();
pair<int, int> pos2 = inputPos();
int d;
scanf("%d", &d);
cst[pos1.first][pos1.second][pos2.first][pos2.second] += d;
cst[pos2.first][pos2.second][pos1.first][pos1.second] += d;
}
sp = inputPos();
gp = inputPos();
solve();
}
} | #include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int INF = 1 << 28;
inline pair<int, int> inputPos() {
string str;
cin >> str;
return pair<int, int>(str[0] - 'a', atoi(str.c_str() + 2) - 1);
}
int m, n;
int D;
int sign[20][20];
int cant[20][20][20][20];
int cst[20][20][20][20];
pair<int, int> sp, gp;
bool ok(int x, int y) { return x >= 0 && x < m && y >= 0 && y < n; }
void solve() {
static int dist[101][20][20][4];
fill_n(***dist, 101 * 20 * 20 * 4, INF);
dist[0][sp.first][sp.second][3] = 0;
for (int t = 0; t < 100; t++) {
for (int x = 0; x < m; x++) {
for (int y = 0; y < n; y++) {
for (int d = 0; d < 4; d++) {
if (dist[t][x][y][d] == INF)
continue;
for (int dd = 3; dd <= 5; dd++) {
int dir = (d + dd) % 4;
int nx = x + dx[dir];
int ny = y + dy[dir];
if (!ok(nx, ny))
continue;
if (cant[x][y][nx][ny])
continue;
int nt = t + D + cst[x][y][nx][ny];
if (nt > 100)
continue;
if (sign[nx][ny]) {
if ((nt / sign[nx][ny] + dir) % 2 == 1) {
continue;
}
}
dist[nt][nx][ny][dir] = min(dist[nt][nx][ny][dir], nt);
}
}
}
}
}
for (int t = 0;; t++) {
for (int d = 0; d < 4; d++) {
if (dist[t][gp.first][gp.second][d] < INF) {
printf("%d\n", dist[t][gp.first][gp.second][d]);
return;
}
}
}
}
int main() {
while (scanf("%d %d", &m, &n), m) {
memset(sign, 0, sizeof(sign));
memset(cant, 0, sizeof(cant));
memset(cst, 0, sizeof(cst));
scanf("%d", &D);
int ns;
scanf("%d", &ns);
for (int i = 0; i < ns; i++) {
pair<int, int> pos = inputPos();
int k;
scanf("%d", &k);
sign[pos.first][pos.second] = k;
}
int nc;
scanf("%d", &nc);
for (int i = 0; i < nc; i++) {
pair<int, int> pos1 = inputPos();
pair<int, int> pos2 = inputPos();
cant[pos1.first][pos1.second][pos2.first][pos2.second] = true;
cant[pos2.first][pos2.second][pos1.first][pos1.second] = true;
}
int nj;
scanf("%d", &nj);
for (int i = 0; i < nj; i++) {
pair<int, int> pos1 = inputPos();
pair<int, int> pos2 = inputPos();
int d;
scanf("%d", &d);
cst[pos1.first][pos1.second][pos2.first][pos2.second] += d;
cst[pos2.first][pos2.second][pos1.first][pos1.second] += d;
}
sp = inputPos();
gp = inputPos();
solve();
}
} | insert | 39 | 39 | 39 | 41 | -11 | |
p00195 | C++ | Time Limit Exceeded | #include <cstdio>
#define buf 10000
using namespace std;
int B[10];
void BubbleSort(void);
int main(void) {
int i, n, m, A[20001];
char str[buf];
for (;;) {
for (i = 0; i < 5; i++) {
fgets(str, sizeof(str), stdin);
sscanf(str, "%d %d", &n, &m);
if (n == 0 && m == 0)
break;
B[i] = n + m;
A[n + m] = i;
}
BubbleSort();
printf("%c %d\n", 'A' + A[B[0]], B[0]);
}
return 0;
}
void BubbleSort(void) {
int i, j, tmp;
for (i = 0; i < 4; i++) {
for (j = 4; j > i; j--) {
if (B[j] > B[j - 1]) {
tmp = B[j - 1];
B[j - 1] = B[j];
B[j] = tmp;
}
}
}
} | #include <cstdio>
#define buf 10000
using namespace std;
int B[10];
void BubbleSort(void);
int main(void) {
int i, n, m, A[20001];
char str[buf];
for (;;) {
for (i = 0; i < 5; i++) {
fgets(str, sizeof(str), stdin);
sscanf(str, "%d %d", &n, &m);
if (n == 0 && m == 0)
return 0;
B[i] = n + m;
A[n + m] = i;
}
BubbleSort();
printf("%c %d\n", 'A' + A[B[0]], B[0]);
}
return 0;
}
void BubbleSort(void) {
int i, j, tmp;
for (i = 0; i < 4; i++) {
for (j = 4; j > i; j--) {
if (B[j] > B[j - 1]) {
tmp = B[j - 1];
B[j - 1] = B[j];
B[j] = tmp;
}
}
}
} | replace | 15 | 16 | 15 | 16 | TLE | |
p00197 | C++ | Runtime Error | #include <stdio.h>
int main() {
int a, b, n, step = 1;
while (scanf("%d %d", &a, &b) != EOF) {
if (a <= b) {
n = a;
a = b;
b = n;
}
if (a % b == 0)
n = b;
while (a % b != 0) {
step++;
if (a % b == 0) {
n = b;
break;
}
a = a % b;
n = a;
a = b;
b = n;
}
printf("%d %d\n", n, step);
step = 1;
}
return 0;
} | #include <stdio.h>
int main() {
int a, b, n, step = 1;
while (scanf("%d %d", &a, &b) != 0) {
if (a == 0 || b == 0)
break;
if (a <= b) {
n = a;
a = b;
b = n;
}
if (a % b == 0)
n = b;
while (a % b != 0) {
step++;
if (a % b == 0) {
n = b;
break;
}
a = a % b;
n = a;
a = b;
b = n;
}
printf("%d %d\n", n, step);
step = 1;
}
return 0;
} | replace | 4 | 5 | 4 | 7 | -8 | |
p00197 | C++ | Time Limit Exceeded | #include <cstdio>
#include <iostream>
using namespace std;
void change(int *, int *);
int main(void) {
int x, y;
while (scanf("%d %d", &x, &y)) {
int count = 0;
if (x < y)
change(&x, &y);
while (y) {
count++;
x %= y;
change(&x, &y);
}
cout << x << ' ' << count << endl;
}
return 0;
}
void change(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
} | #include <cstdio>
#include <iostream>
using namespace std;
void change(int *, int *);
int main(void) {
int x, y;
while (1) {
scanf("%d %d", &x, &y);
if (x == 0 && y == 0)
break;
int count = 0;
if (x < y)
change(&x, &y);
while (y) {
count++;
x %= y;
change(&x, &y);
}
cout << x << ' ' << count << endl;
}
return 0;
}
void change(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
} | replace | 10 | 11 | 10 | 14 | TLE | |
p00197 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
while (1) {
long long x, y, cnt = 0;
cin >> x >> y;
if (x < y)
swap(x, y);
while (1) {
cnt++;
x %= y;
swap(x, y);
if (y == 0)
break;
}
printf("%lld %lld\n", x, cnt);
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
while (1) {
long long x, y, cnt = 0;
cin >> x >> y;
if (x + y == 0)
break;
if (x < y)
swap(x, y);
while (1) {
cnt++;
x %= y;
swap(x, y);
if (y == 0)
break;
}
printf("%lld %lld\n", x, cnt);
}
return 0;
} | insert | 8 | 8 | 8 | 10 | -8 | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pi acos(-1)
#define all(v) v.begin(), v.end()
using namespace std;
const double eps = 1e-8;
const double INF = 1e5;
typedef pair<int, int> P;
typedef double Real;
typedef complex<Real> Point;
typedef vector<Point> G;
double cross(const Point &a, const Point &b) { // calculate ad-bc(vector
// product)
return imag(conj(a) * b);
}
struct L : public vector<Point> { // line data
L(const Point &a, const Point &b) {
push_back(a);
push_back(b);
}
};
int main() {
int n, m;
while (1) {
cin >> n >> m;
if (!n && !m)
break;
int cost[m + 1][m + 1], ti[m + 1][m + 1];
rep(i, m) rep(j, n) cost[i][j] = ti[i][j] = INF;
int a, b, c, t;
rep(i, n) {
cin >> a >> b;
cin >> cost[a][b] >> ti[a][b];
cost[b][a] = cost[a][b];
ti[b][a] = ti[a][b];
}
loop(k, 1, m + 1) {
loop(i, 1, m + 1) {
loop(j, 1, m + 1) {
cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
ti[i][j] = min(ti[i][j], ti[i][k] + ti[k][j]);
}
}
}
int k, p, q, r;
cin >> k;
rep(i, k) {
cin >> p >> q >> r;
if (r == 0) {
cout << cost[p][q] << endl;
} else {
cout << ti[p][q] << endl;
}
}
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pi acos(-1)
#define all(v) v.begin(), v.end()
using namespace std;
const double eps = 1e-8;
const double INF = 1e5;
typedef pair<int, int> P;
typedef double Real;
typedef complex<Real> Point;
typedef vector<Point> G;
double cross(const Point &a, const Point &b) { // calculate ad-bc(vector
// product)
return imag(conj(a) * b);
}
struct L : public vector<Point> { // line data
L(const Point &a, const Point &b) {
push_back(a);
push_back(b);
}
};
int main() {
int n, m;
while (1) {
cin >> n >> m;
if (!n && !m)
break;
long long cost[m + 1][m + 1], ti[m + 1][m + 1];
rep(i, m + 1) rep(j, m + 1) cost[i][j] = ti[i][j] = INF;
int a, b, c, t;
rep(i, n) {
cin >> a >> b;
cin >> cost[a][b] >> ti[a][b];
cost[b][a] = cost[a][b];
ti[b][a] = ti[a][b];
}
loop(k, 1, m + 1) {
loop(i, 1, m + 1) {
loop(j, 1, m + 1) {
cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
ti[i][j] = min(ti[i][j], ti[i][k] + ti[k][j]);
}
}
}
int k, p, q, r;
cin >> k;
rep(i, k) {
cin >> p >> q >> r;
if (r == 0) {
cout << cost[p][q] << endl;
} else {
cout << ti[p][q] << endl;
}
}
}
return 0;
} | replace | 44 | 46 | 44 | 46 | 0 | |
p00200 | C++ | Time Limit Exceeded | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
class S {
public:
S(int pos, int cost) {
p = pos;
c = cost;
}
int p, c;
};
bool operator<(S a, S b) { return a.c > b.c; }
int m;
vector<S> edge[100][2];
void add_edge(int a, int b, int c, int t) {
edge[a][0].push_back(S(b, c));
edge[a][1].push_back(S(b, t));
edge[b][0].push_back(S(a, c));
edge[b][1].push_back(S(a, t));
}
int solve(int p, int q, int r) {
int ans = 0;
bool use[100];
for (int i = 0; i < 100; i++) {
use[i] = 0;
}
priority_queue<S> que;
que.push(S(p, 0));
while (!que.empty()) {
S n = que.top();
que.pop();
if (n.p == q) {
ans = n.c;
break;
}
if (use[n.p]) {
continue;
}
use[n.p] = 1;
for (int i = 0; i < edge[n.p][r].size(); i++) {
if (!use[edge[n.p][r][i].p]) {
que.push(S(edge[n.p][r][i].p, n.c + edge[n.p][r][i].c));
}
}
}
return ans;
}
int main() {
int n;
while (cin >> n >> m, n != 0 || m != 0) {
for (int i = 0; i < n; i++) {
int a, b, c, t;
cin >> a >> b >> c >> t;
a--;
b--;
add_edge(a, b, c, t);
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int p, q, r;
cin >> p >> q >> r;
p--;
q--;
cout << solve(p, q, r) << endl;
}
}
} | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
class S {
public:
S(int pos, int cost) {
p = pos;
c = cost;
}
int p, c;
};
bool operator<(S a, S b) { return a.c > b.c; }
int m;
vector<S> edge[100][2];
void add_edge(int a, int b, int c, int t) {
edge[a][0].push_back(S(b, c));
edge[a][1].push_back(S(b, t));
edge[b][0].push_back(S(a, c));
edge[b][1].push_back(S(a, t));
}
int solve(int p, int q, int r) {
int ans = 0;
bool use[100];
for (int i = 0; i < 100; i++) {
use[i] = 0;
}
priority_queue<S> que;
que.push(S(p, 0));
while (!que.empty()) {
S n = que.top();
que.pop();
if (n.p == q) {
ans = n.c;
break;
}
if (use[n.p]) {
continue;
}
use[n.p] = 1;
for (int i = 0; i < edge[n.p][r].size(); i++) {
if (!use[edge[n.p][r][i].p]) {
que.push(S(edge[n.p][r][i].p, n.c + edge[n.p][r][i].c));
}
}
}
return ans;
}
int main() {
int n;
while (cin >> n >> m, n != 0 || m != 0) {
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 2; j++) {
edge[i][j].clear();
}
}
for (int i = 0; i < n; i++) {
int a, b, c, t;
cin >> a >> b >> c >> t;
a--;
b--;
add_edge(a, b, c, t);
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int p, q, r;
cin >> p >> q >> r;
p--;
q--;
cout << solve(p, q, r) << endl;
}
}
} | insert | 59 | 59 | 59 | 64 | TLE | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
struct Edge {
int to, cost, time;
};
typedef pair<int, int> P;
const int MAX_V = 110;
const int INF = 1 << 30;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
while (cin >> n >> m && n != 0 && m != 0) {
vector<Edge> vec[MAX_V];
int d[MAX_V];
for (int i = 0; i < n; ++i) {
int a, b, c, t;
cin >> a >> b >> c >> t;
--a, --b;
vec[a].push_back(Edge{b, c, t});
vec[b].push_back(Edge{a, c, t});
}
int k;
cin >> k;
while (k > 0) {
--k;
priority_queue<P, vector<P>, greater<P>> que;
fill(d, d + n, INF);
int p, q, r;
cin >> p >> q >> r;
--p, --q;
d[p] = 0;
que.push(P(0, p));
while (!que.empty()) {
P p0 = que.top();
que.pop();
int v = p0.second;
if (d[v] < p0.first)
continue;
for (int i = 0; i < vec[v].size(); ++i) {
Edge e = vec[v][i];
if (r == 0 && d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
if (r == 1 && d[e.to] > d[v] + e.time) {
d[e.to] = d[v] + e.time;
que.push(P(d[e.to], e.to));
}
}
}
cout << d[q] << endl;
}
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
struct Edge {
int to, cost, time;
};
typedef pair<int, int> P;
const int MAX_V = 3010;
const int INF = 1 << 30;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
while (cin >> n >> m && n != 0 && m != 0) {
vector<Edge> vec[MAX_V];
int d[MAX_V];
for (int i = 0; i < n; ++i) {
int a, b, c, t;
cin >> a >> b >> c >> t;
--a, --b;
vec[a].push_back(Edge{b, c, t});
vec[b].push_back(Edge{a, c, t});
}
int k;
cin >> k;
while (k > 0) {
--k;
priority_queue<P, vector<P>, greater<P>> que;
fill(d, d + n, INF);
int p, q, r;
cin >> p >> q >> r;
--p, --q;
d[p] = 0;
que.push(P(0, p));
while (!que.empty()) {
P p0 = que.top();
que.pop();
int v = p0.second;
if (d[v] < p0.first)
continue;
for (int i = 0; i < vec[v].size(); ++i) {
Edge e = vec[v][i];
if (r == 0 && d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
if (r == 1 && d[e.to] > d[v] + e.time) {
d[e.to] = d[v] + e.time;
que.push(P(d[e.to], e.to));
}
}
}
cout << d[q] << endl;
}
}
return 0;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p00200 | C++ | Time Limit Exceeded | #include <functional>
#include <iostream>
#include <limits.h>
#include <queue>
#include <vector>
using namespace std;
struct Edge {
int to, cost;
Edge() {}
Edge(int to, int cost) : to(to), cost(cost) {}
};
int dijkstra(const vector<vector<Edge>> vve, int s, int t) {
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
que;
pair<int, int> pii;
pii.first = 0;
pii.second = s;
que.push(pii);
vector<int> dist(vve.size(), INT_MAX);
while (!que.empty()) {
int cost = que.top().first;
int now = que.top().second;
que.pop();
if (dist[now] <= cost) {
continue;
}
dist[now] = cost;
for (int i = 0; i < vve[now].size(); i++) {
que.push(make_pair(vve[now][i].cost + cost, vve[now][i].to));
}
}
return dist[t];
}
int main() {
int n, m;
while (true) {
cin >> n >> m;
if (n == 0 && m == 0) {
break;
}
vector<vector<Edge>> coin(m);
vector<vector<Edge>> time(m);
for (int i = 0; i < n; i++) {
int a, b, c, t;
cin >> a >> b >> c >> t;
coin[a - 1].push_back(Edge(b - 1, c));
coin[b - 1].push_back(Edge(a - 1, c));
time[a - 1].push_back(Edge(b - 1, t));
time[b - 1].push_back(Edge(a - 1, t));
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int p, q, r;
cin >> p >> q >> r;
int ans;
if (r) {
ans = dijkstra(time, p - 1, q - 1);
} else {
ans = dijkstra(coin, p - 1, q - 1);
}
cout << ans << endl;
}
}
} | #include <functional>
#include <iostream>
#include <limits.h>
#include <queue>
#include <vector>
using namespace std;
struct Edge {
int to, cost;
Edge() {}
Edge(int to, int cost) : to(to), cost(cost) {}
};
int dijkstra(const vector<vector<Edge>> vve, int s, int t) {
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
que;
pair<int, int> pii;
pii.first = 0;
pii.second = s;
que.push(pii);
vector<int> dist(vve.size(), INT_MAX);
while (!que.empty()) {
int cost = que.top().first;
int now = que.top().second;
que.pop();
if (dist[now] <= cost) {
if (dist[t] < cost) {
break;
}
continue;
}
dist[now] = cost;
for (int i = 0; i < vve[now].size(); i++) {
que.push(make_pair(vve[now][i].cost + cost, vve[now][i].to));
}
}
return dist[t];
}
int main() {
int n, m;
while (true) {
cin >> n >> m;
if (n == 0 && m == 0) {
break;
}
vector<vector<Edge>> coin(m);
vector<vector<Edge>> time(m);
for (int i = 0; i < n; i++) {
int a, b, c, t;
cin >> a >> b >> c >> t;
coin[a - 1].push_back(Edge(b - 1, c));
coin[b - 1].push_back(Edge(a - 1, c));
time[a - 1].push_back(Edge(b - 1, t));
time[b - 1].push_back(Edge(a - 1, t));
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int p, q, r;
cin >> p >> q >> r;
int ans;
if (r) {
ans = dijkstra(time, p - 1, q - 1);
} else {
ans = dijkstra(coin, p - 1, q - 1);
}
cout << ans << endl;
}
}
} | insert | 31 | 31 | 31 | 34 | TLE | |
p00200 | C++ | Runtime Error | // #include "bipartite-graph.hpp"
// #include <vector>
// using namespace std;
// int main(){
// vector<vector<int> > G(4);
// cout << bipartite_graph(G) << endl;
// }
// AOJ0200
#include <iostream>
#include <vector>
using namespace std;
struct edge {
edge() {}
edge(int to, int from, int cost, int time)
: to(to), from(from), cost(cost), time(time) {}
int to, from, cost, time;
};
const int INF = 100000000;
int d[101];
edge es[2000];
int n, m;
// 頂点sから各頂点への最短経路を求める
void bellman_ford(int s, bool r) {
for (int i = 0; i < m; ++i)
d[i] = INF;
d[s] = 0;
while (1) {
bool update = false;
for (int i = 0; i < n * 2; ++i) {
edge e = es[i];
int cost;
if (r)
cost = e.time;
else
cost = e.cost;
if (d[e.from] != INF && d[e.to] > d[e.from] + cost) {
d[e.to] = d[e.from] + cost;
update = true;
}
}
if (!update)
break;
}
}
int main() {
while (cin >> n >> m, n + m) {
for (int i = 0; i < n; ++i) {
int to, from, cost, time;
cin >> to >> from >> cost >> time;
es[i] = edge(to - 1, from - 1, cost, time);
es[n + i] = edge(from - 1, to - 1, cost, time);
}
int k;
cin >> k;
for (int i = 0; i < k; ++i) {
int p, q, r;
cin >> p >> q >> r;
bellman_ford(p - 1, r);
cout << d[q - 1] << endl;
}
}
} | // #include "bipartite-graph.hpp"
// #include <vector>
// using namespace std;
// int main(){
// vector<vector<int> > G(4);
// cout << bipartite_graph(G) << endl;
// }
// AOJ0200
#include <iostream>
#include <vector>
using namespace std;
struct edge {
edge() {}
edge(int to, int from, int cost, int time)
: to(to), from(from), cost(cost), time(time) {}
int to, from, cost, time;
};
const int INF = 100000000;
int d[101];
edge es[7000];
int n, m;
// 頂点sから各頂点への最短経路を求める
void bellman_ford(int s, bool r) {
for (int i = 0; i < m; ++i)
d[i] = INF;
d[s] = 0;
while (1) {
bool update = false;
for (int i = 0; i < n * 2; ++i) {
edge e = es[i];
int cost;
if (r)
cost = e.time;
else
cost = e.cost;
if (d[e.from] != INF && d[e.to] > d[e.from] + cost) {
d[e.to] = d[e.from] + cost;
update = true;
}
}
if (!update)
break;
}
}
int main() {
while (cin >> n >> m, n + m) {
for (int i = 0; i < n; ++i) {
int to, from, cost, time;
cin >> to >> from >> cost >> time;
es[i] = edge(to - 1, from - 1, cost, time);
es[n + i] = edge(from - 1, to - 1, cost, time);
}
int k;
cin >> k;
for (int i = 0; i < k; ++i) {
int p, q, r;
cin >> p >> q >> r;
bellman_ford(p - 1, r);
cout << d[q - 1] << endl;
}
}
} | replace | 23 | 24 | 23 | 24 | 0 | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#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 = 211;
int dpcost[N][N];
int dptime[N][N];
int main() {
A:;
int n, m;
cin >> n >> m;
if (n == 0)
return 0;
const int INF = 1000000000;
rep(i, N) rep(j, N) dpcost[i][j] = dptime[i][j] = INF;
rep(i, N) dpcost[i][i] = dptime[i][i] = 0;
rep(i, n) {
int a, b, c, d;
cin >> a >> b >> c >> d;
dpcost[a][b] = c;
dpcost[b][a] = c;
dptime[a][b] = d;
dptime[b][a] = d;
}
reps(k, 1, n + 1) reps(i, 1, n + 1) reps(j, 1, n + 1) {
dpcost[i][j] = min(dpcost[i][j], dpcost[i][k] + dpcost[k][j]);
dptime[i][j] = min(dptime[i][j], dptime[i][k] + dptime[k][j]);
}
int r;
cin >> r;
rep(i, r) {
int a, b, c;
cin >> a >> b >> c;
if (c == 0)
printf("%d\n", dpcost[a][b]);
if (c == 1)
printf("%d\n", dptime[a][b]);
}
goto A;
} | #include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#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 = 211;
int dpcost[N][N];
int dptime[N][N];
int main() {
A:;
int n, m;
cin >> n >> m;
if (n == 0)
return 0;
const int INF = 1000000000;
rep(i, N) rep(j, N) dpcost[i][j] = dptime[i][j] = INF;
rep(i, N) dpcost[i][i] = dptime[i][i] = 0;
rep(i, n) {
int a, b, c, d;
cin >> a >> b >> c >> d;
dpcost[a][b] = c;
dpcost[b][a] = c;
dptime[a][b] = d;
dptime[b][a] = d;
}
reps(k, 1, m + 1) reps(i, 1, m + 1) reps(j, 1, m + 1) {
dpcost[i][j] = min(dpcost[i][j], dpcost[i][k] + dpcost[k][j]);
dptime[i][j] = min(dptime[i][j], dptime[i][k] + dptime[k][j]);
}
int r;
cin >> r;
rep(i, r) {
int a, b, c;
cin >> a >> b >> c;
if (c == 0)
printf("%d\n", dpcost[a][b]);
if (c == 1)
printf("%d\n", dptime[a][b]);
}
goto A;
} | replace | 41 | 42 | 41 | 42 | 0 | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define sint(i) scanf("%d", &i);
#define sintt(i, j) scanf("%d%d", &i, &j);
#define sinttt(i, j, k) scanf("%d%d%d", &i, &j, &k);
#define sintttt(i, j, k, m) scanf("%d%d%d%d", &i, &j, &k, &m);
#define INF 1010000000
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
while (1) {
int n, m;
int time[101][101];
int cost[101][101];
rep(i, 101) rep(j, 101) time[i][j] = cost[i][j] = INF;
sintt(n, m);
if (n == 0)
break;
rep(i, n) {
int a, b, c, d;
sintttt(a, b, c, d);
time[a][b] = d;
time[b][a] = d;
cost[a][b] = c;
cost[b][a] = c;
}
reps(k, n) {
reps(i, n) {
reps(j, n) {
cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
}
}
}
int q;
sint(q);
rep(i, q) {
int a, b, c;
sinttt(a, b, c);
if (c == 1) {
printf("%d\n", time[a][b]);
} else {
printf("%d\n", cost[a][b]);
}
}
}
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define sint(i) scanf("%d", &i);
#define sintt(i, j) scanf("%d%d", &i, &j);
#define sinttt(i, j, k) scanf("%d%d%d", &i, &j, &k);
#define sintttt(i, j, k, m) scanf("%d%d%d%d", &i, &j, &k, &m);
#define INF 1010000000
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
while (1) {
int n, m;
int time[101][101];
int cost[101][101];
rep(i, 101) rep(j, 101) time[i][j] = cost[i][j] = INF;
sintt(n, m);
if (n == 0)
break;
rep(i, n) {
int a, b, c, d;
sintttt(a, b, c, d);
time[a][b] = d;
time[b][a] = d;
cost[a][b] = c;
cost[b][a] = c;
}
reps(k, m) {
reps(i, m) {
reps(j, m) {
cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
}
}
}
int q;
sint(q);
rep(i, q) {
int a, b, c;
sinttt(a, b, c);
if (c == 1) {
printf("%d\n", time[a][b]);
} else {
printf("%d\n", cost[a][b]);
}
}
}
} | replace | 44 | 47 | 44 | 47 | 0 | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
const int INF = 1 << 20;
const int N = 101;
int cost[N][N];
int tim[N][N];
using namespace std;
int main() {
int n, m, a, b, co, ti, r;
while (1) {
cin >> n >> m;
if (n == 0 && m == 0)
return 0;
for (int i = 0; i < m + 2; i++) {
for (int i2 = 0; i2 < m + 2; i2++) {
cost[i][i2] = INF;
tim[i][i2] = INF;
}
}
for (int i = 0; i < n; i++) {
cin >> a >> b >> co >> ti;
cost[a][b] = co;
cost[b][a] = co;
tim[a][b] = ti;
tim[b][a] = ti;
}
for (int k = 0; k < m + 1; ++k)
for (int i = 0; i < m + 1; ++i)
for (int j = 0; j < m + 1; ++j) {
cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
tim[i][j] = min(tim[i][j], tim[i][k] + tim[k][j]);
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> a >> b >> r;
if (r == 0) {
cout << cost[a][b] << endl;
} else
cout << tim[a][b] << endl;
}
}
} | #include <algorithm>
#include <iostream>
const int INF = 1 << 20;
const int N = 101;
int cost[N][N];
int tim[N][N];
using namespace std;
int main() {
int n, m, a, b, co, ti, r;
while (1) {
cin >> n >> m;
if (n == 0 && m == 0)
return 0;
for (int i = 0; i < N; i++) {
for (int i2 = 0; i2 < N; i2++) {
cost[i][i2] = INF;
tim[i][i2] = INF;
}
}
for (int i = 0; i < n; i++) {
cin >> a >> b >> co >> ti;
cost[a][b] = co;
cost[b][a] = co;
tim[a][b] = ti;
tim[b][a] = ti;
}
for (int k = 0; k < m + 1; ++k)
for (int i = 0; i < m + 1; ++i)
for (int j = 0; j < m + 1; ++j) {
cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
tim[i][j] = min(tim[i][j], tim[i][k] + tim[k][j]);
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> a >> b >> r;
if (r == 0) {
cout << cost[a][b] << endl;
} else
cout << tim[a][b] << endl;
}
}
} | replace | 13 | 15 | 13 | 15 | 0 | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int road1[111][111];
int road2[111][111];
int n, m;
int a, b, cost, time;
int p, q, r;
int x;
while (1) {
for (int i = 0; i < 111; i++) {
for (int j = 0; j < 111; j++) {
road1[i][j] = road2[i][j] = 111111111;
}
}
cin >> n >> m;
if (n == 0 && m == 0)
break;
for (int i = 0; i < n; i++) {
cin >> a >> b >> cost >> time;
road1[a][b] = road1[b][a] = cost;
road2[a][b] = road2[b][a] = time;
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (road1[i][j] > road1[i][k] + road1[k][j]) {
road1[i][j] = road1[i][k] + road1[k][j];
}
if (road2[i][j] > road2[i][k] + road2[k][j]) {
road2[i][j] = road2[i][k] + road2[k][j];
}
}
}
}
cin >> x;
for (int i = 0; i < x; i++) {
cin >> p >> q >> r;
cout << (r == 0 ? road1[p][q] : road2[p][q]) << endl;
}
}
} | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int road1[111][111];
int road2[111][111];
int n, m;
int a, b, cost, time;
int p, q, r;
int x;
while (1) {
for (int i = 0; i < 111; i++) {
for (int j = 0; j < 111; j++) {
road1[i][j] = road2[i][j] = 111111111;
}
}
cin >> n >> m;
if (n == 0 && m == 0)
break;
for (int i = 0; i < n; i++) {
cin >> a >> b >> cost >> time;
road1[a][b] = road1[b][a] = cost;
road2[a][b] = road2[b][a] = time;
}
for (int k = 1; k <= m; k++) {
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= m; j++) {
if (road1[i][j] > road1[i][k] + road1[k][j]) {
road1[i][j] = road1[i][k] + road1[k][j];
}
if (road2[i][j] > road2[i][k] + road2[k][j]) {
road2[i][j] = road2[i][k] + road2[k][j];
}
}
}
}
cin >> x;
for (int i = 0; i < x; i++) {
cin >> p >> q >> r;
cout << (r == 0 ? road1[p][q] : road2[p][q]) << endl;
}
}
} | replace | 25 | 28 | 25 | 28 | 0 | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define _m_ 1
const int INF = 1e9;
int cost[101][101];
int tim[101][101];
int d[101];
bool used[101];
int V;
void dijkstra(int s, int o) {
std::fill(d, d + V, INF);
std::fill(used, used + V, false);
d[s] = 0;
while (_m_) {
int v = -1;
for (int u = 0; u < V; ++u) {
if (!used[u] && (v == -1 || d[u] < d[v]))
v = u;
}
if (v == -1)
break;
used[v] = true;
if (o == 0) {
for (int u = 0; u < V; ++u) {
d[u] = std::min(d[u], d[v] + cost[v][u]);
}
} else {
for (int u = 0; u < V; ++u) {
d[u] = std::min(d[u], d[v] + tim[v][u]);
}
}
}
}
int main() {
int n, m;
while (scanf("%d %d", &n, &m), n + m) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cost[i][j] = INF;
tim[i][j] = INF;
}
}
for (int i = 0; i < n; ++i) {
int a, b, co, ti;
scanf("%d %d %d %d", &a, &b, &co, &ti);
a--, b--;
cost[a][b] = co, cost[b][a] = co;
tim[a][b] = ti, tim[b][a] = ti;
}
V = m;
int k;
scanf("%d", &k);
for (int i = 0; i < k; ++i) {
int p, q, r;
scanf("%d %d %d", &p, &q, &r);
p--, q--;
dijkstra(p, r);
printf("%d\n", d[q]);
}
}
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define _m_ 1
const int INF = 1e9;
int cost[101][101];
int tim[101][101];
int d[101];
bool used[101];
int V;
void dijkstra(int s, int o) {
std::fill(d, d + V, INF);
std::fill(used, used + V, false);
d[s] = 0;
while (_m_) {
int v = -1;
for (int u = 0; u < V; ++u) {
if (!used[u] && (v == -1 || d[u] < d[v]))
v = u;
}
if (v == -1)
break;
used[v] = true;
if (o == 0) {
for (int u = 0; u < V; ++u) {
d[u] = std::min(d[u], d[v] + cost[v][u]);
}
} else {
for (int u = 0; u < V; ++u) {
d[u] = std::min(d[u], d[v] + tim[v][u]);
}
}
}
}
int main() {
int n, m;
while (scanf("%d %d", &n, &m), n + m) {
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
cost[i][j] = INF;
tim[i][j] = INF;
}
}
for (int i = 0; i < n; ++i) {
int a, b, co, ti;
scanf("%d %d %d %d", &a, &b, &co, &ti);
a--, b--;
cost[a][b] = co, cost[b][a] = co;
tim[a][b] = ti, tim[b][a] = ti;
}
V = m;
int k;
scanf("%d", &k);
for (int i = 0; i < k; ++i) {
int p, q, r;
scanf("%d %d %d", &p, &q, &r);
p--, q--;
dijkstra(p, r);
printf("%d\n", d[q]);
}
}
} | replace | 41 | 43 | 41 | 43 | 0 | |
p00200 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int MAX = 100000;
int main() {
while (true) {
int n, m;
cin >> n >> m;
int a, b, cost, timer;
int costs[m][m];
int timers[m][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
costs[i][j] = MAX;
timers[i][j] = MAX;
}
costs[i][i] = 0;
timers[i][i] = 0;
}
for (int i = 0; i < n; i++) {
cin >> a >> b >> cost >> timer;
costs[a - 1][b - 1] = cost;
timers[a - 1][b - 1] = timer;
costs[b - 1][a - 1] = cost;
timers[b - 1][a - 1] = timer;
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < m; k++) {
if (costs[j][k] > (costs[j][i] + costs[i][k])) {
costs[j][k] = costs[j][i] + costs[i][k];
costs[k][j] = costs[j][i] + costs[i][k];
}
if (timers[j][k] > (timers[j][i] + timers[i][k])) {
timers[j][k] = timers[j][i] + timers[i][k];
timers[k][j] = timers[j][i] + timers[i][k];
}
}
}
}
int k;
int p;
int q;
bool r;
cin >> k;
for (int i = 0; i < k; i++) {
cin >> p >> q >> r;
if (r) {
cout << timers[p - 1][q - 1] << endl;
} else if (!r) {
cout << costs[p - 1][q - 1] << endl;
}
}
}
return 0;
}
| #include <iostream>
using namespace std;
int MAX = 100000;
int main() {
while (true) {
int n, m;
cin >> n >> m;
if (!n && !m)
break;
int a, b, cost, timer;
int costs[m][m];
int timers[m][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
costs[i][j] = MAX;
timers[i][j] = MAX;
}
costs[i][i] = 0;
timers[i][i] = 0;
}
for (int i = 0; i < n; i++) {
cin >> a >> b >> cost >> timer;
costs[a - 1][b - 1] = cost;
timers[a - 1][b - 1] = timer;
costs[b - 1][a - 1] = cost;
timers[b - 1][a - 1] = timer;
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < m; k++) {
if (costs[j][k] > (costs[j][i] + costs[i][k])) {
costs[j][k] = costs[j][i] + costs[i][k];
costs[k][j] = costs[j][i] + costs[i][k];
}
if (timers[j][k] > (timers[j][i] + timers[i][k])) {
timers[j][k] = timers[j][i] + timers[i][k];
timers[k][j] = timers[j][i] + timers[i][k];
}
}
}
}
int k;
int p;
int q;
bool r;
cin >> k;
for (int i = 0; i < k; i++) {
cin >> p >> q >> r;
if (r) {
cout << timers[p - 1][q - 1] << endl;
} else if (!r) {
cout << costs[p - 1][q - 1] << endl;
}
}
}
return 0;
}
| insert | 7 | 7 | 7 | 9 | TLE | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#define INF (1145141919)
using namespace std;
int n;
int from[3000];
int to[3000];
int cost[3000][2];
int d[100];
int r;
void Search(int s) {
for (int i = 0; i < 100; i++) {
d[i] = INF;
}
d[s] = 0;
while (true) {
bool update = false;
for (int i = 0; i < n * 2; i++) {
if (d[from[i]] != INF && d[to[i]] > d[from[i]] + cost[i][r]) {
d[to[i]] = d[from[i]] + cost[i][r];
update = true;
}
}
if (!update) {
break;
}
}
}
int main() {
int m;
while (scanf("%d %d", &n, &m), n + m) {
for (int i = 0; i < n; i++) {
int a, b, c, t;
scanf("%d %d %d %d", &a, &b, &c, &t);
a--;
b--;
from[i * 2] = a;
to[i * 2] = b;
cost[i * 2][0] = c;
cost[i * 2][1] = t;
from[i * 2 + 1] = b;
to[i * 2 + 1] = a;
cost[i * 2 + 1][0] = c;
cost[i * 2 + 1][1] = t;
}
int k;
scanf("%d", &k);
for (int i = 0; i < k; i++) {
int p, q;
scanf("%d %d %d", &p, &q, &r);
p--;
q--;
Search(p);
printf("%d\n", d[q]);
}
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#define INF (1145141919)
using namespace std;
int n;
int from[6000];
int to[6000];
int cost[6000][2];
int d[100];
int r;
void Search(int s) {
for (int i = 0; i < 100; i++) {
d[i] = INF;
}
d[s] = 0;
while (true) {
bool update = false;
for (int i = 0; i < n * 2; i++) {
if (d[from[i]] != INF && d[to[i]] > d[from[i]] + cost[i][r]) {
d[to[i]] = d[from[i]] + cost[i][r];
update = true;
}
}
if (!update) {
break;
}
}
}
int main() {
int m;
while (scanf("%d %d", &n, &m), n + m) {
for (int i = 0; i < n; i++) {
int a, b, c, t;
scanf("%d %d %d %d", &a, &b, &c, &t);
a--;
b--;
from[i * 2] = a;
to[i * 2] = b;
cost[i * 2][0] = c;
cost[i * 2][1] = t;
from[i * 2 + 1] = b;
to[i * 2 + 1] = a;
cost[i * 2 + 1][0] = c;
cost[i * 2 + 1][1] = t;
}
int k;
scanf("%d", &k);
for (int i = 0; i < k; i++) {
int p, q;
scanf("%d %d %d", &p, &q, &r);
p--;
q--;
Search(p);
printf("%d\n", d[q]);
}
}
return 0;
} | replace | 8 | 11 | 8 | 11 | 0 | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
#define MAX 256
#define INF 1000000
struct edge {
int money;
int time;
};
int main() {
int n, m;
int count;
edge G[120][120];
while (cin >> n >> m, n || m) {
for (int i = 0; i < 120; i++) {
for (int j = 0; j < 120; j++) {
G[i][j].money = INF;
G[i][j].time = INF;
}
}
for (int i = 0; i < 120; i++) {
G[i][i].money = 0;
G[i][i].time = 0;
}
for (int i = 0; i < n; i++) {
int a, b, mo, t;
cin >> a >> b >> mo >> t;
G[a][b].time = t;
G[a][b].money = mo;
G[b][a].time = t;
G[b][a].money = mo;
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
G[i][j].time = min(G[i][j].time, G[i][k].time + G[k][j].time);
G[i][j].money = min(G[i][j].money, G[i][k].money + G[k][j].money);
}
}
}
cin >> count;
for (int i = 0; i < count; i++) {
int p;
cin >> n >> m >> p;
if (p)
cout << G[n][m].time << endl;
else
cout << G[n][m].money << endl;
}
}
} | #include <algorithm>
#include <iostream>
using namespace std;
#define MAX 256
#define INF 1000000
struct edge {
int money;
int time;
};
int main() {
int n, m;
int count;
edge G[120][120];
while (cin >> n >> m, n || m) {
for (int i = 0; i < 120; i++) {
for (int j = 0; j < 120; j++) {
G[i][j].money = INF;
G[i][j].time = INF;
}
}
for (int i = 0; i < 120; i++) {
G[i][i].money = 0;
G[i][i].time = 0;
}
for (int i = 0; i < n; i++) {
int a, b, mo, t;
cin >> a >> b >> mo >> t;
G[a][b].time = t;
G[a][b].money = mo;
G[b][a].time = t;
G[b][a].money = mo;
}
for (int k = 1; k <= m; k++) {
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= m; j++) {
G[i][j].time = min(G[i][j].time, G[i][k].time + G[k][j].time);
G[i][j].money = min(G[i][j].money, G[i][k].money + G[k][j].money);
}
}
}
cin >> count;
for (int i = 0; i < count; i++) {
int p;
cin >> n >> m >> p;
if (p)
cout << G[n][m].time << endl;
else
cout << G[n][m].money << endl;
}
}
} | replace | 39 | 42 | 39 | 42 | 0 | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
struct AboutTo;
class Node {
public:
int aa;
int Lowest;
bool IsSelected;
vector<AboutTo> ToCollection;
Node() {
Lowest = 2147483647;
IsSelected = false;
}
void Reset() {
Lowest = 2147483647;
IsSelected = false;
}
};
struct AboutTo {
Node *To;
int Cost;
int Time;
};
int GetShortest(const int a, const Node GoalNode, vector<Node> &aNodeCol,
const int IsTime) {
aNodeCol[a].IsSelected = true;
int TempMin = 2147483647;
int aNode;
for (auto i = aNodeCol.begin(); i != aNodeCol.end(); ++i) {
if ((*i).IsSelected) {
for (auto j = (*i).ToCollection.begin(); j != (*i).ToCollection.end();
++j) {
if (!(*((*j).To)).IsSelected) {
if (IsTime) {
if (TempMin > (*i).Lowest + (*j).Time) {
TempMin = (*i).Lowest + (*j).Time;
aNode = (*((*j).To)).aa;
}
} else {
if (TempMin > (*i).Lowest + (*j).Cost) {
TempMin = (*i).Lowest + (*j).Cost;
aNode = (*((*j).To)).aa;
}
}
}
}
}
}
aNodeCol[aNode].Lowest = TempMin;
if (aNode != GoalNode.aa) {
return GetShortest(aNode, GoalNode, aNodeCol, IsTime);
}
return TempMin;
}
int main() {
while (true) {
int n, m;
cin >> n >> m;
if (n == 0 && m == 0)
break;
static vector<Node> NodeCollection;
for (int i = 0; i < m; ++i) {
Node aNode;
aNode.aa = i;
NodeCollection.push_back(aNode);
}
for (int i = 0; i < n; ++i) {
int a, b, cost, time;
cin >> a >> b >> cost >> time;
NodeCollection[a - 1].ToCollection.push_back(
{&NodeCollection[b - 1], cost, time});
NodeCollection[b - 1].ToCollection.push_back(
{&NodeCollection[a - 1], cost, time});
}
/*
12 9
1 2 5 5
2 3 4 4
3 6 3 10
6 9 1 1
9 8 7 6
8 7 4 3
7 4 2 1
4 1 10 14
2 5 7 8
5 8 9 10
4 5 9 9
5 6 10 1
*/
int k;
cin >> k;
for (int i = 0; i < k; ++i) {
int p, q, r;
cin >> p >> q >> r;
NodeCollection[p - 1].Lowest = 0;
if (p == q) {
cout << 0 << endl;
} else {
cout << GetShortest(p - 1, NodeCollection[q - 1], NodeCollection, r)
<< endl;
}
for (auto i = NodeCollection.begin(); i != NodeCollection.end(); ++i) {
(*i).Reset();
}
}
}
return 0;
} | #include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
struct AboutTo;
class Node {
public:
int aa;
int Lowest;
bool IsSelected;
vector<AboutTo> ToCollection;
Node() {
Lowest = 2147483647;
IsSelected = false;
}
void Reset() {
Lowest = 2147483647;
IsSelected = false;
}
};
struct AboutTo {
Node *To;
int Cost;
int Time;
};
int GetShortest(const int a, const Node GoalNode, vector<Node> &aNodeCol,
const int IsTime) {
aNodeCol[a].IsSelected = true;
int TempMin = 2147483647;
int aNode;
for (auto i = aNodeCol.begin(); i != aNodeCol.end(); ++i) {
if ((*i).IsSelected) {
for (auto j = (*i).ToCollection.begin(); j != (*i).ToCollection.end();
++j) {
if (!(*((*j).To)).IsSelected) {
if (IsTime) {
if (TempMin > (*i).Lowest + (*j).Time) {
TempMin = (*i).Lowest + (*j).Time;
aNode = (*((*j).To)).aa;
}
} else {
if (TempMin > (*i).Lowest + (*j).Cost) {
TempMin = (*i).Lowest + (*j).Cost;
aNode = (*((*j).To)).aa;
}
}
}
}
}
}
aNodeCol[aNode].Lowest = TempMin;
if (aNode != GoalNode.aa) {
return GetShortest(aNode, GoalNode, aNodeCol, IsTime);
}
return TempMin;
}
int main() {
while (true) {
int n, m;
cin >> n >> m;
if (n == 0 && m == 0)
break;
vector<Node> NodeCollection;
for (int i = 0; i < m; ++i) {
Node aNode;
aNode.aa = i;
NodeCollection.push_back(aNode);
}
for (int i = 0; i < n; ++i) {
int a, b, cost, time;
cin >> a >> b >> cost >> time;
NodeCollection[a - 1].ToCollection.push_back(
{&NodeCollection[b - 1], cost, time});
NodeCollection[b - 1].ToCollection.push_back(
{&NodeCollection[a - 1], cost, time});
}
/*
12 9
1 2 5 5
2 3 4 4
3 6 3 10
6 9 1 1
9 8 7 6
8 7 4 3
7 4 2 1
4 1 10 14
2 5 7 8
5 8 9 10
4 5 9 9
5 6 10 1
*/
int k;
cin >> k;
for (int i = 0; i < k; ++i) {
int p, q, r;
cin >> p >> q >> r;
NodeCollection[p - 1].Lowest = 0;
if (p == q) {
cout << 0 << endl;
} else {
cout << GetShortest(p - 1, NodeCollection[q - 1], NodeCollection, r)
<< endl;
}
for (auto i = NodeCollection.begin(); i != NodeCollection.end(); ++i) {
(*i).Reset();
}
}
}
return 0;
} | replace | 68 | 69 | 68 | 69 | 0 | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
const int MAX = 1e5;
for (int n, m; cin >> n >> m, n;) {
int cost[m][m];
int time[m][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
cost[i][j] = MAX;
time[i][j] = MAX;
}
}
int a, b, c, t;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c >> t;
cost[max(a - 1, b - 1)][min(a - 1, b - 1)] = c;
time[max(a - 1, b - 1)][min(a - 1, b - 1)] = t;
}
for (int k = 0; k < m; k++) {
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= i; j++) {
int mai = max(i, k), mii = min(i, k);
int maj = max(j, k), mij = min(j, k);
if (cost[i][j] > cost[mai][mii] + cost[maj][mij])
cost[i][j] = cost[mai][mii] + cost[maj][mij];
if (time[i][j] > time[mai][mii] + time[maj][mij])
time[i][j] = time[mai][mii] + time[maj][mij];
}
}
}
int l, r;
cin >> l;
for (int i = 0; i < l; i++) {
cin >> a >> b >> r;
if (r)
cout << time[max(a - 1, b - 1)][min(a - 1, b - 1)] << endl;
else
cout << cost[max(a - 1, b - 1)][min(a - 1, b - 1)] << endl;
}
}
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
const int MAX = 1e5;
for (int n, m; cin >> n >> m, n;) {
int cost[m][m];
int time[m][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
cost[i][j] = MAX;
time[i][j] = MAX;
}
}
int a, b, c, t;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c >> t;
cost[max(a - 1, b - 1)][min(a - 1, b - 1)] = c;
time[max(a - 1, b - 1)][min(a - 1, b - 1)] = t;
}
for (int k = 0; k < m; k++) {
for (int i = 0; i < m; i++) {
for (int j = 0; j <= i; j++) {
int mai = max(i, k), mii = min(i, k);
int maj = max(j, k), mij = min(j, k);
if (cost[i][j] > cost[mai][mii] + cost[maj][mij])
cost[i][j] = cost[mai][mii] + cost[maj][mij];
if (time[i][j] > time[mai][mii] + time[maj][mij])
time[i][j] = time[mai][mii] + time[maj][mij];
}
}
}
int l, r;
cin >> l;
for (int i = 0; i < l; i++) {
cin >> a >> b >> r;
if (r)
cout << time[max(a - 1, b - 1)][min(a - 1, b - 1)] << endl;
else
cout << cost[max(a - 1, b - 1)][min(a - 1, b - 1)] << endl;
}
}
return 0;
} | replace | 21 | 22 | 21 | 22 | 0 | |
p00200 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
const int MAX = 1e5;
for (int n, m; cin >> n >> m, n;) {
int cost[m][m];
int time[m][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
cost[i][j] = MAX;
time[i][j] = MAX;
}
}
int a, b, c, t;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c >> t;
cost[a - 1][b - 1] = c;
cost[b - 1][a - 1] = c;
time[a - 1][b - 1] = t;
time[b - 1][a - 1] = t;
}
for (int k = 0; k < m; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (cost[i][j] > cost[i][k] + cost[k][j])
cost[i][j] = cost[i][k] + cost[k][j];
if (time[i][j] > time[i][k] + time[k][j])
time[i][j] = time[i][k] + time[k][j];
}
}
}
int l, r;
cin >> l;
for (int i = 0; i < l; i++) {
cin >> a >> b >> r;
if (r)
cout << time[a - 1][b - 1] << endl;
else
cout << cost[a - 1][b - 1] << endl;
}
}
return 0;
} | #include <iostream>
using namespace std;
int main() {
const int MAX = 1e5;
for (int n, m; cin >> n >> m, n;) {
int cost[m][m];
int time[m][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
cost[i][j] = MAX;
time[i][j] = MAX;
}
}
int a, b, c, t;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c >> t;
cost[a - 1][b - 1] = c;
cost[b - 1][a - 1] = c;
time[a - 1][b - 1] = t;
time[b - 1][a - 1] = t;
}
for (int k = 0; k < m; k++) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if (cost[i][j] > cost[i][k] + cost[k][j])
cost[i][j] = cost[i][k] + cost[k][j];
if (time[i][j] > time[i][k] + time[k][j])
time[i][j] = time[i][k] + time[k][j];
}
}
}
int l, r;
cin >> l;
for (int i = 0; i < l; i++) {
cin >> a >> b >> r;
if (r)
cout << time[a - 1][b - 1] << endl;
else
cout << cost[a - 1][b - 1] << endl;
}
}
return 0;
} | replace | 22 | 24 | 22 | 24 | 0 | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
constexpr int INF = 1000000000;
using Pair = std::pair<int, int>;
int n, m;
std::vector<Pair> cost[101];
std::vector<Pair> time_[101];
int d[101];
int dijkstra(int st, int gl, std::vector<Pair> *elm) {
std::fill(d, d + 101, INF);
d[st] = 0;
std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> queue;
queue.push({0, st});
while (!queue.empty()) {
auto p = queue.top();
queue.pop();
if (p.first > d[p.second]) {
continue;
}
for (int i = 0; i < elm[p.second].size(); ++i) {
auto e = elm[p.second][i];
// To, cost
if (d[e.first] > d[p.second] + e.second) {
d[e.first] = d[p.second] + e.second;
queue.push({d[e.first], e.first});
}
}
}
return d[gl];
}
int main() {
while (std::cin >> n >> m, n + m) {
int a, b, c, t;
for (int i = 0; i < n; ++i) {
cost[i].clear();
time_[i].clear();
}
for (int i = 0; i < n; ++i) {
std::cin >> a >> b >> c >> t;
--a, --b;
cost[a].push_back({b, c});
cost[b].push_back({a, c});
time_[a].push_back({b, t});
time_[b].push_back({a, t});
}
int k;
std::cin >> k;
for (int i = 0; i < k; ++i) {
int p, q, r;
std::cin >> p >> q >> r;
if (r == 0) {
std::cout << dijkstra(p - 1, q - 1, cost) << std::endl;
} else {
std::cout << dijkstra(p - 1, q - 1, time_) << std::endl;
}
}
}
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
constexpr int INF = 1000000000;
using Pair = std::pair<int, int>;
int n, m;
std::vector<Pair> cost[101];
std::vector<Pair> time_[101];
int d[101];
int dijkstra(int st, int gl, std::vector<Pair> *elm) {
std::fill(d, d + 101, INF);
d[st] = 0;
std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> queue;
queue.push({0, st});
while (!queue.empty()) {
auto p = queue.top();
queue.pop();
if (p.first > d[p.second]) {
continue;
}
for (int i = 0; i < elm[p.second].size(); ++i) {
auto e = elm[p.second][i];
// To, cost
if (d[e.first] > d[p.second] + e.second) {
d[e.first] = d[p.second] + e.second;
queue.push({d[e.first], e.first});
}
}
}
return d[gl];
}
int main() {
while (std::cin >> n >> m, n + m) {
int a, b, c, t;
for (int i = 0; i < m; ++i) {
cost[i].clear();
time_[i].clear();
}
for (int i = 0; i < n; ++i) {
std::cin >> a >> b >> c >> t;
--a, --b;
cost[a].push_back({b, c});
cost[b].push_back({a, c});
time_[a].push_back({b, t});
time_[b].push_back({a, t});
}
int k;
std::cin >> k;
for (int i = 0; i < k; ++i) {
int p, q, r;
std::cin >> p >> q >> r;
if (r == 0) {
std::cout << dijkstra(p - 1, q - 1, cost) << std::endl;
} else {
std::cout << dijkstra(p - 1, q - 1, time_) << std::endl;
}
}
}
} | replace | 51 | 52 | 51 | 52 | 0 | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
using namespace std;
int n, m;
long long gc[110][110];
long long gt[110][110];
void warshallfloyd() {
for (int k = 0; k < m; k++) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]);
gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]);
}
}
}
}
int main(void) {
while (cin >> n >> m, n || m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
gc[i][j] = 9999999;
gt[i][j] = 9999999;
}
}
for (int i = 0; i < n; i++) {
int a, b, c, t;
cin >> a >> b >> c >> t;
a--;
b--;
gc[a][b] = gc[b][a] = c;
gt[a][b] = gt[b][a] = t;
}
warshallfloyd();
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int p, q, r;
cin >> p >> q >> r;
p--;
q--;
if (r == 0) {
cout << gc[p][q] << endl;
} else {
cout << gt[p][q] << endl;
}
}
}
return 0;
}
| #include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
using namespace std;
int n, m;
long long gc[110][110];
long long gt[110][110];
void warshallfloyd() {
for (int k = 0; k < m; k++) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]);
gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]);
}
}
}
}
int main(void) {
while (cin >> n >> m, n || m) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
gc[i][j] = 9999999;
gt[i][j] = 9999999;
}
}
for (int i = 0; i < n; i++) {
int a, b, c, t;
cin >> a >> b >> c >> t;
a--;
b--;
gc[a][b] = gc[b][a] = c;
gt[a][b] = gt[b][a] = t;
}
warshallfloyd();
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int p, q, r;
cin >> p >> q >> r;
p--;
q--;
if (r == 0) {
cout << gc[p][q] << endl;
} else {
cout << gt[p][q] << endl;
}
}
}
return 0;
}
| replace | 30 | 32 | 30 | 32 | 0 | |
p00200 | C++ | Runtime Error | #include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
const int INF = 9999999;
int solve(int from, int to, int cost[101][101], int n) {
int dist[101];
int history[101];
for (int i = 0; i < 101; i++) {
dist[i] = INF;
history[i] = 0;
}
dist[from] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
q;
q.push(make_pair(0, from));
while (!q.empty()) {
pair<int, int> a = q.top();
int u = a.second;
history[u] = 1;
q.pop();
for (int i = 1; i <= n; i++) {
if (history[i])
continue;
if (cost[u][i] == INF)
continue;
if (dist[i] > cost[u][i] + dist[u]) {
dist[i] = cost[u][i] + dist[u];
q.push(make_pair(dist[i], i));
}
}
}
return dist[to];
}
int main(void) {
int n, m;
int cost[101][101];
int time[101][101];
while (cin >> n >> m && n != 0) {
for (int i = 0; i < 101; i++) {
for (int j = 0; j < 101; j++) {
cost[i][j] = INF;
time[i][j] = INF;
}
}
for (int i = 0; i < 101; i++) {
cost[i][i] = 0;
time[i][i] = 0;
}
for (int i = 0; i < n; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
cost[a][b] = c;
cost[b][a] = c;
time[a][b] = d;
time[b][a] = d;
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int p, q, r;
cin >> p >> q >> r;
cout << solve(p, q, r ? time : cost, n) << endl;
}
}
return 0;
} | #include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
const int INF = 9999999;
int solve(int from, int to, int cost[101][101], int n) {
int dist[101];
int history[101];
for (int i = 0; i < 101; i++) {
dist[i] = INF;
history[i] = 0;
}
dist[from] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
q;
q.push(make_pair(0, from));
while (!q.empty()) {
pair<int, int> a = q.top();
int u = a.second;
history[u] = 1;
q.pop();
for (int i = 1; i <= n; i++) {
if (history[i])
continue;
if (cost[u][i] == INF)
continue;
if (dist[i] > cost[u][i] + dist[u]) {
dist[i] = cost[u][i] + dist[u];
q.push(make_pair(dist[i], i));
}
}
}
return dist[to];
}
int main(void) {
int n, m;
int cost[101][101];
int time[101][101];
while (cin >> n >> m && n != 0) {
for (int i = 0; i < 101; i++) {
for (int j = 0; j < 101; j++) {
cost[i][j] = INF;
time[i][j] = INF;
}
}
for (int i = 0; i < 101; i++) {
cost[i][i] = 0;
time[i][i] = 0;
}
for (int i = 0; i < n; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
cost[a][b] = c;
cost[b][a] = c;
time[a][b] = d;
time[b][a] = d;
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int p, q, r;
cin >> p >> q >> r;
cout << solve(p, q, r ? time : cost, m) << endl;
}
}
return 0;
} | replace | 79 | 80 | 79 | 80 | 0 | |
p00200 | C++ | Runtime Error | // 青春の片道切符::ワーシャルフロイド法
/*for(int k=0;k<N;k++){//経由箇所(N<200)
for(int i=0;i<N;i++){//スタート(N<200)
for(int j=0;j<N;j++){//目的(N<200)
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
}
}*/
#include <algorithm>
#include <cctype>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
using namespace std;
long long gc[110][110];
long long gt[110][110];
int n, m;
void warshallfroid() {
for (int k = 0; k < m; k++) { // 経由箇所(N<200)
for (int i = 0; i < m; i++) { // スタート(N<200)
for (int j = 0; j < n; j++) { // 目的(N<200)
gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]);
gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]);
}
}
}
}
int main() {
while (cin >> n >> m, n || m /*n,mが両方0のときお終わり*/) {
for (int i = 0; i < m; i++) { // スタート(N<200)
for (int j = 0; j < m; j++) { // 目的(N<200)
gc[i][j] = 9999999; // 初期化
gt[i][j] = 9999999; // 初期化
}
}
for (int i = 0; i < n; i++) {
int a, b, c, t;
cin >> a >> b >> c >> t;
a--, b--;
gc[a][b] = gc[b][a] = c;
gt[a][b] = gt[b][a] = t;
}
warshallfroid();
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int p, q, r;
cin >> p >> q >> r;
p--, q--;
if (r == 0)
cout << gc[p][q] << endl;
else
cout << gt[p][q] << endl;
}
}
}
| // 青春の片道切符::ワーシャルフロイド法
/*for(int k=0;k<N;k++){//経由箇所(N<200)
for(int i=0;i<N;i++){//スタート(N<200)
for(int j=0;j<N;j++){//目的(N<200)
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
}
}*/
#include <algorithm>
#include <cctype>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
using namespace std;
long long gc[110][110];
long long gt[110][110];
int n, m;
void warshallfroid() {
for (int k = 0; k < m; k++) { // 経由箇所(N<200)
for (int i = 0; i < m; i++) { // スタート(N<200)
for (int j = 0; j < m; j++) { // 目的(N<200)
gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]);
gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]);
}
}
}
}
int main() {
while (cin >> n >> m, n || m /*n,mが両方0のときお終わり*/) {
for (int i = 0; i < m; i++) { // スタート(N<200)
for (int j = 0; j < m; j++) { // 目的(N<200)
gc[i][j] = 9999999; // 初期化
gt[i][j] = 9999999; // 初期化
}
}
for (int i = 0; i < n; i++) {
int a, b, c, t;
cin >> a >> b >> c >> t;
a--, b--;
gc[a][b] = gc[b][a] = c;
gt[a][b] = gt[b][a] = t;
}
warshallfroid();
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int p, q, r;
cin >> p >> q >> r;
p--, q--;
if (r == 0)
cout << gc[p][q] << endl;
else
cout << gt[p][q] << endl;
}
}
}
| replace | 25 | 26 | 25 | 26 | 0 | |
p00200 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// common
typedef int i32;
typedef long long i64, ll;
#define BR "\n"
#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<string> vs;
typedef long double ld;
// config
#define MODE_DEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;
// debug
#ifdef MODE_DEBUG
#define LINE() cerr << " (L" << __LINE__ << ")"
#define LINELN() LINE() << endl
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) \
DUMP(x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl
#define CHECK(exp, act) \
if (exp != act) { \
DUMP(exp); \
DEBUG(act); \
}
#define STOP(e) \
CHECK(e, true); \
if (!(e)) \
exit(1);
#else
#define DUMP(x)
#define DEBUG(x)
#define CHECK(exp, act)
#define STOP(e)
#endif
template <class T> inline string toString(const vector<T> &x) {
stringstream ss;
REP(i, x.size()) {
if (i != 0)
ss << " ";
ss << x[i];
}
return ss.str();
}
template <class T> inline string toString(const vector<vector<T>> &map) {
stringstream ss;
REP(i, map.size()) {
if (i != 0)
ss << BR;
ss << toString(map[i]);
}
return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
string res;
stringstream ss;
for (auto &p : x)
ss << p.first << ":" << p.second << " ";
return ss.str();
}
template <typename T, typename V> inline T mod(T v, V MOD) {
return (v % MOD + MOD) % MOD;
}
typedef vector<vector<int>> Mat;
void warshall_floyd(Mat &d) {
const int V = d.size();
for (int k = 0; k < V; k++)
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
if (d[i][j] > d[i][k] + d[k][j]) {
d[i][j] = d[i][k] + d[k][j];
// prev[i][j]=k;
}
}
const int INF = 1 << 28;
int main() {
while (true) {
int N, M;
cin >> N >> M;
if (N == 0 && M == 0)
return 0;
Mat ds(M + 1, vector<int>(M + 1, INF));
Mat cs(M + 1, vector<int>(M + 1, INF));
REP(i, M + 1) ds[i][i] = cs[i][i] = 0;
REP(i, N) {
int a, b, c, t;
cin >> a >> b >> c >> t;
ds[a][b] = min(ds[a][b], t);
ds[b][a] = min(ds[b][a], t);
cs[a][b] = min(cs[a][b], c);
cs[b][a] = min(cs[b][a], c);
}
warshall_floyd(ds);
DUMP(toString(ds));
warshall_floyd(cs);
DUMP(toString(cs));
int k;
cin >> k;
REP(i, k) {
int p, q, r;
cin >> p >> q >> r;
if (r == 0)
cout << cs[p][q] << endl;
else
cout << ds[p][q] << endl;
}
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// common
typedef int i32;
typedef long long i64, ll;
#define BR "\n"
#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<string> vs;
typedef long double ld;
// config
// #define MODE_DEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;
// debug
#ifdef MODE_DEBUG
#define LINE() cerr << " (L" << __LINE__ << ")"
#define LINELN() LINE() << endl
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) \
DUMP(x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl
#define CHECK(exp, act) \
if (exp != act) { \
DUMP(exp); \
DEBUG(act); \
}
#define STOP(e) \
CHECK(e, true); \
if (!(e)) \
exit(1);
#else
#define DUMP(x)
#define DEBUG(x)
#define CHECK(exp, act)
#define STOP(e)
#endif
template <class T> inline string toString(const vector<T> &x) {
stringstream ss;
REP(i, x.size()) {
if (i != 0)
ss << " ";
ss << x[i];
}
return ss.str();
}
template <class T> inline string toString(const vector<vector<T>> &map) {
stringstream ss;
REP(i, map.size()) {
if (i != 0)
ss << BR;
ss << toString(map[i]);
}
return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
string res;
stringstream ss;
for (auto &p : x)
ss << p.first << ":" << p.second << " ";
return ss.str();
}
template <typename T, typename V> inline T mod(T v, V MOD) {
return (v % MOD + MOD) % MOD;
}
typedef vector<vector<int>> Mat;
void warshall_floyd(Mat &d) {
const int V = d.size();
for (int k = 0; k < V; k++)
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
if (d[i][j] > d[i][k] + d[k][j]) {
d[i][j] = d[i][k] + d[k][j];
// prev[i][j]=k;
}
}
const int INF = 1 << 28;
int main() {
while (true) {
int N, M;
cin >> N >> M;
if (N == 0 && M == 0)
return 0;
Mat ds(M + 1, vector<int>(M + 1, INF));
Mat cs(M + 1, vector<int>(M + 1, INF));
REP(i, M + 1) ds[i][i] = cs[i][i] = 0;
REP(i, N) {
int a, b, c, t;
cin >> a >> b >> c >> t;
ds[a][b] = min(ds[a][b], t);
ds[b][a] = min(ds[b][a], t);
cs[a][b] = min(cs[a][b], c);
cs[b][a] = min(cs[b][a], c);
}
warshall_floyd(ds);
DUMP(toString(ds));
warshall_floyd(cs);
DUMP(toString(cs));
int k;
cin >> k;
REP(i, k) {
int p, q, r;
cin >> p >> q >> r;
if (r == 0)
cout << cs[p][q] << endl;
else
cout << ds[p][q] << endl;
}
}
return 0;
} | replace | 41 | 42 | 41 | 42 | 0 | toString(ds) = 0 268435456 268435456 268435456 268435456 268435456
268435456 0 10 25 15 35
268435456 10 0 35 10 30
268435456 25 35 0 40 20
268435456 15 10 40 0 20
268435456 35 30 20 20 0
toString(cs) = 0 268435456 268435456 268435456 268435456 268435456
268435456 0 200 250 300 450
268435456 200 0 450 100 250
268435456 250 450 0 450 300
268435456 300 100 450 0 150
268435456 450 250 300 150 0
|
p00201 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
map<string, int> data;
map<string, vector<string>> ren;
int res(string s) {
if (!ren.count(s))
return data[s];
int sum = 0;
for (int i = 0; i < ren[s].size(); i++) {
sum += res(ren[s][i]);
}
if (data[s] > sum) {
data[s] = sum;
}
return data[s];
}
int main() {
int n, m;
while (cin >> n, n) {
for (int i = 0; i < n; i++) {
string item;
int cst;
cin >> item >> cst;
data[item] = cst;
}
cin >> m;
int k;
for (int i = 0; i < m; i++) {
string renkin;
cin >> renkin >> k;
// int sum=0;
for (int j = 0; j < k; j++) {
string tmp;
cin >> tmp;
ren[renkin].push_back(tmp);
}
// data[renkin]=min(sum,data[renkin]);
}
string ans;
cin >> ans;
cout << res(ans) << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
map<string, int> data;
map<string, vector<string>> ren;
int res(string s) {
if (!ren.count(s))
return data[s];
int sum = 0;
for (int i = 0; i < ren[s].size(); i++) {
sum += res(ren[s][i]);
}
if (data[s] > sum) {
data[s] = sum;
}
return data[s];
}
int main() {
int n, m;
while (cin >> n, n) {
data.clear();
ren.clear();
for (int i = 0; i < n; i++) {
string item;
int cst;
cin >> item >> cst;
data[item] = cst;
}
cin >> m;
int k;
for (int i = 0; i < m; i++) {
string renkin;
cin >> renkin >> k;
// int sum=0;
for (int j = 0; j < k; j++) {
string tmp;
cin >> tmp;
ren[renkin].push_back(tmp);
}
// data[renkin]=min(sum,data[renkin]);
}
string ans;
cin >> ans;
cout << res(ans) << endl;
}
return 0;
} | insert | 57 | 57 | 57 | 59 | TLE | |
p00201 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second
#define MAX 10000000
using namespace std;
typedef pair<int, bool> P;
map<string, P> material;
vector<string> vec[110];
map<string, P> index;
int rec(string target) {
int value = MAX;
map<string, P>::iterator it = material.begin();
while (it != material.end()) {
if ((*it).F == target)
value = min(value, (*it).S.F);
it++;
}
int X = 10000000;
int id = -1, pre = X;
if (index[target].S)
id = index[target].F;
if (id != -1) {
for (int i = 0; i < vec[id].size(); i++) {
pre += rec(vec[id][i]);
}
}
value = min(id == -1 ? X : pre - X, value);
assert(value != MAX);
return value;
}
int main() {
int n, m;
while (true) {
cin >> n;
if (n == 0)
break;
material.clear();
for (int i = 0; i < n; i++) {
vec[i].clear();
string s;
int val;
cin >> s >> val;
material[s] = P(val, true);
}
index.clear();
cin >> m;
for (int i = 0; i < m; i++) {
string s, ss;
int cn;
cin >> s >> cn;
index[s] = P(i, true);
for (int j = 0; j < cn; j++) {
cin >> ss;
vec[i].push_back(ss);
}
}
string st;
cin >> st;
cout << rec(st) << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second
#define MAX 10000000
using namespace std;
typedef pair<int, bool> P;
map<string, P> material;
vector<string> vec[110];
map<string, P> index;
int rec(string target) {
int value = MAX;
map<string, P>::iterator it = material.begin();
while (it != material.end()) {
if ((*it).F == target)
value = min(value, (*it).S.F);
it++;
}
int X = 10000000;
int id = -1, pre = X;
if (index[target].S)
id = index[target].F;
if (id != -1) {
for (int i = 0; i < vec[id].size(); i++) {
pre += rec(vec[id][i]);
}
}
value = min(id == -1 ? X : pre - X, value);
return value;
}
int main() {
int n, m;
while (true) {
cin >> n;
if (n == 0)
break;
material.clear();
for (int i = 0; i < n; i++) {
vec[i].clear();
string s;
int val;
cin >> s >> val;
material[s] = P(val, true);
}
index.clear();
cin >> m;
for (int i = 0; i < m; i++) {
string s, ss;
int cn;
cin >> s >> cn;
index[s] = P(i, true);
for (int j = 0; j < cn; j++) {
cin >> ss;
vec[i].push_back(ss);
}
}
string st;
cin >> st;
cout << rec(st) << endl;
}
return 0;
} | replace | 36 | 37 | 36 | 37 | 0 | |
p00201 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int n, m;
map<string, int> t;
int c[111];
vector<int> v[111];
int solve(int k) {
int sum = 0;
if (v[k].empty())
return c[k];
for (int i = 0; i < (int)v[k].size(); i++) {
sum += solve(v[k][i]);
}
return min(sum, c[k]);
}
int main(void) {
while (cin >> n && n) {
t.clear();
for (int i = 0; i < 111; i++)
v[i].clear();
for (int i = 0; i < n; i++) {
string s;
int a;
cin >> s >> a;
t[s] = n;
c[n] = a;
}
cin >> m;
for (int j = 0; j < m; j++) {
string s;
int a;
cin >> s >> a;
int id = t[s];
for (int i = 0; i < a; i++) {
cin >> s;
v[id].push_back(t[s]);
}
}
string s;
cin >> s;
cout << solve(t[s]) << endl;
}
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int n, m;
map<string, int> t;
int c[111];
vector<int> v[111];
int solve(int k) {
int sum = 0;
if (v[k].empty())
return c[k];
for (int i = 0; i < (int)v[k].size(); i++) {
sum += solve(v[k][i]);
}
return min(sum, c[k]);
}
int main(void) {
while (cin >> n && n) {
t.clear();
for (int i = 0; i < 111; i++)
v[i].clear();
for (int i = 0; i < n; i++) {
string s;
int a;
cin >> s >> a;
t[s] = i;
c[i] = a;
}
cin >> m;
for (int j = 0; j < m; j++) {
string s;
int a;
cin >> s >> a;
int id = t[s];
for (int i = 0; i < a; i++) {
cin >> s;
v[id].push_back(t[s]);
}
}
string s;
cin >> s;
cout << solve(t[s]) << endl;
}
} | replace | 31 | 33 | 31 | 33 | -11 | |
p00201 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
map<string, vector<string>> recipe;
map<string, int> cost;
int search(string item) {
if (recipe[item].size() == 0)
return cost[item];
else {
int sum = 0;
for (int i = 0; i < recipe[item].size(); i++) {
sum += search(recipe[item][i]);
}
return min(cost[item], sum);
}
}
int main() {
int n;
while (cin >> n, n) {
for (int i = 0; i < n; i++) {
string name;
int m;
cin >> name >> m;
cost[name] = m;
}
cin >> n;
for (int i = 0; i < n; i++) {
string item;
cin >> item;
int m;
cin >> m;
for (int j = 0; j < m; j++) {
string material;
cin >> material;
recipe[item].push_back(material);
}
}
string item;
cin >> item;
cout << search(item) << endl;
}
} | #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
map<string, vector<string>> recipe;
map<string, int> cost;
int search(string item) {
if (recipe[item].size() == 0)
return cost[item];
else {
int sum = 0;
for (int i = 0; i < recipe[item].size(); i++) {
sum += search(recipe[item][i]);
}
return min(cost[item], sum);
}
}
int main() {
int n;
while (cin >> n, n) {
recipe.clear();
cost.clear();
for (int i = 0; i < n; i++) {
string name;
int m;
cin >> name >> m;
cost[name] = m;
}
cin >> n;
for (int i = 0; i < n; i++) {
string item;
cin >> item;
int m;
cin >> m;
for (int j = 0; j < m; j++) {
string material;
cin >> material;
recipe[item].push_back(material);
}
}
string item;
cin >> item;
cout << search(item) << endl;
}
} | insert | 25 | 25 | 25 | 27 | TLE | |
p00201 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;
int N, M;
int cost[100];
string S[100], O[100], Q[100][100], T;
int P[100], K[100];
vector<int> g[100];
int main() {
while (cin >> N, N) {
REP(i, N) cin >> S[i] >> P[i];
cin >> M;
REP(i, M) {
cin >> O[i] >> K[i];
REP(j, K[i]) cin >> Q[i][j];
}
cin >> T;
map<string, int> conv;
REP(i, N) conv[S[i]] = conv.size() - 1;
REP(i, M) g[i].clear();
REP(i, M) {
g[i].push_back(conv[O[i]]);
REP(j, K[i]) g[i].push_back(conv[Q[i][j]]);
}
fill(cost, cost + 200, INF);
REP(i, N) chmin(cost[conv[S[i]]], P[i]);
REP(t, M - 1) {
REP(i, M) {
ll sum = 0;
FOR(j, 1, g[i].size()) sum += cost[g[i][j]];
chmin(cost[g[i][0]], sum);
}
}
cout << cost[conv[T]] << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;
int N, M;
int cost[100];
string S[100], O[100], Q[100][100], T;
int P[100], K[100];
vector<int> g[100];
int main() {
while (cin >> N, N) {
REP(i, N) cin >> S[i] >> P[i];
cin >> M;
REP(i, M) {
cin >> O[i] >> K[i];
REP(j, K[i]) cin >> Q[i][j];
}
cin >> T;
map<string, int> conv;
REP(i, N) conv[S[i]] = conv.size() - 1;
REP(i, M) g[i].clear();
REP(i, M) {
g[i].push_back(conv[O[i]]);
REP(j, K[i]) g[i].push_back(conv[Q[i][j]]);
}
fill(cost, cost + 100, INF);
REP(i, N) chmin(cost[conv[S[i]]], P[i]);
REP(t, M - 1) {
REP(i, M) {
ll sum = 0;
FOR(j, 1, g[i].size()) sum += cost[g[i][j]];
chmin(cost[g[i][0]], sum);
}
}
cout << cost[conv[T]] << endl;
}
return 0;
} | replace | 58 | 59 | 58 | 59 | -11 | |
p00201 | C++ | Time Limit Exceeded | #include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define reep(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) reep(i, 0, n)
using namespace std;
map<string, int> name_id;
vector<int> recipe[100];
int price[100];
int solve(int x) {
int len = recipe[x].size();
if (len == 0) {
return price[x];
}
int ans = 0;
rep(i, len) { ans += solve(recipe[x][i]); }
// printf("%d->%d\n",x,price[x]);
return min(ans, price[x]);
}
int main() {
int n;
while (cin >> n, n) {
rep(i, n) {
string name;
int p;
cin >> name >> p;
name_id[name] = i;
price[i] = p;
}
int m;
cin >> m;
rep(i, m) {
string name;
int num;
cin >> name >> num;
int id = name_id[name];
rep(k, num) {
string r;
cin >> r;
recipe[id].push_back(name_id[r]);
}
}
string a;
cin >> a;
cout << solve(name_id[a]) << endl;
}
return 0;
} | #include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define reep(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) reep(i, 0, n)
using namespace std;
map<string, int> name_id;
vector<int> recipe[100];
int price[100];
int solve(int x) {
int len = recipe[x].size();
if (len == 0) {
return price[x];
}
int ans = 0;
rep(i, len) { ans += solve(recipe[x][i]); }
// printf("%d->%d\n",x,price[x]);
return min(ans, price[x]);
}
int main() {
int n;
while (cin >> n, n) {
name_id.clear();
rep(i, 100) recipe[i].clear();
rep(i, n) {
string name;
int p;
cin >> name >> p;
name_id[name] = i;
price[i] = p;
}
int m;
cin >> m;
rep(i, m) {
string name;
int num;
cin >> name >> num;
int id = name_id[name];
rep(k, num) {
string r;
cin >> r;
recipe[id].push_back(name_id[r]);
}
}
string a;
cin >> a;
cout << solve(name_id[a]) << endl;
}
return 0;
} | insert | 32 | 32 | 32 | 34 | TLE | |
p00201 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
map<string, int> normal;
map<string, vector<string>> rec;
int f(string x) {
int ans = normal[x], sum = 0;
if (rec[x].size() == 0)
return ans;
for (int i = 0; i < rec[x].size(); i++) {
sum += f(rec[x][i]);
}
return min(ans, sum);
}
int main() {
int n;
while (cin >> n && n) {
for (int i = 0; i < n; i++) {
string a;
int b;
cin >> a >> b;
normal[a] = b;
}
int m;
cin >> m;
for (int i = 0; i < m; i++) {
string x, y;
cin >> x;
int t;
cin >> t;
for (int j = 0; j < t; j++) {
cin >> y;
rec[x].push_back(y);
}
}
string want;
cin >> want;
cout << f(want) << endl;
}
} | #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
map<string, int> normal;
map<string, vector<string>> rec;
int f(string x) {
int ans = normal[x], sum = 0;
if (rec[x].size() == 0)
return ans;
for (int i = 0; i < rec[x].size(); i++) {
sum += f(rec[x][i]);
}
return min(ans, sum);
}
int main() {
int n;
while (cin >> n && n) {
normal.clear();
rec.clear();
for (int i = 0; i < n; i++) {
string a;
int b;
cin >> a >> b;
normal[a] = b;
}
int m;
cin >> m;
for (int i = 0; i < m; i++) {
string x, y;
cin >> x;
int t;
cin >> t;
for (int j = 0; j < t; j++) {
cin >> y;
rec[x].push_back(y);
}
}
string want;
cin >> want;
cout << f(want) << endl;
}
} | insert | 22 | 22 | 22 | 24 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.