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
p02235
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> using namespace std; int main() { int q, t1, t2; string s1, s2; const int maxS = 10; int m[maxS][maxS]; cin >> q; for (int i = 0; i < q; i++) { for (int i = 0; i < maxS; i++) for (int j = 0; j < maxS; j++) m[i][j] = 0; cin >> s1 >> s2; t1 = s1.size() - 1; t2 = s2.size() - 1; if (s1[0] == s2[0]) m[0][0] = 1; for (int i = 0; i < t1; i++) { int f = 0; if (s1[i + 1] == s2[0]) f = 1; m[i + 1][0] = max(m[i][0], f); } for (int j = 0; j < t2; j++) { int f = 0; if (s2[j + 1] == s1[0]) f = 1; m[0][j + 1] = max(m[0][j], f); } for (int i = 0; i < t1; i++) { for (int j = 0; j < t2; j++) { if (s1[i + 1] == s2[j + 1]) m[i + 1][j + 1] = m[i][j] + 1; else m[i + 1][j + 1] = max(m[i][j + 1], m[i + 1][j]); } } cout << m[t1][t2] << endl; } return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; int main() { int q, t1, t2; string s1, s2; const int maxS = 1000; int m[maxS][maxS]; cin >> q; for (int i = 0; i < q; i++) { for (int i = 0; i < maxS; i++) for (int j = 0; j < maxS; j++) m[i][j] = 0; cin >> s1 >> s2; t1 = s1.size() - 1; t2 = s2.size() - 1; if (s1[0] == s2[0]) m[0][0] = 1; for (int i = 0; i < t1; i++) { int f = 0; if (s1[i + 1] == s2[0]) f = 1; m[i + 1][0] = max(m[i][0], f); } for (int j = 0; j < t2; j++) { int f = 0; if (s2[j + 1] == s1[0]) f = 1; m[0][j + 1] = max(m[0][j], f); } for (int i = 0; i < t1; i++) { for (int j = 0; j < t2; j++) { if (s1[i + 1] == s2[j + 1]) m[i + 1][j + 1] = m[i][j] + 1; else m[i + 1][j + 1] = max(m[i][j + 1], m[i + 1][j]); } } cout << m[t1][t2] << endl; } return 0; }
replace
8
9
8
9
0
p02235
C++
Runtime Error
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int main() { int n; char x[10000], y[10000]; int dp[10010][10010]; // load n; cin >> n; for (int i = 0; i < n; i++) { // load string scanf("%s", x); scanf("%s", y); // dp for (int j = 0; j < strlen(x); j++) { for (int k = 0; k < strlen(y); k++) { if (x[j] == y[k]) { dp[j + 1][k + 1] = dp[j][k] + 1; } else { if (dp[j][k + 1] > dp[j + 1][k]) { dp[j + 1][k + 1] = dp[j][k + 1]; } else { dp[j + 1][k + 1] = dp[j + 1][k]; } } } } cout << dp[strlen(x)][strlen(y)] << endl; } return 0; }
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int main() { int n; char x[1001], y[1001]; int dp[1010][1010]; // load n; cin >> n; for (int i = 0; i < n; i++) { // load string scanf("%s", x); scanf("%s", y); // dp for (int j = 0; j < strlen(x); j++) { for (int k = 0; k < strlen(y); k++) { if (x[j] == y[k]) { dp[j + 1][k + 1] = dp[j][k] + 1; } else { if (dp[j][k + 1] > dp[j + 1][k]) { dp[j + 1][k + 1] = dp[j][k + 1]; } else { dp[j + 1][k + 1] = dp[j + 1][k]; } } } } cout << dp[strlen(x)][strlen(y)] << endl; } return 0; }
replace
7
9
7
9
-11
p02235
C++
Runtime Error
#include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 1000 void solve(void); int q, count[MAX][MAX] = {}, xlength, ylength; char X[MAX], Y[MAX]; void solve(void) { int i, j; xlength = strlen(X); ylength = strlen(Y); for (i = 1; i <= xlength; i++) { for (j = 1; j <= ylength; j++) { if (X[i - 1] == Y[j - 1]) count[i][j] = count[i - 1][j - 1] + 1; else if (count[i - 1][j] >= count[i][j - 1]) count[i][j] = count[i - 1][j]; else count[i][j] = count[i][j - 1]; } } } int main() { int i; scanf("%d", &q); for (i = 1; i <= q; i++) { scanf("%s", X); scanf("%s", Y); solve(); printf("%d\n", count[xlength][ylength]); } /*for(i=0;i<q;i++){ printf("%d\n",count[][]); }*/ return 0; }
#include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 1001 void solve(void); int q, count[MAX][MAX] = {}, xlength, ylength; char X[MAX], Y[MAX]; void solve(void) { int i, j; xlength = strlen(X); ylength = strlen(Y); for (i = 1; i <= xlength; i++) { for (j = 1; j <= ylength; j++) { if (X[i - 1] == Y[j - 1]) count[i][j] = count[i - 1][j - 1] + 1; else if (count[i - 1][j] >= count[i][j - 1]) count[i][j] = count[i - 1][j]; else count[i][j] = count[i][j - 1]; } } } int main() { int i; scanf("%d", &q); for (i = 1; i <= q; i++) { scanf("%s", X); scanf("%s", Y); solve(); printf("%d\n", count[xlength][ylength]); } /*for(i=0;i<q;i++){ printf("%d\n",count[][]); }*/ return 0; }
replace
4
5
4
5
0
p02235
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; // --------------------- // repetition #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; // --------------------- #define INF 922337203685477580 typedef long long ll; int dp[1000][1000]; int main() { int q; cin >> q; REP(loop, q) { char S[1000], T[1000]; cin >> S >> T; REP(i, strlen(S)) { REP(j, strlen(T)) { dp[i][j] = 0; } } for (int i = 0; i < strlen(S); i++) { for (int j = 0; j < strlen(T); j++) { if (S[i] == T[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; } else { dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); } } } cout << dp[strlen(S)][strlen(T)] << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; // --------------------- // repetition #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; // --------------------- #define INF 922337203685477580 typedef long long ll; int dp[1010][1010]; int main() { int q; cin >> q; REP(loop, q) { char S[1000], T[1000]; cin >> S >> T; REP(i, strlen(S)) { REP(j, strlen(T)) { dp[i][j] = 0; } } for (int i = 0; i < strlen(S); i++) { for (int j = 0; j < strlen(T); j++) { if (S[i] == T[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; } else { dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); } } } cout << dp[strlen(S)][strlen(T)] << endl; } return 0; }
replace
31
32
31
32
0
p02235
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> using namespace std; int N, len1, len2; string seq1, seq2; int dp[1000][1000]; int main() { cin >> N; for (int M = 0; M < N; M++) { cin >> seq1 >> seq2; len1 = seq1.length(); len2 = seq2.length(); for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { if (seq1[i - 1] == seq2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else if (dp[i][j - 1] > dp[i - 1][j]) { dp[i][j] = dp[i][j - 1]; } else { dp[i][j] = dp[i - 1][j]; } } } cout << dp[len1][len2] << endl; } return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; int N, len1, len2; string seq1, seq2; int dp[1000 + 10][1000 + 10]; int main() { cin >> N; for (int M = 0; M < N; M++) { cin >> seq1 >> seq2; len1 = seq1.length(); len2 = seq2.length(); for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { if (seq1[i - 1] == seq2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else if (dp[i][j - 1] > dp[i - 1][j]) { dp[i][j] = dp[i][j - 1]; } else { dp[i][j] = dp[i - 1][j]; } } } cout << dp[len1][len2] << endl; } return 0; }
replace
7
8
7
8
0
p02235
C++
Runtime Error
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define VARIABLE(x) cerr << #x << "=" << x << endl #define BINARY(x) static_cast<bitset<16>>(x); #define if_range(x, w, h) if (w <= x && x <= h) #define BitCheck(a, b) (a >> b) & 1 #define BitSet(a, b) a |= (1 << b) #define BitunSet(a, b) a &= ~(1 << b) #define max(a, b) (((a) > (b)) ? (a) : (b)) #define min(a, b) (((a) < (b)) ? (a) : (b)) int compare_int(const void *a, const void *b) { return *(int *)a - *(int *)b; } int main() { int n, m, a, dp[102][102] = {0}; char s[1001], t[1001]; scanf("%d", &a); for (int k = 0; a > k; k++) { scanf("%s %s", s, t); n = strlen(s); m = strlen(t); for (int i = 1; n >= i; i++) { for (int j = 1; m >= j; j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } printf("%d\n", dp[n][m]); } return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define VARIABLE(x) cerr << #x << "=" << x << endl #define BINARY(x) static_cast<bitset<16>>(x); #define if_range(x, w, h) if (w <= x && x <= h) #define BitCheck(a, b) (a >> b) & 1 #define BitSet(a, b) a |= (1 << b) #define BitunSet(a, b) a &= ~(1 << b) #define max(a, b) (((a) > (b)) ? (a) : (b)) #define min(a, b) (((a) < (b)) ? (a) : (b)) int compare_int(const void *a, const void *b) { return *(int *)a - *(int *)b; } int main() { int n, m, a, dp[1002][1002] = {0}; char s[1001], t[1001]; scanf("%d", &a); for (int k = 0; a > k; k++) { scanf("%s %s", s, t); n = strlen(s); m = strlen(t); for (int i = 1; n >= i; i++) { for (int j = 1; m >= j; j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } printf("%d\n", dp[n][m]); } return 0; }
replace
29
30
29
30
0
p02235
C++
Runtime Error
#include <algorithm> #include <cstring> #include <iostream> using namespace std; const int MAX_LENGTH = 256; char A[MAX_LENGTH + 1]; char B[MAX_LENGTH + 1]; int tu[MAX_LENGTH][MAX_LENGTH]; void lcs(int, int); int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> A >> B; int lA = strlen(A), lB = strlen(B); lcs(lA, lB); cout << tu[lA][lB] << endl; } } void lcs(int lA, int lB) { for (int i = 0; i < lA; ++i) tu[0][i] = 0; for (int i = 0; i < lB; ++i) tu[i][0] = 0; for (int i = 1; i <= lA; i++) { for (int j = 1; j <= lB; j++) { if (A[i - 1] == B[j - 1]) tu[i][j] = tu[i - 1][j - 1] + 1; else tu[i][j] = max(tu[i][j - 1], tu[i - 1][j]); } } }
#include <algorithm> #include <cstring> #include <iostream> using namespace std; const int MAX_LENGTH = 1024; char A[MAX_LENGTH + 1]; char B[MAX_LENGTH + 1]; int tu[MAX_LENGTH][MAX_LENGTH]; void lcs(int, int); int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> A >> B; int lA = strlen(A), lB = strlen(B); lcs(lA, lB); cout << tu[lA][lB] << endl; } } void lcs(int lA, int lB) { for (int i = 0; i < lA; ++i) tu[0][i] = 0; for (int i = 0; i < lB; ++i) tu[i][0] = 0; for (int i = 1; i <= lA; i++) { for (int j = 1; j <= lB; j++) { if (A[i - 1] == B[j - 1]) tu[i][j] = tu[i - 1][j - 1] + 1; else tu[i][j] = max(tu[i][j - 1], tu[i - 1][j]); } } }
replace
5
6
5
6
0
p02237
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const double pi = 2 * acos(0.0); const double eps = 1e-8; #define REP(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) REP(i, 0, n) #define INF (1 << 29) #define INFLL (1L << 29) typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef int Cost; struct Edge { int src, dst; Cost cost; Edge(int s, int d, Cost c) : src(s), dst(d), cost(c) {} }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; typedef vector<Cost> Array; typedef vector<Array> Matrix; int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1}; int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1}; #define MAX_N 100 int G[MAX_N][MAX_N]; int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; rep(i, n) { int u, k; cin >> u >> k; rep(j, k) { int node_num; cin >> node_num; G[u][node_num] = 1; } } REP(i, 1, n + 1) { REP(j, 1, n + 1) { if (j != 1) cout << " "; cout << G[i][j]; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const double pi = 2 * acos(0.0); const double eps = 1e-8; #define REP(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) REP(i, 0, n) #define INF (1 << 29) #define INFLL (1L << 29) typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef int Cost; struct Edge { int src, dst; Cost cost; Edge(int s, int d, Cost c) : src(s), dst(d), cost(c) {} }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; typedef vector<Cost> Array; typedef vector<Array> Matrix; int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1}; int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1}; #define MAX_N 101 int G[MAX_N][MAX_N]; int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; rep(i, n) { int u, k; cin >> u >> k; rep(j, k) { int node_num; cin >> node_num; G[u][node_num] = 1; } } REP(i, 1, n + 1) { REP(j, 1, n + 1) { if (j != 1) cout << " "; cout << G[i][j]; } cout << endl; } return 0; }
replace
30
31
30
31
0
p02237
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<vector<int>> G(n, vector<int>(n, 0)); for (int i = 0; i < n; i++) { int u, k; cin >> u >> k; for (int j = 0; j < k; j++) { int v; cin >> v; G[u][v - 1] = 1; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j) cout << " "; cout << G[i][j]; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<vector<int>> G(n, vector<int>(n, 0)); for (int i = 0; i < n; i++) { int u, k; cin >> u >> k; for (int j = 0; j < k; j++) { int v; cin >> v; G[u - 1][v - 1] = 1; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j) cout << " "; cout << G[i][j]; } cout << endl; } }
replace
13
14
13
14
-11
p02237
C++
Runtime Error
#include <iostream> using namespace std; static const int N = 100; int main() { int M[N][N]; int n, u, k, v; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) M[i][j] = 0; } for (int i = 0; i < n; i++) { cin >> u >> k; u--; for (int j = 0; j < k; j++) { cin >> v; v--; M[u][v] = 1; } } for (int i = 0; i < n; i++) { for (int j = 0; i < n; j++) { if (j) cout << " "; cout << M[i][j]; } cout << endl; } return 0; }
#include <iostream> using namespace std; static const int N = 100; int main() { int M[N][N]; int n, u, k, v; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) M[i][j] = 0; } for (int i = 0; i < n; i++) { cin >> u >> k; u--; for (int j = 0; j < k; j++) { cin >> v; v--; M[u][v] = 1; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j) cout << " "; cout << M[i][j]; } cout << endl; } return 0; }
replace
26
27
26
27
-11
p02237
C++
Runtime Error
#include <iostream> #include <stdio.h> #define MAX 100 int main() { int n; int M[MAX][MAX]; scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { M[i][j] = 0; } } for (int i = 1; i <= n; i++) { int id, k; scanf("%d %d", &id, &k); if (k == 0) continue; else { for (int j = 1; j <= k; j++) { int v; scanf("%d", &v); M[id][v] = 1; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (j == n) printf("%d\n", M[i][j]); else printf("%d ", M[i][j]); } } return 0; }
#include <iostream> #include <stdio.h> #define MAX 101 int main() { int n; int M[MAX][MAX]; scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { M[i][j] = 0; } } for (int i = 1; i <= n; i++) { int id, k; scanf("%d %d", &id, &k); if (k == 0) continue; else { for (int j = 1; j <= k; j++) { int v; scanf("%d", &v); M[id][v] = 1; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (j == n) printf("%d\n", M[i][j]); else printf("%d ", M[i][j]); } } return 0; }
replace
3
4
3
4
0
p02237
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(x) (x).begin(), (x).end() const int INF = 1e9; using namespace std; vector<int> adj[100]; int res[100][100] = {0}; int main() { int n; cin >> n; REP(i, n) { int u, k; cin >> u >> k; REP(j, k) { int tmp; cin >> tmp; res[u][tmp] = 1; } } for (int i = 1; i <= n; i++) { for (int j = 1; j < n; j++) cout << res[i][j] << " "; cout << res[i][n] << endl; } return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(x) (x).begin(), (x).end() const int INF = 1e9; using namespace std; vector<int> adj[100]; int res[105][105] = {0}; int main() { int n; cin >> n; REP(i, n) { int u, k; cin >> u >> k; REP(j, k) { int tmp; cin >> tmp; res[u][tmp] = 1; } } for (int i = 1; i <= n; i++) { for (int j = 1; j < n; j++) cout << res[i][j] << " "; cout << res[i][n] << endl; } return 0; }
replace
8
9
8
9
0
p02237
C++
Runtime Error
#include <algorithm> #include <array> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define ALL(v) (v).begin(), (v).end() #define REP(i, p, n) for (int i = p; i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define DUMP(list) \ cout << "{ "; \ for (auto nth : list) { \ cout << nth << " "; \ } \ cout << "}" << endl #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) \ ; using namespace std; int main() { int n; cin >> n; vector<vector<int>> ans(n, vector<int>(4, 0)); rep(i, n) { int u, k; cin >> u >> k; u--; rep(j, k) { int v; cin >> v; v--; ans[u][v] = 1; } } rep(i, n) { rep(j, n - 1) { cout << ans[i][j] << " "; } cout << ans[i][n - 1] << endl; } return 0; }
#include <algorithm> #include <array> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define ALL(v) (v).begin(), (v).end() #define REP(i, p, n) for (int i = p; i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define DUMP(list) \ cout << "{ "; \ for (auto nth : list) { \ cout << nth << " "; \ } \ cout << "}" << endl #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) \ ; using namespace std; int main() { int n; cin >> n; vector<vector<int>> ans(n, vector<int>(n, 0)); rep(i, n) { int u, k; cin >> u >> k; u--; rep(j, k) { int v; cin >> v; v--; ans[u][v] = 1; } } rep(i, n) { rep(j, n - 1) { cout << ans[i][j] << " "; } cout << ans[i][n - 1] << endl; } return 0; }
replace
35
36
35
36
0
p02238
C++
Runtime Error
#include <stdio.h> #define N 100 #define WHITE 0 #define GRAY 1 #define BLACK 2 int n, M[N][N]; int color[N], d[N], f[N], tt; void dfs_visit(int u) { int v; color[u] = GRAY; d[u] = ++tt; for (v = 0; v < n; v++) { if (M[u][v] == 0) continue; if (color[v] == WHITE) { dfs_visit(v); } } color[u] = BLACK; f[u] = ++tt; } void dfs() { int u; for (u = 0; u < n; u++) color[u] = WHITE; tt = 0; for (u = 0; u < n; u++) { if (color[u] == WHITE) dfs_visit(u); } for (u = 0; u < n; u++) { printf("%d %d %d\n", u + 1, d[u], f[u]); } } int main() { int u, k, v, i, j; scanf("%d", n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { M[i][j] = 0; } } for (int i = 0; i < n; i++) { scanf("%d %d", &u, &k); u--; for (int j = 0; j < k; j++) { scanf("%d", &v); v--; M[u][v] = 1; } } dfs(); return 0; }
#include <stdio.h> #define N 100 #define WHITE 0 #define GRAY 1 #define BLACK 2 int n, M[N][N]; int color[N], d[N], f[N], tt; void dfs_visit(int u) { int v; color[u] = GRAY; d[u] = ++tt; for (v = 0; v < n; v++) { if (M[u][v] == 0) continue; if (color[v] == WHITE) { dfs_visit(v); } } color[u] = BLACK; f[u] = ++tt; } void dfs() { int u; for (u = 0; u < n; u++) color[u] = WHITE; tt = 0; for (u = 0; u < n; u++) { if (color[u] == WHITE) dfs_visit(u); } for (u = 0; u < n; u++) { printf("%d %d %d\n", u + 1, d[u], f[u]); } } int main() { int u, k, v, i, j; scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { M[i][j] = 0; } } for (int i = 0; i < n; i++) { scanf("%d %d", &u, &k); u--; for (int j = 0; j < k; j++) { scanf("%d", &v); v--; M[u][v] = 1; } } dfs(); return 0; }
replace
43
44
43
44
-11
p02238
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; struct StSeq { int m_nBgn; int m_nEnd; StSeq() : m_nBgn(0), m_nEnd(0) {} }; void fnInput(vector<vector<int>> &rvvnAdjcy, vector<StSeq> &rvoDFSrch) { int nMaxSiz; cin >> nMaxSiz; rvvnAdjcy.resize(nMaxSiz + 1, vector<int>(nMaxSiz + 1, 0)); for (int i = 0; i < nMaxSiz; ++i) { int nx, nSecSiz; cin >> nx >> nSecSiz; for (int j = 0; j < nSecSiz; ++j) { int ny; cin >> ny; rvvnAdjcy[nx][ny] = 1; } } StSeq oSeq; rvoDFSrch.resize(nMaxSiz + 1, oSeq); } void fnDepthFirstSearch(const vector<vector<int>> &cnrvvnAdjcy, vector<StSeq> &rvoDFSrch, int nx) { static int stnCnt = 0; if (rvoDFSrch[nx].m_nEnd) return; rvoDFSrch[nx].m_nBgn = ++stnCnt; for (int ny = 1; ny < cnrvvnAdjcy.size(); ++ny) if (cnrvvnAdjcy[nx][ny] == 1 && nx != ny) fnDepthFirstSearch(cnrvvnAdjcy, rvoDFSrch, ny); rvoDFSrch[nx].m_nEnd = ++stnCnt; } void fnResult(const vector<StSeq> &cnrvoDFSrch) { for (int i = 1; i < cnrvoDFSrch.size(); ++i) cout << i << " " << cnrvoDFSrch[i].m_nBgn << " " << cnrvoDFSrch[i].m_nEnd << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); vector<vector<int>> vvnAdjcy; vector<StSeq> voDFSrch; fnInput(vvnAdjcy, voDFSrch); for (int nx = 1; nx < voDFSrch.size(); ++nx) fnDepthFirstSearch(vvnAdjcy, voDFSrch, nx); fnResult(voDFSrch); return 0; }
#include <iostream> #include <vector> using namespace std; struct StSeq { int m_nBgn; int m_nEnd; StSeq() : m_nBgn(0), m_nEnd(0) {} }; void fnInput(vector<vector<int>> &rvvnAdjcy, vector<StSeq> &rvoDFSrch) { int nMaxSiz; cin >> nMaxSiz; rvvnAdjcy.resize(nMaxSiz + 1, vector<int>(nMaxSiz + 1, 0)); for (int i = 0; i < nMaxSiz; ++i) { int nx, nSecSiz; cin >> nx >> nSecSiz; for (int j = 0; j < nSecSiz; ++j) { int ny; cin >> ny; rvvnAdjcy[nx][ny] = 1; } } StSeq oSeq; rvoDFSrch.resize(nMaxSiz + 1, oSeq); } void fnDepthFirstSearch(const vector<vector<int>> &cnrvvnAdjcy, vector<StSeq> &rvoDFSrch, int nx) { static int stnCnt = 0; if (rvoDFSrch[nx].m_nBgn) return; rvoDFSrch[nx].m_nBgn = ++stnCnt; for (int ny = 1; ny < cnrvvnAdjcy.size(); ++ny) if (cnrvvnAdjcy[nx][ny] == 1 && nx != ny) fnDepthFirstSearch(cnrvvnAdjcy, rvoDFSrch, ny); rvoDFSrch[nx].m_nEnd = ++stnCnt; } void fnResult(const vector<StSeq> &cnrvoDFSrch) { for (int i = 1; i < cnrvoDFSrch.size(); ++i) cout << i << " " << cnrvoDFSrch[i].m_nBgn << " " << cnrvoDFSrch[i].m_nEnd << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); vector<vector<int>> vvnAdjcy; vector<StSeq> voDFSrch; fnInput(vvnAdjcy, voDFSrch); for (int nx = 1; nx < voDFSrch.size(); ++nx) fnDepthFirstSearch(vvnAdjcy, voDFSrch, nx); fnResult(voDFSrch); return 0; }
replace
33
34
33
34
0
p02238
C++
Time Limit Exceeded
#include <iostream> #include <stack> using namespace std; static const int N = 100; static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, M[N][N]; int color[N], d[N], f[N], tt; int nt[N]; // uに隣接するvを番号順に取得 int next(int u) { for (int v = 0; v < n; v++) { // 訪問した場所は探索しなくていいため、v+1にしている? nt[u] = v + 1; if (M[u][v]) return v; } return -1; } // スタックを用いた深さ優先探索 void dfs_visit(int r) { for (int i = 0; i < n; i++) nt[i] = 0; stack<int> S; S.push(r); color[r] = GRAY; d[r] = ++tt; while (!S.empty()) { int u = S.top(); int v = next(u); if (v != -1) { if (color[v] == WHITE) { color[v] = GRAY; d[v] = ++tt; S.push(v); } } else { S.pop(); color[u] = BLACK; f[u] = ++tt; } } } void dfs() { // 初期化 for (int i = 0; i < n; i++) { color[i] = WHITE; nt[i] = 0; } tt = 0; // 未訪問のuを始点として深さ優先探索 for (int u = 0; u < n; u++) { if (color[u] == WHITE) dfs_visit(u); } for (int i = 0; i < n; i++) { cout << i + 1 << " " << d[i] << " " << f[i] << endl; } } int main(int argc, char const *argv[]) { int u, k, v; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) M[i][j] = 0; } for (int i = 0; i < n; i++) { cin >> u >> k; u--; // 0 origin for (int j = 0; j < k; j++) { cin >> v; v--; M[u][v] = 1; } } dfs(); return 0; }
#include <iostream> #include <stack> using namespace std; static const int N = 100; static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, M[N][N]; int color[N], d[N], f[N], tt; int nt[N]; // uに隣接するvを番号順に取得 int next(int u) { for (int v = nt[u]; v < n; v++) { // 訪問した場所は探索しなくていいため、v+1にしている? nt[u] = v + 1; if (M[u][v]) return v; } return -1; } // スタックを用いた深さ優先探索 void dfs_visit(int r) { for (int i = 0; i < n; i++) nt[i] = 0; stack<int> S; S.push(r); color[r] = GRAY; d[r] = ++tt; while (!S.empty()) { int u = S.top(); int v = next(u); if (v != -1) { if (color[v] == WHITE) { color[v] = GRAY; d[v] = ++tt; S.push(v); } } else { S.pop(); color[u] = BLACK; f[u] = ++tt; } } } void dfs() { // 初期化 for (int i = 0; i < n; i++) { color[i] = WHITE; nt[i] = 0; } tt = 0; // 未訪問のuを始点として深さ優先探索 for (int u = 0; u < n; u++) { if (color[u] == WHITE) dfs_visit(u); } for (int i = 0; i < n; i++) { cout << i + 1 << " " << d[i] << " " << f[i] << endl; } } int main(int argc, char const *argv[]) { int u, k, v; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) M[i][j] = 0; } for (int i = 0; i < n; i++) { cin >> u >> k; u--; // 0 origin for (int j = 0; j < k; j++) { cin >> v; v--; M[u][v] = 1; } } dfs(); return 0; }
replace
15
16
15
16
TLE
p02238
C++
Runtime Error
#include <iostream> #include <stack> #include <string> #include <vector> using namespace std; const int max_v = 100; vector<vector<int>> adj_list; int d[max_v] = {0}; int f[max_v] = {0}; int discovered[max_v] = {0}; int t = 1; // Depth-first Search ( Stack implementation) void DFS(int v) { stack<int> S; S.push(v); discovered[v] = 1; d[v] = t++; int i = 0; while (!S.empty()) { v = S.top(); if (i < adj_list[v - 1].size()) { int u = adj_list[v - 1][i++]; if (discovered[u] == 0) { discovered[u] = 1; d[u] = t++; S.push(u); i = 0; } } else { S.pop(); f[v] = t++; i = 0; } } } int main(int argc, char **argv) { // Input int n; int u, k; cin >> n; for (int i = 0; i < n; i++) { cin >> u >> k; int v; vector<int> adj; for (int j = 0; j < k; j++) { cin >> v; adj.push_back(v); } adj_list.push_back(adj); } // Depth-first Search for (int i = 1; i <= n; i++) { if (discovered[i] == 0) DFS(i); } // Output for (int i = 1; i <= n; i++) { cout << i << " " << d[i] << " " << f[i] << endl; } return 0; }
#include <iostream> #include <stack> #include <string> #include <vector> using namespace std; const int max_v = 101; vector<vector<int>> adj_list; int d[max_v] = {0}; int f[max_v] = {0}; int discovered[max_v] = {0}; int t = 1; // Depth-first Search ( Stack implementation) void DFS(int v) { stack<int> S; S.push(v); discovered[v] = 1; d[v] = t++; int i = 0; while (!S.empty()) { v = S.top(); if (i < adj_list[v - 1].size()) { int u = adj_list[v - 1][i++]; if (discovered[u] == 0) { discovered[u] = 1; d[u] = t++; S.push(u); i = 0; } } else { S.pop(); f[v] = t++; i = 0; } } } int main(int argc, char **argv) { // Input int n; int u, k; cin >> n; for (int i = 0; i < n; i++) { cin >> u >> k; int v; vector<int> adj; for (int j = 0; j < k; j++) { cin >> v; adj.push_back(v); } adj_list.push_back(adj); } // Depth-first Search for (int i = 1; i <= n; i++) { if (discovered[i] == 0) DFS(i); } // Output for (int i = 1; i <= n; i++) { cout << i << " " << d[i] << " " << f[i] << endl; } return 0; }
replace
7
8
7
8
0
p02238
C++
Runtime Error
#include <iostream> #include <stack> using namespace std; const int MAX_N = 100; int A[MAX_N + 1][MAX_N + 1]; pair<int, int> TIMESTAMPS[MAX_N + 1]; int N; void solve() { int ts = 0; stack<int> S; for (int i = N; i > 0; i--) { if (TIMESTAMPS[i].first == 0) S.push(i); } while (!S.empty()) { int c = S.top(); if (TIMESTAMPS[c].first == 0) TIMESTAMPS[c].first = ++ts; for (int i = N; i > 0; i--) { if (A[c][i] == 1 && TIMESTAMPS[i].second == 0) S.push(i); } if (S.top() == c) { if (TIMESTAMPS[c].second == 0) TIMESTAMPS[c].second = ++ts; S.pop(); } } } int main() { cin >> N; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { A[i][j] = 0; } TIMESTAMPS[i] = make_pair(0, 0); } for (int i = 0; i < N; i++) { int u, k; cin >> u >> k; for (int j = 0; j < k; j++) { int t; cin >> t; A[u][t] = 1; } } solve(); for (int i = 1; i <= N; i++) { cout << i << ' ' << TIMESTAMPS[i].first << ' ' << TIMESTAMPS[i].second << endl; } return 0; }
#include <iostream> #include <stack> using namespace std; const int MAX_N = 100; int A[MAX_N + 1][MAX_N + 1]; pair<int, int> TIMESTAMPS[MAX_N + 1]; int N; void solve() { int ts = 0; stack<int> S; for (int i = N; i > 0; i--) { if (TIMESTAMPS[i].first == 0) S.push(i); } while (!S.empty()) { int c = S.top(); if (TIMESTAMPS[c].first == 0) TIMESTAMPS[c].first = ++ts; for (int i = N; i > 0; i--) { if (A[c][i] == 1 && TIMESTAMPS[i].first == 0) S.push(i); } if (S.top() == c) { if (TIMESTAMPS[c].second == 0) TIMESTAMPS[c].second = ++ts; S.pop(); } } } int main() { cin >> N; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { A[i][j] = 0; } TIMESTAMPS[i] = make_pair(0, 0); } for (int i = 0; i < N; i++) { int u, k; cin >> u >> k; for (int j = 0; j < k; j++) { int t; cin >> t; A[u][t] = 1; } } solve(); for (int i = 1; i <= N; i++) { cout << i << ' ' << TIMESTAMPS[i].first << ' ' << TIMESTAMPS[i].second << endl; } return 0; }
replace
25
26
25
26
0
p02238
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; vector<int> graph[100]; int start[100], endt[100]; int cnt; void dfs(int num) { start[num] = ++cnt; for (int i = 0; i < graph[num].size(); i++) { if (graph[num][i] != num && !endt[graph[num][i]]) dfs(graph[num][i]); } endt[num] = ++cnt; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int now, m; cin >> now >> m; for (int j = 0; j < m; j++) { int t; cin >> t; graph[now - 1].push_back(t - 1); } } dfs(0); for (int i = 0; i < n; i++) { if (!endt[i]) dfs(i); } for (int i = 0; i < n; i++) { cout << i + 1 << " " << start[i] << " " << endt[i] << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; vector<int> graph[100]; int start[100], endt[100]; int cnt; void dfs(int num) { start[num] = ++cnt; for (int i = 0; i < graph[num].size(); i++) { if (graph[num][i] != num && !start[graph[num][i]] && !endt[graph[num][i]]) dfs(graph[num][i]); } endt[num] = ++cnt; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int now, m; cin >> now >> m; for (int j = 0; j < m; j++) { int t; cin >> t; graph[now - 1].push_back(t - 1); } } dfs(0); for (int i = 0; i < n; i++) { if (!endt[i]) dfs(i); } for (int i = 0; i < n; i++) { cout << i + 1 << " " << start[i] << " " << endt[i] << endl; } return 0; }
replace
11
12
11
12
0
p02238
C++
Runtime Error
#include <iostream> #include <stack> using namespace std; static const int N = 100; static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, tt; int color[N], nt[N], M[N][N], d[N], f[N]; int next(int u) { for (int v = nt[u]; v < n; v++) { nt[u] = v + 1; if (M[u][v]) return v; } return -1; } void dfs_visit(int r) { for (int i = 0; i < n; i++) nt[i] = 0; stack<int> S; S.push(r); color[r] = GRAY; d[r] = ++tt; while (!S.empty()) { int u = S.top(); int v = next(u); if (v != -1) { if (color[v] == WHITE) { color[v] = GRAY; d[v] = ++tt; S.push(v); } } else { S.pop(); color[u] = BLACK; f[u] = ++tt; } } } void dfs() { for (int i = 0; i < n; i++) { color[i] = WHITE; nt[i] = 0; } tt = 0; for (int i = 0; i < n; i++) { if (color[i] == WHITE) { dfs_visit(i); } } for (int i = 0; i < n; i++) { cout << i + 1 << " " << d[i] << " " << f[i] << endl; } } int main() { int u, k, v; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { M[i][j] = 0; } } for (int i = 0; i < n; i++) { cin >> u >> k; u--; for (int j = 0; j < k; j--) { cin >> v; v--; M[u][v] = 1; } } dfs(); return 0; }
#include <iostream> #include <stack> using namespace std; static const int N = 100; static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, tt; int color[N], nt[N], M[N][N], d[N], f[N]; int next(int u) { for (int v = nt[u]; v < n; v++) { nt[u] = v + 1; if (M[u][v]) return v; } return -1; } void dfs_visit(int r) { for (int i = 0; i < n; i++) nt[i] = 0; stack<int> S; S.push(r); color[r] = GRAY; d[r] = ++tt; while (!S.empty()) { int u = S.top(); int v = next(u); if (v != -1) { if (color[v] == WHITE) { color[v] = GRAY; d[v] = ++tt; S.push(v); } } else { S.pop(); color[u] = BLACK; f[u] = ++tt; } } } void dfs() { for (int i = 0; i < n; i++) { color[i] = WHITE; nt[i] = 0; } tt = 0; for (int i = 0; i < n; i++) { if (color[i] == WHITE) { dfs_visit(i); } } for (int i = 0; i < n; i++) { cout << i + 1 << " " << d[i] << " " << f[i] << endl; } } int main() { int u, k, v; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { M[i][j] = 0; } } for (int i = 0; i < n; i++) { cin >> u >> k; u--; for (int j = 0; j < k; j++) { cin >> v; v--; M[u][v] = 1; } } dfs(); return 0; }
replace
76
77
76
77
-6
terminate called after throwing an instance of 'std::__ios_failure' what(): basic_ios::clear: iostream error
p02238
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; static const int N = 100; static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, M[N][N]; int color[N], d[N], f[N], tt; int nt[N]; // u?????£??\??????v????????????????????? int next(int u) { for (int v = 0; v < n; ++v) { nt[u] = v + 1; if (M[u][v]) return v; } return -1; } //?????????????????¨????????±???????????¢?´¢ void dfs_visit(int r) { for (int i = 0; i < n; ++i) { nt[i] = 0; } stack<int> S; S.push(r); color[r] = GRAY; d[r] = ++tt; while (!S.empty()) { int u = S.top(); int v = next(u); if (v != -1) { if (color[v] == WHITE) { color[v] = GRAY; d[v] = ++tt; S.push(v); } } else { S.pop(); color[u] = BLACK; f[u] = ++tt; } } } void dfs() { //????????? for (int i = 0; i < n; ++i) { color[i] = WHITE; nt[i] = 0; } tt = 0; //????¨???????u????§??????¨????????±???????????¢?´¢ for (int u = 0; u < n; ++u) { if (color[u] == WHITE) dfs_visit(u); } for (int i = 0; i < n; ++i) { cout << i + 1 << " " << d[i] << " " << f[i] << endl; } } int main() { int u, k, v; cin >> n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { M[i][j] = 0; } } for (int i = 0; i < n; ++i) { cin >> u >> k; u--; for (int j = 0; j < k; ++j) { cin >> v; v--; M[u][v] = 1; } } dfs(); return 0; }
#include "bits/stdc++.h" using namespace std; static const int N = 100; static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, M[N][N]; int color[N], d[N], f[N], tt; int nt[N]; // u?????£??\??????v????????????????????? int next(int u) { for (int v = nt[u]; v < n; ++v) { nt[u] = v + 1; if (M[u][v]) return v; } return -1; } //?????????????????¨????????±???????????¢?´¢ void dfs_visit(int r) { for (int i = 0; i < n; ++i) { nt[i] = 0; } stack<int> S; S.push(r); color[r] = GRAY; d[r] = ++tt; while (!S.empty()) { int u = S.top(); int v = next(u); if (v != -1) { if (color[v] == WHITE) { color[v] = GRAY; d[v] = ++tt; S.push(v); } } else { S.pop(); color[u] = BLACK; f[u] = ++tt; } } } void dfs() { //????????? for (int i = 0; i < n; ++i) { color[i] = WHITE; nt[i] = 0; } tt = 0; //????¨???????u????§??????¨????????±???????????¢?´¢ for (int u = 0; u < n; ++u) { if (color[u] == WHITE) dfs_visit(u); } for (int i = 0; i < n; ++i) { cout << i + 1 << " " << d[i] << " " << f[i] << endl; } } int main() { int u, k, v; cin >> n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { M[i][j] = 0; } } for (int i = 0; i < n; ++i) { cin >> u >> k; u--; for (int j = 0; j < k; ++j) { cin >> v; v--; M[u][v] = 1; } } dfs(); return 0; }
replace
13
14
13
14
TLE
p02239
C++
Memory Limit Exceeded
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; const int INF = 1 << 21; const int maxn = 100 + 10; int n; vector<int> g[maxn]; int vis[maxn]; int d[maxn]; void bfs(int s) { queue<int> que; que.push(s); vis[s] = 1; d[s] = 0; while (!que.empty()) { int u = que.front(); que.pop(); for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (!vis[v]) { que.push(v); d[v] = min(d[v], d[u] + 1); } } } } int main() { // freopen("in.txt","r",stdin); scanf("%d", &n); int no, cnt, dd; for (int i = 0; i < n; i++) { scanf("%d%d", &no, &cnt); for (int j = 0; j < cnt; j++) { scanf("%d", &dd); g[no - 1].push_back(dd - 1); } } for (int i = 0; i < n; i++) d[i] = INF; bfs(0); for (int i = 0; i < n; i++) { printf("%d %d\n", i + 1, d[i] == INF ? -1 : d[i]); } return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; const int INF = 1 << 21; const int maxn = 100 + 10; int n; vector<int> g[maxn]; int vis[maxn]; int d[maxn]; void bfs(int s) { queue<int> que; que.push(s); vis[s] = 1; d[s] = 0; while (!que.empty()) { int u = que.front(); que.pop(); for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (!vis[v]) { que.push(v); vis[v] = 1; d[v] = min(d[v], d[u] + 1); } } } } int main() { // freopen("in.txt","r",stdin); scanf("%d", &n); int no, cnt, dd; for (int i = 0; i < n; i++) { scanf("%d%d", &no, &cnt); for (int j = 0; j < cnt; j++) { scanf("%d", &dd); g[no - 1].push_back(dd - 1); } } for (int i = 0; i < n; i++) d[i] = INF; bfs(0); for (int i = 0; i < n; i++) { printf("%d %d\n", i + 1, d[i] == INF ? -1 : d[i]); } return 0; }
insert
25
25
25
26
MLE
p02239
C++
Time Limit Exceeded
// Ref : https://book.mynavi.jp/ec/products/detail/id=35408 #include <iostream> #include <limits.h> #include <queue> using namespace std; static const int NMAX = 100; int N, D[NMAX] = {}; bool G[NMAX][NMAX] = {{false}}; void breadthFirstSearch(int base) { int ix; for (int i = 0; i < N; i++) { D[i] = INT_MAX; } D[base] = 0; queue<int> q; q.push(base); while (!q.empty()) { ix = q.front(); q.pop(); for (int i = 0; i < N; i++) { if (G[ix][i]) { q.push(i); if (D[ix] + 1 < D[i]) D[i] = D[ix] + 1; } } } for (int i = 0; i < N; i++) { cout << i + 1 << " " << ((D[i] == INT_MAX) ? (-1) : D[i]) << endl; } } int main() { int ix, deg, cache; cin >> N; for (int i = 0; i < N; i++) { cin >> ix >> deg; for (int j = 0; j < deg; j++) { cin >> cache; G[ix - 1][cache - 1] = true; } } breadthFirstSearch(0); return 0; }
// Ref : https://book.mynavi.jp/ec/products/detail/id=35408 #include <iostream> #include <limits.h> #include <queue> using namespace std; static const int NMAX = 100; int N, D[NMAX] = {}; bool G[NMAX][NMAX] = {{false}}; void breadthFirstSearch(int base) { int ix; for (int i = 0; i < N; i++) { D[i] = INT_MAX; } D[base] = 0; queue<int> q; q.push(base); while (!q.empty()) { ix = q.front(); q.pop(); for (int i = 0; i < N; i++) { if (!G[ix][i]) continue; if (D[i] != INT_MAX) continue; D[i] = D[ix] + 1; q.push(i); } } for (int i = 0; i < N; i++) { cout << i + 1 << " " << ((D[i] == INT_MAX) ? (-1) : D[i]) << endl; } } int main() { int ix, deg, cache; cin >> N; for (int i = 0; i < N; i++) { cin >> ix >> deg; for (int j = 0; j < deg; j++) { cin >> cache; G[ix - 1][cache - 1] = true; } } breadthFirstSearch(0); return 0; }
replace
24
29
24
30
TLE
p02239
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <numeric> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int inf = 1000000000; class Graph { public: int v; int cost = inf; int ftime = -1; int ltime = -1; vector<int> e; }; vector<Graph> g; int cnt = 0; void dfs(int pos) { if (g[pos].ftime != -1) return; cnt++; g[pos].ftime = cnt; for (int i = 0; i < g[pos].e.size(); i++) { dfs(g[pos].e[i]); } cnt++; g[pos].ltime = cnt; } void bfs(int pos) { for (int i = 0; i < g[pos].e.size(); i++) { // if( g[ g[pos].e[i] ].cost != inf ) continue; g[g[pos].e[i]].cost = min(g[g[pos].e[i]].cost, g[pos].cost + 1); bfs(g[pos].e[i]); } } int main(void) { int n; cin >> n; g.resize(n); int u, k; for (int i = 0; i < n; i++) { cin >> u >> k; u--; g[u].v = u; g[u].e.resize(k); for (int j = 0; j < k; j++) { cin >> g[u].e[j]; g[u].e[j]--; } sort(g[u].e.begin(), g[u].e.end()); } g[0].cost = 0; bfs(0); for (int i = 0; i < n; i++) cout << g[i].v + 1 << " " << (g[i].cost == inf ? -1 : g[i].cost) << endl; // for(int i=0; i<n; i++) dfs(i); // for(int i=0; i<n; i++){ // cout << g[i].v + 1 << " " << g[i].ftime << " " << g[i].ltime << endl; // } return 0; } // EOF
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <numeric> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int inf = 1000000000; class Graph { public: int v; int cost = inf; int ftime = -1; int ltime = -1; vector<int> e; }; vector<Graph> g; int cnt = 0; void dfs(int pos) { if (g[pos].ftime != -1) return; cnt++; g[pos].ftime = cnt; for (int i = 0; i < g[pos].e.size(); i++) { dfs(g[pos].e[i]); } cnt++; g[pos].ltime = cnt; } void bfs(int pos) { queue<int> q; q.push(pos); while (!q.empty()) { int p = q.front(); q.pop(); for (int i = 0; i < g[p].e.size(); i++) { if (g[g[p].e[i]].cost != inf) continue; g[g[p].e[i]].cost = min(g[g[p].e[i]].cost, g[p].cost + 1); q.push(g[p].e[i]); } } } int main(void) { int n; cin >> n; g.resize(n); int u, k; for (int i = 0; i < n; i++) { cin >> u >> k; u--; g[u].v = u; g[u].e.resize(k); for (int j = 0; j < k; j++) { cin >> g[u].e[j]; g[u].e[j]--; } sort(g[u].e.begin(), g[u].e.end()); } g[0].cost = 0; bfs(0); for (int i = 0; i < n; i++) cout << g[i].v + 1 << " " << (g[i].cost == inf ? -1 : g[i].cost) << endl; // for(int i=0; i<n; i++) dfs(i); // for(int i=0; i<n; i++){ // cout << g[i].v + 1 << " " << g[i].ftime << " " << g[i].ltime << endl; // } return 0; } // EOF
replace
37
41
37
48
0
p02239
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; int main() { int n, u, k, v; cin >> n; vector<int> ans(n, 1000000000); vector<vector<int>> adj(n); for (int i = 0; i < n; i++) { cin >> u >> k; while (k--) cin >> v, adj[u - 1].push_back(v - 1); } queue<pair<int, int>> q; //<idx,path> q.push(pair<int, int>(0, 1)); while (!q.empty()) { auto tmp = q.front(); q.pop(); ans[tmp.first] = min(ans[tmp.first], tmp.second); for (int i = 0; i < adj[tmp.first].size(); i++) q.push(pair<int, int>(adj[tmp.first][i], tmp.second + 1)); } for (int i = 0; i < n; i++) cout << i + 1 << " " << (ans[i] == 1000000000 ? -1 : ans[i] - 1) << endl; }
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; int main() { int n, u, k, v; cin >> n; vector<int> ans(n, 1000000000); vector<vector<int>> adj(n); for (int i = 0; i < n; i++) { cin >> u >> k; while (k--) cin >> v, adj[u - 1].push_back(v - 1); } queue<pair<int, int>> q; //<idx,path> q.push(pair<int, int>(0, 1)); while (!q.empty()) { auto tmp = q.front(); q.pop(); ans[tmp.first] = min(ans[tmp.first], tmp.second); if (ans[tmp.first] != tmp.second) continue; for (int i = 0; i < adj[tmp.first].size(); i++) q.push(pair<int, int>(adj[tmp.first][i], tmp.second + 1)); } for (int i = 0; i < n; i++) cout << i + 1 << " " << (ans[i] == 1000000000 ? -1 : ans[i] - 1) << endl; }
insert
23
23
23
25
TLE
p02239
C++
Runtime Error
#include <algorithm> #include <cstdlib> #include <iostream> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; int n, m, l, p; int a[100][100]; int D[100]; void bfs(int src) { queue<int> Q; Q.push(src); D[src] = 0; while (!Q.empty()) { int cur = Q.front(); Q.pop(); for (int dst = 0; dst < n; dst++) { if (D[dst] < 0 && a[cur][dst] == 1) { D[dst] = D[cur] + 1; Q.push(dst); } } } for (int i = 0; i < n; i++) { cerr << i + 1 << " " << D[i] << endl; } } int main() { cin >> n; for (int i = 0; i < n; i++) { D[i] = -1; for (int j = 0; j < n; j++) { a[i][j] = 0; } } for (int i = 0; i < n; i++) { cin >> m; cin >> l; for (int j = 0; j < l; j++) { cin >> p; a[m - 1][p - 1] = 1; } } bfs(0); }
#include <algorithm> #include <cstdlib> #include <iostream> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; int n, m, l, p; int a[100][100]; int D[100]; void bfs(int src) { queue<int> Q; Q.push(src); D[src] = 0; while (!Q.empty()) { int cur = Q.front(); Q.pop(); for (int dst = 0; dst < n; dst++) { if (D[dst] < 0 && a[cur][dst] == 1) { D[dst] = D[cur] + 1; Q.push(dst); } } } for (int i = 0; i < n; i++) { cout << i + 1 << " " << D[i] << endl; } } int main() { cin >> n; for (int i = 0; i < n; i++) { D[i] = -1; for (int j = 0; j < n; j++) { a[i][j] = 0; } } for (int i = 0; i < n; i++) { cin >> m; cin >> l; for (int j = 0; j < l; j++) { cin >> p; a[m - 1][p - 1] = 1; } } bfs(0); }
replace
28
29
28
29
0
1 0 2 1 3 2 4 1
p02239
C++
Runtime Error
#include <iostream> #include <queue> using namespace std; static const int N = 100; static const int INFTY = (1 << 21); int n, M[N][N]; int d[N]; void bfs(int s) { queue<int> q; q.push(s); for (int i = 0; i < n; i++) { d[i] = INFTY; } d[s] = 0; int u; while (!q.empty()) { u = q.front(); q.pop(); for (int v = 0; v < n; v++) { if (M[u][v] == 0) continue; if (d[v] != INFTY) continue; d[v] = d[u] + 1; q.push(v); } } for (int i = 0; i < n; i++) { cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl; } } int main() { int u, k, v; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < k; j++) M[i][j] = 0; } for (int i = 0; i < n; i++) { cin >> u >> k; u--; for (int j = 0; j < k; j++) { cin >> v; v--; M[u][v] = 1; } } bfs(0); return 0; }
#include <iostream> #include <queue> using namespace std; static const int N = 100; static const int INFTY = (1 << 21); int n, M[N][N]; int d[N]; void bfs(int s) { queue<int> q; q.push(s); for (int i = 0; i < n; i++) { d[i] = INFTY; } d[s] = 0; int u; while (!q.empty()) { u = q.front(); q.pop(); for (int v = 0; v < n; v++) { if (M[u][v] == 0) continue; if (d[v] != INFTY) continue; d[v] = d[u] + 1; q.push(v); } } for (int i = 0; i < n; i++) { cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl; } } int main() { int u, k, v; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) M[i][j] = 0; } for (int i = 0; i < n; i++) { cin >> u >> k; u--; for (int j = 0; j < k; j++) { cin >> v; v--; M[u][v] = 1; } } bfs(0); return 0; }
replace
38
39
38
39
-11
p02239
C++
Runtime Error
#include <iostream> #include <queue> using namespace std; int D[100]; int a[100][100]; int main() { for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { a[i][j] = 0; } } int n, u, k, v; cin >> n; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v; a[u][v] = 1; } } queue<int> Q; // 整数を管理するキューの定義 for (int i = 1; i < n + 1; i++) { D[i] = -1; } int src = 1; Q.push(src); D[src] = 0; // 出発点 while (!Q.empty()) { int cur = Q.front(); // 先頭要素を取り出す Q.pop(); // 動作確認用表示 cerr << "visiting " << cur << ' ' << D[cur] << endl; for (int dst = 1; dst <= n; dst++) { // 各行き先dstに対して if ((a[cur][dst] == 1 && D[dst] == -1)) { // curからdstに辺があり、dstが未訪問なら D[dst] = D[cur] + 1; // Q.push(dst); // dstを訪問先に入れる } } } for (int i = 1; i < n + 1; i++) { cout << i << " " << D[i] << endl; } }
#include <iostream> #include <queue> using namespace std; int D[100]; int a[100][100]; int main() { for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { a[i][j] = 0; } } int n, u, k, v; cin >> n; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v; a[u][v] = 1; } } queue<int> Q; // 整数を管理するキューの定義 for (int i = 1; i < n + 1; i++) { D[i] = -1; } int src = 1; Q.push(src); D[src] = 0; // 出発点 while (!Q.empty()) { int cur = Q.front(); // 先頭要素を取り出す Q.pop(); for (int dst = 1; dst <= n; dst++) { // 各行き先dstに対して if ((a[cur][dst] == 1 && D[dst] == -1)) { // curからdstに辺があり、dstが未訪問なら D[dst] = D[cur] + 1; // Q.push(dst); // dstを訪問先に入れる } } } for (int i = 1; i < n + 1; i++) { cout << i << " " << D[i] << endl; } }
delete
32
34
32
32
0
visiting 1 0 visiting 2 1 visiting 4 1 visiting 3 2
p02239
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <queue> #define INF 999999999 using namespace std; int res[101]; int G[101][101] = {0}; int main() { int n, u, v, k; std::queue<int> Q; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> u >> k; for (int j = 0; j < k; j++) { std::cin >> v; G[u - 1][v - 1] = 1; } } for (int i = 0; i < n; i++) res[i] = INF; Q.push(0); res[0] = 0; while (!Q.empty()) { u = Q.front(); for (int i = 0; i < n; i++) { if (G[u][i]) { Q.push(i); res[i] = std::min(res[i], res[u] + 1); } } Q.pop(); } for (int i = 0; i < n; i++) { if (res[i] != INF) std::cout << i + 1 << " " << res[i] << "\n"; else std::cout << i + 1 << " -1\n"; } return 0; }
#include <algorithm> #include <iostream> #include <queue> #define INF 999999999 using namespace std; int res[101]; int G[101][101] = {0}; int main() { int n, u, v, k; std::queue<int> Q; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> u >> k; for (int j = 0; j < k; j++) { std::cin >> v; G[u - 1][v - 1] = 1; } } for (int i = 0; i < n; i++) res[i] = INF; Q.push(0); res[0] = 0; while (!Q.empty()) { u = Q.front(); for (int i = 0; i < n; i++) { if (G[u][i]) { Q.push(i); res[i] = std::min(res[i], res[u] + 1); G[u][i] = 0; } } Q.pop(); } for (int i = 0; i < n; i++) { if (res[i] != INF) std::cout << i + 1 << " " << res[i] << "\n"; else std::cout << i + 1 << " -1\n"; } return 0; }
insert
33
33
33
34
TLE
p02240
C++
Runtime Error
#include <iostream> #include <stack> #include <vector> using namespace std; static const int MAX = 10000; static const int NIL = -1; int n; vector<int> G[MAX]; int color[MAX]; void dfs(int r, int c) { stack<int> S; S.push(r); color[r] = c; while (!S.empty()) { int u = S.top(); S.pop(); for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (color[v] == NIL) { color[v] = c; S.push(v); } } } } void assign() { int id = 1; for (int i = 0; i < n; i++) { color[i] = NIL; } for (int j = 0; j < n; j++) { if (color[j] == NIL) { dfs(j, id++); } } } int main() { int s, t, m, q; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> s >> t; G[s].push_back(t); G[t].push_back(s); } assign(); cin >> q; for (int i = 0; i < q; i++) { cin >> s >> t; if (color[s] == color[t]) { cout << "yes" << endl; } else cout << "no" << endl; } return 0; }
#include <iostream> #include <stack> #include <vector> using namespace std; static const int MAX = 100000; static const int NIL = -1; int n; vector<int> G[MAX]; int color[MAX]; void dfs(int r, int c) { stack<int> S; S.push(r); color[r] = c; while (!S.empty()) { int u = S.top(); S.pop(); for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (color[v] == NIL) { color[v] = c; S.push(v); } } } } void assign() { int id = 1; for (int i = 0; i < n; i++) { color[i] = NIL; } for (int j = 0; j < n; j++) { if (color[j] == NIL) { dfs(j, id++); } } } int main() { int s, t, m, q; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> s >> t; G[s].push_back(t); G[t].push_back(s); } assign(); cin >> q; for (int i = 0; i < q; i++) { cin >> s >> t; if (color[s] == color[t]) { cout << "yes" << endl; } else cout << "no" << endl; } return 0; }
replace
4
5
4
5
0
p02240
C++
Runtime Error
#include <climits> #include <iostream> #include <stack> #include <vector> using namespace std; static const int MAX = 100; static const int NIL = -1; int n; vector<int> G[MAX]; int color[MAX]; void dfs(int r, int c) { stack<int> S; S.push(r); color[r] = c; while (!S.empty()) { int u = S.top(); S.pop(); for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (color[v] == NIL) { color[v] = c; S.push(v); } } } } void assign_color() { int id = 1; for (int i = 0; i < n; i++) color[i] = NIL; for (int u = 0; u < n; u++) { if (color[u] == NIL) dfs(u, id++); } } int main() { int s, t, m, q; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> s >> t; G[s].push_back(t); G[t].push_back(s); } assign_color(); cin >> q; for (int i = 0; i < q; i++) { cin >> s >> t; if (color[s] == color[t]) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0; }
#include <climits> #include <iostream> #include <stack> #include <vector> using namespace std; static const int MAX = 100000; static const int NIL = -1; int n; vector<int> G[MAX]; int color[MAX]; void dfs(int r, int c) { stack<int> S; S.push(r); color[r] = c; while (!S.empty()) { int u = S.top(); S.pop(); for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (color[v] == NIL) { color[v] = c; S.push(v); } } } } void assign_color() { int id = 1; for (int i = 0; i < n; i++) color[i] = NIL; for (int u = 0; u < n; u++) { if (color[u] == NIL) dfs(u, id++); } } int main() { int s, t, m, q; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> s >> t; G[s].push_back(t); G[t].push_back(s); } assign_color(); cin >> q; for (int i = 0; i < q; i++) { cin >> s >> t; if (color[s] == color[t]) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0; }
replace
6
7
6
7
0
p02240
C++
Runtime Error
#include <stdio.h> int ancestor[10]; int find(int x) { if (ancestor[x] == x) return x; return ancestor[x] = find(ancestor[x]); } int min(int a, int b) { if (a < b) return a; return b; } int max(int a, int b) { if (a > b) return a; return b; } int main() { int n, q, m, temp, x, y; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) ancestor[i] = i; for (int i = 0; i < m; i++) { scanf("%d%d", &x, &y); temp = find(x); q = find(y); ancestor[max(temp, q)] = min(temp, q); } scanf("%d", &q); for (int i = 0; i < q; i++) { scanf("%d%d", &x, &y); if (find(x) != find(y)) printf("no\n"); else printf("yes\n"); } }
#include <stdio.h> int ancestor[100000]; int find(int x) { if (ancestor[x] == x) return x; return ancestor[x] = find(ancestor[x]); } int min(int a, int b) { if (a < b) return a; return b; } int max(int a, int b) { if (a > b) return a; return b; } int main() { int n, q, m, temp, x, y; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) ancestor[i] = i; for (int i = 0; i < m; i++) { scanf("%d%d", &x, &y); temp = find(x); q = find(y); ancestor[max(temp, q)] = min(temp, q); } scanf("%d", &q); for (int i = 0; i < q; i++) { scanf("%d%d", &x, &y); if (find(x) != find(y)) printf("no\n"); else printf("yes\n"); } }
replace
1
2
1
2
0
p02240
C++
Runtime Error
#include <iostream> #include <stack> #include <vector> using namespace std; static const int MAX = 10000; static const int NIL = -1; int n, color[MAX]; vector<int> G[MAX]; void dfs(int r, int c) { stack<int> S; int u, i, v; S.push(r); color[r] = c; while (!S.empty()) { u = S.top(); S.pop(); for (i = 0; i < G[u].size(); i++) { v = G[u][i]; if (color[v] == NIL) { color[v] = c; S.push(v); } } } } void assignColor() { int id = 1; int i, u; for (i = 0; i < n; i++) color[i] = NIL; for (u = 0; u < n; u++) { if (color[u] == NIL) dfs(u, id++); } } int main() { int s, t, m, q; int i; cin >> n >> m; for (i = 0; i < m; i++) { cin >> s >> t; G[s].push_back(t); G[t].push_back(s); } assignColor(); cin >> q; for (i = 0; i < q; i++) { cin >> s >> t; if (color[s] == color[t]) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0; }
#include <iostream> #include <stack> #include <vector> using namespace std; static const int MAX = 100000; static const int NIL = -1; int n, color[MAX]; vector<int> G[MAX]; void dfs(int r, int c) { stack<int> S; int u, i, v; S.push(r); color[r] = c; while (!S.empty()) { u = S.top(); S.pop(); for (i = 0; i < G[u].size(); i++) { v = G[u][i]; if (color[v] == NIL) { color[v] = c; S.push(v); } } } } void assignColor() { int id = 1; int i, u; for (i = 0; i < n; i++) color[i] = NIL; for (u = 0; u < n; u++) { if (color[u] == NIL) dfs(u, id++); } } int main() { int s, t, m, q; int i; cin >> n >> m; for (i = 0; i < m; i++) { cin >> s >> t; G[s].push_back(t); G[t].push_back(s); } assignColor(); cin >> q; for (i = 0; i < q; i++) { cin >> s >> t; if (color[s] == color[t]) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0; }
replace
4
5
4
5
0
p02240
C++
Runtime Error
#include <stdio.h> #include <string> #include <vector> using namespace std; vector<vector<int>> g; int n, m, q, s, t; vector<bool> visited; vector<int> color; void dfs(int s) { for (int i = 0; i < g[s].size(); i++) { if (color[g[s][i]] == -1) { color[g[s][i]] = color[s]; dfs(g[s][i]); } } } bool dfs2(int s, int t) { if (s == t) return true; visited[s] = true; for (int i = 0; i < g[s].size(); i++) { if (!visited[g[s][i]]) { if (dfs2(g[s][i], t)) { return true; } } } return false; } int main() { scanf("%d %d", &n, &m); g.resize(n); color.resize(n, -1); for (int i = 0; i < m; i++) { scanf("%d %d", &s, &t); g[s].push_back(t); g[t].push_back(s); } for (int i = 0; i < n; i++) { if (color[i] == -1) { color[i] = i; dfs(i); } } scanf("%d", &q); for (int i = 0; i < q; i++) { scanf("%d %d", &s, &t); visited.resize(n, false); string s1 = (dfs2(s, t) ? "yes" : "no"); string s2 = (color[s] == color[t] ? "yes" : "no"); if (s1 != s2) return 1; printf((color[s] == color[t] ? "yes\n" : "no\n")); } return 0; }
#include <stdio.h> #include <string> #include <vector> using namespace std; vector<vector<int>> g; int n, m, q, s, t; vector<bool> visited; vector<int> color; void dfs(int s) { for (int i = 0; i < g[s].size(); i++) { if (color[g[s][i]] == -1) { color[g[s][i]] = color[s]; dfs(g[s][i]); } } } bool dfs2(int s, int t) { if (s == t) return true; visited[s] = true; for (int i = 0; i < g[s].size(); i++) { if (!visited[g[s][i]]) { if (dfs2(g[s][i], t)) { return true; } } } return false; } int main() { scanf("%d %d", &n, &m); g.resize(n); color.resize(n, -1); for (int i = 0; i < m; i++) { scanf("%d %d", &s, &t); g[s].push_back(t); g[t].push_back(s); } for (int i = 0; i < n; i++) { if (color[i] == -1) { color[i] = i; dfs(i); } } scanf("%d", &q); for (int i = 0; i < q; i++) { scanf("%d %d", &s, &t); visited.resize(n, false); string s1 = (dfs2(s, t) ? "yes" : "no"); string s2 = (color[s] == color[t] ? "yes" : "no"); if (s1 != s2 && s1 == "yes") return 1; printf((color[s] == color[t] ? "yes\n" : "no\n")); } return 0; }
replace
52
53
52
53
0
p02240
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> #include <string> using namespace std; int n; string *color; queue<int> q; int *ans; vector<int> g[10000]; void bfs(int x) { color[x] = "gray"; q.push(x); while (q.size() != 0) { int u = q.front(); q.pop(); for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (color[v] == "white") { color[v] = "gray"; q.push(v); } } color[u] = "black"; ans[u] = x; } return; } int main() { cin >> n; color = new string[n]; ans = new int[n]; int m; cin >> m; int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } for (int i = 0; i < n; i++) { color[i] = "white"; } for (int j = 0; j < n; j++) { if (color[j] == "white") { bfs(j); } } int q; cin >> q; for (int i = 0; i < q; i++) { cin >> a >> b; if (ans[a] == ans[b]) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> #include <string> using namespace std; int n; string *color; queue<int> q; int *ans; vector<int> g[1000000]; void bfs(int x) { color[x] = "gray"; q.push(x); while (q.size() != 0) { int u = q.front(); q.pop(); for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (color[v] == "white") { color[v] = "gray"; q.push(v); } } color[u] = "black"; ans[u] = x; } return; } int main() { cin >> n; color = new string[n]; ans = new int[n]; int m; cin >> m; int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } for (int i = 0; i < n; i++) { color[i] = "white"; } for (int j = 0; j < n; j++) { if (color[j] == "white") { bfs(j); } } int q; cin >> q; for (int i = 0; i < q; i++) { cin >> a >> b; if (ans[a] == ans[b]) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0; }
replace
15
16
15
16
0
p02240
C++
Runtime Error
#include <stdio.h> using namespace std; const int MAX = 100000; int parent(int table[], int child) { if (table[child] == -1) return child; return table[child] = parent(table, table[child]); } int main() { int n, m, q; int table[MAX]; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) table[i] = -1; for (int i = 0; i < m; i++) { int s, t; scanf("%d %d", &s, &t); int ps = parent(table, s); int pt = parent(table, t); if (ps != pt) { table[ps] = pt; } else { table[s] = ps; table[t] = pt; } } scanf("%d", &q); for (int i = 0; i < q; i++) { int s, t; scanf("%d %d", &s, &t); int ps = parent(table, s); int pt = parent(table, t); if (ps == pt) { printf("yes\n"); } else { printf("no\n"); } } return 0; }
#include <stdio.h> using namespace std; const int MAX = 100000; int parent(int table[], int child) { if (table[child] == -1) return child; return table[child] = parent(table, table[child]); } int main() { int n, m, q; int table[MAX]; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) table[i] = -1; for (int i = 0; i < m; i++) { int s, t; scanf("%d %d", &s, &t); int ps = parent(table, s); int pt = parent(table, t); if (ps != pt) { table[ps] = pt; } } scanf("%d", &q); for (int i = 0; i < q; i++) { int s, t; scanf("%d %d", &s, &t); int ps = parent(table, s); int pt = parent(table, t); if (ps == pt) { printf("yes\n"); } else { printf("no\n"); } } return 0; }
delete
27
30
27
27
-11
p02240
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; vector<vector<int>> G(100); vector<int> c(100); void dfs(int s, int x) { if (c[s]) return; c[s] = x; for (auto a : G[s]) dfs(a, x); } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } for (int i = 0; i < n; ++i) dfs(i, i + 1); int q; cin >> q; for (int i = 0; i < q; ++i) { int a, b; cin >> a >> b; cout << (c[a] - c[b] ? "no" : "yes") << endl; } }
#include <iostream> #include <vector> using namespace std; vector<vector<int>> G(100000); vector<int> c(100000); void dfs(int s, int x) { if (c[s]) return; c[s] = x; for (auto a : G[s]) dfs(a, x); } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } for (int i = 0; i < n; ++i) dfs(i, i + 1); int q; cin >> q; for (int i = 0; i < q; ++i) { int a, b; cin >> a >> b; cout << (c[a] - c[b] ? "no" : "yes") << endl; } }
replace
3
5
3
5
0
p02240
C++
Runtime Error
#include <iostream> #include <stack> #include <vector> using namespace std; vector<int> G[10000]; int color[10000]; int n; void dfs(int idx, int c) { stack<int> s; s.push(idx); color[idx] = c; while (!s.empty()) { int u = s.top(); s.pop(); for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (color[v] == -1) { color[v] = c; s.push(v); } } } } void AssignColor() { int id = 1; for (int i = 0; i < n; ++i) color[i] = -1; for (int u = 0; u < n; ++u) { if (color[u] == -1) dfs(u, id++); } } int main() { int s, t, m, q; cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> s >> t; G[s].push_back(t); G[t].push_back(s); } AssignColor(); cin >> q; for (int i = 0; i < q; ++i) { cin >> s >> t; if (color[s] == color[t]) { cout << "yes" << endl; } else cout << "no" << endl; } return 0; }
#include <iostream> #include <stack> #include <vector> using namespace std; vector<int> G[100000]; int color[100000]; int n; void dfs(int idx, int c) { stack<int> s; s.push(idx); color[idx] = c; while (!s.empty()) { int u = s.top(); s.pop(); for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (color[v] == -1) { color[v] = c; s.push(v); } } } } void AssignColor() { int id = 1; for (int i = 0; i < n; ++i) color[i] = -1; for (int u = 0; u < n; ++u) { if (color[u] == -1) dfs(u, id++); } } int main() { int s, t, m, q; cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> s >> t; G[s].push_back(t); G[t].push_back(s); } AssignColor(); cin >> q; for (int i = 0; i < q; ++i) { cin >> s >> t; if (color[s] == color[t]) { cout << "yes" << endl; } else cout << "no" << endl; } return 0; }
replace
6
8
6
8
0
p02240
C++
Runtime Error
#include <iostream> #include <queue> #include <stack> #include <vector> using namespace std; static const int N = 100; int n, m; int color[N], dist; vector<int> G[N]; void dfs(int i, int c) { queue<int> q; q.push(i); color[i] = c; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (-1 == color[v]) { color[v] = c; q.push(v); } } } } void search() { for (int i = 0; i < n; ++i) color[i] = -1; int c = 0; for (int i = 0; i < n; ++i) { if (color[i] == -1) dfs(i, c++); } } int main() { cin >> n >> m; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } search(); cin >> n; for (int i = 0; i < n; ++i) { int u, v; cin >> u >> v; if (color[u] == color[v]) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
#include <iostream> #include <queue> #include <stack> #include <vector> using namespace std; static const int N = 100001; int n, m; int color[N], dist; vector<int> G[N]; void dfs(int i, int c) { queue<int> q; q.push(i); color[i] = c; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (-1 == color[v]) { color[v] = c; q.push(v); } } } } void search() { for (int i = 0; i < n; ++i) color[i] = -1; int c = 0; for (int i = 0; i < n; ++i) { if (color[i] == -1) dfs(i, c++); } } int main() { cin >> n >> m; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } search(); cin >> n; for (int i = 0; i < n; ++i) { int u, v; cin >> u >> v; if (color[u] == color[v]) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
replace
7
8
7
8
0
p02241
C++
Runtime Error
#include <iostream> using namespace std; static const int MAX = 100; static const int INFTY = (1 << 21); static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, M[MAX][MAX]; int prim() { int u, minv; int d[MAX], p[MAX], color[MAX]; for (int i = 0; i < n; i++) { d[i] = INFTY; p[i] = -1; color[i] = WHITE; } d[0] = 0; while (1) { minv = INFTY; u = -1; for (int i = 0; i < n; i++) { if (minv > d[i] && color[i] != BLACK) { u = i; minv = d[i]; } } if (u == -1) break; color[u] = BLACK; for (int v = 0; v < n; v++) { if (color[v] != BLACK && M[u][v] != INFTY) { if (d[v] > M[u][v]) { d[v] = M[u][v]; p[v] = u; color[v] = GRAY; } } } } int sum = 0; for (int i = 0; i < n; i++) { if (p[i] != -1) sum += M[i][p[i]]; } return sum; } int main() { cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; i++) { int e; cin >> e; M[i][j] = (e == -1) ? INFTY : e; } } cout << prim() << endl; return 0; }
#include <iostream> using namespace std; static const int MAX = 100; static const int INFTY = (1 << 21); static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, M[MAX][MAX]; int prim() { int u, minv; int d[MAX], p[MAX], color[MAX]; for (int i = 0; i < n; i++) { d[i] = INFTY; p[i] = -1; color[i] = WHITE; } d[0] = 0; while (1) { minv = INFTY; u = -1; for (int i = 0; i < n; i++) { if (minv > d[i] && color[i] != BLACK) { u = i; minv = d[i]; } } if (u == -1) break; color[u] = BLACK; for (int v = 0; v < n; v++) { if (color[v] != BLACK && M[u][v] != INFTY) { if (d[v] > M[u][v]) { d[v] = M[u][v]; p[v] = u; color[v] = GRAY; } } } } int sum = 0; for (int i = 0; i < n; i++) { if (p[i] != -1) sum += M[i][p[i]]; } return sum; } int main() { cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int e; cin >> e; M[i][j] = (e == -1) ? INFTY : e; } } cout << prim() << endl; return 0; }
replace
58
59
58
59
-11
p02241
C++
Time Limit Exceeded
#include <iostream> using namespace std; static const int MAX = 100; static const int INFTY = (1 << 21); static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, M[MAX][MAX]; int prim() { int u, minv; int d[MAX], p[MAX], color[MAX]; // Tに属する頂点とV-Tに属する頂点をつなぐ辺の中で、重みが最小の辺の重みを記録 // MSTにおける頂点vの親を記録 // 訪問状態を記録 for (int i = 0; i < n; i++) { // 全ての頂点に関して、colorをwhite、dをinftyに初期化 d[i] = INFTY; p[i] = -1; color[i] = WHITE; } d[0] = 0; while (1) { minv = INFTY; u = -1; for (int i = 0; i < n; i++) { // 最小の辺を探索 if (minv > d[i] && color[i] != BLACK) { u = i; minv = d[i]; } } if (u == -1) break; for (int v = 0; v < n; v++) { if (color[v] != BLACK && M[u][v] != INFTY) { // 未訪問かつuからvに辺があるとき if (d[v] > M[u][v]) { d[v] = M[u][v]; p[v] = u; color[v] = GRAY; } } } } int sum = 0; for (int i = 0; i < n; i++) { if (p[i] != -1) sum += M[i][p[i]]; } return sum; } int main(int argc, char const *argv[]) { cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int e; cin >> e; M[i][j] = (e == -1) ? INFTY : e; } } cout << prim() << endl; return 0; }
#include <iostream> using namespace std; static const int MAX = 100; static const int INFTY = (1 << 21); static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, M[MAX][MAX]; int prim() { int u, minv; int d[MAX], p[MAX], color[MAX]; // Tに属する頂点とV-Tに属する頂点をつなぐ辺の中で、重みが最小の辺の重みを記録 // MSTにおける頂点vの親を記録 // 訪問状態を記録 for (int i = 0; i < n; i++) { // 全ての頂点に関して、colorをwhite、dをinftyに初期化 d[i] = INFTY; p[i] = -1; color[i] = WHITE; } d[0] = 0; while (1) { minv = INFTY; u = -1; for (int i = 0; i < n; i++) { // 最小の辺を探索 if (minv > d[i] && color[i] != BLACK) { u = i; minv = d[i]; } } if (u == -1) break; color[u] = BLACK; for (int v = 0; v < n; v++) { if (color[v] != BLACK && M[u][v] != INFTY) { // 未訪問かつuからvに辺があるとき if (d[v] > M[u][v]) { d[v] = M[u][v]; p[v] = u; color[v] = GRAY; } } } } int sum = 0; for (int i = 0; i < n; i++) { if (p[i] != -1) sum += M[i][p[i]]; } return sum; } int main(int argc, char const *argv[]) { cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int e; cin >> e; M[i][j] = (e == -1) ? INFTY : e; } } cout << prim() << endl; return 0; }
insert
37
37
37
38
TLE
p02241
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; int n; // number of vertices int P[110], A[2020], B[2020], C[2020]; pair<int, int> D[2020]; // Weights and coordinates void init(int N) { // Initially all the points are individualized for (int i = 0; i <= N; ++i) P[i] = i; } int root(int a) { // find the root of a if (P[a] == a) return a; // a is the root return (P[a] = root(P[a])); // find root of a, make it a's parent } bool is_same_set(int a, int b) { // a and b are in the same group? return root(a) == root(b); } void unite(int a, int b) { // put a and b in same group P[root(a)] = root(b); } int main() { cin >> n; // the number of vertices for (int i = 0; i < n; ++i) { P[i] = i; // Initialize vertices to disjoint sets for (int j = 0; j < n; ++j) { A[(n * i) + j] = i; B[(n * i) + j] = j; cin >> C[(n * i) + j]; } } for (int i = 0; i < n * n; i++) { D[i].first = C[i]; D[i].second = i; } sort(D, D + n * n); int sum = 0; for (int i = 0; i < n * n; ++i) { if (D[i].first >= 0) { // Assuming the weight is not -1 if (!is_same_set(A[D[i].second], B[D[i].second])) { unite(A[D[i].second], B[D[i].second]); sum += D[i].first; } } } cout << sum << endl; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int n; // number of vertices int P[20200], A[20200], B[20200], C[20200]; pair<int, int> D[20200]; // Weights and coordinates int root(int a) { // find the root of a if (P[a] == a) return a; // a is the root return (P[a] = root(P[a])); // find root of a, make it a's parent } bool is_same_set(int a, int b) { // a and b are in the same group? return root(a) == root(b); } void unite(int a, int b) { // put a and b in same group P[root(a)] = root(b); } int main() { cin >> n; // the number of vertices for (int i = 0; i < n; ++i) { P[i] = i; // Initialize vertices to disjoint sets for (int j = 0; j < n; ++j) { A[(n * i) + j] = i; B[(n * i) + j] = j; cin >> C[(n * i) + j]; } } for (int i = 0; i < n * n; i++) { D[i].first = C[i]; D[i].second = i; } sort(D, D + n * n); int sum = 0; for (int i = 0; i < n * n; ++i) { if (D[i].first >= 0) { // Assuming the weight is not -1 if (!is_same_set(A[D[i].second], B[D[i].second])) { unite(A[D[i].second], B[D[i].second]); sum += D[i].first; } } } cout << sum << endl; }
replace
6
13
6
8
0
p02241
C++
Runtime Error
// kruskal tree #include <algorithm> #include <iostream> using namespace std; #define M 999 int parent[M], a[M], b[M]; pair<int, int> node[M]; int root(int a) { return parent[a] == a ? a : parent[a] = root(parent[a]); } int unite(int a, int b) { int x = root(a), y = root(b); if (x == y) return 0; parent[x] = y; return 1; } int main() { int i, j, s, n, m, x; cin >> n; for (m = i = 0; i < n; i++) for (j = 0; j < n; j++) if (cin >> x, ~x) a[m] = i, b[m] = j, node[m].first = x, node[m].second = m++; 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; cout << s << endl; }
// 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) { return parent[a] == a ? a : parent[a] = root(parent[a]); } int unite(int a, int b) { int x = root(a), y = root(b); if (x == y) return 0; parent[x] = y; return 1; } int main() { int i, j, s, n, m, x; cin >> n; for (m = i = 0; i < n; i++) for (j = 0; j < n; j++) if (cin >> x, ~x) a[m] = i, b[m] = j, node[m].first = x, node[m].second = m++; 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; cout << s << endl; }
replace
4
5
4
5
0
p02242
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define M 1000000007 #define ff first.first #define fs first.second #define sf second.first #define ss second.second typedef pair<int, int> P; typedef pair<P, P> PP; int main(void) { int i, j, k, u, n, flg[1001], v, c, sum, ky[1001], t, min, g; PP ke[1001]; scanf("%d", &n); sum = 0; for (i = 0; i < n; i++) ky[i] = M, flg[i] = 0; ky[0] = 0; for (i = 0; i < n; i++) { scanf("%d%d", &u, &k); for (j = sum; j < k + sum; j++) { scanf("%d%d", &v, &c); ke[j].ff = u, ke[j].fs = v, ke[j].sf = c; } sum += k; } sort(ke, ke + sum); // for(i=0;i<sum;i++) printf("ke[%d].ff=%d ke[%d].fs=%d // ke[%d].sf=%d\n",i,ke[i].ff,i,ke[i].fs,i,ke[i].sf); for (i = 0; i < sum; i++) { min = M; for (j = 0; j < sum; j++) if (flg[j] == 0 && min > ky[j]) g = j, min = j; flg[g] = 1; if (min == M) break; for (j = 0; j < sum; j++) { t = ky[ke[j].ff] + ke[j].sf; if (ky[ke[j].fs] > t) g = j, ky[ke[j].fs] = t; } } for (i = 0; i < n; i++) printf("%d %d\n", i, ky[i]); return 0; }
#include <bits/stdc++.h> using namespace std; #define M 1000000007 #define ff first.first #define fs first.second #define sf second.first #define ss second.second typedef pair<int, int> P; typedef pair<P, P> PP; int main(void) { int i, j, k, u, n, flg[10001], v, c, sum, ky[10001], t, min, g; PP ke[10001]; scanf("%d", &n); sum = 0; for (i = 0; i < n; i++) ky[i] = M, flg[i] = 0; ky[0] = 0; for (i = 0; i < n; i++) { scanf("%d%d", &u, &k); for (j = sum; j < k + sum; j++) { scanf("%d%d", &v, &c); ke[j].ff = u, ke[j].fs = v, ke[j].sf = c; } sum += k; } sort(ke, ke + sum); // for(i=0;i<sum;i++) printf("ke[%d].ff=%d ke[%d].fs=%d // ke[%d].sf=%d\n",i,ke[i].ff,i,ke[i].fs,i,ke[i].sf); for (i = 0; i < sum; i++) { min = M; for (j = 0; j < sum; j++) if (flg[j] == 0 && min > ky[j]) g = j, min = j; flg[g] = 1; if (min == M) break; for (j = 0; j < sum; j++) { t = ky[ke[j].ff] + ke[j].sf; if (ky[ke[j].fs] > t) g = j, ky[ke[j].fs] = t; } } for (i = 0; i < n; i++) printf("%d %d\n", i, ky[i]); return 0; }
replace
10
12
10
12
0
p02242
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <limits> #include <vector> using namespace std; using graph = vector<vector<int>>; int main() { int n; cin >> n; graph G(n, vector<int>(n, -1)); for (int i = 0; i < n; ++i) { int u, k; cin >> u >> k; for (int j = 0; j < k; ++j) { int v, c; cin >> v >> c; G[u][v] = c; } } // Dijkstra's algorithm vector<int> d(n, -1); vector<bool> visited(n, false); d[0] = 0; visited[0] = true; while (1) { int u = -1; int v = -1; int minw = numeric_limits<int>::max(); for (int i = 0; i < n; ++i) { if (!visited[i]) continue; for (int j = 0; j < n; ++j) { if (visited[j] && G[i][j] < 0) continue; int w = G[i][j] + d[i]; if (w < minw) { u = i; v = j; minw = w; } } } if (v < 0) { break; } d[v] = minw; visited[v] = true; } for (int i = 0; i < n; ++i) { cout << i << " " << d[i] << endl; } return 0; }
#include <algorithm> #include <iostream> #include <limits> #include <vector> using namespace std; using graph = vector<vector<int>>; int main() { int n; cin >> n; graph G(n, vector<int>(n, -1)); for (int i = 0; i < n; ++i) { int u, k; cin >> u >> k; for (int j = 0; j < k; ++j) { int v, c; cin >> v >> c; G[u][v] = c; } } // Dijkstra's algorithm vector<int> d(n, -1); vector<bool> visited(n, false); d[0] = 0; visited[0] = true; while (1) { int u = -1; int v = -1; int minw = numeric_limits<int>::max(); for (int i = 0; i < n; ++i) { if (!visited[i]) continue; for (int j = 0; j < n; ++j) { if (visited[j] || G[i][j] < 0) continue; int w = G[i][j] + d[i]; if (w < minw) { u = i; v = j; minw = w; } } } if (v < 0) { break; } d[v] = minw; visited[v] = true; } for (int i = 0; i < n; ++i) { cout << i << " " << d[i] << endl; } return 0; }
replace
38
39
38
39
TLE
p02242
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define vvi vector<vector<int>> #define vec vector #define pq priority_queue #define all(v) (v).begin(), (v).end() #define uniqueV(x) \ sort(x.begin(), x.end()); \ x.erase(unique(x.begin(), x.end()), x.end()); #define rep(i, n) for (int(i) = (0); (i) < (n); ++(i)) #define repp(i, m, n) for (int(i) = (m); (i) < (n); ++(i)) #define debug(x) cerr << #x << ": " << x << endl; #define debug2(x, y) \ cerr << "(" << #x << ", " << #y << ") = " \ << "(" << x << ", " << y << ")" << endl; #define debug3(x, y, z) \ cerr << "(" << #x << ", " << #y << ", " << #z << ") = " \ << "(" << x << ", " << y << ", " << z << ")" << endl; #define debugB(x, y) cerr << #x << ": " << bitset<y>(x) << endl; #define line() cerr << "---------------" << endl; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, -1, 1}; const double PI = 3.14159265358979323846; template <typename T> void printA(vector<T> &printArray, char between = ' ') { int paSize = printArray.size(); for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between; } if (between != '\n') { cerr << endl; } } // ------------------------------------------------------------------------------------------ struct edge { int to, cost; }; struct P { int dist, number; bool operator<(const P &p) const { return dist < p.dist; } }; const int INF = 1e8; int n; vec<vec<edge>> G; int d[111]; void printDist() { rep(i, n) { printf("%d %d\n", i, d[i]); } } void dijkstra(int s) { fill(d, d + n, INF); d[s] = 0; priority_queue<P> Q; Q.push({0, s}); while (Q.size()) { P p = Q.top(); Q.pop(); int v = p.dist; for (int i = 0; i < G[v].size(); i++) { edge e = G[v][i]; if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; Q.push({d[e.to], e.to}); } } // printDist(); // line() } } int main() { // 入力 cin >> n; G = vec<vec<edge>>(n); rep(i, n) { int u, k; cin >> u >> k; rep(j, k) { int v, c; cin >> v >> c; G[u].push_back({v, c}); } } // ダイクストラ法 dijkstra(0); // 出力 printDist(); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define vvi vector<vector<int>> #define vec vector #define pq priority_queue #define all(v) (v).begin(), (v).end() #define uniqueV(x) \ sort(x.begin(), x.end()); \ x.erase(unique(x.begin(), x.end()), x.end()); #define rep(i, n) for (int(i) = (0); (i) < (n); ++(i)) #define repp(i, m, n) for (int(i) = (m); (i) < (n); ++(i)) #define debug(x) cerr << #x << ": " << x << endl; #define debug2(x, y) \ cerr << "(" << #x << ", " << #y << ") = " \ << "(" << x << ", " << y << ")" << endl; #define debug3(x, y, z) \ cerr << "(" << #x << ", " << #y << ", " << #z << ") = " \ << "(" << x << ", " << y << ", " << z << ")" << endl; #define debugB(x, y) cerr << #x << ": " << bitset<y>(x) << endl; #define line() cerr << "---------------" << endl; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, -1, 1}; const double PI = 3.14159265358979323846; template <typename T> void printA(vector<T> &printArray, char between = ' ') { int paSize = printArray.size(); for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between; } if (between != '\n') { cerr << endl; } } // ------------------------------------------------------------------------------------------ struct edge { int to, cost; }; struct P { int dist, number; bool operator<(const P &p) const { return dist < p.dist; } }; const int INF = 1e8; int n; vec<vec<edge>> G; int d[111]; void printDist() { rep(i, n) { printf("%d %d\n", i, d[i]); } } void dijkstra(int s) { fill(d, d + n, INF); d[s] = 0; priority_queue<P> Q; Q.push({0, s}); while (Q.size()) { P p = Q.top(); Q.pop(); int v = p.number; if (d[v] < p.dist) continue; for (int i = 0; i < G[v].size(); i++) { edge e = G[v][i]; if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; Q.push({d[e.to], e.to}); } } // printDist(); // line() } } int main() { // 入力 cin >> n; G = vec<vec<edge>>(n); rep(i, n) { int u, k; cin >> u >> k; rep(j, k) { int v, c; cin >> v >> c; G[u].push_back({v, c}); } } // ダイクストラ法 dijkstra(0); // 出力 printDist(); return 0; }
replace
64
65
64
67
0
p02242
C++
Time Limit Exceeded
#include <iostream> using namespace std; static const int MAX = 100; static const int INFTY = (1 << 21); static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, M[MAX][MAX]; void dijkstra() { int minv; int d[MAX], color[MAX]; for (int i = 0; i < n; i++) { d[i] = INFTY; color[i] = WHITE; } d[0] = 0; color[0] = GRAY; while (1) { minv = INFTY; int u = -1; for (int i = 0; i < n; i++) if (minv > d[i] && color[i] != BLACK) { u = i; minv = d[i]; } if (u == -1) break; color[u] == BLACK; for (int v = 0; v < n; v++) if (color[v] != BLACK && M[u][v] != INFTY) if (d[v] > d[u] + M[u][v]) { d[v] = d[u] + M[u][v]; color[v] = GRAY; } } for (int i = 0; i < n; i++) cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl; } int main() { cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) M[i][j] = INFTY; int k, c, u, v; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; M[u][v] = c; } } dijkstra(); return 0; }
#include <iostream> using namespace std; static const int MAX = 100; static const int INFTY = (1 << 21); static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n, M[MAX][MAX]; void dijkstra() { int minv; int d[MAX], color[MAX]; for (int i = 0; i < n; i++) { d[i] = INFTY; color[i] = WHITE; } d[0] = 0; color[0] = GRAY; while (1) { minv = INFTY; int u = -1; for (int i = 0; i < n; i++) if (minv > d[i] && color[i] != BLACK) { u = i; minv = d[i]; } if (u == -1) break; color[u] = BLACK; for (int v = 0; v < n; v++) if (color[v] != BLACK && M[u][v] != INFTY) if (d[v] > d[u] + M[u][v]) { d[v] = d[u] + M[u][v]; color[v] = GRAY; } } for (int i = 0; i < n; i++) cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl; } int main() { cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) M[i][j] = INFTY; int k, c, u, v; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; M[u][v] = c; } } dijkstra(); return 0; }
replace
31
32
31
32
TLE
p02242
C++
Runtime Error
/*宣言*/ #include <algorithm> #include <cstdio> #include <iostream> #include <math.h> #include <queue> #include <string.h> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) int main() { int n, u, k, w[101][101], v, c; int d[101], kas[101] = {0}, K; cin >> n; memset(d, 0x7f, sizeof(d)); memset(w, 0x7f, sizeof(w)); rep(i, n) { cin >> u >> k; rep(j, k) { cin >> v >> c; w[u][v] = c; } } d[0] = 0; int p[101]; int Min, Min1; Min = 0; Min1 = 0x7f; kas[0] = 1; rep(j, n) { rep(i, n) { if (d[i] > d[Min] + w[Min][i]) { d[i] = d[Min] + w[Min][i]; p[i] = Min; } if (Min1 > d[i] && kas[i] == 0) { Min1 = d[i]; K = i; } } kas[K] = 1; Min = K; Min1 = 100000000; } rep(i, n) { cout << i << ' ' << d[i] << endl; } return 0; }
/*宣言*/ #include <algorithm> #include <cstdio> #include <iostream> #include <math.h> #include <queue> #include <string.h> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) int main() { int n, u, k, w[101][101], v, c; int d[101], kas[101] = {0}, K; cin >> n; memset(d, 0x7f, sizeof(d)); memset(w, 0x7f, sizeof(w)); rep(i, n) { cin >> u >> k; rep(j, k) { cin >> v >> c; w[u][v] = c; } } d[0] = 0; int p[101]; int Min, Min1; Min = 0; Min1 = 100000000; kas[0] = 1; rep(j, n) { rep(i, n) { if (d[i] > d[Min] + w[Min][i]) { d[i] = d[Min] + w[Min][i]; p[i] = Min; } if (Min1 > d[i] && kas[i] == 0) { Min1 = d[i]; K = i; } } kas[K] = 1; Min = K; Min1 = 100000000; } rep(i, n) { cout << i << ' ' << d[i] << endl; } return 0; }
replace
29
30
29
30
0
p02242
C++
Memory Limit Exceeded
#include <climits> #include <cstdio> #include <iostream> using namespace std; #define N 10000 int n; int g[N][N]; int weight[N]; bool us[N]; // prim void prim() { int v; weight[0] = 0; while (true) { v = -1; for (int i = 0; i < n; i++) { if (us[i] != true && (v == -1 || weight[i] < weight[v])) v = i; } if (v == -1) break; us[v] = true; for (int i = 0; i < n; i++) { if (weight[i] > (g[v][i] + weight[v])) { weight[i] = (g[v][i] + weight[v]); // cout <<"fsafa"<<endl; } } } } int main() { int u, k1, c, v; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { g[i][j] = 9953535; } } // load n cin >> n; // load data for (int i = 0; i < n; i++) { weight[i] = INT_MAX; us[i] = false; scanf("%d %d", &u, &k1); for (int j = 0; j < k1; j++) { scanf("%d %d", &v, &c); g[u][v] = c; // cout <<g[i][j]<<" "; } // cout <<endl; } // print prim(); for (int i = 0; i < n; i++) { cout << i << " " << weight[i] << endl; } return 0; }
#include <climits> #include <cstdio> #include <iostream> using namespace std; #define N 1010 int n; int g[N][N]; int weight[N]; bool us[N]; // prim void prim() { int v; weight[0] = 0; while (true) { v = -1; for (int i = 0; i < n; i++) { if (us[i] != true && (v == -1 || weight[i] < weight[v])) v = i; } if (v == -1) break; us[v] = true; for (int i = 0; i < n; i++) { if (weight[i] > (g[v][i] + weight[v])) { weight[i] = (g[v][i] + weight[v]); // cout <<"fsafa"<<endl; } } } } int main() { int u, k1, c, v; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { g[i][j] = 9953535; } } // load n cin >> n; // load data for (int i = 0; i < n; i++) { weight[i] = INT_MAX; us[i] = false; scanf("%d %d", &u, &k1); for (int j = 0; j < k1; j++) { scanf("%d %d", &v, &c); g[u][v] = c; // cout <<g[i][j]<<" "; } // cout <<endl; } // print prim(); for (int i = 0; i < n; i++) { cout << i << " " << weight[i] << endl; } return 0; }
replace
5
6
5
6
MLE
p02243
C++
Runtime Error
#include <bits/stdc++.h> #define INF 1000000000 #define MAX_N 100 using namespace std; int n; int d[MAX_N]; struct edge { int to, cost; edge(int to_, int cost_) : to(to_), cost(cost_) {} }; typedef pair<int, int> P; vector<edge> G[MAX_N]; void Dijkstra(int start) { fill(d, d + n, INF); priority_queue<P, vector<P>, greater<P>> que; d[start] = 0; que.push(P(0, start)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (int i = 0; i < G[v].size(); i++) { edge e = G[v][i]; if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } } } int main() { cin >> n; for (int i = 0, u, k; i < n; i++) { cin >> u >> k; for (int j = 0, v, c; j < k; j++) { cin >> v >> c; G[u].push_back(edge(v, c)); } } Dijkstra(0); for (int i = 0; i < n; i++) { cout << i << ' ' << d[i] << endl; } return 0; }
#include <bits/stdc++.h> #define INF 1000000000 #define MAX_N 10000 using namespace std; int n; int d[MAX_N]; struct edge { int to, cost; edge(int to_, int cost_) : to(to_), cost(cost_) {} }; typedef pair<int, int> P; vector<edge> G[MAX_N]; void Dijkstra(int start) { fill(d, d + n, INF); priority_queue<P, vector<P>, greater<P>> que; d[start] = 0; que.push(P(0, start)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (int i = 0; i < G[v].size(); i++) { edge e = G[v][i]; if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } } } int main() { cin >> n; for (int i = 0, u, k; i < n; i++) { cin >> u >> k; for (int j = 0, v, c; j < k; j++) { cin >> v >> c; G[u].push_back(edge(v, c)); } } Dijkstra(0); for (int i = 0; i < n; i++) { cout << i << ' ' << d[i] << endl; } return 0; }
replace
2
3
2
3
0
p02243
C++
Runtime Error
#include <algorithm> #include <cstring> #include <iostream> #include <queue> using namespace std; typedef pair<int, int> pii; #define INF 0x3f3f3f3f #define fi first #define se second int mcos[105]; vector<pii> G[105]; int N; void dijkstra(int start) { priority_queue<pii, vector<pii>, greater<pii>> pq; pq.push(pii(0, start)); memset(mcos, INF, sizeof(mcos)); mcos[start] = 0; while (!pq.empty()) { const pii tmp = pq.top(); pq.pop(); const int now = tmp.se; if (mcos[now] < tmp.fi) continue; for (int i = 0; i < G[now].size(); ++i) { const int newcost = G[now][i].fi + tmp.fi; const int next = G[now][i].se; if (newcost < mcos[next]) { mcos[next] = newcost; pq.push(pii(newcost, next)); } } } return; } int main() { cin >> N; int k, u, v, c; for (int i = 0; i < N; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; G[u].push_back(make_pair(c, v)); } } dijkstra(0); for (int i = 0; i < N; i++) { cout << i << " " << mcos[i] << endl; } return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <queue> using namespace std; typedef pair<int, int> pii; #define INF 0x3f3f3f3f #define fi first #define se second int mcos[10005]; vector<pii> G[1000005]; int N; void dijkstra(int start) { priority_queue<pii, vector<pii>, greater<pii>> pq; pq.push(pii(0, start)); memset(mcos, INF, sizeof(mcos)); mcos[start] = 0; while (!pq.empty()) { const pii tmp = pq.top(); pq.pop(); const int now = tmp.se; if (mcos[now] < tmp.fi) continue; for (int i = 0; i < G[now].size(); ++i) { const int newcost = G[now][i].fi + tmp.fi; const int next = G[now][i].se; if (newcost < mcos[next]) { mcos[next] = newcost; pq.push(pii(newcost, next)); } } } return; } int main() { cin >> N; int k, u, v, c; for (int i = 0; i < N; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; G[u].push_back(make_pair(c, v)); } } dijkstra(0); for (int i = 0; i < N; i++) { cout << i << " " << mcos[i] << endl; } return 0; }
replace
12
14
12
14
0
p02243
C++
Runtime Error
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; static const int NMAX = 100; #define INF (1 << 30) int main() { int n; cin >> n; vector<pair<int, int>> adj[NMAX]; for (int i = 0; i < n; i++) { int id, nn; cin >> id >> nn; for (int j = 0; j < nn; j++) { int to, weight; cin >> to >> weight; // use negative weights for priority_queue adj[id].push_back(make_pair(-weight, to)); } } bool visited[n]; int dist[n]; for (int i = 0; i < n; i++) { visited[i] = false; dist[i] = -INF; } priority_queue<pair<int, int>> distQ; // add node 0 for (int i = 0; i < adj[0].size(); i++) { distQ.push(adj[0][i]); int weight = adj[0][i].first; int id = adj[0][i].second; dist[id] = weight; } visited[0] = true; while (!distQ.empty()) { // find nearest node int minnode = distQ.top().second; distQ.pop(); // add node visited[minnode] = true; // update dist for (int i = 0; i < adj[minnode].size(); i++) { int weight = adj[minnode][i].first; int id = adj[minnode][i].second; if (visited[id] == false) { int newWeight = dist[minnode] + weight; if (-newWeight < -dist[id]) { distQ.push(make_pair(newWeight, id)); dist[id] = newWeight; } } } } dist[0] = 0; for (int i = 0; i < n; i++) cout << i << " " << -dist[i] << endl; }
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; static const int NMAX = 10000; #define INF (1 << 30) int main() { int n; cin >> n; vector<pair<int, int>> adj[NMAX]; for (int i = 0; i < n; i++) { int id, nn; cin >> id >> nn; for (int j = 0; j < nn; j++) { int to, weight; cin >> to >> weight; // use negative weights for priority_queue adj[id].push_back(make_pair(-weight, to)); } } bool visited[n]; int dist[n]; for (int i = 0; i < n; i++) { visited[i] = false; dist[i] = -INF; } priority_queue<pair<int, int>> distQ; // add node 0 for (int i = 0; i < adj[0].size(); i++) { distQ.push(adj[0][i]); int weight = adj[0][i].first; int id = adj[0][i].second; dist[id] = weight; } visited[0] = true; while (!distQ.empty()) { // find nearest node int minnode = distQ.top().second; distQ.pop(); // add node visited[minnode] = true; // update dist for (int i = 0; i < adj[minnode].size(); i++) { int weight = adj[minnode][i].first; int id = adj[minnode][i].second; if (visited[id] == false) { int newWeight = dist[minnode] + weight; if (-newWeight < -dist[id]) { distQ.push(make_pair(newWeight, id)); dist[id] = newWeight; } } } } dist[0] = 0; for (int i = 0; i < n; i++) cout << i << " " << -dist[i] << endl; }
replace
5
6
5
6
0
p02243
C++
Runtime Error
#include <bits/stdc++.h> #define d first #define wgt first #define to second using namespace std; typedef pair<int, int> pii; typedef vector<pii> vec; typedef vector<vec> Graph; Graph G(11000); int n, d[11000]; void Dijkstra() { bool used[1100]; priority_queue<pii, vector<pii>, greater<pii>> pq; fill(d, d + n, INT_MAX); memset(used, false, sizeof(used)); pq.push(make_pair(0, 0)); while (!pq.empty()) { pii u = pq.top(); pq.pop(); if (used[u.to]) continue; used[u.to] = true; d[u.to] = u.d; for (int i = 0; i < G[u.to].size(); i++) if (d[G[u.to][i].to] > d[u.to] + G[u.to][i].wgt) { d[G[u.to][i].to] = d[u.to] + G[u.to][i].wgt; pq.push(make_pair(d[G[u.to][i].to], G[u.to][i].to)); } } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int u, t; scanf("%d%d", &u, &t); for (int j = 0; j < t; j++) { int v, w; scanf("%d%d", &v, &w); G[u].push_back(make_pair(w, v)); } } Dijkstra(); for (int i = 0; i < n; i++) printf("%d %d\n", i, d[i]); return 0; }
#include <bits/stdc++.h> #define d first #define wgt first #define to second using namespace std; typedef pair<int, int> pii; typedef vector<pii> vec; typedef vector<vec> Graph; Graph G(11000); int n, d[11000]; void Dijkstra() { bool used[11000]; priority_queue<pii, vector<pii>, greater<pii>> pq; fill(d, d + n, INT_MAX); memset(used, false, sizeof(used)); pq.push(make_pair(0, 0)); while (!pq.empty()) { pii u = pq.top(); pq.pop(); if (used[u.to]) continue; used[u.to] = true; d[u.to] = u.d; for (int i = 0; i < G[u.to].size(); i++) if (d[G[u.to][i].to] > d[u.to] + G[u.to][i].wgt) { d[G[u.to][i].to] = d[u.to] + G[u.to][i].wgt; pq.push(make_pair(d[G[u.to][i].to], G[u.to][i].to)); } } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int u, t; scanf("%d%d", &u, &t); for (int j = 0; j < t; j++) { int v, w; scanf("%d%d", &v, &w); G[u].push_back(make_pair(w, v)); } } Dijkstra(); for (int i = 0; i < n; i++) printf("%d %d\n", i, d[i]); return 0; }
replace
15
16
15
16
0
p02243
C++
Runtime Error
#include <algorithm> #include <iostream> #include <queue> #define MAX 1000 #define INF (1 << 21) #define WHITE 0 #define GRAY 1 #define BLACK 2 using namespace std; int n; vector<pair<int, int>> adj_list[MAX]; void Dijkstra() { priority_queue<pair<int, int>> PQ; int next, min_cost; int color[n], dist[n]; for (int i = 0; i < n; ++i) { color[i] = WHITE; dist[i] = INF; } dist[0] = 0; PQ.push(make_pair(0, 0)); color[0] = GRAY; while (!PQ.empty()) { pair<int, int> next = PQ.top(); PQ.pop(); int u = next.second; color[u] = BLACK; if (dist[u] < next.first * (-1)) continue; for (int i = 0; i < adj_list[u].size(); ++i) { int v = adj_list[u][i].first; if (color[v] == BLACK) continue; if (dist[v] > dist[u] + adj_list[u][i].second) { color[v] = GRAY; dist[v] = dist[u] + adj_list[u][i].second; PQ.push(make_pair(dist[v] * (-1), v)); } } } int sum = 0; for (int i = 0; i < n; ++i) { cout << i << ' ' << (dist[i] != INF ? dist[i] : -1) << endl; } } int main() { int id, deg, v, w; cin >> n; for (int i = 0; i < n; ++i) { cin >> id >> deg; for (int j = 0; j < deg; ++j) { cin >> v >> w; adj_list[id].push_back(make_pair(v, w)); } } Dijkstra(); return 0; }
#include <algorithm> #include <iostream> #include <queue> #define MAX 10000 #define INF (1 << 21) #define WHITE 0 #define GRAY 1 #define BLACK 2 using namespace std; int n; vector<pair<int, int>> adj_list[MAX]; void Dijkstra() { priority_queue<pair<int, int>> PQ; int next, min_cost; int color[n], dist[n]; for (int i = 0; i < n; ++i) { color[i] = WHITE; dist[i] = INF; } dist[0] = 0; PQ.push(make_pair(0, 0)); color[0] = GRAY; while (!PQ.empty()) { pair<int, int> next = PQ.top(); PQ.pop(); int u = next.second; color[u] = BLACK; if (dist[u] < next.first * (-1)) continue; for (int i = 0; i < adj_list[u].size(); ++i) { int v = adj_list[u][i].first; if (color[v] == BLACK) continue; if (dist[v] > dist[u] + adj_list[u][i].second) { color[v] = GRAY; dist[v] = dist[u] + adj_list[u][i].second; PQ.push(make_pair(dist[v] * (-1), v)); } } } int sum = 0; for (int i = 0; i < n; ++i) { cout << i << ' ' << (dist[i] != INF ? dist[i] : -1) << endl; } } int main() { int id, deg, v, w; cin >> n; for (int i = 0; i < n; ++i) { cin >> id >> deg; for (int j = 0; j < deg; ++j) { cin >> v >> w; adj_list[id].push_back(make_pair(v, w)); } } Dijkstra(); return 0; }
replace
3
4
3
4
0
p02243
C++
Time Limit Exceeded
#include <algorithm> #include <cstring> #include <iostream> using namespace std; int x[10000][2000]; int x2[10000][2000]; int y[10000]; int z[10000]; int main() { memset(x, 1, sizeof(x)); memset(y, 1, sizeof(y)); int n, a, b, c, d, e; cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b; for (int i = 0; i < b; i++) { cin >> c >> d; x[a][z[a]] = c; x2[a][z[a]] = d; z[a]++; } } y[0] = 0; e = min(n, 2000); for (int i = 0; i < e; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < z[j]; k++) { y[x[j][k]] = min(y[x[j][k]], y[j] + x2[j][k]); } } } for (int i = 0; i < n; i++) { cout << i << ' ' << y[i] << endl; } return 0; }
#include <algorithm> #include <cstring> #include <iostream> using namespace std; int x[10000][2000]; int x2[10000][2000]; int y[10000]; int z[10000]; int main() { memset(x, 1, sizeof(x)); memset(y, 1, sizeof(y)); int n, a, b, c, d, e; cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b; for (int i = 0; i < b; i++) { cin >> c >> d; x[a][z[a]] = c; x2[a][z[a]] = d; z[a]++; } } y[0] = 0; e = min(n, 1600); for (int i = 0; i < e; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < z[j]; k++) { y[x[j][k]] = min(y[x[j][k]], y[j] + x2[j][k]); } } } for (int i = 0; i < n; i++) { cout << i << ' ' << y[i] << endl; } return 0; }
replace
25
26
25
26
TLE
p02243
C++
Runtime Error
#include <iostream> #include <vector> struct Edge { int to, weight; }; int main() { int n, u, k, v, c, distance[100]{}, upd_num = 1; bool must_update[100]{1}; std::vector<Edge> dest[100]; std::cin >> n; for (int i = 0; i < n; ++i) { std::cin >> u >> k; for (int j = 0; j < k; ++j) { std::cin >> v >> c; dest[u].push_back({v, c}); } if (i) distance[i] = 2000000000; } while (upd_num) for (int i = 0; i < n && upd_num; i++) { if (!must_update[i]) continue; must_update[i] = false; upd_num--; for (auto e : dest[i]) { int tmp = distance[i] + e.weight; if (tmp < distance[e.to]) { distance[e.to] = tmp; if (!must_update[e.to]) { must_update[e.to] = true; upd_num++; } } } } for (int i = 0; i < n; i++) { std::cout << i << " " << distance[i] << std::endl; } return 0; }
#include <iostream> #include <vector> struct Edge { int to, weight; }; int main() { int n, u, k, v, c, distance[100000]{}, upd_num = 1; bool must_update[100000]{1}; std::vector<Edge> dest[500000]; std::cin >> n; for (int i = 0; i < n; ++i) { std::cin >> u >> k; for (int j = 0; j < k; ++j) { std::cin >> v >> c; dest[u].push_back({v, c}); } if (i) distance[i] = 2000000000; } while (upd_num) for (int i = 0; i < n && upd_num; i++) { if (!must_update[i]) continue; must_update[i] = false; upd_num--; for (auto e : dest[i]) { int tmp = distance[i] + e.weight; if (tmp < distance[e.to]) { distance[e.to] = tmp; if (!must_update[e.to]) { must_update[e.to] = true; upd_num++; } } } } for (int i = 0; i < n; i++) { std::cout << i << " " << distance[i] << std::endl; } return 0; }
replace
8
11
8
11
0
p02243
C++
Runtime Error
#include <algorithm> #include <iostream> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; #define MAX 100 #define WHITE 0 #define GRAY 1 #define BLACK 2 #define INFTY (1 << 30) int n; vector<pair<int, int>> adj[MAX]; void dijkstra() { priority_queue<pair<int, int>> PQ; int d[MAX], color[MAX]; for (int i = 0; i < n; i++) { d[i] = INFTY; color[i] = WHITE; } d[0] = 0; PQ.push(make_pair(0, 0)); color[0] = GRAY; while (!PQ.empty()) { pair<int, int> f = PQ.top(); PQ.pop(); int u = f.second; color[u] = BLACK; if (d[u] < f.first * (-1)) continue; for (int j = 0; j < adj[u].size(); j++) { int v = adj[u][j].first; if (color[v] == BLACK) continue; if (d[v] > d[u] + adj[u][j].second) { d[v] = d[u] + adj[u][j].second; PQ.push(make_pair(d[v] * (-1), v)); color[v] = GRAY; } } } for (int i = 0; i < n; i++) { cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl; } } int main() { cin >> n; int k, c, u, v; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; adj[u].push_back(make_pair(v, c)); } } dijkstra(); return 0; }
#include <algorithm> #include <iostream> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; #define MAX 10000 #define WHITE 0 #define GRAY 1 #define BLACK 2 #define INFTY (1 << 30) int n; vector<pair<int, int>> adj[MAX]; void dijkstra() { priority_queue<pair<int, int>> PQ; int d[MAX], color[MAX]; for (int i = 0; i < n; i++) { d[i] = INFTY; color[i] = WHITE; } d[0] = 0; PQ.push(make_pair(0, 0)); color[0] = GRAY; while (!PQ.empty()) { pair<int, int> f = PQ.top(); PQ.pop(); int u = f.second; color[u] = BLACK; if (d[u] < f.first * (-1)) continue; for (int j = 0; j < adj[u].size(); j++) { int v = adj[u][j].first; if (color[v] == BLACK) continue; if (d[v] > d[u] + adj[u][j].second) { d[v] = d[u] + adj[u][j].second; PQ.push(make_pair(d[v] * (-1), v)); color[v] = GRAY; } } } for (int i = 0; i < n; i++) { cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl; } } int main() { cin >> n; int k, c, u, v; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; adj[u].push_back(make_pair(v, c)); } } dijkstra(); return 0; }
replace
7
8
7
8
0
p02243
C++
Runtime Error
#include <algorithm> #include <iostream> #include <queue> using namespace std; static const int MAX = 100; static const int INFTY = (1 << 21); static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n; vector<pair<int, int>> adj[MAX]; void dijkstra() { priority_queue<pair<int, int>> PQ; int d[MAX], color[MAX]; for (int i = 0; i < n; i++) { d[i] = INFTY; color[i] = WHITE; } d[0] = 0; PQ.push(make_pair(0, 0)); color[0] = GRAY; while (!PQ.empty()) { pair<int, int> f = PQ.top(); PQ.pop(); int u = f.second; color[u] = BLACK; if (d[u] < f.first * (-1)) continue; for (int j = 0; j < adj[u].size(); j++) { int v = adj[u][j].first; if (color[v] == BLACK) continue; if (d[v] > d[u] + adj[u][j].second) { d[v] = d[u] + adj[u][j].second; PQ.push(make_pair(d[v] * (-1), v)); color[v] = GRAY; } } } for (int i = 0; i < n; i++) { cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl; } } int main() { int k, c, u, v; cin >> n; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; adj[u].push_back(make_pair(v, c)); } } dijkstra(); return 0; }
#include <algorithm> #include <iostream> #include <queue> using namespace std; static const int MAX = 10000; static const int INFTY = (1 << 21); static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n; vector<pair<int, int>> adj[MAX]; void dijkstra() { priority_queue<pair<int, int>> PQ; int d[MAX], color[MAX]; for (int i = 0; i < n; i++) { d[i] = INFTY; color[i] = WHITE; } d[0] = 0; PQ.push(make_pair(0, 0)); color[0] = GRAY; while (!PQ.empty()) { pair<int, int> f = PQ.top(); PQ.pop(); int u = f.second; color[u] = BLACK; if (d[u] < f.first * (-1)) continue; for (int j = 0; j < adj[u].size(); j++) { int v = adj[u][j].first; if (color[v] == BLACK) continue; if (d[v] > d[u] + adj[u][j].second) { d[v] = d[u] + adj[u][j].second; PQ.push(make_pair(d[v] * (-1), v)); color[v] = GRAY; } } } for (int i = 0; i < n; i++) { cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl; } } int main() { int k, c, u, v; cin >> n; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; adj[u].push_back(make_pair(v, c)); } } dijkstra(); return 0; }
replace
4
5
4
5
0
p02243
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; #define MAX 10000 #define INFTY 99999999 #define WHITE 0 #define GRAY 1 #define BLACK 2 struct Node { vector<int> ch; vector<int> co; }; Node G[MAX]; int n; int d[MAX]; int p[MAX]; int color[MAX]; void dijkstra(int s) { int u; for (int i = 0; i < n; i++) { p[i] = -1; d[i] = INFTY; } d[s] = 0; while (true) { int mincost = INFTY; for (int i = 0; i < n; i++) { if (color[i] != BLACK && d[i] < mincost) { mincost = d[i]; u = i; } } if (mincost == INFTY) { break; } color[u] = BLACK; for (int i = 0; i < G[u].ch.size(); i++) { if (color[G[u].ch[i]] != BLACK) { if (d[u] + G[u].co[i] < d[G[u].ch[i]]) { d[G[u].co[i]] = d[u] + G[u].co[i]; p[G[u].co[i]] = d[u]; color[G[u].co[i]] = GRAY; } } } } } int main(void) { int u; int k; int v; int c; cin >> n; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; G[u].ch.push_back(v); G[u].co.push_back(c); } } dijkstra(0); for (int i = 0; i < n; i++) { cout << i << " " << d[i] << endl; } }
#include <iostream> #include <vector> using namespace std; #define MAX 10000 #define INFTY 99999999 #define WHITE 0 #define GRAY 1 #define BLACK 2 struct Node { vector<int> ch; vector<int> co; }; Node G[MAX]; int n; int d[MAX]; int p[MAX]; int color[MAX]; void dijkstra(int s) { int u; for (int i = 0; i < n; i++) { p[i] = -1; d[i] = INFTY; } d[s] = 0; while (true) { int mincost = INFTY; for (int i = 0; i < n; i++) { if (color[i] != BLACK && d[i] < mincost) { mincost = d[i]; u = i; } } if (mincost == INFTY) { break; } color[u] = BLACK; for (int i = 0; i < G[u].ch.size(); i++) { int co = G[u].co[i]; int ch = G[u].ch[i]; if (color[ch] != BLACK) { if (d[u] + co < d[ch]) { d[ch] = d[u] + co; p[ch] = d[u]; color[ch] = GRAY; } } } } } int main(void) { int u; int k; int v; int c; cin >> n; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; G[u].ch.push_back(v); G[u].co.push_back(c); } } dijkstra(0); for (int i = 0; i < n; i++) { cout << i << " " << d[i] << endl; } }
replace
52
57
52
60
TLE
p02243
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <vector> using namespace std; #define FOR(I, A, B) for (int I = (A); I < (B); ++I) #define CLR(mat) memset(mat, 0, sizeof(mat)) typedef long long ll; const int inf = 1e9; // ダイクストラO(elogv) struct edge { int to, cost; edge(int t, int c) { to = t, cost = c; } }; typedef pair<int, int> P; int N; vector<edge> G[111]; int d[111]; void dijkstra(int s) { priority_queue<P, vector<P>, greater<P>> que; FOR(i, 1, N + 1) d[i] = inf; d[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (auto &e : G[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; FOR(i, 0, N) { int u, k; cin >> u >> k; FOR(j, 0, k) { int v, c; cin >> v >> c; G[u].push_back(edge(v, c)); } } dijkstra(0); FOR(i, 0, N) { cout << i << " " << d[i] << endl; } }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <vector> using namespace std; #define FOR(I, A, B) for (int I = (A); I < (B); ++I) #define CLR(mat) memset(mat, 0, sizeof(mat)) typedef long long ll; const int inf = 1e9; // ダイクストラO(elogv) struct edge { int to, cost; edge(int t, int c) { to = t, cost = c; } }; typedef pair<int, int> P; int N; vector<edge> G[11111]; int d[11111]; void dijkstra(int s) { priority_queue<P, vector<P>, greater<P>> que; FOR(i, 1, N + 1) d[i] = inf; d[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (auto &e : G[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; FOR(i, 0, N) { int u, k; cin >> u >> k; FOR(j, 0, k) { int v, c; cin >> v >> c; G[u].push_back(edge(v, c)); } } dijkstra(0); FOR(i, 0, N) { cout << i << " " << d[i] << endl; } }
replace
31
33
31
33
0
p02243
C++
Runtime Error
#include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; typedef pair<int, int> P; struct edge { int to, cost; }; const int INF = 2100000000; int n; vector<edge> G[110]; vector<int> dijkstra(int s) { priority_queue<P, vector<P>, greater<P>> que; que.push(P(0, s)); vector<int> d(n, INF); d[s] = 0; while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (auto e : G[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } } return d; } int main(void) { cin >> n; for (int i = 0; i < n; i++) { int u, k, v, c; cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; G[u].push_back(edge{v, c}); } } vector<int> d = dijkstra(0); for (int i = 0; i < n; i++) { cout << i << " " << d[i] << endl; } return 0; }
#include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; typedef pair<int, int> P; struct edge { int to, cost; }; const int INF = 2100000000; int n; vector<edge> G[10010]; vector<int> dijkstra(int s) { priority_queue<P, vector<P>, greater<P>> que; que.push(P(0, s)); vector<int> d(n, INF); d[s] = 0; while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (auto e : G[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } } return d; } int main(void) { cin >> n; for (int i = 0; i < n; i++) { int u, k, v, c; cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; G[u].push_back(edge{v, c}); } } vector<int> d = dijkstra(0); for (int i = 0; i < n; i++) { cout << i << " " << d[i] << endl; } return 0; }
replace
11
12
11
12
0
p02243
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> #define P pair<int, int> // cost to using namespace std; vector<P> rinsetu[100]; int mincost[100]; signed main() { int a; cin >> a; for (int b = 0; b < a; b++) { int c, d; cin >> c >> d; for (int e = 0; e < d; e++) { int f, g; scanf("%d%d", &f, &g); rinsetu[c].push_back(P(g, f)); } } fill(mincost, mincost + a, 1 << 29); mincost[0] = 0; priority_queue<P, vector<P>, greater<P>> Q; Q.push(P(0, 0)); while (Q.size()) { P t = Q.top(); Q.pop(); if (t.first > mincost[t.second]) continue; for (P i : rinsetu[t.second]) { if (mincost[i.second] > t.first + i.first) { mincost[i.second] = t.first + i.first; Q.push(P(t.first + i.first, i.second)); } } } for (int i = 0; i < a; i++) { cout << i << " " << mincost[i] << endl; } }
#include <algorithm> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> #define P pair<int, int> // cost to using namespace std; vector<P> rinsetu[10000]; int mincost[10000]; signed main() { int a; cin >> a; for (int b = 0; b < a; b++) { int c, d; cin >> c >> d; for (int e = 0; e < d; e++) { int f, g; scanf("%d%d", &f, &g); rinsetu[c].push_back(P(g, f)); } } fill(mincost, mincost + a, 1 << 29); mincost[0] = 0; priority_queue<P, vector<P>, greater<P>> Q; Q.push(P(0, 0)); while (Q.size()) { P t = Q.top(); Q.pop(); if (t.first > mincost[t.second]) continue; for (P i : rinsetu[t.second]) { if (mincost[i.second] > t.first + i.first) { mincost[i.second] = t.first + i.first; Q.push(P(t.first + i.first, i.second)); } } } for (int i = 0; i < a; i++) { cout << i << " " << mincost[i] << endl; } }
replace
12
14
12
14
0
p02243
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctype.h> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define mod 1000000007 int main() { int n; cin >> n; int ans[101] = {}; vector<vector<pair<int, int>>> graph(n + 1); for (int i = 0; i < n; i++) { ans[i] = -1; int u, k; cin >> u >> k; for (int j = 0; j < k; j++) { int v, c; cin >> v >> c; graph[u].push_back(make_pair(v, c)); } } priority_queue<pair<int, int>> qu; qu.push(make_pair(0, 0)); ans[0] = 0; while (!qu.empty()) { int nowd = -(qu.top()).first; int nownode = (qu.top()).second; qu.pop(); for (int i = 0; i < graph[nownode].size(); i++) { int node = graph[nownode][i].first; int next = nowd + graph[nownode][i].second; if (ans[node] < 0 || ans[node] > next) { ans[node] = next; qu.push(make_pair(-next, node)); } } } for (int i = 0; i < n; i++) { cout << i << " " << ans[i] << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctype.h> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define mod 1000000007 int main() { int n; cin >> n; int ans[10001] = {}; vector<vector<pair<int, int>>> graph(n + 1); for (int i = 0; i < n; i++) { ans[i] = -1; int u, k; cin >> u >> k; for (int j = 0; j < k; j++) { int v, c; cin >> v >> c; graph[u].push_back(make_pair(v, c)); } } priority_queue<pair<int, int>> qu; qu.push(make_pair(0, 0)); ans[0] = 0; while (!qu.empty()) { int nowd = -(qu.top()).first; int nownode = (qu.top()).second; qu.pop(); for (int i = 0; i < graph[nownode].size(); i++) { int node = graph[nownode][i].first; int next = nowd + graph[nownode][i].second; if (ans[node] < 0 || ans[node] > next) { ans[node] = next; qu.push(make_pair(-next, node)); } } } for (int i = 0; i < n; i++) { cout << i << " " << ans[i] << endl; } }
replace
24
25
24
25
0
p02243
C++
Runtime Error
#include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; const int MAX_N = 100; const int INF = (1 << 21); int N; vector<pair<int, int>> A[MAX_N]; void dj() { int d[MAX_N], c[MAX_N]; // (first: distance*-1, second: adjacent) priority_queue<pair<int, int>> PQ; for (int i = 0; i < N; i++) { d[i] = INF; c[i] = -1; } d[0] = c[0] = 0; PQ.push(make_pair(0, 0)); while (!PQ.empty()) { pair<int, int> t = PQ.top(); PQ.pop(); int cur_n = t.second; c[cur_n] = 1; if (d[cur_n] < t.first * -1) continue; for (int i = 0; i < A[cur_n].size(); i++) { int adj_n = A[cur_n][i].first; if (c[adj_n] == 1) continue; if (d[cur_n] + A[cur_n][i].second < d[adj_n]) { d[adj_n] = d[cur_n] + A[cur_n][i].second; PQ.push(make_pair(d[adj_n] * -1, adj_n)); c[adj_n] = 0; } } } for (int i = 0; i < N; i++) cout << i << ' ' << d[i] << endl; } int main() { cin >> N; for (int i = 0; i < N; i++) { int cur_n, k; cin >> cur_n >> k; for (int j = 0; j < k; j++) { int u, v; cin >> u >> v; A[cur_n].push_back(make_pair(u, v)); } } dj(); return 0; }
#include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; const int MAX_N = 10000; const int INF = (1 << 21); int N; vector<pair<int, int>> A[MAX_N]; void dj() { int d[MAX_N], c[MAX_N]; // (first: distance*-1, second: adjacent) priority_queue<pair<int, int>> PQ; for (int i = 0; i < N; i++) { d[i] = INF; c[i] = -1; } d[0] = c[0] = 0; PQ.push(make_pair(0, 0)); while (!PQ.empty()) { pair<int, int> t = PQ.top(); PQ.pop(); int cur_n = t.second; c[cur_n] = 1; if (d[cur_n] < t.first * -1) continue; for (int i = 0; i < A[cur_n].size(); i++) { int adj_n = A[cur_n][i].first; if (c[adj_n] == 1) continue; if (d[cur_n] + A[cur_n][i].second < d[adj_n]) { d[adj_n] = d[cur_n] + A[cur_n][i].second; PQ.push(make_pair(d[adj_n] * -1, adj_n)); c[adj_n] = 0; } } } for (int i = 0; i < N; i++) cout << i << ' ' << d[i] << endl; } int main() { cin >> N; for (int i = 0; i < N; i++) { int cur_n, k; cin >> cur_n >> k; for (int j = 0; j < k; j++) { int u, v; cin >> u >> v; A[cur_n].push_back(make_pair(u, v)); } } dj(); return 0; }
replace
6
7
6
7
0
p02243
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <functional> #include <queue> #include <utility> #include <vector> using namespace std; typedef pair<int, int> pii; int main() { int n; scanf("%d", &n); vector<vector<pii>> G(n); for (int i = 0; i < n; ++i) { int u, k, v, c; scanf("%d%d", &u, &k); for (int j = 0; j < k; ++j) { scanf("%d%d", &v, &c); G[u].push_back(pii(c, v)); } } vector<int> d(n, 1 << 30); d[0] = 0; priority_queue<pii, vector<pii>, greater<pii>> pq; pq.push(pii(0, 0)); while (!pq.empty()) { int c = pq.top().first; int u = pq.top().second; if (d[u] != c) { continue; } for (int i = 0; i < G[u].size(); ++i) { int b = c + G[u][i].first; int v = G[u][i].second; if (d[v] > b) { d[v] = b; pq.push(pii(b, v)); } } } for (int i = 0; i < n; ++i) { printf("%d %d\n", i, d[i]); } }
#include <algorithm> #include <cstdio> #include <functional> #include <queue> #include <utility> #include <vector> using namespace std; typedef pair<int, int> pii; int main() { int n; scanf("%d", &n); vector<vector<pii>> G(n); for (int i = 0; i < n; ++i) { int u, k, v, c; scanf("%d%d", &u, &k); for (int j = 0; j < k; ++j) { scanf("%d%d", &v, &c); G[u].push_back(pii(c, v)); } } vector<int> d(n, 1 << 30); d[0] = 0; priority_queue<pii, vector<pii>, greater<pii>> pq; pq.push(pii(0, 0)); while (!pq.empty()) { int c = pq.top().first; int u = pq.top().second; pq.pop(); if (d[u] != c) { continue; } for (int i = 0; i < G[u].size(); ++i) { int b = c + G[u][i].first; int v = G[u][i].second; if (d[v] > b) { d[v] = b; pq.push(pii(b, v)); } } } for (int i = 0; i < n; ++i) { printf("%d %d\n", i, d[i]); } }
insert
30
30
30
31
TLE
p02243
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAX = 105; int state[MAX], N, D[MAX]; vector<pair<int, int>> S[MAX]; priority_queue<pair<int, int>> Q; void dijkstra() { int cnt = 1; for (int i = 0; i < N; i++) { state[i] = 0; D[i] = 10000000; } state[0] = 1; D[0] = 0; for (int i = 0; i < S[0].size(); i++) { Q.push(make_pair(-S[0][i].second, S[0][i].first)); D[S[0][i].first] = min(D[S[0][i].first], D[0] + S[0][i].second); } while (1) { int a = Q.top().second; if (state[a] == 0) { Q.pop(); state[a] = 1; for (int i = 0; i < S[a].size(); i++) { if (state[S[a][i].first] == 0) { Q.push(make_pair(-(D[a] + S[a][i].second), S[a][i].first)); D[S[a][i].first] = min(D[S[a][i].first], D[a] + S[a][i].second); } } cnt++; } else Q.pop(); if (cnt == N) break; } return; } int main() { cin >> N; for (int i = 0; i < N; i++) { int l, a; cin >> l >> a; for (int j = 0; j < a; j++) { int b, c; cin >> b >> c; S[i].push_back(make_pair(b, c)); } } dijkstra(); for (int i = 0; i < N; i++) { cout << i << " " << D[i] << endl; } }
#include <bits/stdc++.h> using namespace std; const int MAX = 10005; int state[MAX], N, D[MAX]; vector<pair<int, int>> S[MAX]; priority_queue<pair<int, int>> Q; void dijkstra() { int cnt = 1; for (int i = 0; i < N; i++) { state[i] = 0; D[i] = 10000000; } state[0] = 1; D[0] = 0; for (int i = 0; i < S[0].size(); i++) { Q.push(make_pair(-S[0][i].second, S[0][i].first)); D[S[0][i].first] = min(D[S[0][i].first], D[0] + S[0][i].second); } while (1) { int a = Q.top().second; if (state[a] == 0) { Q.pop(); state[a] = 1; for (int i = 0; i < S[a].size(); i++) { if (state[S[a][i].first] == 0) { Q.push(make_pair(-(D[a] + S[a][i].second), S[a][i].first)); D[S[a][i].first] = min(D[S[a][i].first], D[a] + S[a][i].second); } } cnt++; } else Q.pop(); if (cnt == N) break; } return; } int main() { cin >> N; for (int i = 0; i < N; i++) { int l, a; cin >> l >> a; for (int j = 0; j < a; j++) { int b, c; cin >> b >> c; S[i].push_back(make_pair(b, c)); } } dijkstra(); for (int i = 0; i < N; i++) { cout << i << " " << D[i] << endl; } }
replace
3
4
3
4
0
p02243
C++
Runtime Error
#include <climits> #include <cstdio> #include <cstring> #include <map> #include <queue> using namespace std; typedef map<int, int>::iterator mitr; map<int, int> w[100]; int d[100]; char pos[100]; priority_queue<pair<int, int>> others; pair<int, int> p; void findPath() { p = others.top(); others.pop(); while (pos[p.second]) { p = others.top(); others.pop(); } pos[p.second] = 1; int tmpu = p.second; int tmpd = -p.first; for (mitr i = w[tmpu].begin(); i != w[tmpu].end(); i++) { if (tmpd + i->second < d[i->first]) { d[i->first] = tmpd + i->second; others.push(make_pair(-d[i->first], i->first)); } } } int main() { int i, j, u, k, n, v, c; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d %d", &u, &k); for (j = 0; j < k; j++) { scanf("%d %d", &v, &c); w[u][v] = c; } } memset(pos, 0, sizeof(char) * 100); for (i = 0; i < n; i++) d[i] = INT_MAX; d[0] = 0; others.push(make_pair(0, 0)); for (i = 0; i < n; i++) findPath(); for (i = 0; i < n; i++) printf("%d %d\n", i, d[i]); return 0; }
#include <climits> #include <cstdio> #include <cstring> #include <map> #include <queue> using namespace std; typedef map<int, int>::iterator mitr; map<int, int> w[10000]; int d[10000]; char pos[10000]; priority_queue<pair<int, int>> others; pair<int, int> p; void findPath() { p = others.top(); others.pop(); while (pos[p.second]) { p = others.top(); others.pop(); } pos[p.second] = 1; int tmpu = p.second; int tmpd = -p.first; for (mitr i = w[tmpu].begin(); i != w[tmpu].end(); i++) { if (tmpd + i->second < d[i->first]) { d[i->first] = tmpd + i->second; others.push(make_pair(-d[i->first], i->first)); } } } int main() { int i, j, u, k, n, v, c; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d %d", &u, &k); for (j = 0; j < k; j++) { scanf("%d %d", &v, &c); w[u][v] = c; } } memset(pos, 0, sizeof(char) * 100); for (i = 0; i < n; i++) d[i] = INT_MAX; d[0] = 0; others.push(make_pair(0, 0)); for (i = 0; i < n; i++) findPath(); for (i = 0; i < n; i++) printf("%d %d\n", i, d[i]); return 0; }
replace
9
12
9
12
0
p02243
C++
Runtime Error
#include <iostream> #include <limits.h> #include <queue> #include <vector> using namespace std; struct Node { int id; int cost; bool done; vector<int> next_node; vector<int> len_cost; bool operator<(const Node &right) const { return cost > right.cost; } } node[100]; int main() { int n, id, k; cin >> n; for (int i = 0; i < n; i++) { cin >> id >> k; node[id].id = id; node[id].cost = INT_MAX; node[id].done = false; node[id].next_node.resize(k); node[id].len_cost.resize(k); for (int j = 0; j < k; j++) { cin >> node[id].next_node[j] >> node[id].len_cost[j]; } } priority_queue<Node> q; node[0].cost = 0; q.push(node[0]); while (!q.empty()) { Node temp = q.top(); q.pop(); if (temp.done == true) { continue; } node[temp.id].done = true; for (int i = 0; i < temp.next_node.size(); i++) { int to = temp.next_node[i]; int cost = temp.cost + temp.len_cost[i]; if (node[to].cost > cost) { node[to].cost = cost; if (node[to].done == false) { q.push(node[to]); } } } } for (int i = 0; i < n; i++) { cout << i << " " << node[i].cost << endl; } return 0; }
#include <iostream> #include <limits.h> #include <queue> #include <vector> using namespace std; struct Node { int id; int cost; bool done; vector<int> next_node; vector<int> len_cost; bool operator<(const Node &right) const { return cost > right.cost; } } node[10000]; int main() { int n, id, k; cin >> n; for (int i = 0; i < n; i++) { cin >> id >> k; node[id].id = id; node[id].cost = INT_MAX; node[id].done = false; node[id].next_node.resize(k); node[id].len_cost.resize(k); for (int j = 0; j < k; j++) { cin >> node[id].next_node[j] >> node[id].len_cost[j]; } } priority_queue<Node> q; node[0].cost = 0; q.push(node[0]); while (!q.empty()) { Node temp = q.top(); q.pop(); if (temp.done == true) { continue; } node[temp.id].done = true; for (int i = 0; i < temp.next_node.size(); i++) { int to = temp.next_node[i]; int cost = temp.cost + temp.len_cost[i]; if (node[to].cost > cost) { node[to].cost = cost; if (node[to].done == false) { q.push(node[to]); } } } } for (int i = 0; i < n; i++) { cout << i << " " << node[i].cost << endl; } return 0; }
replace
15
16
15
16
0
p02243
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; using LL = long long; using ULL = unsigned long long; const double PI = acos(-1); template <class T> constexpr T INF() { return ::std::numeric_limits<T>::max(); } template <class T> constexpr T HINF() { return INF<T>() / 2; } template <typename T_char> T_char TL(T_char cX) { return tolower(cX); }; template <typename T_char> T_char TU(T_char cX) { return toupper(cX); }; typedef pair<LL, LL> pii; const int vy[] = {-1, -1, -1, 0, 1, 1, 1, 0}, vx[] = {-1, 0, 1, 1, 1, 0, -1, -1}; const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++) if ((n >> i) & 1) cnt++; return cnt; } int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; } return ret; } int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; } return ret; } LL gcd(LL a, LL b) { if (b == 0) return a; return gcd(b, a % b); }; LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g * b; }; #define ALL(qpqpq) (qpqpq).begin(), (qpqpq).end() #define UNIQUE(wpwpw) \ sort(ALL((wpwpw))); \ (wpwpw).erase(unique(ALL((wpwpw))), (wpwpw).end()) #define LOWER(epepe) transform(ALL((epepe)), (epepe).begin(), TL<char>) #define UPPER(rprpr) transform(ALL((rprpr)), (rprpr).begin(), TU<char>) #define FOR(i, tptpt, ypypy) for (LL i = (tptpt); i < (ypypy); i++) #define REP(i, upupu) FOR(i, 0, upupu) #define INIT \ std::ios::sync_with_stdio(false); \ std::cin.tie(0) #pragma warning(disable : 4996) int V, E; struct edge { LL to, cost; }; int start; LL d[100100]; // 距離 vector<edge> vec[1010]; void dijkstra() { for (int i = 0; i < 100100; i++) d[i] = HINF<LL>(); priority_queue<pii, std::vector<pii>, std::greater<pii>> pq; d[start] = 0; pq.push(make_pair(d[start], start)); // cost,to while (!pq.empty()) { pii p = pq.top(); pq.pop(); int i = p.second; if (d[i] < p.first) continue; for (int k = 0; k < vec[i].size(); k++) { edge e = vec[i][k]; if (d[e.to] <= d[i] + e.cost) continue; d[e.to] = d[i] + e.cost; pq.push(make_pair(d[e.to], e.to)); } } } int main() { cin >> V; REP(i, V) { int u; cin >> u; int num; cin >> num; REP(j, num) { int v, c; cin >> v >> c; vec[u].emplace_back(edge{v, c}); } } dijkstra(); REP(i, V) { cout << i << " " << d[i] << endl; } }
#include "bits/stdc++.h" using namespace std; using LL = long long; using ULL = unsigned long long; const double PI = acos(-1); template <class T> constexpr T INF() { return ::std::numeric_limits<T>::max(); } template <class T> constexpr T HINF() { return INF<T>() / 2; } template <typename T_char> T_char TL(T_char cX) { return tolower(cX); }; template <typename T_char> T_char TU(T_char cX) { return toupper(cX); }; typedef pair<LL, LL> pii; const int vy[] = {-1, -1, -1, 0, 1, 1, 1, 0}, vx[] = {-1, 0, 1, 1, 1, 0, -1, -1}; const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++) if ((n >> i) & 1) cnt++; return cnt; } int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; } return ret; } int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; } return ret; } LL gcd(LL a, LL b) { if (b == 0) return a; return gcd(b, a % b); }; LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g * b; }; #define ALL(qpqpq) (qpqpq).begin(), (qpqpq).end() #define UNIQUE(wpwpw) \ sort(ALL((wpwpw))); \ (wpwpw).erase(unique(ALL((wpwpw))), (wpwpw).end()) #define LOWER(epepe) transform(ALL((epepe)), (epepe).begin(), TL<char>) #define UPPER(rprpr) transform(ALL((rprpr)), (rprpr).begin(), TU<char>) #define FOR(i, tptpt, ypypy) for (LL i = (tptpt); i < (ypypy); i++) #define REP(i, upupu) FOR(i, 0, upupu) #define INIT \ std::ios::sync_with_stdio(false); \ std::cin.tie(0) #pragma warning(disable : 4996) int V, E; struct edge { LL to, cost; }; int start; LL d[100100]; // 距離 vector<edge> vec[101010]; void dijkstra() { for (int i = 0; i < 100100; i++) d[i] = HINF<LL>(); priority_queue<pii, std::vector<pii>, std::greater<pii>> pq; d[start] = 0; pq.push(make_pair(d[start], start)); // cost,to while (!pq.empty()) { pii p = pq.top(); pq.pop(); int i = p.second; if (d[i] < p.first) continue; for (int k = 0; k < vec[i].size(); k++) { edge e = vec[i][k]; if (d[e.to] <= d[i] + e.cost) continue; d[e.to] = d[i] + e.cost; pq.push(make_pair(d[e.to], e.to)); } } } int main() { cin >> V; REP(i, V) { int u; cin >> u; int num; cin >> num; REP(j, num) { int v, c; cin >> v >> c; vec[u].emplace_back(edge{v, c}); } } dijkstra(); REP(i, V) { cout << i << " " << d[i] << endl; } }
replace
64
65
64
65
0
p02243
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> #define rep(index, num) for (int index = 0; index < num; index++) #define rep1(index, num) for (int index = 1; index <= num; index++) #define scan(argument) cin >> argument #define prin(argument) cout << argument << endl #define kaigyo cout << endl #define eps 1e-15 #define mp(a1, a2) make_pair(a1, a2) typedef long long ll; using namespace std; typedef pair<int, int> pint; typedef vector<int> vint; typedef vector<pint> vpint; int INF = 1e+9 + 1; int N; vpint adj[10000]; int d[100], color[100], p[100]; void dijkstra() { priority_queue<pint> PQ; rep(i, N) { d[i] = INF; color[i] = 0; } d[0] = 0; color[0] = 1; PQ.push(mp(0, 0)); while (!PQ.empty()) { pint f = PQ.top(); PQ.pop(); int u = f.second; color[u] = 2; if (d[u] < -f.first) continue; rep(j, adj[u].size()) { int v = adj[u][j].first; if (color[v] == 2) continue; if (d[v] > d[u] + adj[u][j].second) { d[v] = d[u] + adj[u][j].second; PQ.push(mp(-d[v], v)); color[v] = 1; } } } } int main() { scan(N); int k, c, u, v; rep(i, N) { scan(u); scan(k); rep(j, k) { scan(v); scan(c); adj[u].push_back(mp(v, c)); } } dijkstra(); rep(i, N) { printf("%d %d\n", i, (d[i] == INF ? -1 : d[i])); } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> #define rep(index, num) for (int index = 0; index < num; index++) #define rep1(index, num) for (int index = 1; index <= num; index++) #define scan(argument) cin >> argument #define prin(argument) cout << argument << endl #define kaigyo cout << endl #define eps 1e-15 #define mp(a1, a2) make_pair(a1, a2) typedef long long ll; using namespace std; typedef pair<int, int> pint; typedef vector<int> vint; typedef vector<pint> vpint; int INF = 1e+9 + 1; int N; vpint adj[10000]; int d[10000], color[10000], p[10000]; void dijkstra() { priority_queue<pint> PQ; rep(i, N) { d[i] = INF; color[i] = 0; } d[0] = 0; color[0] = 1; PQ.push(mp(0, 0)); while (!PQ.empty()) { pint f = PQ.top(); PQ.pop(); int u = f.second; color[u] = 2; if (d[u] < -f.first) continue; rep(j, adj[u].size()) { int v = adj[u][j].first; if (color[v] == 2) continue; if (d[v] > d[u] + adj[u][j].second) { d[v] = d[u] + adj[u][j].second; PQ.push(mp(-d[v], v)); color[v] = 1; } } } } int main() { scan(N); int k, c, u, v; rep(i, N) { scan(u); scan(k); rep(j, k) { scan(v); scan(c); adj[u].push_back(mp(v, c)); } } dijkstra(); rep(i, N) { printf("%d %d\n", i, (d[i] == INF ? -1 : d[i])); } return 0; }
replace
25
26
25
26
0
p02243
C++
Runtime Error
#include <iostream> #include <queue> using namespace std; #define INFTY (1 << 30) //??????????????? enum Color { WHITE, GRAY, BLACK, }; // SSSP void Dijkstra(vector<pair<int, int>> *v_w_m, int verNum) { //???????¨???? priority_queue<pair<int, int>> pQ; int distance[10000]; Color color[10000]; int parent[10000]; for (int i = 0; i < verNum; i++) { color[i] = WHITE; distance[i] = INFTY; parent[i] = -1; } distance[0] = 0; pQ.push(make_pair(0, 0)); // SSSP?????? while (!pQ.empty()) { //??°??¬????????????????????????????°?????????????????????¶ // int min_cost = INFTY; int subScript; pair<int, int> wei_sub_map = pQ.top(); pQ.pop(); subScript = wei_sub_map.second; // for (int i = 0; i < verNum; i++) { // if (color[i] != BLACK && distance[i] < min_cost) { // min_cost = distance[i]; // subScript = i; // } // } //????????????????????? color[subScript] = BLACK; //???????????????????????°?????????????????????????????????????????? //?????????????????¨4169407B?????¨??????0.01sec???????????? if (distance[subScript] < wei_sub_map.first * -1) continue; //??£??\?????°??¬?????? for (int i = 0; i < v_w_m[subScript].size(); i++) { int outV = v_w_m[subScript][i].first; if (color[outV] == BLACK) continue; if (distance[outV] < distance[subScript] + v_w_m[subScript][i].second) continue; distance[outV] = distance[subScript] + v_w_m[subScript][i].second; parent[outV] = subScript; color[outV] = GRAY; pQ.push(make_pair(distance[outV] * -1, outV)); } } //?????? int weight_sum = 0; for (int i = 0; i < verNum; i++) { cout << i << " " << distance[i] << endl; } } int main() { int verNum; // int verValWeight[100][100]; vector<pair<int, int>> *v_w_m; int parent[10000]; //???????¨???? // v_w_m = new vector<pair<int, int>>(); cin >> verNum; // for (int i = 0; i < verNum; i++) // for (int j = 0; j < verNum; j++) // verValWeight[i][j] = INFTY; //??\????????? for (int i = 0; i < verNum; i++) { int verVal, outNum; cin >> verVal >> outNum; for (int j = 0; j < outNum; j++) { int outVerVal, weight; cin >> outVerVal >> weight; // v_w_m[verVal].push_back(make_pair(outVerVal, //weight)); v_w_m[verVal].push_back(make_pair(outVerVal, weight)); // verValWeight[i][outVerVal] = weight; } } // SSSP Dijkstra(v_w_m, verNum); return 0; }
#include <iostream> #include <queue> using namespace std; #define INFTY (1 << 30) //??????????????? enum Color { WHITE, GRAY, BLACK, }; // SSSP void Dijkstra(vector<pair<int, int>> *v_w_m, int verNum) { //???????¨???? priority_queue<pair<int, int>> pQ; int distance[10000]; Color color[10000]; int parent[10000]; for (int i = 0; i < verNum; i++) { color[i] = WHITE; distance[i] = INFTY; parent[i] = -1; } distance[0] = 0; pQ.push(make_pair(0, 0)); // SSSP?????? while (!pQ.empty()) { //??°??¬????????????????????????????°?????????????????????¶ // int min_cost = INFTY; int subScript; pair<int, int> wei_sub_map = pQ.top(); pQ.pop(); subScript = wei_sub_map.second; // for (int i = 0; i < verNum; i++) { // if (color[i] != BLACK && distance[i] < min_cost) { // min_cost = distance[i]; // subScript = i; // } // } //????????????????????? color[subScript] = BLACK; //???????????????????????°?????????????????????????????????????????? //?????????????????¨4169407B?????¨??????0.01sec???????????? if (distance[subScript] < wei_sub_map.first * -1) continue; //??£??\?????°??¬?????? for (int i = 0; i < v_w_m[subScript].size(); i++) { int outV = v_w_m[subScript][i].first; if (color[outV] == BLACK) continue; if (distance[outV] < distance[subScript] + v_w_m[subScript][i].second) continue; distance[outV] = distance[subScript] + v_w_m[subScript][i].second; parent[outV] = subScript; color[outV] = GRAY; pQ.push(make_pair(distance[outV] * -1, outV)); } } //?????? int weight_sum = 0; for (int i = 0; i < verNum; i++) { cout << i << " " << distance[i] << endl; } } int main() { int verNum; // int verValWeight[100][100]; vector<pair<int, int>> v_w_m[10000]; int parent[10000]; //???????¨???? // v_w_m = new vector<pair<int, int>>(); cin >> verNum; // for (int i = 0; i < verNum; i++) // for (int j = 0; j < verNum; j++) // verValWeight[i][j] = INFTY; //??\????????? for (int i = 0; i < verNum; i++) { int verVal, outNum; cin >> verVal >> outNum; for (int j = 0; j < outNum; j++) { int outVerVal, weight; cin >> outVerVal >> weight; // v_w_m[verVal].push_back(make_pair(outVerVal, //weight)); v_w_m[verVal].push_back(make_pair(outVerVal, weight)); // verValWeight[i][outVerVal] = weight; } } // SSSP Dijkstra(v_w_m, verNum); return 0; }
replace
79
80
79
80
-11
p02243
C++
Runtime Error
#include <iostream> #include <queue> using namespace std; #define INFTY (1 << 30) //??????????????? enum Color { WHITE, GRAY, BLACK, }; // SSSP void Dijkstra(vector<pair<int, int>> v_w_m[], int verNum) { //???????¨???? priority_queue<pair<int, int>> pQ; int distance[100]; Color color[100]; int parent[100]; for (int i = 0; i < verNum; i++) { color[i] = WHITE; distance[i] = INFTY; parent[i] = -1; } distance[0] = 0; pQ.push(make_pair(0, 0)); // SSSP?????? while (!pQ.empty()) { //??°??¬????????????????????????????°?????????????????????¶ // int min_cost = INFTY; int subScript; pair<int, int> wei_sub_map = pQ.top(); pQ.pop(); subScript = wei_sub_map.second; // for (int i = 0; i < verNum; i++) { // if (color[i] != BLACK && distance[i] < min_cost) { // min_cost = distance[i]; // subScript = i; // } // } //????????????????????? color[subScript] = BLACK; //???????????????????????°?????????????????????????????????????????? if (distance[subScript] < wei_sub_map.first * -1) continue; //??£??\?????°??¬?????? for (int i = 0; i < v_w_m[subScript].size(); i++) { int outV = v_w_m[subScript][i].first; if (color[outV] == BLACK) continue; if (distance[outV] < distance[subScript] + v_w_m[subScript][i].second) continue; distance[outV] = distance[subScript] + v_w_m[subScript][i].second; parent[outV] = subScript; color[outV] = GRAY; pQ.push(make_pair(distance[outV] * -1, outV)); } } //?????? int weight_sum = 0; for (int i = 0; i < verNum; i++) { cout << i << " " << distance[i] << endl; } } int main() { int verNum; // int verValWeight[100][100]; vector<pair<int, int>> v_w_m[10000]; int parent[100]; //???????¨???? // v_w_m = new vector<pair<int, int>>(); cin >> verNum; // for (int i = 0; i < verNum; i++) // for (int j = 0; j < verNum; j++) // verValWeight[i][j] = INFTY; //??\????????? for (int i = 0; i < verNum; i++) { int verVal, outNum; cin >> verVal >> outNum; for (int j = 0; j < outNum; j++) { int outVerVal, weight; cin >> outVerVal >> weight; // v_w_m[verVal].push_back(make_pair(outVerVal, //weight)); v_w_m[verVal].push_back(make_pair(outVerVal, weight)); // verValWeight[i][outVerVal] = weight; } } // SSSP Dijkstra(v_w_m, verNum); return 0; }
#include <iostream> #include <queue> using namespace std; #define INFTY (1 << 30) //??????????????? enum Color { WHITE, GRAY, BLACK, }; // SSSP void Dijkstra(vector<pair<int, int>> v_w_m[], int verNum) { //???????¨???? priority_queue<pair<int, int>> pQ; int distance[10000]; Color color[10000]; int parent[10000]; for (int i = 0; i < verNum; i++) { color[i] = WHITE; distance[i] = INFTY; parent[i] = -1; } distance[0] = 0; pQ.push(make_pair(0, 0)); // SSSP?????? while (!pQ.empty()) { //??°??¬????????????????????????????°?????????????????????¶ // int min_cost = INFTY; int subScript; pair<int, int> wei_sub_map = pQ.top(); pQ.pop(); subScript = wei_sub_map.second; // for (int i = 0; i < verNum; i++) { // if (color[i] != BLACK && distance[i] < min_cost) { // min_cost = distance[i]; // subScript = i; // } // } //????????????????????? color[subScript] = BLACK; //???????????????????????°?????????????????????????????????????????? if (distance[subScript] < wei_sub_map.first * -1) continue; //??£??\?????°??¬?????? for (int i = 0; i < v_w_m[subScript].size(); i++) { int outV = v_w_m[subScript][i].first; if (color[outV] == BLACK) continue; if (distance[outV] < distance[subScript] + v_w_m[subScript][i].second) continue; distance[outV] = distance[subScript] + v_w_m[subScript][i].second; parent[outV] = subScript; color[outV] = GRAY; pQ.push(make_pair(distance[outV] * -1, outV)); } } //?????? int weight_sum = 0; for (int i = 0; i < verNum; i++) { cout << i << " " << distance[i] << endl; } } int main() { int verNum; // int verValWeight[100][100]; vector<pair<int, int>> v_w_m[10000]; int parent[100]; //???????¨???? // v_w_m = new vector<pair<int, int>>(); cin >> verNum; // for (int i = 0; i < verNum; i++) // for (int j = 0; j < verNum; j++) // verValWeight[i][j] = INFTY; //??\????????? for (int i = 0; i < verNum; i++) { int verVal, outNum; cin >> verVal >> outNum; for (int j = 0; j < outNum; j++) { int outVerVal, weight; cin >> outVerVal >> weight; // v_w_m[verVal].push_back(make_pair(outVerVal, //weight)); v_w_m[verVal].push_back(make_pair(outVerVal, weight)); // verValWeight[i][outVerVal] = weight; } } // SSSP Dijkstra(v_w_m, verNum); return 0; }
replace
18
21
18
21
0
p02243
C++
Runtime Error
#include <algorithm> #include <climits> #include <iostream> #include <queue> #include <vector> using namespace std; static const int MAX = 1000; static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n; vector<pair<int, int>> adj[MAX]; void dijkstra() { priority_queue<pair<int, int>> PQ; int color[MAX]; int d[MAX]; for (int i = 0; i < n; i++) { d[i] = INT_MAX; color[i] = WHITE; } d[0] = 0; PQ.push(make_pair(0, 0)); color[0] = GRAY; while (!PQ.empty()) { int u; pair<int, int> f = PQ.top(); PQ.pop(); u = f.second; color[u] = BLACK; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i].first; if (color[v] == BLACK) continue; if (d[u] + adj[u][i].second < d[v]) { d[v] = d[u] + adj[u][i].second; PQ.push(make_pair(d[v] * (-1), v)); color[v] = GRAY; } } } for (int i = 0; i < n; i++) cout << i << " " << d[i] << endl; } int main() { int k, u, v, c; cin >> n; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; adj[u].push_back(make_pair(v, c)); } } dijkstra(); return 0; }
#include <algorithm> #include <climits> #include <iostream> #include <queue> #include <vector> using namespace std; static const int MAX = 10000; static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n; vector<pair<int, int>> adj[MAX]; void dijkstra() { priority_queue<pair<int, int>> PQ; int color[MAX]; int d[MAX]; for (int i = 0; i < n; i++) { d[i] = INT_MAX; color[i] = WHITE; } d[0] = 0; PQ.push(make_pair(0, 0)); color[0] = GRAY; while (!PQ.empty()) { int u; pair<int, int> f = PQ.top(); PQ.pop(); u = f.second; color[u] = BLACK; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i].first; if (color[v] == BLACK) continue; if (d[u] + adj[u][i].second < d[v]) { d[v] = d[u] + adj[u][i].second; PQ.push(make_pair(d[v] * (-1), v)); color[v] = GRAY; } } } for (int i = 0; i < n; i++) cout << i << " " << d[i] << endl; } int main() { int k, u, v, c; cin >> n; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; adj[u].push_back(make_pair(v, c)); } } dijkstra(); return 0; }
replace
7
8
7
8
0
p02243
C++
Runtime Error
// // Created by ?????°?????? on 2016/05/19. // #include <algorithm> #include <iostream> #include <queue> using namespace std; const int MAX = 100; const int INFTY = (1 << 21); const int WHITE = 0; const int GRAY = 1; const int BLACK = 2; int n; vector<pair<int, int>> adj[MAX]; void dijkstra() { priority_queue<pair<int, int>> PQ; int color[MAX]; int d[MAX]; for (int i = 0; i < n; ++i) { d[i] = INFTY; color[i] = WHITE; } d[0] = 0; PQ.push(make_pair(0, 0)); color[0] = GRAY; while (!PQ.empty()) { pair<int, int> f = PQ.top(); PQ.pop(); int u = f.second; color[u] = BLACK; if (d[u] < f.first * -1) continue; for (int j = 0; j < adj[u].size(); ++j) { int v = adj[u][j].first; if (color[v] == BLACK) continue; if (d[v] > d[u] + adj[u][j].second) { d[v] = d[u] + adj[u][j].second; PQ.push(make_pair(d[v] * (-1), v)); color[v] = GRAY; } } } for (int i = 0; i < n; ++i) { cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl; } } int main() { cin >> n; int k, c, u, v; for (int i = 0; i < n; ++i) { cin >> u >> k; for (int j = 0; j < k; ++j) { cin >> v >> c; adj[u].push_back(make_pair(v, c)); } } dijkstra(); return 0; }
// // Created by ?????°?????? on 2016/05/19. // #include <algorithm> #include <iostream> #include <queue> using namespace std; const int MAX = 10000; const int INFTY = (1 << 20); const int WHITE = 0; const int GRAY = 1; const int BLACK = 2; int n; vector<pair<int, int>> adj[MAX]; void dijkstra() { priority_queue<pair<int, int>> PQ; int color[MAX]; int d[MAX]; for (int i = 0; i < n; ++i) { d[i] = INFTY; color[i] = WHITE; } d[0] = 0; PQ.push(make_pair(0, 0)); color[0] = GRAY; while (!PQ.empty()) { pair<int, int> f = PQ.top(); PQ.pop(); int u = f.second; color[u] = BLACK; if (d[u] < f.first * -1) continue; for (int j = 0; j < adj[u].size(); ++j) { int v = adj[u][j].first; if (color[v] == BLACK) continue; if (d[v] > d[u] + adj[u][j].second) { d[v] = d[u] + adj[u][j].second; PQ.push(make_pair(d[v] * (-1), v)); color[v] = GRAY; } } } for (int i = 0; i < n; ++i) { cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl; } } int main() { cin >> n; int k, c, u, v; for (int i = 0; i < n; ++i) { cin >> u >> k; for (int j = 0; j < k; ++j) { cin >> v >> c; adj[u].push_back(make_pair(v, c)); } } dijkstra(); return 0; }
replace
8
10
8
10
0
p02243
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <limits> #include <queue> #include <utility> #include <vector> using namespace std; typedef pair<int, int> Edge; int main() { int n; cin >> n; vector<vector<Edge>> lists; for (int i = 0; i < n; i++) { vector<Edge> list; int u, k; cin >> u >> k; for (int j = 0; j < k; j++) { Edge edge; cin >> edge.second >> edge.first; list.push_back(edge); } lists.push_back(list); } vector<int> costs(n, -1); costs[0] = 0; priority_queue<Edge> queue; queue.push(Edge(0, 0)); while (!queue.empty()) { Edge edge = queue.top(); queue.pop(); int dest = edge.second; int cost = edge.first; if (costs[dest] < cost) continue; for (Edge next : lists[dest]) { next.first += cost; if (costs[next.second] > next.first || costs[next.second] == -1) { costs[next.second] = next.first; queue.push(next); } } } for (int i = 0; i < n; i++) cout << i << " " << costs[i] << endl; char c; cin >> c; return 0; }
#include <algorithm> #include <iostream> #include <limits> #include <queue> #include <utility> #include <vector> using namespace std; typedef pair<int, int> Edge; int main() { int n; cin >> n; vector<vector<Edge>> lists; for (int i = 0; i < n; i++) { vector<Edge> list; int u, k; cin >> u >> k; for (int j = 0; j < k; j++) { Edge edge; cin >> edge.second >> edge.first; list.push_back(edge); } lists.push_back(list); } vector<int> costs(n, -1); costs[0] = 0; priority_queue<Edge, vector<Edge>, greater<Edge>> queue; queue.push(Edge(0, 0)); while (!queue.empty()) { Edge edge = queue.top(); queue.pop(); int dest = edge.second; int cost = edge.first; if (costs[dest] < cost) continue; for (Edge next : lists[dest]) { next.first += cost; if (costs[next.second] > next.first || costs[next.second] == -1) { costs[next.second] = next.first; queue.push(next); } } } for (int i = 0; i < n; i++) cout << i << " " << costs[i] << endl; char c; cin >> c; return 0; }
replace
27
28
27
28
TLE
p02243
C++
Runtime Error
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long int; #define rep(i, n) for (int i = 0; i < n; i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define pb push_back #define SORT(v, n) sort(v, v + n) #define ALL(x) (x).begin(), (x).end() #define debug(x) cerr << #x << ": " << x << '\n' #define elif else if #define itn ll #define int ll const int INF = 100100100; const int MOD = (int)1e9 + 7; const double EPS = 1e-9; /// →,↑,←,↓,↗,↖,↙,↘ int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; typedef vector<vector<int>> vvi; typedef vector<vector<vector<int>>> vvvi; int i, j, k; static const int MAX = 100; static const int White = 0; static const int Gray = 1; static const int Black = 2; vector<pair<int, int>> adj[MAX]; int n; void dijkstra() { int d[MAX], p[MAX], color[MAX]; priority_queue<pair<int, int>> PQ; rep(i, n) { color[i] = White; d[i] = INF; } d[0] = 0; PQ.push(make_pair(0, 0)); color[0] = Gray; while (!PQ.empty()) { pair<int, int> f = PQ.top(); PQ.pop(); int u = f.second; color[u] = Black; if (d[u] < f.first * (-1)) continue; rep(j, adj[u].size()) { int v = adj[u][j].first; if (color[v] == Black) continue; if (d[v] > d[u] + adj[u][j].second) { d[v] = d[u] + adj[u][j].second; PQ.push(make_pair(d[v] * -1, v)); color[v] = Gray; } } } rep(i, n) { cout << i << ' ' << (d[i] == INF ? -1 : d[i]) << '\n'; } } signed main() { ios::sync_with_stdio(false); cin >> n; rep(i, n) { int u, k; cin >> u >> k; rep(j, k) { int v, c; cin >> v >> c; adj[u].pb(make_pair(v, c)); } } dijkstra(); return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long int; #define rep(i, n) for (int i = 0; i < n; i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define pb push_back #define SORT(v, n) sort(v, v + n) #define ALL(x) (x).begin(), (x).end() #define debug(x) cerr << #x << ": " << x << '\n' #define elif else if #define itn ll #define int ll const int INF = 100100100; const int MOD = (int)1e9 + 7; const double EPS = 1e-9; /// →,↑,←,↓,↗,↖,↙,↘ int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; typedef vector<vector<int>> vvi; typedef vector<vector<vector<int>>> vvvi; int i, j, k; static const int MAX = 10000; static const int White = 0; static const int Gray = 1; static const int Black = 2; vector<pair<int, int>> adj[MAX]; int n; void dijkstra() { int d[MAX], p[MAX], color[MAX]; priority_queue<pair<int, int>> PQ; rep(i, n) { color[i] = White; d[i] = INF; } d[0] = 0; PQ.push(make_pair(0, 0)); color[0] = Gray; while (!PQ.empty()) { pair<int, int> f = PQ.top(); PQ.pop(); int u = f.second; color[u] = Black; if (d[u] < f.first * (-1)) continue; rep(j, adj[u].size()) { int v = adj[u][j].first; if (color[v] == Black) continue; if (d[v] > d[u] + adj[u][j].second) { d[v] = d[u] + adj[u][j].second; PQ.push(make_pair(d[v] * -1, v)); color[v] = Gray; } } } rep(i, n) { cout << i << ' ' << (d[i] == INF ? -1 : d[i]) << '\n'; } } signed main() { ios::sync_with_stdio(false); cin >> n; rep(i, n) { int u, k; cin >> u >> k; rep(j, k) { int v, c; cin >> v >> c; adj[u].pb(make_pair(v, c)); } } dijkstra(); return 0; }
replace
37
38
37
38
0
p02243
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; vector<int> adja[10000], cost[10000]; #define INF (1 << 30) int main() { int n, id, k; cin >> n; for (int i = 0; i < n; ++i) { cin >> id >> k; adja[id].resize(k); cost[id].resize(k); for (int j = 0; j < k; ++j) { cin >> adja[id][j] >> cost[id][j]; } } priority_queue<pair<int, int>> que; que.push(make_pair(0, 0)); // cost, node int best[10000]; fill(best, best + n, INF); best[0] = 0; while (!que.empty()) { pair<int, int> top = que.top(); que.pop(); if (best[top.second] < top.first) { continue; } for (int j = 0, len = adja[top.second].size(); j < len; ++j) { if (best[adja[top.second][j]] > top.first + cost[top.second][j]) { best[adja[top.second][j]] = top.first + cost[top.second][j]; que.push( make_pair(top.first + cost[top.second][j], adja[top.second][j])); } } } for (int i = 0; i < n; ++i) { cout << i << " " << best[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> adja[10000], cost[10000]; #define INF (1 << 30) int main() { int n, id, k; cin >> n; for (int i = 0; i < n; ++i) { cin >> id >> k; adja[id].resize(k); cost[id].resize(k); for (int j = 0; j < k; ++j) { cin >> adja[id][j] >> cost[id][j]; } } priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que; que.push(make_pair(0, 0)); // cost, node int best[10000]; fill(best, best + n, INF); best[0] = 0; while (!que.empty()) { pair<int, int> top = que.top(); que.pop(); if (best[top.second] < top.first) { continue; } for (int j = 0, len = adja[top.second].size(); j < len; ++j) { if (best[adja[top.second][j]] > top.first + cost[top.second][j]) { best[adja[top.second][j]] = top.first + cost[top.second][j]; que.push( make_pair(top.first + cost[top.second][j], adja[top.second][j])); } } } for (int i = 0; i < n; ++i) { cout << i << " " << best[i] << endl; } return 0; }
replace
21
22
21
24
TLE
p02243
C++
Time Limit Exceeded
// -*- mode:c++; coding:utf-8; c-basic-offset:2; -*- // ALDS1_12_C: Single Source Shortest Path II #include <algorithm> #include <cstdio> #include <queue> #include <utility> #include <vector> using uint = unsigned int; class Graph { using vc_t = std::pair<uint, uint>; std::vector<std::vector<vc_t>> g_; public: Graph(const uint n) { g_.resize(n); } void push_to(const uint u, const uint v, const uint c) { g_[u].push_back(std::make_pair(v, c)); } void dijkstra(uint *dist) { auto n = g_.size(); const uint INFINIT = 100000 * (n - 1) + 1; std::fill_n(dist, n, INFINIT); auto cmp = [](vc_t left, vc_t right) { return left.second < right.second; }; std::priority_queue<vc_t, std::vector<vc_t>, decltype(cmp)> q(cmp); q.push(vc_t(0, 0)); dist[0] = 0u; while (!q.empty()) { auto bvc = q.top(); q.pop(); auto v = bvc.first; auto c = bvc.second; if (dist[v] < c) { continue; } dist[v] = c; for (auto vc : g_[v]) { q.push(std::make_pair(vc.first, vc.second + c)); } } } }; int main() { uint n; std::scanf("%u", &n); Graph g(n); uint u, k, v, c; for (auto i = 0u; i < n; ++i) { std::scanf("%u %u", &u, &k); for (auto j = 0u; j < k; ++j) { std::scanf("%u %u", &v, &c); g.push_to(u, v, c); } } uint dist[n]; g.dijkstra(dist); for (auto i = 0u; i < n; ++i) { std::printf("%u %u\n", i, dist[i]); } } // eof
// -*- mode:c++; coding:utf-8; c-basic-offset:2; -*- // ALDS1_12_C: Single Source Shortest Path II #include <algorithm> #include <cstdio> #include <queue> #include <utility> #include <vector> using uint = unsigned int; class Graph { using vc_t = std::pair<uint, uint>; std::vector<std::vector<vc_t>> g_; public: Graph(const uint n) { g_.resize(n); } void push_to(const uint u, const uint v, const uint c) { g_[u].push_back(std::make_pair(v, c)); } void dijkstra(uint *dist) { auto n = g_.size(); const uint INFINIT = 100000 * (n - 1) + 1; std::fill_n(dist, n, INFINIT); auto cmp = [](vc_t left, vc_t right) { return left.second > right.second; }; std::priority_queue<vc_t, std::vector<vc_t>, decltype(cmp)> q(cmp); q.push(vc_t(0, 0)); dist[0] = 0u; while (!q.empty()) { auto bvc = q.top(); q.pop(); auto v = bvc.first; auto c = bvc.second; if (dist[v] < c) { continue; } dist[v] = c; for (auto vc : g_[v]) { q.push(std::make_pair(vc.first, vc.second + c)); } } } }; int main() { uint n; std::scanf("%u", &n); Graph g(n); uint u, k, v, c; for (auto i = 0u; i < n; ++i) { std::scanf("%u %u", &u, &k); for (auto j = 0u; j < k; ++j) { std::scanf("%u %u", &v, &c); g.push_to(u, v, c); } } uint dist[n]; g.dijkstra(dist); for (auto i = 0u; i < n; ++i) { std::printf("%u %u\n", i, dist[i]); } } // eof
replace
26
27
26
27
TLE
p02243
C++
Runtime Error
#include <algorithm> #include <iostream> #include <queue> using namespace std; static const int MAX = 100; static const int INFTY = 2000000000; static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n; vector<pair<int, int>> adj[MAX]; void dijkstra() { priority_queue<pair<int, int>> PQ; int d[MAX], color[MAX]; for (int i = 0; i < n; i++) { d[i] = INFTY; color[i] = WHITE; } d[0] = 0; PQ.push(make_pair(0, 0)); color[0] = GRAY; while (!PQ.empty()) { pair<int, int> f = PQ.top(); PQ.pop(); int u = f.second; color[u] = BLACK; if (d[u] < f.first * (-1)) continue; for (int j = 0; j < adj[u].size(); j++) { int v = adj[u][j].first; if (color[v] == BLACK) continue; if (d[v] > d[u] + adj[u][j].second) { d[v] = d[u] + adj[u][j].second; PQ.push(make_pair(d[v] * (-1), v)); color[v] = GRAY; } } } for (int i = 0; i < n; i++) cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << "\n"; } int main() { int k, u, v, c; cin >> n; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; adj[u].push_back(make_pair(v, c)); } } dijkstra(); return 0; }
#include <algorithm> #include <iostream> #include <queue> using namespace std; static const int MAX = 100000; static const int INFTY = 2000000000; static const int WHITE = 0; static const int GRAY = 1; static const int BLACK = 2; int n; vector<pair<int, int>> adj[MAX]; void dijkstra() { priority_queue<pair<int, int>> PQ; int d[MAX], color[MAX]; for (int i = 0; i < n; i++) { d[i] = INFTY; color[i] = WHITE; } d[0] = 0; PQ.push(make_pair(0, 0)); color[0] = GRAY; while (!PQ.empty()) { pair<int, int> f = PQ.top(); PQ.pop(); int u = f.second; color[u] = BLACK; if (d[u] < f.first * (-1)) continue; for (int j = 0; j < adj[u].size(); j++) { int v = adj[u][j].first; if (color[v] == BLACK) continue; if (d[v] > d[u] + adj[u][j].second) { d[v] = d[u] + adj[u][j].second; PQ.push(make_pair(d[v] * (-1), v)); color[v] = GRAY; } } } for (int i = 0; i < n; i++) cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << "\n"; } int main() { int k, u, v, c; cin >> n; for (int i = 0; i < n; i++) { cin >> u >> k; for (int j = 0; j < k; j++) { cin >> v >> c; adj[u].push_back(make_pair(v, c)); } } dijkstra(); return 0; }
replace
4
5
4
5
0
p02243
C++
Runtime Error
#include <iostream> #include <queue> #include <stdio.h> #include <tuple> #include <vector> using namespace std; #define NUM 100 #define NIL -1 #define INF 1000000000 #define WHITE 0 #define BLACK 1 int node_num; vector<tuple<int, int>> G[NUM]; priority_queue<tuple<int, int>> PQ; int V[NUM]; int color[NUM]; void init(void) { scanf("%d", &node_num); for (int i = 0; i < node_num; i++) { int u, k; scanf("%d", &u); scanf("%d", &k); for (int j = 0; j < k; j++) { int v_, c; scanf("%d", &v_); scanf("%d", &c); G[u].push_back(make_tuple(v_, c)); } } for (int i = 0; i < node_num; i++) { color[i] = WHITE; V[i] = INF; } } void updateAround(int n) { color[n] = BLACK; for (int i = 0; i < G[n].size(); i++) { int old_dist = V[get<0>(G[n][i])]; int new_dist = V[n] + get<1>(G[n][i]); if (new_dist < old_dist) { V[get<0>(G[n][i])] = new_dist; PQ.push(make_tuple(-new_dist, get<0>(G[n][i]))); } } int min_index = NIL; int min_value = INF; while (!PQ.empty()) { tuple<int, int> f = PQ.top(); PQ.pop(); if (color[get<1>(f)] == BLACK) { continue; } min_value = -get<0>(f); min_index = get<1>(f); break; } if (min_index != NIL) { updateAround(min_index); } } int main(void) { init(); V[0] = 0; updateAround(0); for (int i = 0; i < node_num; i++) { printf("%d %d\n", i, V[i]); } }
#include <iostream> #include <queue> #include <stdio.h> #include <tuple> #include <vector> using namespace std; #define NUM 10000 #define NIL -1 #define INF 1000000000 #define WHITE 0 #define BLACK 1 int node_num; vector<tuple<int, int>> G[NUM]; priority_queue<tuple<int, int>> PQ; int V[NUM]; int color[NUM]; void init(void) { scanf("%d", &node_num); for (int i = 0; i < node_num; i++) { int u, k; scanf("%d", &u); scanf("%d", &k); for (int j = 0; j < k; j++) { int v_, c; scanf("%d", &v_); scanf("%d", &c); G[u].push_back(make_tuple(v_, c)); } } for (int i = 0; i < node_num; i++) { color[i] = WHITE; V[i] = INF; } } void updateAround(int n) { color[n] = BLACK; for (int i = 0; i < G[n].size(); i++) { int old_dist = V[get<0>(G[n][i])]; int new_dist = V[n] + get<1>(G[n][i]); if (new_dist < old_dist) { V[get<0>(G[n][i])] = new_dist; PQ.push(make_tuple(-new_dist, get<0>(G[n][i]))); } } int min_index = NIL; int min_value = INF; while (!PQ.empty()) { tuple<int, int> f = PQ.top(); PQ.pop(); if (color[get<1>(f)] == BLACK) { continue; } min_value = -get<0>(f); min_index = get<1>(f); break; } if (min_index != NIL) { updateAround(min_index); } } int main(void) { init(); V[0] = 0; updateAround(0); for (int i = 0; i < node_num; i++) { printf("%d %d\n", i, V[i]); } }
replace
7
8
7
8
0
p02243
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <vector> using namespace std; #define int(x) \ int x; \ scanf("%d", &x) #define input(x) scanf("%d", &x) #define rep(x, n) for (int x = 0; x < n; x++) #define _br printf("\n") #define INF 2000000000 vector<pair<int, int>> PathList[100]; int d[100]; bool complete[100]; void dijkstra(int n, vector<pair<int, int>> PathList[]) { rep(i, n) { d[i] = INF; complete[i] = false; } d[0] = 0; int count = 0; while (count != n) { int mind = INF; int mindi = 0; // select rep(i, n) { if (!complete[i] && d[i] < mind) { mind = d[i]; mindi = i; } } complete[mindi] = true; count++; // update rep(i, PathList[mindi].size()) { if (PathList[mindi][i].second + d[mindi] < d[PathList[mindi][i].first]) { d[PathList[mindi][i].first] = PathList[mindi][i].second + d[mindi]; } } } } int main() { int(n); rep(i, n) { int(k); int(num); rep(j, num) { int(s); int(t); PathList[i].push_back(make_pair(s, t)); } } dijkstra(n, PathList); rep(i, n) { printf("%d %d\n", i, d[i]); } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <vector> using namespace std; #define int(x) \ int x; \ scanf("%d", &x) #define input(x) scanf("%d", &x) #define rep(x, n) for (int x = 0; x < n; x++) #define _br printf("\n") #define INF 2000000000 vector<pair<int, int>> PathList[10000]; int d[10000]; bool complete[10000]; void dijkstra(int n, vector<pair<int, int>> PathList[]) { rep(i, n) { d[i] = INF; complete[i] = false; } d[0] = 0; int count = 0; while (count != n) { int mind = INF; int mindi = 0; // select rep(i, n) { if (!complete[i] && d[i] < mind) { mind = d[i]; mindi = i; } } complete[mindi] = true; count++; // update rep(i, PathList[mindi].size()) { if (PathList[mindi][i].second + d[mindi] < d[PathList[mindi][i].first]) { d[PathList[mindi][i].first] = PathList[mindi][i].second + d[mindi]; } } } } int main() { int(n); rep(i, n) { int(k); int(num); rep(j, num) { int(s); int(t); PathList[i].push_back(make_pair(s, t)); } } dijkstra(n, PathList); rep(i, n) { printf("%d %d\n", i, d[i]); } return 0; }
replace
15
18
15
18
0
p02243
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdlib> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <stack> #include <string> #include <tuple> #include <vector> #define INT_MAX 2147483647 #define INT_MIN -2147483646 #define Loop(i, n) for (int i = 0; i < (int)n; i++) #define Loop1(i, n) for (int i = 1; i <= (int)n; i++) #define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--) #define Loopr1(i, n) for (int i = (int)n; i >= 1; i--) using namespace std; typedef long long int ll; typedef vector<int> vi; typedef vector<vector<int>> vvi; static const int N = 105; struct vertex { int id; int p; int d; bool operator<(const vertex &another) const { return (d != another.d ? d > another.d : id > another.id); } }; struct edge { int end; int w; }; int main() { int n; cin >> n; vector<vector<edge>> E(N); Loop(i, n) { int u, k; cin >> u >> k; Loop(i, k) { int v, c; cin >> v >> c; E[u].push_back({v, c}); } } // priority_queue<vertex> pq; vi color(N, 0), dist(N, 0); pq.push({0, -1, 0}); while (pq.size() > 0) { vertex v = pq.top(); pq.pop(); if (color[v.id] == 1) continue; dist[v.id] = v.d; color[v.id] = 1; Loop(j, E[v.id].size()) { pq.push({E[v.id][j].end, v.id, dist[v.id] + E[v.id][j].w}); } } Loop(i, n) { cout << i << " " << dist[i] << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdlib> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <stack> #include <string> #include <tuple> #include <vector> #define INT_MAX 2147483647 #define INT_MIN -2147483646 #define Loop(i, n) for (int i = 0; i < (int)n; i++) #define Loop1(i, n) for (int i = 1; i <= (int)n; i++) #define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--) #define Loopr1(i, n) for (int i = (int)n; i >= 1; i--) using namespace std; typedef long long int ll; typedef vector<int> vi; typedef vector<vector<int>> vvi; static const int N = 10005; struct vertex { int id; int p; int d; bool operator<(const vertex &another) const { return (d != another.d ? d > another.d : id > another.id); } }; struct edge { int end; int w; }; int main() { int n; cin >> n; vector<vector<edge>> E(N); Loop(i, n) { int u, k; cin >> u >> k; Loop(i, k) { int v, c; cin >> v >> c; E[u].push_back({v, c}); } } // priority_queue<vertex> pq; vi color(N, 0), dist(N, 0); pq.push({0, -1, 0}); while (pq.size() > 0) { vertex v = pq.top(); pq.pop(); if (color[v.id] == 1) continue; dist[v.id] = v.d; color[v.id] = 1; Loop(j, E[v.id].size()) { pq.push({E[v.id][j].end, v.id, dist[v.id] + E[v.id][j].w}); } } Loop(i, n) { cout << i << " " << dist[i] << endl; } return 0; }
replace
27
28
27
28
0
p02244
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <stdio.h> #include <string.h> namespace { const int N = 8; // the positions of k-queens are fixed. // Once initialized, the values remain unchanged. bool row[N] = {false}; bool col[N] = {false}; bool down[2 * N - 1] = {false}; bool up[2 * N - 1] = {false}; int pos[N]; void Print() { for (int r = 0; r < N; ++r) { const int qc = pos[r]; for (int c = 0; c < N; ++c) { if (c == qc) printf("Q"); else printf("."); } printf("\n"); } } // OK returns true if we can put a queen at (r, c) inline bool OK(int r, int c) { return !col[c] && !down[r - c + N - 1] && !up[r + c]; } inline void Update(int r, int c) { pos[r] = c; col[c] = true; down[r - c + N - 1] = true; up[r + c] = true; } inline void Cancel(int r, int c) { pos[r] = -1; col[c] = false; down[r - c + N - 1] = false; up[r + c] = false; } bool Put(int r) { if (row[r]) return Put(r + 1); for (int c = 0; c < 8; ++c) { if (!OK(r, c)) continue; Update(r, c); if (r == 7) return true; if (Put(r + 1)) return true; else Cancel(r, c); } return false; } } // namespace int main() { int k; std::cin >> k; memset(pos, -1, sizeof(int) * N); for (int i = 0; i < k; ++i) { int r, c; std::cin >> r >> c; Update(r, c); row[r] = true; } if (!Put(0)) { std::cerr << "ERROR: not found" << std::endl; Print(); std::exit(1); } Print(); return 0; }
#include <cstdlib> #include <iostream> #include <stdio.h> #include <string.h> namespace { const int N = 8; // the positions of k-queens are fixed. // Once initialized, the values remain unchanged. bool row[N] = {false}; bool col[N] = {false}; bool down[2 * N - 1] = {false}; bool up[2 * N - 1] = {false}; int pos[N]; void Print() { for (int r = 0; r < N; ++r) { const int qc = pos[r]; for (int c = 0; c < N; ++c) { if (c == qc) printf("Q"); else printf("."); } printf("\n"); } } // OK returns true if we can put a queen at (r, c) inline bool OK(int r, int c) { return !col[c] && !down[r - c + N - 1] && !up[r + c]; } inline void Update(int r, int c) { pos[r] = c; col[c] = true; down[r - c + N - 1] = true; up[r + c] = true; } inline void Cancel(int r, int c) { pos[r] = -1; col[c] = false; down[r - c + N - 1] = false; up[r + c] = false; } bool Put(int r) { if (row[r]) return r < 7 ? Put(r + 1) : true; for (int c = 0; c < 8; ++c) { if (!OK(r, c)) continue; Update(r, c); if (r == 7) return true; if (Put(r + 1)) return true; else Cancel(r, c); } return false; } } // namespace int main() { int k; std::cin >> k; memset(pos, -1, sizeof(int) * N); for (int i = 0; i < k; ++i) { int r, c; std::cin >> r >> c; Update(r, c); row[r] = true; } if (!Put(0)) { std::cerr << "ERROR: not found" << std::endl; Print(); std::exit(1); } Print(); return 0; }
replace
51
52
51
52
0
p02244
C++
Time Limit Exceeded
#include <cassert> #include <iostream> using namespace std; #define N 8 #define FREE -1 #define NOT_FREE 1 int row[N], col[N], dpos[2 * N - 1], dneg[2 * N - 1]; bool X[N][N]; void init() { for (int i = 0; i < N; i++) { row[i] = FREE; col[i] = FREE; } for (int i = 0; i < 2 * N - 1; i++) { dpos[i] = FREE; dneg[i] = FREE; } } void printBoard() { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (X[i][j]) { if (row[i] != j) return; } } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cout << ((row[i] == j) ? "Q" : "."); } cout << endl; } } void recursive(int i) { if (i == N) { // ?????????????????¨???????????????????????? printBoard(); return; } for (int j = 0; j < N; j++) { // (i, j)????????????????????????????????´???????????? if (NOT_FREE == col[j] || NOT_FREE == dpos[i + j] || NOT_FREE == dneg[i - j + N - 1]) continue; // (i, j)??????????????????????????? row[i] = j; col[j] = dpos[i + j] = dneg[i - j + N - 1] = FREE; // ?¬???????????????? recursive(i + 1); // ????¶???§?????????????????????????????£?????????(i, // j)???????????????????????? row[i] = col[j] = dpos[i + j] = dneg[i - j + N - 1] = FREE; } // ??????????????????????????§?????? } int main() { ios::sync_with_stdio(false); init(); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { X[i][j] = false; } } int k; cin >> k; for (int i = 0; i < k; i++) { int r, c; cin >> r >> c; X[r][c] = true; } recursive(0); return 0; }
#include <cassert> #include <iostream> using namespace std; #define N 8 #define FREE -1 #define NOT_FREE 1 int row[N], col[N], dpos[2 * N - 1], dneg[2 * N - 1]; bool X[N][N]; void init() { for (int i = 0; i < N; i++) { row[i] = FREE; col[i] = FREE; } for (int i = 0; i < 2 * N - 1; i++) { dpos[i] = FREE; dneg[i] = FREE; } } void printBoard() { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (X[i][j]) { if (row[i] != j) return; } } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cout << ((row[i] == j) ? "Q" : "."); } cout << endl; } } void recursive(int i) { if (i == N) { // ?????????????????¨???????????????????????? printBoard(); return; } for (int j = 0; j < N; j++) { // (i, j)????????????????????????????????´???????????? if (NOT_FREE == col[j] || NOT_FREE == dpos[i + j] || NOT_FREE == dneg[i - j + N - 1]) continue; // (i, j)??????????????????????????? row[i] = j; col[j] = dpos[i + j] = dneg[i - j + N - 1] = NOT_FREE; // ?¬???????????????? recursive(i + 1); // ????¶???§?????????????????????????????£?????????(i, // j)???????????????????????? row[i] = col[j] = dpos[i + j] = dneg[i - j + N - 1] = FREE; } // ??????????????????????????§?????? } int main() { ios::sync_with_stdio(false); init(); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { X[i][j] = false; } } int k; cin >> k; for (int i = 0; i < k; i++) { int r, c; cin >> r >> c; X[r][c] = true; } recursive(0); return 0; }
replace
51
52
51
52
TLE
p02245
C++
Memory Limit Exceeded
#include <cmath> #include <cstdio> #include <map> #include <queue> #include <string> #include <utility> using namespace std; #define N (3) #define N2 (N * N) struct Puzzle { int f[N2]; int space; string path; bool operator<(const Puzzle &p) const { for (int i = 0; i < N2; i++) { if (f[i] == p.f[i]) continue; return (f[i] < p.f[i]); } // return false; // ????????????????????´???????????\???????????? return true; } }; // x, y?????????????????£?????? static const int dx[4] = {-1, 0, 1, 0}; static const int dy[4] = {0, -1, 0, 1}; static const char dir[4] = {'u', 'l', 'd', 'r'}; bool isTarget(Puzzle p) { for (int i = 0; i < N2; i++) { if (p.f[i] != (i + 1)) return false; } return true; } string bfs(Puzzle s) { queue<Puzzle> Q; map<Puzzle, bool> V; Puzzle u, v; s.path = ""; Q.push(s); V[s] = true; while (!Q.empty()) { u = Q.front(); Q.pop(); if (isTarget(u)) return u.path; int sx = u.space / N; int sy = u.space % N; for (int r = 0; r < 4; r++) { int tx = sx + dx[r]; int ty = sy + dy[r]; if (tx < 0 || ty < 0 || tx >= N || ty >= N) continue; v = u; swap(v.f[u.space], v.f[tx * N + ty]); v.space = tx * N + ty; if (!V[v]) { V[v] = true; v.path += dir[r]; Q.push(v); } } } return "unsolvable"; } int main() { Puzzle in; for (int i = 0; i < N2; i++) { scanf("%d", &in.f[i]); if (in.f[i] == 0) { in.f[i] = N2; in.space = i; } } string ans = bfs(in); printf("%d\n", (int)ans.size()); return 0; }
#include <cmath> #include <cstdio> #include <map> #include <queue> #include <string> #include <utility> using namespace std; #define N (3) #define N2 (N * N) struct Puzzle { int f[N2]; int space; string path; bool operator<(const Puzzle &p) const { for (int i = 0; i < N2; i++) { if (f[i] == p.f[i]) continue; return (f[i] < p.f[i]); } return false; // ????????????????????´???????????\???????????? // return true; } }; // x, y?????????????????£?????? static const int dx[4] = {-1, 0, 1, 0}; static const int dy[4] = {0, -1, 0, 1}; static const char dir[4] = {'u', 'l', 'd', 'r'}; bool isTarget(Puzzle p) { for (int i = 0; i < N2; i++) { if (p.f[i] != (i + 1)) return false; } return true; } string bfs(Puzzle s) { queue<Puzzle> Q; map<Puzzle, bool> V; Puzzle u, v; s.path = ""; Q.push(s); V[s] = true; while (!Q.empty()) { u = Q.front(); Q.pop(); if (isTarget(u)) return u.path; int sx = u.space / N; int sy = u.space % N; for (int r = 0; r < 4; r++) { int tx = sx + dx[r]; int ty = sy + dy[r]; if (tx < 0 || ty < 0 || tx >= N || ty >= N) continue; v = u; swap(v.f[u.space], v.f[tx * N + ty]); v.space = tx * N + ty; if (!V[v]) { V[v] = true; v.path += dir[r]; Q.push(v); } } } return "unsolvable"; } int main() { Puzzle in; for (int i = 0; i < N2; i++) { scanf("%d", &in.f[i]); if (in.f[i] == 0) { in.f[i] = N2; in.space = i; } } string ans = bfs(in); printf("%d\n", (int)ans.size()); return 0; }
replace
23
25
23
25
MLE
p02245
C++
Runtime Error
#include <cstdio> #include <iostream> #include <map> #include <queue> using namespace std; queue<pair<string, int>> Q; void Sw(string st, int x) { int i; int pos, npos; int d[4] = {-3, 3, -1, 1}; string st1; for (i = 0; i <= 8; i++) { if (st[i] == '0') { pos = i; break; } } for (i = 0; i <= 3; i++) { st1 = st; if (i == 2 && pos % 3 == 0) continue; if (i == 3 && pos % 3 == 0) continue; npos = pos + d[i]; if (npos >= 9 || npos <= -1) continue; swap(st1[pos], st1[npos]); Q.push(pair<string, int>(st1, x + 1)); } } int main() { string puzzle = ""; string goal = "123456780"; map<string, bool> M; int i, x; string P; for (i = 0; i <= 8; i++) { cin >> x; puzzle += '0' + x; } Q.push(pair<string, int>(puzzle, 0)); while (1) { pair<string, int> p = Q.front(); Q.pop(); P = p.first; x = p.second; if (P == goal) { cout << x << endl; break; } if (M[P]) continue; else M[P] = 1; Sw(P, x); } return 0; }
#include <cstdio> #include <iostream> #include <map> #include <queue> using namespace std; queue<pair<string, int>> Q; void Sw(string st, int x) { int i; int pos, npos; int d[4] = {-3, 3, -1, 1}; string st1; for (i = 0; i <= 8; i++) { if (st[i] == '0') { pos = i; break; } } for (i = 0; i <= 3; i++) { st1 = st; if (i == 2 && pos % 3 == 0) continue; if (i == 3 && pos % 3 == 2) continue; npos = pos + d[i]; if (npos >= 9 || npos <= -1) continue; swap(st1[pos], st1[npos]); Q.push(pair<string, int>(st1, x + 1)); } } int main() { string puzzle = ""; string goal = "123456780"; map<string, bool> M; int i, x; string P; for (i = 0; i <= 8; i++) { cin >> x; puzzle += '0' + x; } Q.push(pair<string, int>(puzzle, 0)); while (1) { pair<string, int> p = Q.front(); Q.pop(); P = p.first; x = p.second; if (P == goal) { cout << x << endl; break; } if (M[P]) continue; else M[P] = 1; Sw(P, x); } return 0; }
replace
23
24
23
24
0
p02245
C++
Memory Limit Exceeded
#include <array> #include <cctype> #include <cstdio> #include <queue> #include <set> using namespace std; #define si static inline #define gcu getchar_unlocked si int in() { int n = 0, c = gcu(); // bool minus = false; if (c == '-') minus = true, c = gcu(); do { n = 10 * n + (c - '0'), c = gcu(); } while (c >= '0'); return n; } // return minus ? -n : n; } si void scan(char *s) { while (!isspace(*s++ = gcu())) ; } #define pcu putchar_unlocked #define svo si void out template <typename T> svo(T n) { static char buf[20]; char *p = buf; // if(n < 0) pcu('-'), n *= -1; if (!n) *p++ = '0'; else while (n) *p++ = n % 10 + '0', n /= 10; while (p != buf) pcu(*--p); } svo(const char *s) { while (*s) pcu(*s++); } svo(char *s) { while (*s) pcu(*s++); } svo(char c) { pcu(c); } // template <typename T> // svo(vector<T> &v){for(T &x:v)out(' '),out(x);out('\n');} // svo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);out('\n');} template <typename head, typename... tail> svo(head &&h, tail &&...t) { out(h); out(move(t)...); } #undef svo #undef si struct board { enum { size = 3 }; struct pos { int r, c; }; typedef array<array<int, size>, size> arr; arr b; pos p; int cnt = 0; void read() { for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) { b[i][j] = in(); if (!b[i][j]) p.r = i, p.c = j; } } void print() { for (auto &r : b) { for (int &c : r) out(&c == &r[0] ? "" : " ", c); out('\n'); } out('\n'); } int solver() { const arr end = {1, 2, 3, 4, 5, 6, 7, 8, 0}; set<arr> m; queue<board> q; q.push(*this); while (!q.empty()) { board a = q.front(); q.pop(); if (a.b == end) return a.cnt; pos to[] = {a.p.r + 1, a.p.c, a.p.r, a.p.c + 1, a.p.r - 1, a.p.c, a.p.r, a.p.c - 1}; for (pos o : to) { if (0 <= o.r && o.r < size && 0 <= o.c && o.c < size) { board c = a; swap(c.b[a.p.r][a.p.c], c.b[o.r][o.c]); c.p = o; c.cnt = a.cnt + 1; q.push(c); } } } return -1; } }; int main() { board b; b.read(); out(b.solver(), '\n'); }
#include <array> #include <cctype> #include <cstdio> #include <queue> #include <set> using namespace std; #define si static inline #define gcu getchar_unlocked si int in() { int n = 0, c = gcu(); // bool minus = false; if (c == '-') minus = true, c = gcu(); do { n = 10 * n + (c - '0'), c = gcu(); } while (c >= '0'); return n; } // return minus ? -n : n; } si void scan(char *s) { while (!isspace(*s++ = gcu())) ; } #define pcu putchar_unlocked #define svo si void out template <typename T> svo(T n) { static char buf[20]; char *p = buf; // if(n < 0) pcu('-'), n *= -1; if (!n) *p++ = '0'; else while (n) *p++ = n % 10 + '0', n /= 10; while (p != buf) pcu(*--p); } svo(const char *s) { while (*s) pcu(*s++); } svo(char *s) { while (*s) pcu(*s++); } svo(char c) { pcu(c); } // template <typename T> // svo(vector<T> &v){for(T &x:v)out(' '),out(x);out('\n');} // svo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);out('\n');} template <typename head, typename... tail> svo(head &&h, tail &&...t) { out(h); out(move(t)...); } #undef svo #undef si struct board { enum { size = 3 }; struct pos { int r, c; }; typedef array<array<int, size>, size> arr; arr b; pos p; int cnt = 0; void read() { for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) { b[i][j] = in(); if (!b[i][j]) p.r = i, p.c = j; } } void print() { for (auto &r : b) { for (int &c : r) out(&c == &r[0] ? "" : " ", c); out('\n'); } out('\n'); } int solver() { const arr end = {1, 2, 3, 4, 5, 6, 7, 8, 0}; set<arr> m; queue<board> q; q.push(*this); while (!q.empty()) { board a = q.front(); q.pop(); if (a.b == end) return a.cnt; auto it = m.find(a.b); if (it != m.end()) continue; m.insert(a.b); pos to[] = {a.p.r + 1, a.p.c, a.p.r, a.p.c + 1, a.p.r - 1, a.p.c, a.p.r, a.p.c - 1}; for (pos o : to) { if (0 <= o.r && o.r < size && 0 <= o.c && o.c < size) { board c = a; swap(c.b[a.p.r][a.p.c], c.b[o.r][o.c]); c.p = o; c.cnt = a.cnt + 1; q.push(c); } } } return -1; } }; int main() { board b; b.read(); out(b.solver(), '\n'); }
insert
92
92
92
96
MLE
p02246
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int N = 4; const int LIMIT = 100; int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; int limit; int puz[N * N]; int zero; int esti; int MDT[N * N][N * N]; int dfs(int dep, int pre) { if (esti == 0) return dep; if (dep >= limit) return LIMIT; int ret = LIMIT; for (int i = 0; i < 4; i++) { if ((i + 2) % 4 == pre) continue; int y = zero / N, x = zero % N; int ny = y + dy[i], nx = x + dx[i]; if (ny < 0 || nx < 0 || ny >= N || nx >= N) continue; int tmp[N * N]; int tmp_zero = zero; int tmp_esti = esti; for (int j = 0; j < N * N; j++) tmp[j] = puz[j]; int ch_id = ny * N + nx; int ch_dat = puz[ch_id]; esti -= MDT[zero][N * N - 1]; esti -= MDT[ny * N + nx][ch_dat]; esti += MDT[zero][ch_dat]; esti += MDT[ny * N + nx][N * N - 1]; swap(puz[zero], puz[ch_id]); zero = ch_id; ret = min(ret, dfs(dep + 1, i)); for (int j = 0; j < N * N; j++) puz[j] = tmp[j]; zero = tmp_zero; esti = tmp_esti; } return ret; } int main() { for (int i = 0; i < N * N; i++) { for (int j = 0; j < N * N; j++) { MDT[i][j] = abs(i / N - j / N) + abs(i % N - j % N); } } for (int i = 0; i < N * N; i++) { cin >> puz[i]; if (puz[i] == 0) { zero = i; puz[i] = N * N - 1; } else { puz[i]--; } esti += abs(i / N - puz[i] / N) + abs(i % N - puz[i] % N); } for (int i = esti; i <= LIMIT; i++) { limit = i; int ans = dfs(0, -1); if (ans < 100) { cout << ans << endl; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 4; const int LIMIT = 100; int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; int limit; int puz[N * N]; int zero; int esti; int MDT[N * N][N * N]; int dfs(int dep, int pre) { if (esti == 0) return dep; if (dep + esti >= limit) return LIMIT; int ret = LIMIT; for (int i = 0; i < 4; i++) { if ((i + 2) % 4 == pre) continue; int y = zero / N, x = zero % N; int ny = y + dy[i], nx = x + dx[i]; if (ny < 0 || nx < 0 || ny >= N || nx >= N) continue; int tmp[N * N]; int tmp_zero = zero; int tmp_esti = esti; for (int j = 0; j < N * N; j++) tmp[j] = puz[j]; int ch_id = ny * N + nx; int ch_dat = puz[ch_id]; esti -= MDT[zero][N * N - 1]; esti -= MDT[ny * N + nx][ch_dat]; esti += MDT[zero][ch_dat]; esti += MDT[ny * N + nx][N * N - 1]; swap(puz[zero], puz[ch_id]); zero = ch_id; ret = min(ret, dfs(dep + 1, i)); for (int j = 0; j < N * N; j++) puz[j] = tmp[j]; zero = tmp_zero; esti = tmp_esti; } return ret; } int main() { for (int i = 0; i < N * N; i++) { for (int j = 0; j < N * N; j++) { MDT[i][j] = abs(i / N - j / N) + abs(i % N - j % N); } } for (int i = 0; i < N * N; i++) { cin >> puz[i]; if (puz[i] == 0) { zero = i; puz[i] = N * N - 1; } else { puz[i]--; } esti += abs(i / N - puz[i] / N) + abs(i % N - puz[i] % N); } for (int i = esti; i <= LIMIT; i++) { limit = i; int ans = dfs(0, -1); if (ans < 100) { cout << ans << endl; break; } } return 0; }
replace
15
16
15
16
TLE
p02246
C++
Runtime Error
#include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> using namespace std; #define N 4 #define N2 16 static const int dx[4] = {0, -1, 0, 1}; static const int dy[4] = {1, 0, -1, 0}; static const char dir[4] = {'r', 'u', 'l', 'd'}; int MDT[N2][N2]; struct Puzzle { int f[N2], space, MD; int cost; bool operator<(const Puzzle &p) const { for (int i = 0; i < N2; i++) { if (f[i] == p.f[i]) continue; return f[i] < p.f[i]; } return false; } }; struct State { Puzzle puzzle; int estimated; bool operator<(const State &s) const { return estimated > s.estimated; } }; int getAllMD(Puzzle pz) { int sum = 0; for (int i = 0; i < N2; i++) { if (pz.f[i] == N2) continue; sum += MDT[i][pz.f[i] - 1]; } return sum; } int astar(Puzzle s) { priority_queue<State> PQ; s.MD = getAllMD(s); s.cost = 0; map<Puzzle, bool> V; Puzzle u, v; State initial; initial.puzzle = s; initial.estimated = getAllMD(s); PQ.push(initial); while (!PQ.empty()) { State st = PQ.top(); PQ.pop(); u = st.puzzle; if (u.MD == 0) return u.cost; V[u] = true; int sx = u.space / N; int sy = u.space % N; for (int r = 0; r < 4; r++) { int tx = sx + dx[r]; int ty = sy + dy[r]; if (tx < 0 || ty < 0 || tx >= N || ty >= N) continue; v = u; v.MD -= MDT[tx * N + ty][v.f[tx * N + ty] - 1]; v.MD += MDT[sx * N + sy][v.f[tx * N + ty] - 1]; swap(v.f[sx * N + sy], v.f[tx * N + ty]); v.space = tx * N + ty; if (!V[v]) { v.cost++; State news; news.puzzle = v; news.estimated = v.cost + v.MD; PQ.push(news); } } } return -1; } int main() { for (int i = 0; i < N2; i++) for (int j = 0; i < N2; j++) MDT[i][j] = abs(i / N - j / N) + abs(i % N - j % N); Puzzle in; for (int i = 0; i < N2; i++) { cin >> in.f[i]; if (in.f[i] == 0) { in.f[i] = N2; in.space = i; } } cout << astar(in) << endl; return 0; }
#include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> using namespace std; #define N 4 #define N2 16 static const int dx[4] = {0, -1, 0, 1}; static const int dy[4] = {1, 0, -1, 0}; static const char dir[4] = {'r', 'u', 'l', 'd'}; int MDT[N2][N2]; struct Puzzle { int f[N2], space, MD; int cost; bool operator<(const Puzzle &p) const { for (int i = 0; i < N2; i++) { if (f[i] == p.f[i]) continue; return f[i] < p.f[i]; } return false; } }; struct State { Puzzle puzzle; int estimated; bool operator<(const State &s) const { return estimated > s.estimated; } }; int getAllMD(Puzzle pz) { int sum = 0; for (int i = 0; i < N2; i++) { if (pz.f[i] == N2) continue; sum += MDT[i][pz.f[i] - 1]; } return sum; } int astar(Puzzle s) { priority_queue<State> PQ; s.MD = getAllMD(s); s.cost = 0; map<Puzzle, bool> V; Puzzle u, v; State initial; initial.puzzle = s; initial.estimated = getAllMD(s); PQ.push(initial); while (!PQ.empty()) { State st = PQ.top(); PQ.pop(); u = st.puzzle; if (u.MD == 0) return u.cost; V[u] = true; int sx = u.space / N; int sy = u.space % N; for (int r = 0; r < 4; r++) { int tx = sx + dx[r]; int ty = sy + dy[r]; if (tx < 0 || ty < 0 || tx >= N || ty >= N) continue; v = u; v.MD -= MDT[tx * N + ty][v.f[tx * N + ty] - 1]; v.MD += MDT[sx * N + sy][v.f[tx * N + ty] - 1]; swap(v.f[sx * N + sy], v.f[tx * N + ty]); v.space = tx * N + ty; if (!V[v]) { v.cost++; State news; news.puzzle = v; news.estimated = v.cost + v.MD; PQ.push(news); } } } return -1; } int main() { for (int i = 0; i < N2; i++) for (int j = 0; j < N2; j++) MDT[i][j] = abs(i / N - j / N) + abs(i % N - j % N); Puzzle in; for (int i = 0; i < N2; i++) { cin >> in.f[i]; if (in.f[i] == 0) { in.f[i] = N2; in.space = i; } } cout << astar(in) << endl; return 0; }
replace
94
95
94
95
-11
p02246
C++
Memory Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <cmath> #include <iostream> #include <map> #include <queue> using namespace std; const int num = 16, ud[] = {-1, 0, 1, 0}, lr[] = {0, -1, 0, 1}, nd = 4; int md[num][num]; struct puzzle { int sp, p[num], mdp, cost; bool operator<(const puzzle &pu) const { for (int i = 0; i < num; i++) { if (p[i] == pu.p[i]) continue; return p[i] < pu.p[i]; } return false; } }; struct state { puzzle pu; int es; bool operator<(const state &s) const { // return pu.mdp + pu.cost > s.pu.mdp + s.pu.cost; return es > s.es; } }; int astar(puzzle pz) { priority_queue<state> pq; int sum = 0; for (int i = 0; i < num; i++) { if (pz.p[i] == num) continue; sum += md[i][pz.p[i] - 1]; } pz.mdp = sum; pz.cost = 0; map<puzzle, bool> m; puzzle u, v; state initial; initial.pu = pz; initial.es = pz.mdp; pq.push(initial); while (!pq.empty()) { state st = pq.top(); pq.pop(); u = st.pu; if (!u.mdp) return u.cost; m[u] = true; int sx = u.sp / nd; int sy = u.sp % nd; for (int r = 0; r < nd; r++) { int tx = sx + ud[r]; int ty = sy + lr[r]; if (tx >= nd || tx < 0 || ty >= nd || ty < 0) continue; v = u; // v.mdp = p.mdp - md[tx * nd + ty][tmp.p[tx * nd + ty] - 1] + md[sx*nd + // sy][tmp.p[tx * nd + ty] - 1]; v.mdp -= md[tx * nd + ty][v.p[tx * nd + ty] - 1]; v.mdp += md[sx * nd + sy][v.p[tx * nd + ty] - 1]; swap(v.p[sx * nd + sy], v.p[tx * nd + ty]); v.sp = tx * nd + ty; if (!m[v]) { v.cost++; state news; news.pu = v; news.es = v.cost + v.mdp; pq.push(news); } } } return -1; } int main() { puzzle in; for (int i = 0; i < num; i++) for (int j = 0; j < num; j++) md[i][j] = abs(i / nd - j / nd) + abs(i % nd - j % nd); for (int i = 0; i < num; i++) { cin >> in.p[i]; if (!in.p[i]) { in.sp = i; in.p[i] = num; } } cout << astar(in) << endl; return 0; }
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <cmath> #include <iostream> #include <map> #include <queue> using namespace std; const int num = 16, ud[] = {0, -1, 0, 1}, lr[] = {1, 0, -1, 0}, nd = 4; int md[num][num]; struct puzzle { int sp, p[num], mdp, cost; bool operator<(const puzzle &pu) const { for (int i = 0; i < num; i++) { if (p[i] == pu.p[i]) continue; return p[i] < pu.p[i]; } return false; } }; struct state { puzzle pu; int es; bool operator<(const state &s) const { // return pu.mdp + pu.cost > s.pu.mdp + s.pu.cost; return es > s.es; } }; int astar(puzzle pz) { priority_queue<state> pq; int sum = 0; for (int i = 0; i < num; i++) { if (pz.p[i] == num) continue; sum += md[i][pz.p[i] - 1]; } pz.mdp = sum; pz.cost = 0; map<puzzle, bool> m; puzzle u, v; state initial; initial.pu = pz; initial.es = pz.mdp; pq.push(initial); while (!pq.empty()) { state st = pq.top(); pq.pop(); u = st.pu; if (!u.mdp) return u.cost; m[u] = true; int sx = u.sp / nd; int sy = u.sp % nd; for (int r = 0; r < nd; r++) { int tx = sx + ud[r]; int ty = sy + lr[r]; if (tx >= nd || tx < 0 || ty >= nd || ty < 0) continue; v = u; // v.mdp = p.mdp - md[tx * nd + ty][tmp.p[tx * nd + ty] - 1] + md[sx*nd + // sy][tmp.p[tx * nd + ty] - 1]; v.mdp -= md[tx * nd + ty][v.p[tx * nd + ty] - 1]; v.mdp += md[sx * nd + sy][v.p[tx * nd + ty] - 1]; swap(v.p[sx * nd + sy], v.p[tx * nd + ty]); v.sp = tx * nd + ty; if (!m[v]) { v.cost++; state news; news.pu = v; news.es = v.cost + v.mdp; pq.push(news); } } } return -1; } int main() { puzzle in; for (int i = 0; i < num; i++) for (int j = 0; j < num; j++) md[i][j] = abs(i / nd - j / nd) + abs(i % nd - j % nd); for (int i = 0; i < num; i++) { cin >> in.p[i]; if (!in.p[i]) { in.sp = i; in.p[i] = num; } } cout << astar(in) << endl; return 0; }
replace
7
8
7
8
MLE
p02246
C++
Memory Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <cmath> #include <iostream> #include <map> #include <queue> using namespace std; const int num = 16, ud[] = {0, 1, 0, -1}, lr[] = {1, 0, -1, 0}, nd = 4; int md[num][num]; struct puzzle { int sp, p[num], mdp, cost; bool operator<(const puzzle &pu) const { for (int i = 0; i < num; i++) { if (p[i] == pu.p[i]) continue; return p[i] < pu.p[i]; } return false; } }; struct state { puzzle pu; int es; bool operator<(const state &s) const { // return pu.mdp + pu.cost > s.pu.mdp + s.pu.cost; return es > s.es; } }; int astar(puzzle pz) { priority_queue<state> pq; int sum = 0; for (int i = 0; i < num; i++) { if (pz.p[i] == num) continue; sum += md[i][pz.p[i] - 1]; } pz.mdp = sum; pz.cost = 0; map<puzzle, bool> m; puzzle u, v; state initial; initial.pu = pz; initial.es = pz.mdp; pq.push(initial); while (!pq.empty()) { state st = pq.top(); pq.pop(); u = st.pu; if (!u.mdp) return u.cost; m[u] = true; int sx = u.sp / nd; int sy = u.sp % nd; for (int r = 0; r < nd; r++) { int tx = sx + ud[r]; int ty = sy + lr[r]; if (tx >= nd || tx < 0 || ty >= nd || ty < 0) continue; v = u; // v.mdp = p.mdp - md[tx * nd + ty][tmp.p[tx * nd + ty] - 1] + md[sx*nd + // sy][tmp.p[tx * nd + ty] - 1]; v.mdp -= md[tx * nd + ty][v.p[tx * nd + ty] - 1]; v.mdp += md[sx * nd + sy][v.p[tx * nd + ty] - 1]; swap(v.p[sx * nd + sy], v.p[tx * nd + ty]); v.sp = tx * nd + ty; if (!m[v]) { v.cost++; state news; news.pu = v; news.es = v.cost + v.mdp; pq.push(news); } } } return -1; } int main() { puzzle in; for (int i = 0; i < num; i++) for (int j = 0; j < num; j++) md[i][j] = abs(i / nd - j / nd) + abs(i % nd - j % nd); for (int i = 0; i < num; i++) { cin >> in.p[i]; if (!in.p[i]) { in.sp = i; in.p[i] = num; } } cout << astar(in) << endl; return 0; }
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <cmath> #include <iostream> #include <map> #include <queue> using namespace std; const int num = 16, ud[] = {-1, 1, 0, 0}, lr[] = {0, 0, -1, 1}, nd = 4; int md[num][num]; struct puzzle { int sp, p[num], mdp, cost; bool operator<(const puzzle &pu) const { for (int i = 0; i < num; i++) { if (p[i] == pu.p[i]) continue; return p[i] < pu.p[i]; } return false; } }; struct state { puzzle pu; int es; bool operator<(const state &s) const { // return pu.mdp + pu.cost > s.pu.mdp + s.pu.cost; return es > s.es; } }; int astar(puzzle pz) { priority_queue<state> pq; int sum = 0; for (int i = 0; i < num; i++) { if (pz.p[i] == num) continue; sum += md[i][pz.p[i] - 1]; } pz.mdp = sum; pz.cost = 0; map<puzzle, bool> m; puzzle u, v; state initial; initial.pu = pz; initial.es = pz.mdp; pq.push(initial); while (!pq.empty()) { state st = pq.top(); pq.pop(); u = st.pu; if (!u.mdp) return u.cost; m[u] = true; int sx = u.sp / nd; int sy = u.sp % nd; for (int r = 0; r < nd; r++) { int tx = sx + ud[r]; int ty = sy + lr[r]; if (tx >= nd || tx < 0 || ty >= nd || ty < 0) continue; v = u; // v.mdp = p.mdp - md[tx * nd + ty][tmp.p[tx * nd + ty] - 1] + md[sx*nd + // sy][tmp.p[tx * nd + ty] - 1]; v.mdp -= md[tx * nd + ty][v.p[tx * nd + ty] - 1]; v.mdp += md[sx * nd + sy][v.p[tx * nd + ty] - 1]; swap(v.p[sx * nd + sy], v.p[tx * nd + ty]); v.sp = tx * nd + ty; if (!m[v]) { v.cost++; state news; news.pu = v; news.es = v.cost + v.mdp; pq.push(news); } } } return -1; } int main() { puzzle in; for (int i = 0; i < num; i++) for (int j = 0; j < num; j++) md[i][j] = abs(i / nd - j / nd) + abs(i % nd - j % nd); for (int i = 0; i < num; i++) { cin >> in.p[i]; if (!in.p[i]) { in.sp = i; in.p[i] = num; } } cout << astar(in) << endl; return 0; }
replace
7
8
7
8
MLE
p02246
C++
Memory Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <cmath> #include <iostream> #include <map> #include <queue> using namespace std; const int num = 16, ud[] = {-1, 0, 0, 1}, lr[] = {0, -1, 1, 0}, nd = 4; int md[num][num]; struct puzzle { int sp, p[num], mdp, cost; bool operator<(const puzzle &pu) const { for (int i = 0; i < num; i++) { if (p[i] == pu.p[i]) continue; return p[i] < pu.p[i]; } return false; } }; struct state { puzzle pu; int es; bool operator<(const state &s) const { // return pu.mdp + pu.cost > s.pu.mdp + s.pu.cost; return es > s.es; } }; int astar(puzzle pz) { priority_queue<state> pq; int sum = 0; for (int i = 0; i < num; i++) { if (pz.p[i] == num) continue; sum += md[i][pz.p[i] - 1]; } pz.mdp = sum; pz.cost = 0; map<puzzle, bool> m; puzzle u, v; state initial; initial.pu = pz; initial.es = pz.mdp; pq.push(initial); while (!pq.empty()) { state st = pq.top(); pq.pop(); u = st.pu; if (!u.mdp) return u.cost; m[u] = true; int sx = u.sp / nd; int sy = u.sp % nd; for (int r = 0; r < nd; r++) { int tx = sx + ud[r]; int ty = sy + lr[r]; if (tx >= nd || tx < 0 || ty >= nd || ty < 0) continue; v = u; // v.mdp = p.mdp - md[tx * nd + ty][tmp.p[tx * nd + ty] - 1] + md[sx*nd + // sy][tmp.p[tx * nd + ty] - 1]; v.mdp -= md[tx * nd + ty][v.p[tx * nd + ty] - 1]; v.mdp += md[sx * nd + sy][v.p[tx * nd + ty] - 1]; swap(v.p[sx * nd + sy], v.p[tx * nd + ty]); v.sp = tx * nd + ty; if (!m[v]) { v.cost++; state news; news.pu = v; news.es = v.cost + v.mdp; pq.push(news); } } } return -1; } int main() { puzzle in; for (int i = 0; i < num; i++) for (int j = 0; j < num; j++) md[i][j] = abs(i / nd - j / nd) + abs(i % nd - j % nd); for (int i = 0; i < num; i++) { cin >> in.p[i]; if (!in.p[i]) { in.sp = i; in.p[i] = num; } } cout << astar(in) << endl; return 0; }
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <cmath> #include <iostream> #include <map> #include <queue> using namespace std; const int num = 16, ud[] = {1, 0, 0, -1}, lr[] = {0, -1, 1, 0}, nd = 4; int md[num][num]; struct puzzle { int sp, p[num], mdp, cost; bool operator<(const puzzle &pu) const { for (int i = 0; i < num; i++) { if (p[i] == pu.p[i]) continue; return p[i] < pu.p[i]; } return false; } }; struct state { puzzle pu; int es; bool operator<(const state &s) const { // return pu.mdp + pu.cost > s.pu.mdp + s.pu.cost; return es > s.es; } }; int astar(puzzle pz) { priority_queue<state> pq; int sum = 0; for (int i = 0; i < num; i++) { if (pz.p[i] == num) continue; sum += md[i][pz.p[i] - 1]; } pz.mdp = sum; pz.cost = 0; map<puzzle, bool> m; puzzle u, v; state initial; initial.pu = pz; initial.es = pz.mdp; pq.push(initial); while (!pq.empty()) { state st = pq.top(); pq.pop(); u = st.pu; if (!u.mdp) return u.cost; m[u] = true; int sx = u.sp / nd; int sy = u.sp % nd; for (int r = 0; r < nd; r++) { int tx = sx + ud[r]; int ty = sy + lr[r]; if (tx >= nd || tx < 0 || ty >= nd || ty < 0) continue; v = u; // v.mdp = p.mdp - md[tx * nd + ty][tmp.p[tx * nd + ty] - 1] + md[sx*nd + // sy][tmp.p[tx * nd + ty] - 1]; v.mdp -= md[tx * nd + ty][v.p[tx * nd + ty] - 1]; v.mdp += md[sx * nd + sy][v.p[tx * nd + ty] - 1]; swap(v.p[sx * nd + sy], v.p[tx * nd + ty]); v.sp = tx * nd + ty; if (!m[v]) { v.cost++; state news; news.pu = v; news.es = v.cost + v.mdp; pq.push(news); } } } return -1; } int main() { puzzle in; for (int i = 0; i < num; i++) for (int j = 0; j < num; j++) md[i][j] = abs(i / nd - j / nd) + abs(i % nd - j % nd); for (int i = 0; i < num; i++) { cin >> in.p[i]; if (!in.p[i]) { in.sp = i; in.p[i] = num; } } cout << astar(in) << endl; return 0; }
replace
7
8
7
8
MLE
p02246
C++
Time Limit Exceeded
// #define debug // //******************************************************************************************************************************************* //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // [Tips] // XCode??§???EOF??\??????Ctrl+D // ?\???Alt+\ // ans???????§?INT?????????2,147,483,647????¶????????????¨??????????????§long // long?????£??????????????????????????? // ????????°??¨??? = dout << static_cast<bitset<8> >(x) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////// // 15 Puzzle //??????????????????ID-DFS (Iterative Deepening Depth-First Search) ?????? // yukicorder???No.585 // ??\????????????????????????????????????????????????????????£??? // (??¬?§£??????ad???hoc?§£????????£????????§) // ??¨?????????????????????????????§??? // 15???????????????????????£???????£????????????? (????????¬????????????) //////////////////////////////////////// #ifdef debug #include <chrono> #endif #include <algorithm> // next_permutation #include <cmath> #include <cstdio> #include <cstring> //memcpy #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> //accumulate #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> // #include <unordered_map> //hash func. #include <fstream> //ifstream, ofstream #include <iterator> //insert_iterator::inserter #include <set> // #define NDEBUG //If NDEBUG is defined before #include <cassert>, assert will // be ignored. You had better define NDEBUG when u submit the code. #include <cassert> //assert using namespace std; #define dout cout // If u wanna output to a text file instead of standard output, plz define // OUTPUTFILE. #define OUTPUTFILE "output.txt" // //************************************************************ #ifdef OUTPUTFILE #define dout outputfile ofstream outputfile(OUTPUTFILE); #define OutputFilePath \ "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/" \ "output.txt" #endif #define din cin // If u wanna input from a text file instead of standard input, plz define // INPUTFROMTEXTFILE???. #define INPUTFILE "input.txt" // //************************************************************** #ifdef INPUTFILE #define din inputfile ifstream inputfile(INPUTFILE); #endif #define scand(A) scanf("%d", &(A)) #define scans(A) scanf("%s", (A)) #define printd(A) dout << "%d\n", (A)) #define prints(A) dout << "%s\n", (A)) #define disp(A) dout << #A << " = " << setw(3) << (A) << endl #define disP(A) dout << setw(3) << (A) << " " #define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define show(A, s, g) \ dout << #A << " = "; \ rep(j, (s), (g)) { disP(A[j]); } \ dout << endl #define showi(A, s, g) \ dout << #A << " = "; \ rep(j, (s), (g)) { disP(j); } \ dout << endl #define line dout << "----------------\n" #define line2 dout << "================\n" #define sign(x) ((x) > 0) - ((x) < 0) // x<0: -1, x=0: 0, x>0: +1 #define p(i) ((i) / 2) #define l(i) ((i)*2) #define r(i) ((i)*2 + 1) #define sibling(i) (i ^ 1) // the other sibling of i (ex. 16^1 = 17, 17^1 = 16) #define isRightChild(i) (i & 1) // ex. 16&1 = 0, 17&1 = 1 #define isLeftChild(i) (!(i & 1)) // ex. 16&1 = 1, 17&1 = 0 typedef pair<int, int> ii; typedef pair<ii, int> iii; typedef vector<int> vi; typedef long long ll; typedef unsigned long long ull; // const int INF = (1LL<<31)-1; const int NONE = -1; // const ll INF_LL = (ll)9e18-1LL; //Be careful for overflow. // const ull INF_ULL = (ull)1e19-1ULL; // #define MOD 1000000007 // //??§???????´???°?????£??¨??¨????????°????????????10???7?????? #define N_MAX 3 // num of vertex or element // #define M_MAX 124760 //num of edge // #define DATA_MAX 1010 #define N 4 #define N2 16 char dir[4] = {'U', 'L', 'R', 'D'}; // 0??¨3???1??¨2??????????????????????¶??????????move // (??????3?????¨??????????¶??????????) int dx[4] = {-1, 0, 0, 1}; int dy[4] = {0, -1, 1, 0}; int MDT[N2][N2]; // Manhattan Distance Table // MDT[i][j] = // ????????°???i?????????j???????????¨?????????i?????£?????????????????§???MD // (i=0????????????????????????0) #define LIMIT_MAX \ 50 // ID-DFS????????§??±?????¶??? // (100?????\???????????????????????????????§£????????§?????????) int LIMIT; //?????¨?????±?????¶??? int path[LIMIT_MAX]; // GOAL?????§????????????????????????????????????????????????????¨??????¨???path[i] // = ???cost i????????????????????????cost // i+1??????????????????????§?????????¨????????????k ll numOfSearch = 0; //??¢?´¢?????° struct PUZZLE { int state[N2]; int space; // state[space] = 0 int cost; //????????¶??????????????¨??¶????????§????§?????????????????????????????????° int md; // manhattan distance to GOAL // (GOAL?????§????§?????????????????????§????????????????????°??????????????¨???????????\??????????????£??????????¨?) //???????????£??? cost + md > LIMIT ??§????????°??????????????§?????? bool isGoal() { rep(i, 0, N2) { if (i != N2 - 1 and state[i] != i + 1) return false; if (i == N2 - 1) assert(state[i] == 0); } return true; } int calcMD() { int sum = 0; rep(i, 0, N2) { sum += MDT[state[i]][i]; } return md = sum; } void display() { #ifdef debug line; // char s[N+1]; show(state, 0, N2); rep(i, 0, N) { rep(j, 0, N) { // if(state[i*N+j]<10) s[j] = state[i*N+j] + '0'; // else s[j] = state[i*N+j] - 10 + 'A'; disP(state[i * N + j]); } dout << "\n"; // s[N] = '\0'; // prints(s); } dout << "\n"; dout << "space = " << space << "\n"; dout << "cost = " << cost << "\n"; dout << "md = " << md << "\n"; line; #endif } bool rotate( int k) { // k?????? (k = 0, 1, 2, 3) // ???move????????????true????????????????????????0?????????????£?????????????????????????????§????????????????false????????? //???????????????????????????????????°?????´??°????????? assert(0 <= k and k <= 3); assert(0 <= space and space < N2); int current_space_x = space / N; int current_space_y = space % N; int next_space_x = current_space_x + dx[k]; int next_space_y = current_space_y + dy[k]; if (0 <= next_space_x and next_space_x < N and 0 <= next_space_y and next_space_y < N) { int next_space = next_space_x * N + next_space_y; int moveNumber = state[next_space]; swap(state[space], state[next_space]); // update other member variables md -= MDT[moveNumber][next_space]; md += MDT[moveNumber][space]; space = next_space; cost++; return true; } else return false; } } puzzle; //?????¨??????????????????????????? (??????????????¶???) void init() { // initiailize MDT[][] rep(i, 0, N2) { rep(j, 0, N2) { if (i == 0) MDT[i][j] = 0; else MDT[i][j] = abs((i - 1) / N - j / N) + abs((i - 1) % N - j % N); } } } void display() { #ifdef debug line; #endif } bool dfs( int moveHistory [3]) { //??´??????3???????????°??§??????????????????????????´??????NONE?????\??£?????????????????????NONE=-1?????????????????????????????????????????????????????? numOfSearch++; #ifdef debug dout << "========================= dfs( " << moveHistory[2] << ", " << moveHistory[1] << ", " << moveHistory[0] << " )\n"; disp(numOfSearch); puzzle.display(); #endif if (puzzle.isGoal()) { #ifdef debug dout << "GOAL??¨?????´???????????§true?????????\n"; #endif return true; } if (puzzle.cost + puzzle.md > LIMIT) { //???????????§?????????????????´???????????§?????¨???????????? //(?????????) //????????¨?????±?????¶???????¶???????????????¢?´¢??????????????? //(?????¨?????±?????¶?????§??????????????£????????´???????????????????????????????????¨???????????¨????????§) #ifdef debug dout << "puzzle.cost = " << puzzle.cost << " + puzzle.md = " << puzzle.md << " ???????????¨???LIMIT = " << LIMIT << " ????¶????????????§???????????\??????????????????false?????????\n"; #endif return false; } // recursive call for 4 move #ifdef debug dout << "??????????????????1?????§????§???§???????????????????????????dfs????" "????°??????????????????????????? (?????§4???)\n\n"; #endif PUZZLE backup = puzzle; // for track back rep(k, 0, 4) { puzzle = backup; //???k??§puzzle????????¢????????????????????§???k???loop?????¨???puzzle???????????¶?????????????????? #ifdef debug line; dout << "k = " << k << " : " << dir[k] << "\n"; #endif // if( moveHistory[0] + k == N-1 ) { // #ifdef debug // dout << "??´??????1??? " << moveHistory[0] << " // ??????????¶?????????????????????§???continue\n"; // #endif // continue; // } // if( moveHistory[0]!=k and moveHistory[1]+k==N-1 and // moveHistory[0]+moveHistory[2]==N-1 ) { // #ifdef debug // dout << "??´??????3??? (" << moveHistory[2] << ", " << // moveHistory[1] << ", " << moveHistory[0] << ") // ??¨???????????????1??¨????????????????????§???continue\n"; // #endif // continue; // } // move k ????????? bool rotateResult = puzzle.rotate(k); if (rotateResult == false) { #ifdef debug dout << "??????move???????????§??????????????§???continue\n"; #endif continue; } int hist_new[3]; hist_new[2] = moveHistory[1]; hist_new[1] = moveHistory[0]; hist_new[0] = k; if (dfs(hist_new)) { #ifdef debug dout << "------------------ end of ------------------ dfs( " << moveHistory[2] << ", " << moveHistory[1] << ", " << moveHistory[0] << " )\n"; dout << "????¨????dfs????????????????????¨??????puzzle?????\???\n"; backup.display(); dout << "????????????true????????£??????????????§???GOAL?????§???????????" "??????????????????£??????\n"; dout << "????????? k = " << k << " (" << dir[k] << ") ??? path[ cost = " << backup.cost << " ] ???????????????\n"; dout << "--------- END ---------\n"; #endif path[backup.cost] = k; return true; } } // end of k-loop // 4 move // ???????????????????????????true????????£??????????????£??????false????????? return false; } int solve_iterativeDeepening(PUZZLE in) { for (LIMIT = in.calcMD(); LIMIT <= LIMIT_MAX; LIMIT++) { #ifdef debug dout << "//////////////////////// LIMIT = " << LIMIT << " ////////////////////////////////////\n"; #endif puzzle = in; // track back to initial state int moveHistory[3] = {NONE, NONE, NONE}; // call dfs if (dfs(moveHistory)) { return puzzle.cost; } } return NONE; } int main() { // cin, cout???????????? // ?????¨??????cin?????????????????¨??¨cin??§???scanf?????????????????¨??¨scanf??§??±????????????????????? cin.tie(0); // cin??¨cout?????????????????? ios::sync_with_stdio(false); // iostream??¨stdio?????????????????? // read input data // scand(N); rep(i, 0, N) { rep(j, 0, N) { scand(puzzle.state[i * N + j]); if (puzzle.state[i * N + j] == 0) puzzle.space = i * N + j; } } //------------------------------------------------------------------------------------------ #ifdef debug // start timer auto startTime = chrono::system_clock::now(); #endif //------------------------------------------------------------------------------------------ init(); int ans = solve_iterativeDeepening(puzzle); #ifdef debug dout << "=== OUTPUT ===\n"; #endif dout << ans << endl; #ifdef debug dout << "?????????????????????\n"; show(path, 0, LIMIT_MAX); show(path, 0, ans); dout << "solution = "; rep(i, 0, ans) disP(dir[path[i]]); dout << endl; #endif #ifdef OUTPUTFILE cout << "ans = " << ans << endl; cout << "numOfSearch = " << numOfSearch << endl; cout << "?????????????????????\n"; cout << "solution = "; rep(i, 0, ans) cout << dir[path[i]] << " "; cout << endl; #endif //------------------------------------------------------------------------------------------ #ifdef debug // stop timer auto endTime = chrono::system_clock::now(); auto dur = endTime - startTime; auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count(); dout << fixed << setprecision(4) << (double)msec / 1000 << " sec \n"; #ifdef OUTPUTFILE cout << fixed << setprecision(4) << (double)msec / 1000 << " sec \n"; #endif #endif //------------------------------------------------------------------------------------------ #ifdef INPUTFILE inputfile.close(); #endif #ifdef OUTPUTFILE outputfile.close(); cout << "\"" << OutputFilePath << "\"" << endl; #endif return 0; }
// #define debug // //******************************************************************************************************************************************* //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // [Tips] // XCode??§???EOF??\??????Ctrl+D // ?\???Alt+\ // ans???????§?INT?????????2,147,483,647????¶????????????¨??????????????§long // long?????£??????????????????????????? // ????????°??¨??? = dout << static_cast<bitset<8> >(x) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////// // 15 Puzzle //??????????????????ID-DFS (Iterative Deepening Depth-First Search) ?????? // yukicorder???No.585 // ??\????????????????????????????????????????????????????????£??? // (??¬?§£??????ad???hoc?§£????????£????????§) // ??¨?????????????????????????????§??? // 15???????????????????????£???????£????????????? (????????¬????????????) //////////////////////////////////////// #ifdef debug #include <chrono> #endif #include <algorithm> // next_permutation #include <cmath> #include <cstdio> #include <cstring> //memcpy #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> //accumulate #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> // #include <unordered_map> //hash func. #include <fstream> //ifstream, ofstream #include <iterator> //insert_iterator::inserter #include <set> // #define NDEBUG //If NDEBUG is defined before #include <cassert>, assert will // be ignored. You had better define NDEBUG when u submit the code. #include <cassert> //assert using namespace std; #define dout cout // If u wanna output to a text file instead of standard output, plz define // OUTPUTFILE. #define OUTPUTFILE "output.txt" // //************************************************************ #ifdef OUTPUTFILE #define dout outputfile ofstream outputfile(OUTPUTFILE); #define OutputFilePath \ "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/" \ "output.txt" #endif #define din cin // If u wanna input from a text file instead of standard input, plz define // INPUTFROMTEXTFILE???. #define INPUTFILE "input.txt" // //************************************************************** #ifdef INPUTFILE #define din inputfile ifstream inputfile(INPUTFILE); #endif #define scand(A) scanf("%d", &(A)) #define scans(A) scanf("%s", (A)) #define printd(A) dout << "%d\n", (A)) #define prints(A) dout << "%s\n", (A)) #define disp(A) dout << #A << " = " << setw(3) << (A) << endl #define disP(A) dout << setw(3) << (A) << " " #define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define show(A, s, g) \ dout << #A << " = "; \ rep(j, (s), (g)) { disP(A[j]); } \ dout << endl #define showi(A, s, g) \ dout << #A << " = "; \ rep(j, (s), (g)) { disP(j); } \ dout << endl #define line dout << "----------------\n" #define line2 dout << "================\n" #define sign(x) ((x) > 0) - ((x) < 0) // x<0: -1, x=0: 0, x>0: +1 #define p(i) ((i) / 2) #define l(i) ((i)*2) #define r(i) ((i)*2 + 1) #define sibling(i) (i ^ 1) // the other sibling of i (ex. 16^1 = 17, 17^1 = 16) #define isRightChild(i) (i & 1) // ex. 16&1 = 0, 17&1 = 1 #define isLeftChild(i) (!(i & 1)) // ex. 16&1 = 1, 17&1 = 0 typedef pair<int, int> ii; typedef pair<ii, int> iii; typedef vector<int> vi; typedef long long ll; typedef unsigned long long ull; // const int INF = (1LL<<31)-1; const int NONE = -1; // const ll INF_LL = (ll)9e18-1LL; //Be careful for overflow. // const ull INF_ULL = (ull)1e19-1ULL; // #define MOD 1000000007 // //??§???????´???°?????£??¨??¨????????°????????????10???7?????? #define N_MAX 3 // num of vertex or element // #define M_MAX 124760 //num of edge // #define DATA_MAX 1010 #define N 4 #define N2 16 char dir[4] = {'U', 'L', 'R', 'D'}; // 0??¨3???1??¨2??????????????????????¶??????????move // (??????3?????¨??????????¶??????????) int dx[4] = {-1, 0, 0, 1}; int dy[4] = {0, -1, 1, 0}; int MDT[N2][N2]; // Manhattan Distance Table // MDT[i][j] = // ????????°???i?????????j???????????¨?????????i?????£?????????????????§???MD // (i=0????????????????????????0) #define LIMIT_MAX \ 50 // ID-DFS????????§??±?????¶??? // (100?????\???????????????????????????????§£????????§?????????) int LIMIT; //?????¨?????±?????¶??? int path[LIMIT_MAX]; // GOAL?????§????????????????????????????????????????????????????¨??????¨???path[i] // = ???cost i????????????????????????cost // i+1??????????????????????§?????????¨????????????k ll numOfSearch = 0; //??¢?´¢?????° struct PUZZLE { int state[N2]; int space; // state[space] = 0 int cost; //????????¶??????????????¨??¶????????§????§?????????????????????????????????° int md; // manhattan distance to GOAL // (GOAL?????§????§?????????????????????§????????????????????°??????????????¨???????????\??????????????£??????????¨?) //???????????£??? cost + md > LIMIT ??§????????°??????????????§?????? bool isGoal() { rep(i, 0, N2) { if (i != N2 - 1 and state[i] != i + 1) return false; if (i == N2 - 1) assert(state[i] == 0); } return true; } int calcMD() { int sum = 0; rep(i, 0, N2) { sum += MDT[state[i]][i]; } return md = sum; } void display() { #ifdef debug line; // char s[N+1]; show(state, 0, N2); rep(i, 0, N) { rep(j, 0, N) { // if(state[i*N+j]<10) s[j] = state[i*N+j] + '0'; // else s[j] = state[i*N+j] - 10 + 'A'; disP(state[i * N + j]); } dout << "\n"; // s[N] = '\0'; // prints(s); } dout << "\n"; dout << "space = " << space << "\n"; dout << "cost = " << cost << "\n"; dout << "md = " << md << "\n"; line; #endif } bool rotate( int k) { // k?????? (k = 0, 1, 2, 3) // ???move????????????true????????????????????????0?????????????£?????????????????????????????§????????????????false????????? //???????????????????????????????????°?????´??°????????? assert(0 <= k and k <= 3); assert(0 <= space and space < N2); int current_space_x = space / N; int current_space_y = space % N; int next_space_x = current_space_x + dx[k]; int next_space_y = current_space_y + dy[k]; if (0 <= next_space_x and next_space_x < N and 0 <= next_space_y and next_space_y < N) { int next_space = next_space_x * N + next_space_y; int moveNumber = state[next_space]; swap(state[space], state[next_space]); // update other member variables md -= MDT[moveNumber][next_space]; md += MDT[moveNumber][space]; space = next_space; cost++; return true; } else return false; } } puzzle; //?????¨??????????????????????????? (??????????????¶???) void init() { // initiailize MDT[][] rep(i, 0, N2) { rep(j, 0, N2) { if (i == 0) MDT[i][j] = 0; else MDT[i][j] = abs((i - 1) / N - j / N) + abs((i - 1) % N - j % N); } } } void display() { #ifdef debug line; #endif } bool dfs( int moveHistory [3]) { //??´??????3???????????°??§??????????????????????????´??????NONE?????\??£?????????????????????NONE=-1?????????????????????????????????????????????????????? numOfSearch++; #ifdef debug dout << "========================= dfs( " << moveHistory[2] << ", " << moveHistory[1] << ", " << moveHistory[0] << " )\n"; disp(numOfSearch); puzzle.display(); #endif if (puzzle.isGoal()) { #ifdef debug dout << "GOAL??¨?????´???????????§true?????????\n"; #endif return true; } if (puzzle.cost + puzzle.md > LIMIT) { //???????????§?????????????????´???????????§?????¨???????????? //(?????????) //????????¨?????±?????¶???????¶???????????????¢?´¢??????????????? //(?????¨?????±?????¶?????§??????????????£????????´???????????????????????????????????¨???????????¨????????§) #ifdef debug dout << "puzzle.cost = " << puzzle.cost << " + puzzle.md = " << puzzle.md << " ???????????¨???LIMIT = " << LIMIT << " ????¶????????????§???????????\??????????????????false?????????\n"; #endif return false; } // recursive call for 4 move #ifdef debug dout << "??????????????????1?????§????§???§???????????????????????????dfs????" "????°??????????????????????????? (?????§4???)\n\n"; #endif PUZZLE backup = puzzle; // for track back rep(k, 0, 4) { puzzle = backup; //???k??§puzzle????????¢????????????????????§???k???loop?????¨???puzzle???????????¶?????????????????? #ifdef debug line; dout << "k = " << k << " : " << dir[k] << "\n"; #endif if (moveHistory[0] + k == N - 1) { #ifdef debug dout << "??´??????1??? " << moveHistory[0] << " ??????????¶?????????????????????§???continue\n"; #endif continue; } // if( moveHistory[0]!=k and moveHistory[1]+k==N-1 and // moveHistory[0]+moveHistory[2]==N-1 ) { // #ifdef debug // dout << "??´??????3??? (" << moveHistory[2] << ", " << // moveHistory[1] << ", " << moveHistory[0] << ") // ??¨???????????????1??¨????????????????????§???continue\n"; // #endif // continue; // } // move k ????????? bool rotateResult = puzzle.rotate(k); if (rotateResult == false) { #ifdef debug dout << "??????move???????????§??????????????§???continue\n"; #endif continue; } int hist_new[3]; hist_new[2] = moveHistory[1]; hist_new[1] = moveHistory[0]; hist_new[0] = k; if (dfs(hist_new)) { #ifdef debug dout << "------------------ end of ------------------ dfs( " << moveHistory[2] << ", " << moveHistory[1] << ", " << moveHistory[0] << " )\n"; dout << "????¨????dfs????????????????????¨??????puzzle?????\???\n"; backup.display(); dout << "????????????true????????£??????????????§???GOAL?????§???????????" "??????????????????£??????\n"; dout << "????????? k = " << k << " (" << dir[k] << ") ??? path[ cost = " << backup.cost << " ] ???????????????\n"; dout << "--------- END ---------\n"; #endif path[backup.cost] = k; return true; } } // end of k-loop // 4 move // ???????????????????????????true????????£??????????????£??????false????????? return false; } int solve_iterativeDeepening(PUZZLE in) { for (LIMIT = in.calcMD(); LIMIT <= LIMIT_MAX; LIMIT++) { #ifdef debug dout << "//////////////////////// LIMIT = " << LIMIT << " ////////////////////////////////////\n"; #endif puzzle = in; // track back to initial state int moveHistory[3] = {NONE, NONE, NONE}; // call dfs if (dfs(moveHistory)) { return puzzle.cost; } } return NONE; } int main() { // cin, cout???????????? // ?????¨??????cin?????????????????¨??¨cin??§???scanf?????????????????¨??¨scanf??§??±????????????????????? cin.tie(0); // cin??¨cout?????????????????? ios::sync_with_stdio(false); // iostream??¨stdio?????????????????? // read input data // scand(N); rep(i, 0, N) { rep(j, 0, N) { scand(puzzle.state[i * N + j]); if (puzzle.state[i * N + j] == 0) puzzle.space = i * N + j; } } //------------------------------------------------------------------------------------------ #ifdef debug // start timer auto startTime = chrono::system_clock::now(); #endif //------------------------------------------------------------------------------------------ init(); int ans = solve_iterativeDeepening(puzzle); #ifdef debug dout << "=== OUTPUT ===\n"; #endif dout << ans << endl; #ifdef debug dout << "?????????????????????\n"; show(path, 0, LIMIT_MAX); show(path, 0, ans); dout << "solution = "; rep(i, 0, ans) disP(dir[path[i]]); dout << endl; #endif #ifdef OUTPUTFILE cout << "ans = " << ans << endl; cout << "numOfSearch = " << numOfSearch << endl; cout << "?????????????????????\n"; cout << "solution = "; rep(i, 0, ans) cout << dir[path[i]] << " "; cout << endl; #endif //------------------------------------------------------------------------------------------ #ifdef debug // stop timer auto endTime = chrono::system_clock::now(); auto dur = endTime - startTime; auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count(); dout << fixed << setprecision(4) << (double)msec / 1000 << " sec \n"; #ifdef OUTPUTFILE cout << fixed << setprecision(4) << (double)msec / 1000 << " sec \n"; #endif #endif //------------------------------------------------------------------------------------------ #ifdef INPUTFILE inputfile.close(); #endif #ifdef OUTPUTFILE outputfile.close(); cout << "\"" << OutputFilePath << "\"" << endl; #endif return 0; }
replace
299
306
299
306
TLE
p02246
C++
Runtime Error
#include <algorithm> #include <functional> #include <iostream> #include <istream> #include <map> #include <ostream> #include <queue> #include <random> #include <stack> #include <tuple> #include <utility> using namespace std; // puzzle ?????¶?????????key (ULL) ??§??¨????????? // priority_que ?????£????????????stack<key> pq[DPETH_LIMIT]; ???????????? // MHDsum ??????key ?????????????¨????????????? // calc_MHD(int idx, int val) ?????????????¨???????????????¢???????????? // blank ???????????????key ?????????????¨????????????? // key ???????¬?????????¢???????§????????¨????????????¢??°?????¨???????????? inline int abs(int i) { return i > 0 ? i : -i; } constexpr int DEPTH_LIMIT = 45; constexpr int W = 4; constexpr int H = 4; constexpr int B = 4; constexpr int WH = W * H; constexpr unsigned long long key_goal = 1311768467463790320ull; enum class Direction { Left, Right, Up, Down }; using Board = vector<int>; using Key = unsigned long long; Key calc_key(Board &board); int calc_MHDsum(Key key); int find_blank(Key key); int MHD[WH][WH] = {{0}}; void init_MHD(); int calc_MHD(int idx, int val); Key slide(Key key, int blank_idx, Direction d); istream &operator>>(istream &is, Board &board); int solve(Key key_start); //////////////////////////////////////////////// // main function //////////////////////////////////////////////// int main() { init_MHD(); Board board; cin >> board; Key key_start = calc_key(board); cout << solve(key_start) << endl; return 0; } //////////////////////////////////////////////// int solve(Key key_start) { stack<Key> pq[DEPTH_LIMIT + 1]; map<Key, int> k2s; int fstar = 0 + calc_MHDsum(key_start); if (fstar == 0) return 0; pq[fstar].push(key_start); k2s[key_start] = 0; while (fstar <= DEPTH_LIMIT) { if (pq[fstar].empty()) { ++fstar; continue; } auto key = pq[fstar].top(); pq[fstar].pop(); int steps = fstar - calc_MHDsum(key); int next_steps = steps + 1; if (k2s[key] < steps) continue; int blank_idx = find_blank(key); for (auto d : {Direction::Left, Direction::Right, Direction::Up, Direction::Down}) { auto new_key = slide(key, blank_idx, d); if (new_key) { int new_fstar = next_steps + calc_MHDsum(new_key); if (new_fstar > DEPTH_LIMIT) continue; auto it = k2s.find(new_key); if (it == k2s.end() or it->second > next_steps) { k2s[new_key] = next_steps; pq[new_fstar].push(new_key); if (new_fstar < fstar) fstar = new_fstar; if (new_key == key_goal) return next_steps; } } } } return -1; } Key calc_key(Board &board) { Key key = 0ULL; for (auto val : board) { key <<= B; key += val; } return key; } void init_MHD() { for (int idx = 0; idx != WH; ++idx) { for (int val = 0; val != WH; ++val) { MHD[idx][val] = calc_MHD(idx, val); } } } int calc_MHD(int idx, int val) { if (val == 0) return 0; --val; int row_val = val / W; int col_val = val % W; int row_idx = idx / W; int col_idx = idx % W; return abs(row_val - row_idx) + abs(col_val - col_idx); } int calc_MHDsum(Key key) { // Calculates the sum of Manhattan distances // from current position to goal position // for all panels except blank (=0) panel. int mhdsum = 0; int idx = WH; while (idx--) { mhdsum += MHD[idx][key % WH]; key >>= B; } return mhdsum; } int find_blank(Key key) { // Find a blank (=0) panel on a board with width=W and height=H // and return its position (row, col). int idx = WH; while (idx--) { if ((key % WH) == 0) return idx; key >>= B; } } Key slide(Key key, int blank_idx, Direction d) { Key failure_code = 0ULL; int row = blank_idx / W; int col = blank_idx % W; int diff_idx = 0; switch (d) { case Direction::Left: if (col == 0) return failure_code; diff_idx = -1; break; case Direction::Right: if (col == W - 1) return failure_code; diff_idx = 1; break; case Direction::Up: if (row == 0) return failure_code; diff_idx = -W; break; case Direction::Down: if (row == H - 1) return failure_code; diff_idx = W; break; } Key piece = key & (15ULL << ((WH - 1 - blank_idx - diff_idx) * B)); key -= piece; if (diff_idx > 0) { key += (piece << (diff_idx * B)); } else { key += (piece >> (-diff_idx * B)); } return key; } istream &operator>>(istream &is, Board &board) { for (int i = 0; i != WH; ++i) { cin >> board[i]; } return is; }
#include <algorithm> #include <functional> #include <iostream> #include <istream> #include <map> #include <ostream> #include <queue> #include <random> #include <stack> #include <tuple> #include <utility> using namespace std; // puzzle ?????¶?????????key (ULL) ??§??¨????????? // priority_que ?????£????????????stack<key> pq[DPETH_LIMIT]; ???????????? // MHDsum ??????key ?????????????¨????????????? // calc_MHD(int idx, int val) ?????????????¨???????????????¢???????????? // blank ???????????????key ?????????????¨????????????? // key ???????¬?????????¢???????§????????¨????????????¢??°?????¨???????????? inline int abs(int i) { return i > 0 ? i : -i; } constexpr int DEPTH_LIMIT = 45; constexpr int W = 4; constexpr int H = 4; constexpr int B = 4; constexpr int WH = W * H; constexpr unsigned long long key_goal = 1311768467463790320ull; enum class Direction { Left, Right, Up, Down }; using Board = vector<int>; using Key = unsigned long long; Key calc_key(Board &board); int calc_MHDsum(Key key); int find_blank(Key key); int MHD[WH][WH] = {{0}}; void init_MHD(); int calc_MHD(int idx, int val); Key slide(Key key, int blank_idx, Direction d); istream &operator>>(istream &is, Board &board); int solve(Key key_start); //////////////////////////////////////////////// // main function //////////////////////////////////////////////// int main() { init_MHD(); Board board(WH); cin >> board; Key key_start = calc_key(board); cout << solve(key_start) << endl; return 0; } //////////////////////////////////////////////// int solve(Key key_start) { stack<Key> pq[DEPTH_LIMIT + 1]; map<Key, int> k2s; int fstar = 0 + calc_MHDsum(key_start); if (fstar == 0) return 0; pq[fstar].push(key_start); k2s[key_start] = 0; while (fstar <= DEPTH_LIMIT) { if (pq[fstar].empty()) { ++fstar; continue; } auto key = pq[fstar].top(); pq[fstar].pop(); int steps = fstar - calc_MHDsum(key); int next_steps = steps + 1; if (k2s[key] < steps) continue; int blank_idx = find_blank(key); for (auto d : {Direction::Left, Direction::Right, Direction::Up, Direction::Down}) { auto new_key = slide(key, blank_idx, d); if (new_key) { int new_fstar = next_steps + calc_MHDsum(new_key); if (new_fstar > DEPTH_LIMIT) continue; auto it = k2s.find(new_key); if (it == k2s.end() or it->second > next_steps) { k2s[new_key] = next_steps; pq[new_fstar].push(new_key); if (new_fstar < fstar) fstar = new_fstar; if (new_key == key_goal) return next_steps; } } } } return -1; } Key calc_key(Board &board) { Key key = 0ULL; for (auto val : board) { key <<= B; key += val; } return key; } void init_MHD() { for (int idx = 0; idx != WH; ++idx) { for (int val = 0; val != WH; ++val) { MHD[idx][val] = calc_MHD(idx, val); } } } int calc_MHD(int idx, int val) { if (val == 0) return 0; --val; int row_val = val / W; int col_val = val % W; int row_idx = idx / W; int col_idx = idx % W; return abs(row_val - row_idx) + abs(col_val - col_idx); } int calc_MHDsum(Key key) { // Calculates the sum of Manhattan distances // from current position to goal position // for all panels except blank (=0) panel. int mhdsum = 0; int idx = WH; while (idx--) { mhdsum += MHD[idx][key % WH]; key >>= B; } return mhdsum; } int find_blank(Key key) { // Find a blank (=0) panel on a board with width=W and height=H // and return its position (row, col). int idx = WH; while (idx--) { if ((key % WH) == 0) return idx; key >>= B; } } Key slide(Key key, int blank_idx, Direction d) { Key failure_code = 0ULL; int row = blank_idx / W; int col = blank_idx % W; int diff_idx = 0; switch (d) { case Direction::Left: if (col == 0) return failure_code; diff_idx = -1; break; case Direction::Right: if (col == W - 1) return failure_code; diff_idx = 1; break; case Direction::Up: if (row == 0) return failure_code; diff_idx = -W; break; case Direction::Down: if (row == H - 1) return failure_code; diff_idx = W; break; } Key piece = key & (15ULL << ((WH - 1 - blank_idx - diff_idx) * B)); key -= piece; if (diff_idx > 0) { key += (piece << (diff_idx * B)); } else { key += (piece >> (-diff_idx * B)); } return key; } istream &operator>>(istream &is, Board &board) { for (int i = 0; i != WH; ++i) { cin >> board[i]; } return is; }
replace
58
59
58
59
-11
p02246
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long lli; typedef vector<lli> vll; typedef vector<bool> vbl; typedef vector<vector<lli>> mat; typedef vector<vector<bool>> matb; typedef vector<string> vst; typedef pair<lli, lli> pll; typedef pair<double, double> pdd; mat f(4, vll(4)); mat g(4, vll(4)); lli md = 0; const lli dx[] = {1, 0, -1, 0}; const lli dy[] = {0, 1, 0, -1}; lli lim = 0; lli ans = 100; lli x, y; vll px, py; vll fx, fy; vll gx, gy; map<mat, lli> m; map<mat, lli> w; bool flag = false; bool isin(lli x, lli y) { return x >= 0 && x < 4 && y >= 0 && y < 4; } lli m_dist(lli p, lli i, lli j) { return abs(i - px[p]) + abs(j - py[p]); } void dfs(lli md, lli s, lli x, lli y) { if (md + s > lim) return; if (m.find(f) != m.end()) { if (m[f] > s) m[f] = s; else return; } else { m[f] = s; } if (s >= lim / 2) return; for (lli i = 0; i < 4; i++) { if (isin(x + dx[i], y + dy[i])) { swap(f[x + dx[i]][y + dy[i]], f[x][y]); dfs(md + m_dist(f[x][y], x, y) - m_dist(f[x][y], x + dx[i], y + dy[i]), s + 1, x + dx[i], y + dy[i]); swap(f[x + dx[i]][y + dy[i]], f[x][y]); } } } void dfs2(lli md, lli s, lli x, lli y) { if (md + s > lim) return; if (w.find(g) != w.end()) { if (w[g] > s) w[g] = s; else return; } else { w[g] = s; } if (m.find(g) != m.end()) { ans = min(ans, s + m[g]); flag = true; } if (s > lim / 2) return; for (lli i = 0; i < 4; i++) { if (isin(x + dx[i], y + dy[i])) { swap(g[x + dx[i]][y + dy[i]], g[x][y]); dfs2(md + m_dist(g[x][y], x, y) - m_dist(g[x][y], x + dx[i], y + dy[i]), s + 1, x + dx[i], y + dy[i]); swap(g[x + dx[i]][y + dy[i]], g[x][y]); } } } int main() { for (lli i = 0; i < 4; i++) for (lli j = 0; j < 4; j++) cin >> f[i][j]; fx = vll(16); fy = vll(16); for (lli i = 1; i < 16; i++) { fx[i] = (i - 1) / 4; fy[i] = (i - 1) % 4; } px = fx; py = fy; for (lli i = 0; i < 4; i++) { for (lli j = 0; j < 4; j++) { if (f[i][j]) { md += m_dist(f[i][j], i, j); } else { x = i; y = j; } } } for (lli i = 0; i < 4; i++) for (lli j = 0; j < 4; j++) g[i][j] = (i * 4 + j + 1) % 16; gx = vll(16); gy = vll(16); for (lli i = 0; i < 4; i++) { for (lli j = 0; j < 4; j++) { gx[f[i][j]] = i; gy[f[i][j]] = j; } } for (lli i = md; i <= 46; i++) { lim = i; m = map<mat, lli>(); w = map<mat, lli>(); px = fx; py = fy; dfs(md, 0, x, y); px = gx; py = gy; dfs2(md, 0, 3, 3); if (flag) { cout << ans << endl; return 0; } } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long lli; typedef vector<lli> vll; typedef vector<bool> vbl; typedef vector<vector<lli>> mat; typedef vector<vector<bool>> matb; typedef vector<string> vst; typedef pair<lli, lli> pll; typedef pair<double, double> pdd; mat f(4, vll(4)); mat g(4, vll(4)); lli md = 0; const lli dx[] = {1, 0, -1, 0}; const lli dy[] = {0, 1, 0, -1}; lli lim = 0; lli ans = 100; lli x, y; vll px, py; vll fx, fy; vll gx, gy; map<mat, lli> m; map<mat, lli> w; bool flag = false; bool isin(lli x, lli y) { return x >= 0 && x < 4 && y >= 0 && y < 4; } lli m_dist(lli p, lli i, lli j) { return abs(i - px[p]) + abs(j - py[p]); } void dfs(lli md, lli s, lli x, lli y) { if (md + s > lim) return; if (m.find(f) != m.end()) { if (m[f] > s) m[f] = s; else return; } else { m[f] = s; } if (s > lim / 2) return; for (lli i = 0; i < 4; i++) { if (isin(x + dx[i], y + dy[i])) { swap(f[x + dx[i]][y + dy[i]], f[x][y]); dfs(md + m_dist(f[x][y], x, y) - m_dist(f[x][y], x + dx[i], y + dy[i]), s + 1, x + dx[i], y + dy[i]); swap(f[x + dx[i]][y + dy[i]], f[x][y]); } } } void dfs2(lli md, lli s, lli x, lli y) { if (md + s > lim) return; if (w.find(g) != w.end()) { if (w[g] > s) w[g] = s; else return; } else { w[g] = s; } if (m.find(g) != m.end()) { ans = min(ans, s + m[g]); flag = true; } if (s > lim / 2) return; for (lli i = 0; i < 4; i++) { if (isin(x + dx[i], y + dy[i])) { swap(g[x + dx[i]][y + dy[i]], g[x][y]); dfs2(md + m_dist(g[x][y], x, y) - m_dist(g[x][y], x + dx[i], y + dy[i]), s + 1, x + dx[i], y + dy[i]); swap(g[x + dx[i]][y + dy[i]], g[x][y]); } } } int main() { for (lli i = 0; i < 4; i++) for (lli j = 0; j < 4; j++) cin >> f[i][j]; fx = vll(16); fy = vll(16); for (lli i = 1; i < 16; i++) { fx[i] = (i - 1) / 4; fy[i] = (i - 1) % 4; } px = fx; py = fy; for (lli i = 0; i < 4; i++) { for (lli j = 0; j < 4; j++) { if (f[i][j]) { md += m_dist(f[i][j], i, j); } else { x = i; y = j; } } } for (lli i = 0; i < 4; i++) for (lli j = 0; j < 4; j++) g[i][j] = (i * 4 + j + 1) % 16; gx = vll(16); gy = vll(16); for (lli i = 0; i < 4; i++) { for (lli j = 0; j < 4; j++) { gx[f[i][j]] = i; gy[f[i][j]] = j; } } for (lli i = md; i <= 46; i++) { lim = i; m = map<mat, lli>(); w = map<mat, lli>(); px = fx; py = fy; dfs(md, 0, x, y); px = gx; py = gy; dfs2(md, 0, 3, 3); if (flag) { cout << ans << endl; return 0; } } return 0; }
replace
52
53
52
53
TLE
p02246
C++
Memory Limit Exceeded
/* * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_13_C * A* */ #include <iostream> #include <map> #include <queue> #include <vector> #define N 4 #define N2 16 using namespace std; vector<int> move_list[N2]; int dist[N2][N2]; class Game { public: int board[N2]; int w; vector<int> root; int mnh; bool operator<(const Game &other) const { return root.size() + mnh > other.root.size() + other.mnh; } }; Game start; int solve() { priority_queue<Game> q; q.push(start); while (!q.empty()) { Game g = q.top(); q.pop(); if (g.mnh == 0) return g.root.size(); for (int move : move_list[g.w]) { if (g.root.size() > 0 && g.root.back() == -move) continue; Game h = g; int to = h.w + move; h.mnh -= dist[to][h.board[to] - 1]; h.mnh += dist[h.w][h.board[to] - 1]; swap(h.board[h.w], h.board[to]); h.w = to; h.root.push_back(move); q.push(h); } } return -1; } void initialize() { for (int i = 0; i < N2; i++) { if (i % N < N - 1) move_list[i].push_back(1); if (i % N > 0) move_list[i].push_back(-1); if (i / N < N - 1) move_list[i].push_back(N); if (i / N > 0) move_list[i].push_back(-N); } // i=board,j=piece-1 for (int i = 0; i < N2; i++) { for (int j = 0; j < N2; j++) { dist[i][j] = abs(j % N - i % N) + abs(j / N - i / N); } } start.mnh = 0; for (int i = 0; i < N2; i++) { if (i == start.w) continue; start.mnh += dist[i][start.board[i] - 1]; } } int main() { int tmp; for (int i = 0; i < N2; i++) { cin >> tmp; if (tmp == 0) { start.board[i] = N2; start.w = i; } else { start.board[i] = tmp; } } initialize(); cout << solve() << endl; }
/* * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_13_C * A* */ #include <iostream> #include <map> #include <queue> #include <vector> #define N 4 #define N2 16 using namespace std; vector<int> move_list[N2]; int dist[N2][N2]; class Game { public: char board[N2]; char w; vector<char> root; char mnh; bool operator<(const Game &other) const { return root.size() + mnh > other.root.size() + other.mnh; } }; Game start; int solve() { priority_queue<Game> q; q.push(start); while (!q.empty()) { Game g = q.top(); q.pop(); if (g.mnh == 0) return g.root.size(); for (int move : move_list[g.w]) { if (g.root.size() > 0 && g.root.back() == -move) continue; Game h = g; int to = h.w + move; h.mnh -= dist[to][h.board[to] - 1]; h.mnh += dist[h.w][h.board[to] - 1]; swap(h.board[h.w], h.board[to]); h.w = to; h.root.push_back(move); q.push(h); } } return -1; } void initialize() { for (int i = 0; i < N2; i++) { if (i % N < N - 1) move_list[i].push_back(1); if (i % N > 0) move_list[i].push_back(-1); if (i / N < N - 1) move_list[i].push_back(N); if (i / N > 0) move_list[i].push_back(-N); } // i=board,j=piece-1 for (int i = 0; i < N2; i++) { for (int j = 0; j < N2; j++) { dist[i][j] = abs(j % N - i % N) + abs(j / N - i / N); } } start.mnh = 0; for (int i = 0; i < N2; i++) { if (i == start.w) continue; start.mnh += dist[i][start.board[i] - 1]; } } int main() { int tmp; for (int i = 0; i < N2; i++) { cin >> tmp; if (tmp == 0) { start.board[i] = N2; start.w = i; } else { start.board[i] = tmp; } } initialize(); cout << solve() << endl; }
replace
17
21
17
21
MLE
p02246
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #define HEAP_SIZE 1000000 #define HASH_SIZE 10000000 #define MAX_STEPS 45 typedef struct _node { unsigned long long data; int steps; int f; struct _node *next; } node; unsigned int hash(unsigned long long data) { unsigned long long hash = 0; hash += data * 9966091; return (unsigned int)(hash % HASH_SIZE); } node *hash_unique_create(node **hash_table, unsigned long long data, int steps, int f) { unsigned int hash_value = hash(data); // std::printf("%016llx, %x, %d\n", data, hash_value, steps); node *p = hash_table[hash_value]; if (p == NULL) { node *q = (node *)malloc(sizeof(node)); q->data = data; q->steps = steps; q->f = f; q->next = NULL; hash_table[hash_value] = q; return q; } if (p->data == data) { if (p->steps > steps) { p->f = f; p->steps = steps; return p; } return NULL; } while (p->next != NULL) { p = p->next; if (p->data == data) { if (p->steps > steps) { p->f = f; p->steps = steps; return p; } return NULL; } } node *q = (node *)malloc(sizeof(node)); q->data = data; q->steps = steps; q->f = f; q->next = NULL; p->next = q; return q; } int Mdistance(unsigned long long data) { int distance[16]; for (int j = 1; j < 16; j++) { distance[j] = 0; for (int i = 0; i < 16; i++) { unsigned int a; a = data >> (60 - 4 * i); a = a % 16; if ((int)a == j) { distance[j] = 0; distance[j] += std::abs(i % 4 - (j - 1) % 4); distance[j] += std::abs(i / 4 - (j - 1) / 4); } } } int sum = 0; for (int j = 1; j < 16; j++) { sum += distance[j]; } return sum; } void heap_add(node **heap, int *n, node *a) { *n += 1; int tmp_n = *n; while (tmp_n != 1) { if (heap[tmp_n / 2]->f > a->f) { heap[tmp_n] = heap[tmp_n / 2]; tmp_n = tmp_n / 2; } else { break; } } heap[tmp_n] = a; } void heap_delete(node **heap, int *n, int i) { if (i == 1) { heap[1] = heap[*n]; *n -= 1; } if (2 * i > *n) { return; } if (2 * i + 1 > *n) { if (heap[i]->f > heap[2 * i]->f) { node *tmp = heap[i]; heap[i] = heap[2 * i]; heap[2 * i] = tmp; } return; } if (heap[i]->f < heap[2 * i]->f && heap[i]->f < heap[2 * i + 1]->f) { return; } if (heap[2 * i]->f < heap[2 * i + 1]->f) { node *tmp = heap[i]; heap[i] = heap[2 * i]; heap[2 * i] = tmp; heap_delete(heap, n, 2 * i); } else { node *tmp = heap[i]; heap[i] = heap[2 * i + 1]; heap[2 * i + 1] = tmp; heap_delete(heap, n, 2 * i + 1); } } /* At the end, return 1; otherwise, return 0; */ int create(unsigned long long data, int steps, int f, node **hash_table, node **heap, int *n) { node *p = hash_unique_create(hash_table, data, steps, f); if (p == NULL) { return 0; } if (data == 0x123456789abcdef0) { return 1; } if (steps == MAX_STEPS) { return 0; } heap_add(heap, n, p); if (*n > HEAP_SIZE) { printf("heap is full\n"); throw 1; } return 0; } int find_zero(unsigned long long data) { for (int i = 0; i < 16; i++) { unsigned int a; a = data >> (60 - 4 * i); a = a % 16; if (a == 0) { return i; } } return 0; } // zero does move. unsigned long long move_right(unsigned long long data, int zero_index, int *dif_f) { unsigned long long a; a = (data >> (60 - 4 * (zero_index + 1))) % 16; if (((int)a - 1) % 4 < (zero_index + 1) % 4) { *dif_f = 0; } else { *dif_f = 2; } a = a << (60 - 4 * (zero_index + 1)); unsigned long long b; b = data - a; a = a << 4; b += a; return b; } unsigned long long move_left(unsigned long long data, int zero_index, int *dif_f) { unsigned long long a; a = (data >> (60 - 4 * (zero_index - 1))) % 16; if (((int)a - 1) % 4 > (zero_index - 1) % 4) { *dif_f = 0; } else { *dif_f = 2; } a = a << (60 - 4 * (zero_index - 1)); unsigned long long b; b = data - a; a = a >> 4; b += a; return b; } unsigned long long move_down(unsigned long long data, int zero_index, int *dif_f) { unsigned long long a; a = (data >> (60 - 4 * (zero_index + 4))) % 16; if (((int)a - 1) / 4 < (zero_index + 4) / 4) { *dif_f = 0; } else { *dif_f = 2; } a = a << (60 - 4 * (zero_index + 4)); unsigned long long b; b = data - a; a = a << 16; b += a; return b; } unsigned long long move_up(unsigned long long data, int zero_index, int *dif_f) { unsigned long long a; a = (data >> (60 - 4 * (zero_index - 4))) % 16; if (((int)a - 1) / 4 > (zero_index - 4) / 4) { *dif_f = 0; } else { *dif_f = 2; } a = a << (60 - 4 * (zero_index - 4)); unsigned long long b; b = data - a; a = a >> 16; b += a; return b; } int main() { unsigned long long data; // unsigned long long goal = 0x123456789abcdef0; node **heap; int n; node **hash_table; int finished; int result; heap = (node **)malloc(sizeof(node *) * HEAP_SIZE + 1); for (int i = 1; i <= HEAP_SIZE; i++) { heap[i] = NULL; } n = 0; hash_table = (node **)malloc(sizeof(node *) * HASH_SIZE); for (int i = 0; i < HASH_SIZE; i++) { hash_table[i] = NULL; } data = 0; for (int i = 0; i < 16; i++) { unsigned int a; std::cin >> a; data = (data << 4) + a; } try { finished = create(data, 0, Mdistance(data), hash_table, heap, &n); } catch (...) { return 1; } if (finished) { std::printf("0\n"); return 0; } finished = 0; while (1) { if (n == 0) { std::printf("empty heap\n"); return 1; } node *p = heap[1]; heap_delete(heap, &n, 1); unsigned long long new_data; int zero_index = find_zero(p->data); int dif_f = 0; if (zero_index % 4 != 3) { new_data = move_right(p->data, zero_index, &dif_f); try { finished += create(new_data, p->steps + 1, p->f + dif_f, hash_table, heap, &n); } catch (...) { return 1; } } if (zero_index % 4 != 0) { new_data = move_left(p->data, zero_index, &dif_f); try { finished += create(new_data, p->steps + 1, p->f + dif_f, hash_table, heap, &n); } catch (...) { return 1; } } if (zero_index < 12) { new_data = move_down(p->data, zero_index, &dif_f); try { finished += create(new_data, p->steps + 1, p->f + dif_f, hash_table, heap, &n); } catch (...) { return 1; } } if (zero_index > 3) { new_data = move_up(p->data, zero_index, &dif_f); try { finished += create(new_data, p->steps + 1, p->f + dif_f, hash_table, heap, &n); } catch (...) { return 1; } } if (finished) { result = p->steps + 1; break; } } std::printf("%d\n", result); return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #define HEAP_SIZE 5000000 #define HASH_SIZE 10000000 #define MAX_STEPS 45 typedef struct _node { unsigned long long data; int steps; int f; struct _node *next; } node; unsigned int hash(unsigned long long data) { unsigned long long hash = 0; hash += data * 9966091; return (unsigned int)(hash % HASH_SIZE); } node *hash_unique_create(node **hash_table, unsigned long long data, int steps, int f) { unsigned int hash_value = hash(data); // std::printf("%016llx, %x, %d\n", data, hash_value, steps); node *p = hash_table[hash_value]; if (p == NULL) { node *q = (node *)malloc(sizeof(node)); q->data = data; q->steps = steps; q->f = f; q->next = NULL; hash_table[hash_value] = q; return q; } if (p->data == data) { if (p->steps > steps) { p->f = f; p->steps = steps; return p; } return NULL; } while (p->next != NULL) { p = p->next; if (p->data == data) { if (p->steps > steps) { p->f = f; p->steps = steps; return p; } return NULL; } } node *q = (node *)malloc(sizeof(node)); q->data = data; q->steps = steps; q->f = f; q->next = NULL; p->next = q; return q; } int Mdistance(unsigned long long data) { int distance[16]; for (int j = 1; j < 16; j++) { distance[j] = 0; for (int i = 0; i < 16; i++) { unsigned int a; a = data >> (60 - 4 * i); a = a % 16; if ((int)a == j) { distance[j] = 0; distance[j] += std::abs(i % 4 - (j - 1) % 4); distance[j] += std::abs(i / 4 - (j - 1) / 4); } } } int sum = 0; for (int j = 1; j < 16; j++) { sum += distance[j]; } return sum; } void heap_add(node **heap, int *n, node *a) { *n += 1; int tmp_n = *n; while (tmp_n != 1) { if (heap[tmp_n / 2]->f > a->f) { heap[tmp_n] = heap[tmp_n / 2]; tmp_n = tmp_n / 2; } else { break; } } heap[tmp_n] = a; } void heap_delete(node **heap, int *n, int i) { if (i == 1) { heap[1] = heap[*n]; *n -= 1; } if (2 * i > *n) { return; } if (2 * i + 1 > *n) { if (heap[i]->f > heap[2 * i]->f) { node *tmp = heap[i]; heap[i] = heap[2 * i]; heap[2 * i] = tmp; } return; } if (heap[i]->f < heap[2 * i]->f && heap[i]->f < heap[2 * i + 1]->f) { return; } if (heap[2 * i]->f < heap[2 * i + 1]->f) { node *tmp = heap[i]; heap[i] = heap[2 * i]; heap[2 * i] = tmp; heap_delete(heap, n, 2 * i); } else { node *tmp = heap[i]; heap[i] = heap[2 * i + 1]; heap[2 * i + 1] = tmp; heap_delete(heap, n, 2 * i + 1); } } /* At the end, return 1; otherwise, return 0; */ int create(unsigned long long data, int steps, int f, node **hash_table, node **heap, int *n) { node *p = hash_unique_create(hash_table, data, steps, f); if (p == NULL) { return 0; } if (data == 0x123456789abcdef0) { return 1; } if (steps == MAX_STEPS) { return 0; } heap_add(heap, n, p); if (*n > HEAP_SIZE) { printf("heap is full\n"); throw 1; } return 0; } int find_zero(unsigned long long data) { for (int i = 0; i < 16; i++) { unsigned int a; a = data >> (60 - 4 * i); a = a % 16; if (a == 0) { return i; } } return 0; } // zero does move. unsigned long long move_right(unsigned long long data, int zero_index, int *dif_f) { unsigned long long a; a = (data >> (60 - 4 * (zero_index + 1))) % 16; if (((int)a - 1) % 4 < (zero_index + 1) % 4) { *dif_f = 0; } else { *dif_f = 2; } a = a << (60 - 4 * (zero_index + 1)); unsigned long long b; b = data - a; a = a << 4; b += a; return b; } unsigned long long move_left(unsigned long long data, int zero_index, int *dif_f) { unsigned long long a; a = (data >> (60 - 4 * (zero_index - 1))) % 16; if (((int)a - 1) % 4 > (zero_index - 1) % 4) { *dif_f = 0; } else { *dif_f = 2; } a = a << (60 - 4 * (zero_index - 1)); unsigned long long b; b = data - a; a = a >> 4; b += a; return b; } unsigned long long move_down(unsigned long long data, int zero_index, int *dif_f) { unsigned long long a; a = (data >> (60 - 4 * (zero_index + 4))) % 16; if (((int)a - 1) / 4 < (zero_index + 4) / 4) { *dif_f = 0; } else { *dif_f = 2; } a = a << (60 - 4 * (zero_index + 4)); unsigned long long b; b = data - a; a = a << 16; b += a; return b; } unsigned long long move_up(unsigned long long data, int zero_index, int *dif_f) { unsigned long long a; a = (data >> (60 - 4 * (zero_index - 4))) % 16; if (((int)a - 1) / 4 > (zero_index - 4) / 4) { *dif_f = 0; } else { *dif_f = 2; } a = a << (60 - 4 * (zero_index - 4)); unsigned long long b; b = data - a; a = a >> 16; b += a; return b; } int main() { unsigned long long data; // unsigned long long goal = 0x123456789abcdef0; node **heap; int n; node **hash_table; int finished; int result; heap = (node **)malloc(sizeof(node *) * HEAP_SIZE + 1); for (int i = 1; i <= HEAP_SIZE; i++) { heap[i] = NULL; } n = 0; hash_table = (node **)malloc(sizeof(node *) * HASH_SIZE); for (int i = 0; i < HASH_SIZE; i++) { hash_table[i] = NULL; } data = 0; for (int i = 0; i < 16; i++) { unsigned int a; std::cin >> a; data = (data << 4) + a; } try { finished = create(data, 0, Mdistance(data), hash_table, heap, &n); } catch (...) { return 1; } if (finished) { std::printf("0\n"); return 0; } finished = 0; while (1) { if (n == 0) { std::printf("empty heap\n"); return 1; } node *p = heap[1]; heap_delete(heap, &n, 1); unsigned long long new_data; int zero_index = find_zero(p->data); int dif_f = 0; if (zero_index % 4 != 3) { new_data = move_right(p->data, zero_index, &dif_f); try { finished += create(new_data, p->steps + 1, p->f + dif_f, hash_table, heap, &n); } catch (...) { return 1; } } if (zero_index % 4 != 0) { new_data = move_left(p->data, zero_index, &dif_f); try { finished += create(new_data, p->steps + 1, p->f + dif_f, hash_table, heap, &n); } catch (...) { return 1; } } if (zero_index < 12) { new_data = move_down(p->data, zero_index, &dif_f); try { finished += create(new_data, p->steps + 1, p->f + dif_f, hash_table, heap, &n); } catch (...) { return 1; } } if (zero_index > 3) { new_data = move_up(p->data, zero_index, &dif_f); try { finished += create(new_data, p->steps + 1, p->f + dif_f, hash_table, heap, &n); } catch (...) { return 1; } } if (finished) { result = p->steps + 1; break; } } std::printf("%d\n", result); return 0; }
replace
5
6
5
6
0
p02246
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define N 4 #define N2 16 #define LIMIT 100 const int dx[4] = {0, -1, 0, 1}; const int dy[4] = {1, 0, -1, 0}; const char dir[4] = {'r', 'u', 'l', 'd'}; int MDT[N2][N2]; struct puzzle { int f[N2], space, MD; }; puzzle state; int limit; int path[LIMIT]; int getALLMD(puzzle pz) { int sum = 0; for (int i = 0; i < N2; i++) { if (pz.f[i] == N2) { continue; } sum += MDT[i][pz.f[i] - 1]; } return sum; } bool dfs(int depth, int prev) { if (state.MD == 0) { return true; } if (depth + state.MD > limit) { return false; } int sx = state.space / N; int sy = state.space % N; puzzle tmp; for (int r = 0; r < 4; r++) { int tx = sx + dx[r]; int ty = sy + dy[r]; if (tx < 0 || ty < 0 || tx >= N || ty >= N) { continue; } // if(max(prev,r)-min(prev,r)==2){ // continue; // } tmp = state; // 计算曼哈顿距离的差值,同时交换拼图块 state.MD -= MDT[tx * N + ty][state.f[tx * N + ty] - 1]; state.MD += MDT[sx * N + sy][state.f[tx * N + ty] - 1]; swap(state.f[tx * N + ty], state.f[sx * N + sy]); state.space = tx * N + ty; if (dfs(depth + 1, r)) { path[depth] = r; return true; } state = tmp; } return false; } string iterative_deepening(puzzle in) { in.MD = getALLMD(in); // 初始状态的曼哈顿距离 for (limit = in.MD; limit <= LIMIT; limit++) { state = in; if (dfs(0, -100)) { string ans = ""; for (int i = 0; i < limit; i++) { ans += dir[path[i]]; } return ans; } } return "unsolvable"; } int main() { for (int i = 0; i < N2; i++) { for (int j = 0; j < N2; j++) { // 曼哈顿距离,此法太妙 MDT[i][j] = abs(i / N - j / N) + abs(i % N - j % N); } } puzzle in; for (int i = 0; i < N2; i++) { cin >> in.f[i]; if (in.f[i] == 0) { in.f[i] = N2; in.space = i; } } string ans = iterative_deepening(in); // for(int i=0;i<limit;i++){ // cout<<path[i]<<" "; // } cout << ans.size() << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define N 4 #define N2 16 #define LIMIT 100 const int dx[4] = {0, -1, 0, 1}; const int dy[4] = {1, 0, -1, 0}; const char dir[4] = {'r', 'u', 'l', 'd'}; int MDT[N2][N2]; struct puzzle { int f[N2], space, MD; }; puzzle state; int limit; int path[LIMIT]; int getALLMD(puzzle pz) { int sum = 0; for (int i = 0; i < N2; i++) { if (pz.f[i] == N2) { continue; } sum += MDT[i][pz.f[i] - 1]; } return sum; } bool dfs(int depth, int prev) { if (state.MD == 0) { return true; } if (depth + state.MD > limit) { return false; } int sx = state.space / N; int sy = state.space % N; puzzle tmp; for (int r = 0; r < 4; r++) { int tx = sx + dx[r]; int ty = sy + dy[r]; if (tx < 0 || ty < 0 || tx >= N || ty >= N) { continue; } if (max(prev, r) - min(prev, r) == 2) { continue; } tmp = state; // 计算曼哈顿距离的差值,同时交换拼图块 state.MD -= MDT[tx * N + ty][state.f[tx * N + ty] - 1]; state.MD += MDT[sx * N + sy][state.f[tx * N + ty] - 1]; swap(state.f[tx * N + ty], state.f[sx * N + sy]); state.space = tx * N + ty; if (dfs(depth + 1, r)) { path[depth] = r; return true; } state = tmp; } return false; } string iterative_deepening(puzzle in) { in.MD = getALLMD(in); // 初始状态的曼哈顿距离 for (limit = in.MD; limit <= LIMIT; limit++) { state = in; if (dfs(0, -100)) { string ans = ""; for (int i = 0; i < limit; i++) { ans += dir[path[i]]; } return ans; } } return "unsolvable"; } int main() { for (int i = 0; i < N2; i++) { for (int j = 0; j < N2; j++) { // 曼哈顿距离,此法太妙 MDT[i][j] = abs(i / N - j / N) + abs(i % N - j % N); } } puzzle in; for (int i = 0; i < N2; i++) { cin >> in.f[i]; if (in.f[i] == 0) { in.f[i] = N2; in.space = i; } } string ans = iterative_deepening(in); // for(int i=0;i<limit;i++){ // cout<<path[i]<<" "; // } cout << ans.size() << endl; return 0; }
replace
49
52
49
52
TLE
p02246
C++
Time Limit Exceeded
#include <cmath> #include <iostream> #include <map> #include <queue> using namespace std; constexpr int n = 3; const int n2 = n * n; int MDT[n2][n2]; struct Puzzle { public: int f[n2], space, MD; int cost; bool operator<(const Puzzle &p) const { for (int i = 0; i < n2; i++) { if (f[i] == p.f[i]) continue; return f[i] < p.f[i]; } return false; } int getAllMD() { int sum = 0; for (int i = 0; i < n2; i++) { if (f[i] == n2) continue; sum += MDT[f[i] - 1][i]; } return sum; } }; struct State { public: vector<State> solve; Puzzle puzzle; int estimated; void print() { for (int i = 0; i < n2; i++) { cout << ((puzzle.f[i] == n2) ? 0 : puzzle.f[i]) << " "; if (i % n == n - 1) cout << endl; } } bool operator<(const State &s) const { return estimated < s.estimated; } bool operator>(const State &s) const { return estimated > s.estimated; } }; constexpr int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0}; int dfs(int limit, int cnt, State s, int before) { if (cnt == 0) { if (s.puzzle.MD == 0) return s.puzzle.cost; else return -1; } int sx = s.puzzle.space / n, sy = s.puzzle.space % n; for (int i = 0; i < 4; i++) { int nx = sx + dx[i], ny = sy + dy[i]; if (nx < 0 || nx >= n || ny < 0 || ny >= n || before + i == 3) continue; Puzzle np = s.puzzle; np.MD -= MDT[nx * n + ny][np.f[nx * n + ny] - 1]; np.MD += MDT[sx * n + sy][np.f[nx * n + ny] - 1]; swap(np.f[nx * n + ny], np.f[sx * n + sy]); np.cost++; np.space = nx * n + ny; State next; next.puzzle = np; next.estimated = np.MD + np.cost; /* cout << "---------------" << endl; cout << "limit = " << limit << " "; cout << "cnt = " << cnt << " "; cout << "f(x) = " << next.estimated << endl; next.print(); */ if (next.estimated > limit) continue; int tmp = dfs(limit, cnt - 1, next, i); if (tmp != -1) return tmp; } return -1; } int IDA(Puzzle in) { constexpr int MAX = 100; State s; s.puzzle = in; s.puzzle.cost = 0; s.puzzle.MD = s.estimated = in.getAllMD(); for (int i = s.estimated; i <= MAX; i++) { int ans = dfs(i, i, s, -10); if (ans != -1) { return ans; } } return -1; } int main() { for (int i = 0; i < n2; i++) { for (int j = 0; j < n2; j++) { MDT[i][j] = abs(i / n - j / n) + abs(i % n - j % n); } } Puzzle in; for (int i = 0; i < n2; i++) { cin >> in.f[i]; if (in.f[i] == 0) { in.space = i; in.f[i] = n2; } } cout << IDA(in) << endl; return 0; }
#include <cmath> #include <iostream> #include <map> #include <queue> using namespace std; constexpr int n = 4; const int n2 = n * n; int MDT[n2][n2]; struct Puzzle { public: int f[n2], space, MD; int cost; bool operator<(const Puzzle &p) const { for (int i = 0; i < n2; i++) { if (f[i] == p.f[i]) continue; return f[i] < p.f[i]; } return false; } int getAllMD() { int sum = 0; for (int i = 0; i < n2; i++) { if (f[i] == n2) continue; sum += MDT[f[i] - 1][i]; } return sum; } }; struct State { public: vector<State> solve; Puzzle puzzle; int estimated; void print() { for (int i = 0; i < n2; i++) { cout << ((puzzle.f[i] == n2) ? 0 : puzzle.f[i]) << " "; if (i % n == n - 1) cout << endl; } } bool operator<(const State &s) const { return estimated < s.estimated; } bool operator>(const State &s) const { return estimated > s.estimated; } }; constexpr int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0}; int dfs(int limit, int cnt, State s, int before) { if (cnt == 0) { if (s.puzzle.MD == 0) return s.puzzle.cost; else return -1; } int sx = s.puzzle.space / n, sy = s.puzzle.space % n; for (int i = 0; i < 4; i++) { int nx = sx + dx[i], ny = sy + dy[i]; if (nx < 0 || nx >= n || ny < 0 || ny >= n || before + i == 3) continue; Puzzle np = s.puzzle; np.MD -= MDT[nx * n + ny][np.f[nx * n + ny] - 1]; np.MD += MDT[sx * n + sy][np.f[nx * n + ny] - 1]; swap(np.f[nx * n + ny], np.f[sx * n + sy]); np.cost++; np.space = nx * n + ny; State next; next.puzzle = np; next.estimated = np.MD + np.cost; /* cout << "---------------" << endl; cout << "limit = " << limit << " "; cout << "cnt = " << cnt << " "; cout << "f(x) = " << next.estimated << endl; next.print(); */ if (next.estimated > limit) continue; int tmp = dfs(limit, cnt - 1, next, i); if (tmp != -1) return tmp; } return -1; } int IDA(Puzzle in) { constexpr int MAX = 100; State s; s.puzzle = in; s.puzzle.cost = 0; s.puzzle.MD = s.estimated = in.getAllMD(); for (int i = s.estimated; i <= MAX; i++) { int ans = dfs(i, i, s, -10); if (ans != -1) { return ans; } } return -1; } int main() { for (int i = 0; i < n2; i++) { for (int j = 0; j < n2; j++) { MDT[i][j] = abs(i / n - j / n) + abs(i % n - j % n); } } Puzzle in; for (int i = 0; i < n2; i++) { cin >> in.f[i]; if (in.f[i] == 0) { in.space = i; in.f[i] = n2; } } cout << IDA(in) << endl; return 0; }
replace
6
7
6
7
TLE
p02246
C++
Runtime Error
///////////////////////////////////////////////////////// // // ~/Izumi_Chiharu/c/temp.cpp file // Last Updated: 2018-06-17 ...Maybe // // I hope you adding this code to the setting file // alias g++='g++ -std=c++1y -DDEBUG_LOCAL' // // My sweet heart Chiharu Izumi // ///////////////////////////////////////////////////////// #include <bits/stdc++.h> using namespace std; void print_Qbey() { cout << " H M#5J~d " " " << endl << " Hm.?WMM MMB^ .Z d " " " << endl << " MZS.` ?7WMM M#=`` (!`` d " " " << endl << " HP?X,``` ?TMM MMY! ` .d: `` d " " " << endl << " Hb;<U,`````` (TMM HM ^ ``` .V>_` `.W " " " << endl << " ;><?n. `` ````?WHHMMHHHMMMMMM MMY```` ` (3<< `` .M " " " << endl << " HN<>>>?W,`` `` ` (77!~` ```~!?7'``` `` .d>>><`` .M " " " << endl << " M2<>>>>?n.`` `` `` `` ``` `` ```` `` `.X>1++< `` (H " " " << endl << " MK<>>>>+?S.``` `` ```` ``` ``` `` `` .Z``` ?4-.` jM " " " << endl << " N2><+d=(Y^ `` `` ` ` ` ` `` `` `` `` T~.`````?=(dM " " " << endl << " MR;JY~`~``` `` `` ``` ``` `` `` `` ``` u, `` `` 7M " " " << endl << " NV!_`` `` `` ```` ``` `` `` `` ``` ``` C& ` ```` TMM " " " << endl << " MY.(-`` ` `` `` `` ` `` `` `` `` `` `` (n ` ` ``` 7M " " " << endl << " HMD-(D`` ``` `` ``` `` `` `` `` `` ` ``` ```(n ` `` `` (WM " " " << endl << " M _(d```` `` ``` ``` ``` `` `` `` ``` ``` `` .b.`` `` " "```?MH " << endl << " MM!_(%` `` `` `` ` ` `` `` `` ``` `` `` ` `` ` (l`` ``````` " "TMH " << endl << " H#>__d:`` .JXVS, ``` `` ` `` `` `.JXWH&.``` ```` O; `` ` ` " "`` -WM " << endl << " M%.~(k ``.HXH[ dr ```` ``` `` `` dWWN- u. `` ``` ,M,``` ``` " "``` ?MMM" << endl << " MF_~~($`` .WXMMWX$ ` ``` `` ``` ` dXM HHK````` `` " ",HN-``````````.-=jg" << endl << "MMMMHMY.~~~(R ``` 799Y!```` ``` `` `` `` ?UUUY!`` ````` (M N, `` " "```.Z3J=``" << endl << "gaJJdD.~~::_X|`````````````` `````` `` ` ``` ````` ``` .H MN,``` " ".Yiv! ```" << endl << " MD_:~:~:~(T.`````````` ``` ` ` ````` ``` ``` ``````.M N, " ".Ziv!`` ._~" << endl << " MM'_:::::::~(N+.`````````` .`.(.` .` ``` ```` ```` .dM NZ1v! " ".-_~~``" << endl << " #:(::::::::~(HMNa.. ``````_?!`?7! `` ```` ...-(+7WMM HM3(b " "_:<``..`." << endl << "=_;::::::::<(H MMHaJ--_--........-.__~~~~(&v7<~``` ?MM " "M>gM9h,_......." << endl << ";;;;;;;:;::(drWM HHmgggg&gzOtOzz<<~~~~~_`` ``` ?MMMH+ggM " "Mm-......" << endl << "e<>>;>>;;><dMMJM Nc_~~~~~~~~~~_ `` ` `` " "7H&-...?THNe-_._(" << endl << ",4m+>>>>>>j MM(W N-~~~~~~~~:_`` ``` ` `` " "?7UHHqqHWHMm+__" << endl << "vX+vT4k&&dMMD!+M MR_~~~~~:_````` ```` ````````` " "```` ?We" << endl << "???zTwiJ-(((JdM MK_~~~~:_``` ` ` ` `` ` ` ` ``` " "```````?" << endl << "1uukgHM MH_~~~_``` `` ``` `` `` `` ` `` " "` ` ```" << endl << "CugM N;~~:_ ` `` `` ``` `` ````` ``` " "``` ` `" << endl << "H M[~~~_4, ` ` `` `` `` ``` `` " "`` ``` " << endl << " Hb~~~~~?n (: `` `` `` `` ` ` " "`` `` ```" << endl << " Hb~~~~~~(4,J_ `` ``` ```` ``` " "`` `` ` " << endl << " N-~~~~~~(MM_` `` ` (} `` `` " "`` `` ```" << endl << " Mr_~~~~~(HH: `` `` j!`` `` `` " "`` `` ``" << endl << " Mb~~~~~~(WH:`` `` .Z `` `` `` " "``` `` `" << endl << " N:~~~~~(WM{ `` ` .H+.. `` ` " ".``` `` " << endl; } #define fi first #define se second #define mp make_pair #define pb push_back // #define int long long #define _overload3(_1, _2, _3, name, ...) name #define _REP(i, n) REAP(i, 0, n) #define REAP(i, a, b) for (int i = int(a); i < int(b); ++i) #define REP(...) _overload3(__VA_ARGS__, REAP, _REP, )(__VA_ARGS__) #define _REPR(i, n) REAPR(i, n, 0) #define REAPR(i, a, b) for (int i = int(a - 1); i >= int(b); --i) #define REPR(...) _overload3(__VA_ARGS__, REAPR, _REPR, )(__VA_ARGS__) #define ALL(a) a.begin(), a.end() #define rALL(a) a.rbegin(), a.rend() #define coutALL(a) \ { \ int loop_coutALL = 0; \ for (auto e : a) \ cout << (loop_coutALL++ ? " " : "") << e; \ cout << endl; \ } #define coutYN(a) cout << (a ? "Yes" : "No") << endl; #define coutyn(a) cout << (a ? "yes" : "no") << endl; #define pcnt __builtin_popcount #define buli(x) __builtin_popcountll(x) const int INF = 1145141919; const int MOD = (int)1e9 + 7; const double EPS = 1e-12; const int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0}; // const int dx[]={-1,-1,-1,0,1,1,1,0},dy[]={-1,0,1,1,1,0,-1,-1}; using vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>; typedef pair<int, int> pii; typedef pair<int, string> pis; typedef pair<string, int> psi; typedef pair<string, string> pss; typedef long long ll; template <class T> ll upper(T n, T m) { return (n + m - 1) / m; }; template <class T> ll rounding(T n) { return (long double)n + 0.5; }; inline int qp(int a, ll b) { int ans = 1; do { if (b & 1) ans = 1ll * ans * a; a = 1ll * a * a; } while (b >>= 1); return ans; } inline int qp(int a, ll b, int mo) { int ans = 1; do { if (b & 1) ans = 1ll * ans * a % mo; a = 1ll * a * a % mo; } while (b >>= 1); return ans; } struct Arithmetic { Arithmetic() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); } }; // #define DEBUG_LOCAL #ifdef DEBUG_LOCAL template <typename T> void deb(T a) { cerr << "deb: " << a << "ですねぇ!" << endl; } #define debl \ { cerr << "debug: " << __LINE__ << "行目だよーんおほほ" << endl; } void what_cr() { cout << __GCC_ATOMIC_CHAR16_T_LOCK_FREE << " ←なんだろーこの数字?" << endl; } void t_t() { cout << endl << "------------------------" << endl << "| Presented by |" << " Compiled " << __FILE__ << endl << "| " << __DATE__ << " " << __TIME__ << " |" << endl << "| Chiharu Izumi |" << " to get the AC :)" << endl << "------------------------" << endl; } #else template <typename T> void deb(T a) {} #define debl ; void what_cr() {} void t_t() {} #endif struct pz { int pz[16], pz0, man; }; int lim; int mdt[16][16]; pz now; int manpz(pz p) { int dis = 0; REP(i, 16) if (p.pz[i] != 16) dis += mdt[i][p.pz[i] - 1]; return dis; } bool dfs(int dep, int pre) { if (!now.man) return true; if (dep + now.man > lim) return false; int x1 = now.pz0 / 4; int y1 = now.pz0 % 4; pz t; REP(i, 4) { int x2 = x1 + dx[i]; int y2 = y1 + dy[i]; if (x2 < 0 || x2 > 3) continue; if (y2 < 0 || y2 > 3) continue; if (max(pre, i) - min(pre, i) == 2) continue; t = now; now.man -= mdt[x2 * 4 + y2][now.pz[x2 * 4 + y2] - 1]; now.man -= mdt[x1 * 4 + y1][now.pz[x2 * 4 + y2] - 1]; swap(now.pz[x2 * 4 + y2], now.pz[x1 * 4 + y1]); now.pz0 = x2 * 4 + y2; if (dfs(dep + 1, i)) return true; now = t; } return false; } int id(pz p) { p.man = manpz(p); for (lim = p.man; lim < 100; lim++) { now = p; if (dfs(0, -100)) return lim; } } signed main() { Arithmetic Exception; REP(i, 16) REP(j, 16) mdt[i][j] = abs(i / 4 - j / 4) + abs(i % 4 - j % 4); pz p; REP(i, 16) { cin >> p.pz[i]; if (!p.pz[i]) { p.pz[i] = 16; p.pz0 = i; } } cout << id(p) << endl; return 0; }
///////////////////////////////////////////////////////// // // ~/Izumi_Chiharu/c/temp.cpp file // Last Updated: 2018-06-17 ...Maybe // // I hope you adding this code to the setting file // alias g++='g++ -std=c++1y -DDEBUG_LOCAL' // // My sweet heart Chiharu Izumi // ///////////////////////////////////////////////////////// #include <bits/stdc++.h> using namespace std; void print_Qbey() { cout << " H M#5J~d " " " << endl << " Hm.?WMM MMB^ .Z d " " " << endl << " MZS.` ?7WMM M#=`` (!`` d " " " << endl << " HP?X,``` ?TMM MMY! ` .d: `` d " " " << endl << " Hb;<U,`````` (TMM HM ^ ``` .V>_` `.W " " " << endl << " ;><?n. `` ````?WHHMMHHHMMMMMM MMY```` ` (3<< `` .M " " " << endl << " HN<>>>?W,`` `` ` (77!~` ```~!?7'``` `` .d>>><`` .M " " " << endl << " M2<>>>>?n.`` `` `` `` ``` `` ```` `` `.X>1++< `` (H " " " << endl << " MK<>>>>+?S.``` `` ```` ``` ``` `` `` .Z``` ?4-.` jM " " " << endl << " N2><+d=(Y^ `` `` ` ` ` ` `` `` `` `` T~.`````?=(dM " " " << endl << " MR;JY~`~``` `` `` ``` ``` `` `` `` ``` u, `` `` 7M " " " << endl << " NV!_`` `` `` ```` ``` `` `` `` ``` ``` C& ` ```` TMM " " " << endl << " MY.(-`` ` `` `` `` ` `` `` `` `` `` `` (n ` ` ``` 7M " " " << endl << " HMD-(D`` ``` `` ``` `` `` `` `` `` ` ``` ```(n ` `` `` (WM " " " << endl << " M _(d```` `` ``` ``` ``` `` `` `` ``` ``` `` .b.`` `` " "```?MH " << endl << " MM!_(%` `` `` `` ` ` `` `` `` ``` `` `` ` `` ` (l`` ``````` " "TMH " << endl << " H#>__d:`` .JXVS, ``` `` ` `` `` `.JXWH&.``` ```` O; `` ` ` " "`` -WM " << endl << " M%.~(k ``.HXH[ dr ```` ``` `` `` dWWN- u. `` ``` ,M,``` ``` " "``` ?MMM" << endl << " MF_~~($`` .WXMMWX$ ` ``` `` ``` ` dXM HHK````` `` " ",HN-``````````.-=jg" << endl << "MMMMHMY.~~~(R ``` 799Y!```` ``` `` `` `` ?UUUY!`` ````` (M N, `` " "```.Z3J=``" << endl << "gaJJdD.~~::_X|`````````````` `````` `` ` ``` ````` ``` .H MN,``` " ".Yiv! ```" << endl << " MD_:~:~:~(T.`````````` ``` ` ` ````` ``` ``` ``````.M N, " ".Ziv!`` ._~" << endl << " MM'_:::::::~(N+.`````````` .`.(.` .` ``` ```` ```` .dM NZ1v! " ".-_~~``" << endl << " #:(::::::::~(HMNa.. ``````_?!`?7! `` ```` ...-(+7WMM HM3(b " "_:<``..`." << endl << "=_;::::::::<(H MMHaJ--_--........-.__~~~~(&v7<~``` ?MM " "M>gM9h,_......." << endl << ";;;;;;;:;::(drWM HHmgggg&gzOtOzz<<~~~~~_`` ``` ?MMMH+ggM " "Mm-......" << endl << "e<>>;>>;;><dMMJM Nc_~~~~~~~~~~_ `` ` `` " "7H&-...?THNe-_._(" << endl << ",4m+>>>>>>j MM(W N-~~~~~~~~:_`` ``` ` `` " "?7UHHqqHWHMm+__" << endl << "vX+vT4k&&dMMD!+M MR_~~~~~:_````` ```` ````````` " "```` ?We" << endl << "???zTwiJ-(((JdM MK_~~~~:_``` ` ` ` `` ` ` ` ``` " "```````?" << endl << "1uukgHM MH_~~~_``` `` ``` `` `` `` ` `` " "` ` ```" << endl << "CugM N;~~:_ ` `` `` ``` `` ````` ``` " "``` ` `" << endl << "H M[~~~_4, ` ` `` `` `` ``` `` " "`` ``` " << endl << " Hb~~~~~?n (: `` `` `` `` ` ` " "`` `` ```" << endl << " Hb~~~~~~(4,J_ `` ``` ```` ``` " "`` `` ` " << endl << " N-~~~~~~(MM_` `` ` (} `` `` " "`` `` ```" << endl << " Mr_~~~~~(HH: `` `` j!`` `` `` " "`` `` ``" << endl << " Mb~~~~~~(WH:`` `` .Z `` `` `` " "``` `` `" << endl << " N:~~~~~(WM{ `` ` .H+.. `` ` " ".``` `` " << endl; } #define fi first #define se second #define mp make_pair #define pb push_back // #define int long long #define _overload3(_1, _2, _3, name, ...) name #define _REP(i, n) REAP(i, 0, n) #define REAP(i, a, b) for (int i = int(a); i < int(b); ++i) #define REP(...) _overload3(__VA_ARGS__, REAP, _REP, )(__VA_ARGS__) #define _REPR(i, n) REAPR(i, n, 0) #define REAPR(i, a, b) for (int i = int(a - 1); i >= int(b); --i) #define REPR(...) _overload3(__VA_ARGS__, REAPR, _REPR, )(__VA_ARGS__) #define ALL(a) a.begin(), a.end() #define rALL(a) a.rbegin(), a.rend() #define coutALL(a) \ { \ int loop_coutALL = 0; \ for (auto e : a) \ cout << (loop_coutALL++ ? " " : "") << e; \ cout << endl; \ } #define coutYN(a) cout << (a ? "Yes" : "No") << endl; #define coutyn(a) cout << (a ? "yes" : "no") << endl; #define pcnt __builtin_popcount #define buli(x) __builtin_popcountll(x) const int INF = 1145141919; const int MOD = (int)1e9 + 7; const double EPS = 1e-12; const int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0}; // const int dx[]={-1,-1,-1,0,1,1,1,0},dy[]={-1,0,1,1,1,0,-1,-1}; using vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>; typedef pair<int, int> pii; typedef pair<int, string> pis; typedef pair<string, int> psi; typedef pair<string, string> pss; typedef long long ll; template <class T> ll upper(T n, T m) { return (n + m - 1) / m; }; template <class T> ll rounding(T n) { return (long double)n + 0.5; }; inline int qp(int a, ll b) { int ans = 1; do { if (b & 1) ans = 1ll * ans * a; a = 1ll * a * a; } while (b >>= 1); return ans; } inline int qp(int a, ll b, int mo) { int ans = 1; do { if (b & 1) ans = 1ll * ans * a % mo; a = 1ll * a * a % mo; } while (b >>= 1); return ans; } struct Arithmetic { Arithmetic() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); } }; // #define DEBUG_LOCAL #ifdef DEBUG_LOCAL template <typename T> void deb(T a) { cerr << "deb: " << a << "ですねぇ!" << endl; } #define debl \ { cerr << "debug: " << __LINE__ << "行目だよーんおほほ" << endl; } void what_cr() { cout << __GCC_ATOMIC_CHAR16_T_LOCK_FREE << " ←なんだろーこの数字?" << endl; } void t_t() { cout << endl << "------------------------" << endl << "| Presented by |" << " Compiled " << __FILE__ << endl << "| " << __DATE__ << " " << __TIME__ << " |" << endl << "| Chiharu Izumi |" << " to get the AC :)" << endl << "------------------------" << endl; } #else template <typename T> void deb(T a) {} #define debl ; void what_cr() {} void t_t() {} #endif struct pz { int pz[16], pz0, man; }; int lim; int mdt[16][16]; pz now; int manpz(pz p) { int dis = 0; REP(i, 16) if (p.pz[i] != 16) dis += mdt[i][p.pz[i] - 1]; return dis; } bool dfs(int dep, int pre) { if (!now.man) return true; if (dep + now.man > lim) return false; int x1 = now.pz0 / 4; int y1 = now.pz0 % 4; pz t; REP(i, 4) { int x2 = x1 + dx[i]; int y2 = y1 + dy[i]; if (x2 < 0 || x2 > 3) continue; if (y2 < 0 || y2 > 3) continue; if (max(pre, i) - min(pre, i) == 2) continue; t = now; now.man -= mdt[x2 * 4 + y2][now.pz[x2 * 4 + y2] - 1]; now.man += mdt[x1 * 4 + y1][now.pz[x2 * 4 + y2] - 1]; swap(now.pz[x2 * 4 + y2], now.pz[x1 * 4 + y1]); now.pz0 = x2 * 4 + y2; if (dfs(dep + 1, i)) return true; now = t; } return false; } int id(pz p) { p.man = manpz(p); for (lim = p.man; lim < 100; lim++) { now = p; if (dfs(0, -100)) return lim; } } signed main() { Arithmetic Exception; REP(i, 16) REP(j, 16) mdt[i][j] = abs(i / 4 - j / 4) + abs(i % 4 - j % 4); pz p; REP(i, 16) { cin >> p.pz[i]; if (!p.pz[i]) { p.pz[i] = 16; p.pz0 = i; } } cout << id(p) << endl; return 0; }
replace
286
287
286
287
-11