buggy_code
stringlengths
11
625k
fixed_code
stringlengths
17
625k
bug_type
stringlengths
2
4.45k
language
int64
0
8
token_count
int64
5
200k
#include <bits/stdc++.h> using namespace std; int main() { int x, y, x1, y1; int cnt = 0; while (1) { cin >> x >> y; if (x == 0 && y == 0) { break; } if (x < y) swap(x, y); while (1) { x = x % y; swap(x, y); cnt++; if (y == 0) break; } cout << x << " " << cnt << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int x, y, x1, y1; int cnt = 0; while (1) { cnt = 0; cin >> x >> y; if (x == 0 && y == 0) { break; } if (x < y) swap(x, y); while (1) { x = x % y; swap(x, y); cnt++; if (y == 0) break; } cout << x << " " << cnt << endl; } }
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
107
#include <iostream> main() { for (int a, b, t, c; std::cin >> a >> b; printf("%d %d\n", a, c)) { if (a < b) { a ^= b; b ^= a; a ^= b; } for (c = 0; t = b; a = t, c++) b = a % b; } }
#include <iostream> main() { for (int a, b, t, c; std::cin >> a >> b, a; printf("%d %d\n", a, c)) { if (a < b) { a ^= b; b ^= a; a ^= b; } for (c = 0; t = b; a = t, c++) b = a % b; } }
[["+", 0, 14, 8, 9, 0, 7, 15, 34, 0, 21], ["+", 0, 14, 8, 9, 0, 7, 15, 34, 12, 22]]
1
83
#include <bits/stdc++.h> using namespace std; struct BOX { string top; string back, left, right, head; string buttom; bool operator==(const BOX &b) const { return (top == b.top && back == b.back && left == b.left && right == b.right && head == b.head && buttom == b.buttom); } }; class UnionFind { private: vector<int> rank, p; void link(int x, int y) { if (rank[x] > rank[y]) { p[y] = x; } else { p[x] = y; if (rank[x] == rank[y]) rank[y]++; } } public: UnionFind(int size) { rank.resize(size, 0); p.resize(size, 0); for (int i = 0; i < rank.size(); i++) { p[i] = i, rank[i] = 0; } } void Unite(int x, int y) { if (Find(x) != Find(y)) link(Find(x), Find(y)); } int Find(int x) { return (x != p[x] ? p[x] = Find(p[x]) : p[x]); } }; void Roll(BOX &B) { string temp = B.head; B.head = B.right; B.right = B.back; B.back = B.left; B.left = temp; } void RoLL(BOX &B) { string temp = B.head; B.head = B.top; B.top = B.back; B.back = B.buttom; B.buttom = temp; } void RolL(BOX &B) { string temp = B.left; B.left = B.top; B.top = B.right; B.right = B.buttom; B.buttom = temp; } bool Myon(BOX &a, BOX &b) { for (int k = 0; k < 4; k++) { Roll(a); for (int l = 0; l < 4; l++) { RoLL(a); for (int m = 0; m < 4; m++) { RolL(a); if (a == b) return (true); } } } return (false); } int main() { int n; vector<BOX> boxes; while (cin >> n, n) { UnionFind uf(n); boxes.resize(n); int ret = 0; for (int i = 0; i < n; i++) { BOX &b = boxes[i]; cin >> b.top >> b.head >> b.right >> b.back >> b.left >> b.buttom; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (Myon(boxes[i], boxes[j])) uf.Unite(i, j); } } for (int i = 0; i < n; i++) { ret += uf.Find(i) == i; } cout << n - ret << endl; } }
#include <bits/stdc++.h> using namespace std; struct BOX { string top; string back, left, right, head; string buttom; bool operator==(const BOX &b) const { return (top == b.top && back == b.back && left == b.left && right == b.right && head == b.head && buttom == b.buttom); } }; class UnionFind { private: vector<int> rank, p; void link(int x, int y) { if (rank[x] > rank[y]) { p[y] = x; } else { p[x] = y; if (rank[x] == rank[y]) rank[y]++; } } public: UnionFind(int size) { rank.resize(size, 0); p.resize(size, 0); for (int i = 0; i < rank.size(); i++) { p[i] = i, rank[i] = 0; } } void Unite(int x, int y) { if (Find(x) != Find(y)) link(Find(x), Find(y)); } int Find(int x) { return (x != p[x] ? p[x] = Find(p[x]) : p[x]); } }; void Roll(BOX &B) { string temp = B.head; B.head = B.right; B.right = B.back; B.back = B.left; B.left = temp; } void RoLL(BOX &B) { string temp = B.head; B.head = B.top; B.top = B.back; B.back = B.buttom; B.buttom = temp; } void RolL(BOX &B) { string temp = B.left; B.left = B.top; B.top = B.right; B.right = B.buttom; B.buttom = temp; } bool Myon(BOX &a, BOX &b) { for (int k = 0; k < 4; k++) { Roll(a); for (int l = 0; l < 4; l++) { RoLL(a); for (int m = 0; m < 4; m++) { RolL(a); if (a == b) return (true); } } } return (false); } int main() { int n; vector<BOX> boxes; while (cin >> n, n) { UnionFind uf(n); boxes.resize(n); int ret = 0; for (int i = 0; i < n; i++) { BOX &b = boxes[i]; cin >> b.top >> b.head >> b.right >> b.left >> b.back >> b.buttom; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (Myon(boxes[i], boxes[j])) uf.Unite(i, j); } } for (int i = 0; i < n; i++) { ret += uf.Find(i) == i; } cout << n - ret << endl; } }
[["-", 0, 16, 31, 16, 31, 16, 12, 118, 119, 120], ["+", 0, 16, 31, 16, 31, 16, 12, 118, 119, 120], ["-", 0, 1, 0, 16, 31, 16, 12, 118, 119, 120], ["+", 0, 1, 0, 16, 31, 16, 12, 118, 119, 120]]
1
700
#include <bits/stdc++.h> using namespace std; #define pb(n) push_back(n) #define fi first #define se second #define all(r) (r).begin(), (r).end() #define vmax(ary) *max_element(all(ary)) #define vmin(ary) *min_element(all(ary)) #define debug(x) cout << #x << ": " << x << endl #define fcout(n) cout << fixed << setprecision((n)) #define scout(n) cout << setw(n) #define vary(type, name, size, init) vector<type> name(size, init) #define vvl(v, w, h, init) vector<vector<ll>> v(w, vector<ll>(h, init)) #define mp(a, b) make_pair(a, b) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, a, b) for (int i = (a); i < (int)(b); ++i) #define repi(it, array) \ for (auto it = array.begin(), end = array.end(); it != end; ++it) #define repa(n, array) for (auto &n : (array)) using ll = long long; using vi = vector<int>; using vl = vector<ll>; using dict = map<string, int>; using pii = pair<int, int>; using pll = pair<ll, ll>; const int mod = 1000000007; constexpr int inf = ((1 << 30) - 1) * 2 + 1; constexpr double PI = acos(-1.0); double eps = 1e-10; const int dy[] = {-1, 0, 1, 0, 1, -1, 1, -1}; const int dx[] = {0, -1, 0, 1, 1, -1, -1, 1}; inline bool value(int x, int y, int w, int h) { return (x >= 0 && x < w && y >= 0 && y < h); } template <typename T> void Unique(vector<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template <typename T, typename U> ll FindErase(T &v, U tar) { ll cnt = 0; for (auto it = v.begin(); it != v.end();) { if (*it == tar) { it = v.erase(it); ++cnt; } else { ++it; } } return cnt; } enum { t = 0, f, r, b, l, d, }; class Dice { public: array<int, 6> D; Dice() { D = {1, 2, 3, 4, 5, 6}; } Dice(array<int, 6> d) { D = d; } void Spin(int dir) { if (dir == l) { // swap(D[t], D[r]); swap(D[r], D[d]); swap(D[d], D[l]); } else if (dir == r) { swap(D[t], D[l]); swap(D[l], D[d]); swap(D[d], D[r]); } else if (dir == f) { swap(D[t], D[b]); swap(D[b], D[d]); swap(D[d], D[f]); } else if (dir == b) { swap(D[t], D[f]); swap(D[f], D[d]); swap(D[d], D[b]); } else if (dir == d) { // leftspin swap(D[f], D[r]); swap(D[r], D[b]); swap(D[b], D[l]); } else if (dir == t) { swap(D[f], D[l]); swap(D[l], D[b]); swap(D[b], D[r]); } } void MatchDice(int T, int F) { rep(i, 4) { Spin(r); if (D[t] == T) break; } if (D[t] != T) { rep(i, 4) { Spin(f); if (D[t] == T) break; } } rep(i, 4) { Spin(t); if (D[f] == F) break; } } int &operator[](const int i) { return D[i]; } bool operator==(Dice &di2) { rep(i, 4) { di2.Spin(r); rep(j, 4) { di2.Spin(t); rep(k, 4) { di2.Spin(r); if (D == di2.D) { return true; } } } } return false; } friend ostream &operator<<(ostream &os, const Dice &di) { os << "t f r l b d\n"; rep(i, 6) { if (i) os << ' '; os << di.D[i]; } return os; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); map<string, int> m; m["Cyan"] = 0, m["Yellow"] = 1, m["Red"] = 2, m["Magenta"] = 3, m["Green"] = 4, m["Blue"] = 5; ll n; while (cin >> n && n) { vector<Dice> di(n); string s, s1, s2, s3, s4, s5; vector<Dice> st; rep(i, n) { cin >> s >> s1 >> s2 >> s3 >> s4 >> s5; di[i][t] = m[s]; di[i][f] = m[s]; di[i][r] = m[s]; di[i][l] = m[s]; di[i][b] = m[s]; di[i][d] = m[s]; if (st.size() == 0) { st.push_back(di[i]); } else { rep(j, st.size()) { if (st[j] == di[i]) { break; } if (j == st.size() - 1) { st.push_back(di[i]); } } } } cout << n - st.size() << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define pb(n) push_back(n) #define fi first #define se second #define all(r) (r).begin(), (r).end() #define vmax(ary) *max_element(all(ary)) #define vmin(ary) *min_element(all(ary)) #define debug(x) cout << #x << ": " << x << endl #define fcout(n) cout << fixed << setprecision((n)) #define scout(n) cout << setw(n) #define vary(type, name, size, init) vector<type> name(size, init) #define vvl(v, w, h, init) vector<vector<ll>> v(w, vector<ll>(h, init)) #define mp(a, b) make_pair(a, b) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, a, b) for (int i = (a); i < (int)(b); ++i) #define repi(it, array) \ for (auto it = array.begin(), end = array.end(); it != end; ++it) #define repa(n, array) for (auto &n : (array)) using ll = long long; using vi = vector<int>; using vl = vector<ll>; using dict = map<string, int>; using pii = pair<int, int>; using pll = pair<ll, ll>; const int mod = 1000000007; constexpr int inf = ((1 << 30) - 1) * 2 + 1; constexpr double PI = acos(-1.0); double eps = 1e-10; const int dy[] = {-1, 0, 1, 0, 1, -1, 1, -1}; const int dx[] = {0, -1, 0, 1, 1, -1, -1, 1}; inline bool value(int x, int y, int w, int h) { return (x >= 0 && x < w && y >= 0 && y < h); } template <typename T> void Unique(vector<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template <typename T, typename U> ll FindErase(T &v, U tar) { ll cnt = 0; for (auto it = v.begin(); it != v.end();) { if (*it == tar) { it = v.erase(it); ++cnt; } else { ++it; } } return cnt; } enum { t = 0, f, r, b, l, d, }; class Dice { public: array<int, 6> D; Dice() { D = {1, 2, 3, 4, 5, 6}; } Dice(array<int, 6> d) { D = d; } void Spin(int dir) { if (dir == l) { // swap(D[t], D[r]); swap(D[r], D[d]); swap(D[d], D[l]); } else if (dir == r) { swap(D[t], D[l]); swap(D[l], D[d]); swap(D[d], D[r]); } else if (dir == f) { swap(D[t], D[b]); swap(D[b], D[d]); swap(D[d], D[f]); } else if (dir == b) { swap(D[t], D[f]); swap(D[f], D[d]); swap(D[d], D[b]); } else if (dir == d) { // leftspin swap(D[f], D[r]); swap(D[r], D[b]); swap(D[b], D[l]); } else if (dir == t) { swap(D[f], D[l]); swap(D[l], D[b]); swap(D[b], D[r]); } } void MatchDice(int T, int F) { rep(i, 4) { Spin(r); if (D[t] == T) break; } if (D[t] != T) { rep(i, 4) { Spin(f); if (D[t] == T) break; } } rep(i, 4) { Spin(t); if (D[f] == F) break; } } int &operator[](const int i) { return D[i]; } bool operator==(Dice &di2) { rep(i, 4) { di2.Spin(r); rep(j, 4) { di2.Spin(t); rep(k, 4) { di2.Spin(r); if (D == di2.D) { return true; } } } } return false; } friend ostream &operator<<(ostream &os, const Dice &di) { os << "t f r l b d\n"; rep(i, 6) { if (i) os << ' '; os << di.D[i]; } return os; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); map<string, int> m; m["Cyan"] = 0, m["Yellow"] = 1, m["Red"] = 2, m["Magenta"] = 3, m["Green"] = 4, m["Blue"] = 5; ll n; while (cin >> n && n) { vector<Dice> di(n); string s, s1, s2, s3, s4, s5; vector<Dice> st; rep(i, n) { cin >> s >> s1 >> s2 >> s3 >> s4 >> s5; di[i][t] = m[s]; di[i][f] = m[s1]; di[i][r] = m[s2]; di[i][l] = m[s3]; di[i][b] = m[s4]; di[i][d] = m[s5]; if (st.size() == 0) { st.push_back(di[i]); } else { rep(j, st.size()) { if (st[j] == di[i]) { break; } if (j == st.size() - 1) { st.push_back(di[i]); } } } } cout << n - st.size() << endl; } return 0; }
[["-", 0, 1, 0, 11, 12, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 11, 12, 69, 341, 342, 0, 22]]
1
1,340
var input = require('fs').readFileSync('/dev/stdin', 'utf8'); var Arr = input.trim().split("\n"); while (true) { var nm = Arr.shift().split(" ").map(Number); var n = nm[0]; var m = nm[1]; if (n == 0 && m == 0) break; var arr = []; for (var i = 0; i < n; i++) arr.push(0); arr.unshift("X"); arr.push("X"); for (var I = 0; I < m; I++) { var v = Arr.shift(); if (v == "A") { for (var i = 1; i < arr.length - 1; i++) { if (arr[i] == 0) { arr[i] = "A"; break; } } } if (v == "B") { var flag = false; for (var i = arr.length - 2; i >= 1; i--) { if (arr[i - 1] != "A" && arr[i] == 0 && arr[i + 1] != "A") { arr[i] = "B"; flag = true; break; } } if (flag) continue; for (var i = 1; i < arr.length - 1; i++) { if (arr[i] == 0) { arr[i] = "B"; break; } } } if (v == "C") { var flag = false; for (var i = 1; i < arr.length - 1; i++) { if (arr[i] != 0) { if (arr[i + 1] == 0) { arr[i + 1] = "C"; flag = true; break; } else if (arr[i - 1] == 0) { arr[i - 1] = "C"; flag = true; break; } } } if (flag) continue; if (n % 2 == 1) arr[(n + 1) / 2 + 1] = "C"; else arr[n / 2 + 2] = "C"; } if (v == "D") { var max = [ 0, 0 ]; for (var i = 1; i < arr.length - 1; i++) { if (arr[i] != 0) continue; var jA = i; var jB = i; var cnt = 0; while (true) { if (arr[jA] != "X") jA--; if (arr[jB] != "X") jB++; if (arr[jA] == "X" && arr[jB] == "X") break; if (arr[jA] != "X" && arr[jA] != 0) break; if (arr[jB] != "X" && arr[jB] != 0) break; cnt++; if (max[1] < cnt) max = [ i, cnt ]; } } if (max[0] != 0 && max[1] != 0) { arr[max[0]] = "D"; } else { for (var i = 1; i < arr.length - 1; i++) { if (arr[i] == 0) { arr[i] = "D"; break; } } } } } arr.shift(); arr.pop(); var str = arr.join("").replace(/0/g, "#"); console.log(str); }
var input = require('fs').readFileSync('/dev/stdin', 'utf8'); var Arr = input.trim().split("\n"); while (true) { var nm = Arr.shift().split(" ").map(Number); var n = nm[0]; var m = nm[1]; if (n == 0 && m == 0) break; var arr = []; for (var i = 0; i < n; i++) arr.push(0); arr.unshift("X"); arr.push("X"); for (var I = 0; I < m; I++) { var v = Arr.shift(); if (v == "A") { for (var i = 1; i < arr.length - 1; i++) { if (arr[i] == 0) { arr[i] = "A"; break; } } } if (v == "B") { var flag = false; for (var i = arr.length - 2; i >= 1; i--) { if (arr[i - 1] != "A" && arr[i] == 0 && arr[i + 1] != "A") { arr[i] = "B"; flag = true; break; } } if (flag) continue; for (var i = 1; i < arr.length - 1; i++) { if (arr[i] == 0) { arr[i] = "B"; break; } } } if (v == "C") { var flag = false; for (var i = 1; i < arr.length - 1; i++) { if (arr[i] != 0) { if (arr[i + 1] == 0) { arr[i + 1] = "C"; flag = true; break; } else if (arr[i - 1] == 0) { arr[i - 1] = "C"; flag = true; break; } } } if (flag) continue; if (n % 2 == 1) arr[(n - 1) / 2 + 1] = "C"; else arr[n / 2 + 1] = "C"; } if (v == "D") { var max = [ 0, 0 ]; for (var i = 1; i < arr.length - 1; i++) { if (arr[i] != 0) continue; var jA = i; var jB = i; var cnt = 0; while (true) { if (arr[jA] != "X") jA--; if (arr[jB] != "X") jB++; if (arr[jA] == "X" && arr[jB] == "X") break; if (arr[jA] != "X" && arr[jA] != 0) break; if (arr[jB] != "X" && arr[jB] != 0) break; cnt++; if (max[1] < cnt) max = [ i, cnt ]; } } if (max[0] != 0 && max[1] != 0) { arr[max[0]] = "D"; } else { for (var i = 1; i < arr.length - 1; i++) { if (arr[i] == 0) { arr[i] = "D"; break; } } } } } arr.shift(); arr.pop(); var str = arr.join("").replace(/0/g, "#"); console.log(str); }
[["-", 71, 16, 31, 16, 31, 23, 0, 16, 17, 72], ["+", 71, 16, 31, 16, 31, 23, 0, 16, 17, 33], ["-", 0, 1, 0, 11, 31, 69, 71, 16, 12, 555], ["+", 0, 1, 0, 11, 31, 69, 71, 16, 12, 555]]
2
795
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); int m = sc.nextInt(); if (n == 0 && m == 0) { break; } String[] chairs = new String[n]; for (int i = 0; i < n; i++) { chairs[i] = "#"; } for (int i = 0; i < m; i++) { String str = sc.next(); if ("A".equals(str)) { for (int j = 0; j < n; j++) { if (chairs[j].equals("#")) { chairs[j] = "A"; break; } } } else if ("B".equals(str)) { boolean found = false; for (int j = n - 1; j >= 0; j--) { if (chairs[j].equals("#") && (j == (n - 1) || !chairs[j + 1].equals("A")) && (j == 0 || !chairs[j - 1].equals("A"))) { found = true; chairs[j] = "B"; break; } } if (found == false) { for (int j = 0; j < n; j++) { if (chairs[j].equals("#")) { chairs[j] = "B"; break; } } } } else if ("C".equals(str)) { boolean found = false; for (int j = 0; j < n; j++) { if (!chairs[j].equals("#")) { if (j != (n - 1) && chairs[j + 1].equals("#")) { found = true; chairs[j + 1] = "C"; break; } else if (j != 0 && chairs[j - 1].equals("#")) { found = true; chairs[j - 1] = "C"; break; } } } if (found == false) { if (n % 2 == 1) { chairs[(n + 1) / 2] = "C"; } else { chairs[n / 2 + 1] = "C"; } } } else if ("D".equals(str)) { int[] tmp_array = new int[n]; int max = 0; boolean found = false; for (int j = 0; j < n; j++) { if (chairs[j].equals("#")) { tmp_array[j] = Integer.MAX_VALUE; } else { tmp_array[j] = 0; found = true; } } if (found == false) { max = Integer.MAX_VALUE; } boolean fixed = false; for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (tmp_array[k] == j) { if (k != 0 && tmp_array[k - 1] > (j + 1)) { tmp_array[k - 1] = j + 1; fixed = true; } if (k != (n - 1) && tmp_array[k + 1] > (j + 1)) { tmp_array[k + 1] = j + 1; fixed = true; } } } if (fixed == false) { break; } else { fixed = false; max = j + 1; } } for (int j = 0; j < n; j++) { if (tmp_array[j] == max) { chairs[j] = "D"; break; } } } } for (int j = 0; j < n; j++) { System.out.print(chairs[j]); } System.out.println(); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); int m = sc.nextInt(); if (n == 0 && m == 0) { break; } String[] chairs = new String[n]; for (int i = 0; i < n; i++) { chairs[i] = "#"; } for (int i = 0; i < m; i++) { String str = sc.next(); if ("A".equals(str)) { for (int j = 0; j < n; j++) { if (chairs[j].equals("#")) { chairs[j] = "A"; break; } } } else if ("B".equals(str)) { boolean found = false; for (int j = n - 1; j >= 0; j--) { if (chairs[j].equals("#") && (j == (n - 1) || !chairs[j + 1].equals("A")) && (j == 0 || !chairs[j - 1].equals("A"))) { found = true; chairs[j] = "B"; break; } } if (found == false) { for (int j = 0; j < n; j++) { if (chairs[j].equals("#")) { chairs[j] = "B"; break; } } } } else if ("C".equals(str)) { boolean found = false; for (int j = 0; j < n; j++) { if (!chairs[j].equals("#")) { if (j != (n - 1) && chairs[j + 1].equals("#")) { found = true; chairs[j + 1] = "C"; break; } else if (j != 0 && chairs[j - 1].equals("#")) { found = true; chairs[j - 1] = "C"; break; } } } if (found == false) { if (n % 2 == 1) { chairs[(n + 1) / 2 - 1] = "C"; } else { chairs[n / 2 + 1 - 1] = "C"; } } } else if ("D".equals(str)) { int[] tmp_array = new int[n]; int max = 0; boolean found = false; for (int j = 0; j < n; j++) { if (chairs[j].equals("#")) { tmp_array[j] = Integer.MAX_VALUE; } else { tmp_array[j] = 0; found = true; } } if (found == false) { max = Integer.MAX_VALUE; } boolean fixed = false; for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (tmp_array[k] == j) { if (k != 0 && tmp_array[k - 1] > (j + 1)) { tmp_array[k - 1] = j + 1; fixed = true; } if (k != (n - 1) && tmp_array[k + 1] > (j + 1)) { tmp_array[k + 1] = j + 1; fixed = true; } } } if (fixed == false) { break; } else { fixed = false; max = j + 1; } } for (int j = 0; j < n; j++) { if (tmp_array[j] == max) { chairs[j] = "D"; break; } } } /* for (int j = 0; j < n; j++) { System.out.print(chairs[j]); } System.out.println(); */ } for (int j = 0; j < n; j++) { System.out.print(chairs[j]); } System.out.println(); } } }
[["+", 0, 1, 0, 11, 31, 504, 71, 16, 17, 33], ["+", 0, 1, 0, 11, 31, 504, 71, 16, 12, 499], ["+", 0, 11, 31, 504, 71, 16, 31, 16, 12, 499]]
3
864
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); int m = sc.nextInt(); if (n == 0 && m == 0) break; char[] a = new char[n + 2]; Arrays.fill(a, '#'); a[0] = 'X'; a[n + 1] = 'X'; for (int i = 0; i < m; i++) { char c = sc.next().charAt(0); if (c == 'A') { for (int j = 1; j <= n; j++) { if (a[j] == '#') { a[j] = c; break; } } } else if (c == 'B') { boolean flag = true; for (int j = n; j > 0; j--) { if (a[j] == '#' && a[j - 1] != 'A' && a[j + 1] != 'A') { a[j] = c; flag = false; break; } } if (flag == true) { for (int j = 1; j <= n; j++) { if (a[j] == '#') { a[j] = c; break; } } } } else if (c == 'C') { if (i == 0) { if (n % 2 == 0) a[(n + 1) / 2] = c; else a[n / 2 + 1] = c; } else { for (int j = 1; j <= n; j++) { if (a[j] != '#') { if (a[j + 1] == '#') { a[j + 1] = c; break; } if (a[j - 1] == '#') { a[j - 1] = c; break; } } } } } else if (c == 'D') { if (i == 0) { a[1] = c; } else { int max = -1; int p = -1; for (int j = 1; j <= n; j++) { if (a[j] == '#') { int min = Integer.MAX_VALUE; for (int k = j - 1; k >= 0; k--) { if (k == 0) break; if (a[k] != '#') { min = Math.min(min, (j - 1) - k); break; } } for (int k = j + 1; k <= n + 1; k++) { if (k == n + 1) break; if (a[k] != '#') { min = Math.min(min, k - (j + 1)); break; } } if (min > max) { max = min; p = j; } } } a[p] = c; } } } for (int i = 1; i <= n; i++) System.out.print(a[i]); System.out.println(); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); int m = sc.nextInt(); if (n == 0 && m == 0) break; char[] a = new char[n + 2]; Arrays.fill(a, '#'); a[0] = 'X'; a[n + 1] = 'X'; for (int i = 0; i < m; i++) { char c = sc.next().charAt(0); if (c == 'A') { for (int j = 1; j <= n; j++) { if (a[j] == '#') { a[j] = c; break; } } } else if (c == 'B') { boolean flag = true; for (int j = n; j > 0; j--) { if (a[j] == '#' && a[j - 1] != 'A' && a[j + 1] != 'A') { a[j] = c; flag = false; break; } } if (flag == true) { for (int j = 1; j <= n; j++) { if (a[j] == '#') { a[j] = c; break; } } } } else if (c == 'C') { if (i == 0) { if (n % 2 == 1) a[(n + 1) / 2] = c; else a[n / 2 + 1] = c; } else { for (int j = 1; j <= n; j++) { if (a[j] != '#') { if (a[j + 1] == '#') { a[j + 1] = c; break; } if (a[j - 1] == '#') { a[j - 1] = c; break; } } } } } else if (c == 'D') { if (i == 0) { a[1] = c; } else { int max = -1; int p = -1; for (int j = 1; j <= n; j++) { if (a[j] == '#') { int min = Integer.MAX_VALUE; for (int k = j - 1; k >= 0; k--) { if (k == 0) break; if (a[k] != '#') { min = Math.min(min, (j - 1) - k); break; } } for (int k = j + 1; k <= n + 1; k++) { if (k == n + 1) break; if (a[k] != '#') { min = Math.min(min, k - (j + 1)); break; } } if (min > max) { max = min; p = j; } } } a[p] = c; } } } for (int i = 1; i <= n; i++) System.out.print(a[i]); System.out.println(); } } }
[["-", 64, 196, 0, 57, 15, 15, 0, 16, 12, 499], ["+", 64, 196, 0, 57, 15, 15, 0, 16, 12, 499]]
3
673
n, m, p; char s[2], c[101]; A() { for (p = 0; c[p] != '#'; p++) ; } B() { for (p = n - 1; p >= 0; p--) if (c[p] == '#' && (p + 1 == n || c[p + 1] != 'A') && (p - 1 < 0 || c[p - 1] != 'A')) return; A(); } C() { for (p = 0; p < n; p++) { if (c[p] != '#') { if (p + 1 != n && c[p + 1] == '#') { p++; return; } if (p - 1 >= 0 && c[p - 1] == '#') { p--; return; } } } p = n / 2; } D() { int D[100], d = n, q; for (p = -1; ++p < n;) { d++; if (c[p] != '#') d = 0; D[p] = d; } for (d = 0; --p >= 0;) { d++; if (c[p] != '#') d = 0; if (d < D[p]) D[p] = d; } for (p = q = 0; q < n; q++) if (D[q] > D[p]) p = q; } main() { for (; scanf("%d%d", &n, &m), n;) { memset(c, '#', 100); c[n] = 0; for (; m--;) { scanf("%s", s); if (s[0] == 'A') A(); if (s[0] == 'B') B(); if (s[0] == 'C') C(); if (s[0] == 'D') D(); c[p] = s[0]; } puts(c); } exit(0); }
n, m, p; char s[2], c[101]; A() { for (p = 0; c[p] != '#'; p++) ; } B() { for (p = n - 1; p >= 0; p--) if (c[p] == '#' && (p + 1 == n || c[p + 1] != 'A') && (p - 1 < 0 || c[p - 1] != 'A')) return; A(); } C() { for (p = 0; p < n; p++) { if (c[p] != '#') { if (p + 1 != n && c[p + 1] == '#') { p++; return; } if (p - 1 >= 0 && c[p - 1] == '#') { p--; return; } } } p = n / 2; } D() { int D[100], d = n * 2, q; for (p = -1; ++p < n;) { d++; if (c[p] != '#') d = 0; D[p] = d; } for (d = n; --p >= 0;) { d++; if (c[p] != '#') d = 0; if (d < D[p]) D[p] = d; } for (p = q = 0; q < n; q++) if (D[q] > D[p]) p = q; } main() { for (; scanf("%d%d", &n, &m), n;) { memset(c, '#', 100); c[n] = 0; for (; m--;) { scanf("%s", s); if (s[0] == 'A') A(); if (s[0] == 'B') B(); if (s[0] == 'C') C(); if (s[0] == 'D') D(); c[p] = s[0]; } puts(c); } exit(0); }
[["+", 0, 9, 0, 43, 49, 50, 51, 16, 17, 48], ["+", 0, 9, 0, 43, 49, 50, 51, 16, 12, 13], ["-", 0, 30, 0, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 30, 0, 9, 0, 7, 10, 11, 12, 22]]
0
489
#include <iostream> using namespace std; int m, n, j, k, f, g; char c[101]; void acta() { for (j = 0; j < m; j++) if (c[j] == '#') { c[j] = 'A'; break; } } void actb() { f = m - 1; for (j = m - 1; j >= 0; j--) { if (c[j] != '#') continue; f = j; if (j == m - 1) { if (c[m - 2] != 'A') break; else continue; } if (j == 0) { if (c[1] != 'A') break; else continue; } if (c[j - 1] != 'A' && c[j + 1] != 'A') break; } c[f] = 'B'; } void actc() { if (g == 0) { c[(m - 1) / 2] = 'C'; return; } for (j = 0; j < m; j++) { if (c[j] != '#') { if (j < m - 1) if (c[j + 1] == '#') { c[j + 1] = 'C'; break; } if (j > 0) if (c[j - 1] == '#') { c[j - 1] = 'C'; break; } } } } void actd() { int l = 0, sx = 0, x = 0; f = 0; if (g == 0) { c[0] = 'D'; return; } for (j = 0; j < m; j++) { if (c[j] != '#') { if (f == 0) { x = 0; l = j; } else { if ((j - sx) / 2 > l) { l = (j - sx) / 2; x = sx + l; } } sx = j; f = 1; } else if (j == m - 1) if (j - sx > l) x = j; } c[x] = 'D'; } int main() { int i; char a; while (cin >> m >> n) { if (m + n == 0) break; for (i = 0; i < m; i++) c[i] = '#'; c[i] = '\0'; g = 0; for (i = 0; i < n; i++) { cin >> a; switch (a) { case 'A': acta(); break; case 'B': actb(); break; case 'C': actc(); break; case 'D': actd(); break; } g = 1; } cout << c << endl; } return 0; }
#include <iostream> using namespace std; int m, n, j, k, f, g; char c[101]; void acta() { for (j = 0; j < m; j++) if (c[j] == '#') { c[j] = 'A'; break; } } void actb() { f = m - 1; for (j = m - 1; j >= 0; j--) { if (c[j] != '#') continue; f = j; if (j == m - 1) { if (c[m - 2] != 'A') break; else continue; } if (j == 0) { if (c[1] != 'A') break; else continue; } if (c[j - 1] != 'A' && c[j + 1] != 'A') break; } c[f] = 'B'; } void actc() { if (g == 0) { c[m / 2] = 'C'; return; } for (j = 0; j < m; j++) { if (c[j] != '#') { if (j < m - 1) if (c[j + 1] == '#') { c[j + 1] = 'C'; break; } if (j > 0) if (c[j - 1] == '#') { c[j - 1] = 'C'; break; } } } } void actd() { int l = 0, sx = 0, x = 0; f = 0; if (g == 0) { c[0] = 'D'; return; } for (j = 0; j < m; j++) { if (c[j] != '#') { if (f == 0) { x = 0; l = j; } else { if ((j - sx) / 2 > l) { l = (j - sx) / 2; x = sx + l; } } sx = j; f = 1; } else if (j == m - 1) if (j - sx > l) x = j; } c[x] = 'D'; } int main() { int i; char a; while (cin >> m >> n) { if (m + n == 0) break; for (i = 0; i < m; i++) c[i] = '#'; c[i] = '\0'; g = 0; for (i = 0; i < n; i++) { cin >> a; switch (a) { case 'A': acta(); break; case 'B': actb(); break; case 'C': actc(); break; case 'D': actd(); break; } g = 1; } cout << c << endl; } return 0; }
[["-", 31, 69, 341, 342, 0, 16, 31, 23, 0, 24], ["-", 341, 342, 0, 16, 31, 23, 0, 16, 17, 33], ["-", 341, 342, 0, 16, 31, 23, 0, 16, 12, 13], ["-", 31, 69, 341, 342, 0, 16, 31, 23, 0, 25]]
1
636
#include <bits/stdc++.h> using namespace std; // 12:46 string chair; int goA(char c) { for (int i = 0; i < chair.size(); i++) if (chair[i] == '#') { chair[i] = c; return 1; } return 0; } int goB(char c) { for (int i = chair.size() - 1; i >= 0; i--) if (chair[i] == '#' && (!i || chair[i - 1] != 'A') && (i + 1 == chair.size() || chair[i + 1] != 'A')) { chair[i] = c; return 1; } return goA(c); } int goC(char c) { if (count(chair.begin(), chair.end(), '#') == chair.size()) { if (chair.size() % 2) chair[(1 + chair.size()) / 2 - 1] = c; else chair[chair.size() / 2] = c; return 1; } for (int i = 0; i < chair.size(); i++) if (chair[i] != '#') { for (int j = 1; j >= -1; j--) { if (i + j >= 0 && i + j < chair.size() && chair[i + j] == '#') { chair[i + j] = c; return 1; } } } return 0; } int goD(char c) { int pos; int dst = 2e9; for (int i = 0; i < chair.size(); i++) { if (chair[i] == '#') { int dist = 1e9; for (int j = 0; j < chair.size(); j++) if (chair[j] != '#') dist = min(dist, abs(i - j)); if (dst > dist) { dst = dist; pos = i; } } } chair[pos] = c; return 1; } int main() { int n, m; while (cin >> n >> m && n) { chair = string(n, '#'); for (int i = 0; i < m; i++) { char c; cin >> c; if (c == 'A') goA(c); if (c == 'B') goB(c); if (c == 'C') goC(c); if (c == 'D') goD(c); } cout << chair << endl; } }
#include <bits/stdc++.h> using namespace std; // 12:46 string chair; int goA(char c) { for (int i = 0; i < chair.size(); i++) if (chair[i] == '#') { chair[i] = c; return 1; } return 0; } int goB(char c) { for (int i = chair.size() - 1; i >= 0; i--) if (chair[i] == '#' && (!i || chair[i - 1] != 'A') && (i + 1 == chair.size() || chair[i + 1] != 'A')) { chair[i] = c; return 1; } return goA(c); } int goC(char c) { if (count(chair.begin(), chair.end(), '#') == chair.size()) { if (chair.size() % 2) chair[(1 + chair.size()) / 2 - 1] = c; else chair[chair.size() / 2] = c; return 1; } for (int i = 0; i < chair.size(); i++) if (chair[i] != '#') { for (int j = 1; j >= -1; j--) { if (i + j >= 0 && i + j < chair.size() && chair[i + j] == '#') { chair[i + j] = c; return 1; } } } return 0; } int goD(char c) { int pos; int dst = -2e9; for (int i = 0; i < chair.size(); i++) { if (chair[i] == '#') { int dist = 1e9; for (int j = 0; j < chair.size(); j++) if (chair[j] != '#') dist = min(dist, abs(i - j)); if (dst < dist) { dst = dist; pos = i; } } } chair[pos] = c; return 1; } int main() { int n, m; while (cin >> n >> m && n) { chair = string(n, '#'); for (int i = 0; i < m; i++) { char c; cin >> c; if (c == 'A') goA(c); if (c == 'B') goB(c); if (c == 'C') goC(c); if (c == 'D') goD(c); } cout << chair << endl; } }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["-", 64, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 17, 18]]
1
572
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define FFOR(i, a, b) for (int i = (a); i <= (b); i++) #define REP(i, b) FOR(i, 0, b) #define RREP(i, b) FFOR(i, 1, b) #define PB push_back #define F first #define S second #define BE(c) c.begin(), c.end() using namespace std; typedef long long LL; typedef LL ut; typedef long double ld; typedef pair<ut, ut> pr; typedef vector<pr> Vpr; typedef vector<ut> VI; typedef pair<ut, pr> ppr; typedef priority_queue<pr, Vpr, greater<pr>> PQ; const int SIZE = 5 + 3 * 1e+4; const ut INF = 1 << 30; const ld eps = 1e-6; const LL p = 7 + 1e+9; int main() { int n, m; while (cin >> n >> m && (n || m)) { char c; int state[102] = {0}; state[0] = state[n + 1] = -1; REP(i, m) { cin >> c; if (c == 'A') { RREP(j, n) { if (state[j] == 0) { state[j] = 1; break; } } } else if (c == 'B') { bool ok = false; for (int j = n; j > 0; --j) { if (state[j] == 0 && state[j + 1] != 1 && state[j - 1] != 1) { state[j] = 2; ok = true; break; } } if (!ok) { RREP(j, n) { if (state[j] == 0) { state[j] = 2; break; } } } } else if (c == 'C') { if (i) { RREP(j, n) { if (state[j] != 0) { if (state[j + 1] == 0) { state[j + 1] = 3; break; } else if (state[j - 1] == 0) { state[j - 1] = 3; break; } } } } else { if (n % 2) { state[(n + 1) / 2] = 3; } else { state[n / 2 + 1] = 3; } } } else { if (i) { int left = 500, right, tmp; RREP(j, n) { if (state[j] != 0) { right = j; break; } } int maxium = 0, pos = 0; RREP(j, n) { if (state[j] == 0) { int t = min(j - left, right - j); if (maxium < t) { maxium = t; pos = j; } } if (right <= j) { left = right; bool discovered = false; FFOR(k, j + 1, n) { if (state[k] != 0) { right = k; discovered = true; break; } } if (!discovered) { right = 500; } } } state[pos] = 4; } else { state[1] = 4; } } } RREP(i, n) { if (state[i] == 0) cout << "#"; else if (state[i] == 1) cout << "A"; else if (state[i] == 2) cout << "B"; else if (state[i] == 3) cout << "C"; else if (state[i] == 4) cout << "D"; } cout << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define FFOR(i, a, b) for (int i = (a); i <= (b); i++) #define REP(i, b) FOR(i, 0, b) #define RREP(i, b) FFOR(i, 1, b) #define PB push_back #define F first #define S second #define BE(c) c.begin(), c.end() using namespace std; typedef long long LL; typedef LL ut; typedef long double ld; typedef pair<ut, ut> pr; typedef vector<pr> Vpr; typedef vector<ut> VI; typedef pair<ut, pr> ppr; typedef priority_queue<pr, Vpr, greater<pr>> PQ; const int SIZE = 5 + 3 * 1e+4; const ut INF = 1 << 30; const ld eps = 1e-6; const LL p = 7 + 1e+9; int main() { int n, m; while (cin >> n >> m && (n || m)) { char c; int state[102] = {0}; state[0] = state[n + 1] = -1; REP(i, m) { cin >> c; if (c == 'A') { RREP(j, n) { if (state[j] == 0) { state[j] = 1; break; } } } else if (c == 'B') { bool ok = false; for (int j = n; j > 0; --j) { if (state[j] == 0 && state[j + 1] != 1 && state[j - 1] != 1) { state[j] = 2; ok = true; break; } } if (!ok) { RREP(j, n) { if (state[j] == 0) { state[j] = 2; break; } } } } else if (c == 'C') { if (i) { RREP(j, n) { if (state[j] != 0) { if (state[j + 1] == 0) { state[j + 1] = 3; break; } else if (state[j - 1] == 0) { state[j - 1] = 3; break; } } } } else { if (n % 2) { state[(n + 1) / 2] = 3; } else { state[n / 2 + 1] = 3; } } } else { if (i) { int left = -500, right, tmp; RREP(j, n) { if (state[j] != 0) { right = j; break; } } int maxium = 0, pos = 0; RREP(j, n) { if (state[j] == 0) { int t = min(j - left, right - j); if (maxium < t) { maxium = t; pos = j; } } if (right <= j) { left = right; bool discovered = false; FFOR(k, j + 1, n) { if (state[k] != 0) { right = k; discovered = true; break; } } if (!discovered) { right = 500; } } } state[pos] = 4; } else { state[1] = 4; } } } RREP(i, n) { if (state[i] == 0) cout << "#"; else if (state[i] == 1) cout << "A"; else if (state[i] == 2) cout << "B"; else if (state[i] == 3) cout << "C"; else if (state[i] == 4) cout << "D"; } cout << endl; } }
[["-", 0, 57, 64, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 57, 64, 9, 0, 43, 49, 50, 51, 13]]
1
774
#include <bits/stdc++.h> using namespace std; int n, m; char chair[200]; void A(char ch) { int i = 1; while (chair[i] != '#') i++; chair[i] = ch; } void B() { int i = n; while (chair[i] != '#') i--; chair[i] = 'B'; } void C(int kai) { if (kai == 0) { if (n % 2) chair[(n + 1) / 2] = 'C'; else chair[n / 2 + 1] = 'C'; return; } int f = 1; for (int i = 1; i <= n; i++) { if (chair[i] == '#') continue; if (chair[i - 1] == '#') chair[i - 1] = 'C', i = n, f = 0; else if (chair[i + 1] == '#') chair[i + 1] = 'C', i = n, f = 0; } // assert(f); } void D() { int R = 0, L = 0; while (chair[n - R] == '#') R++; while (chair[L + 1] == '#') L++; int M = 0, idx = 0, cnt = 0; for (int i = 1; i <= n; i++) { if (chair[i] == '#') cnt++; else cnt = 0; if (M + M % 2 < cnt) M = cnt, idx = i; } if (M <= 2 && L <= 1 && R <= 1) A('D'); else if (L - 1 >= (M - 1) / 2 && L - 1 >= R - 1) chair[1] = 'D'; else if (R - 1 > (M - 1) / 2) chair[n] = 'D'; else chair[(idx - M + 1) + (M - 1) / 2] = 'D'; } int main() { while (1) { cin >> n >> m; if (!n && !m) break; memset(chair, '#', sizeof(chair)); chair[0] = chair[n + 1] = 0; for (int i = 0; i < m; i++) { char a; cin >> a; if (a == 'A') A(a); if (a == 'B') B(); if (a == 'C') C(i); if (a == 'D') D(); } int cnt = 0; for (int i = 1; i <= n; i++) if (chair[i] != '#') cnt++; assert(cnt == m); printf("%s\n", chair + 1); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; char chair[200]; void A(char ch) { int i = 1; while (chair[i] != '#') i++; chair[i] = ch; } void B() { int i = n; while (chair[i] != '#') i--; chair[i] = 'B'; } void C(int kai) { if (kai == 0) { if (n % 2) chair[(n + 1) / 2] = 'C'; else chair[n / 2 + 1] = 'C'; return; } int f = 1; for (int i = 1; i <= n; i++) { if (chair[i] == '#') continue; if (chair[i + 1] == '#') chair[i + 1] = 'C', i = n, f = 0; else if (chair[i - 1] == '#') chair[i - 1] = 'C', i = n, f = 0; } // assert(f); } void D() { int R = 0, L = 0; while (chair[n - R] == '#') R++; while (chair[L + 1] == '#') L++; int M = 0, idx = 0, cnt = 0; for (int i = 1; i <= n; i++) { if (chair[i] == '#') cnt++; else cnt = 0; if (M + M % 2 < cnt) M = cnt, idx = i; } if (M <= 2 && L <= 1 && R <= 1) A('D'); else if (L - 1 >= (M - 1) / 2 && L - 1 >= R - 1) chair[1] = 'D'; else if (R - 1 > (M - 1) / 2) chair[n] = 'D'; else chair[(idx - M + 1) + (M - 1) / 2] = 'D'; } int main() { while (1) { cin >> n >> m; if (!n && !m) break; memset(chair, '#', sizeof(chair)); chair[0] = chair[n + 1] = 0; for (int i = 0; i < m; i++) { char a; cin >> a; if (a == 'A') A(a); if (a == 'B') B(); if (a == 'C') C(i); if (a == 'D') D(); } int cnt = 0; for (int i = 1; i <= n; i++) if (chair[i] != '#') cnt++; assert(cnt == m); printf("%s\n", chair + 1); } return 0; }
[["-", 51, 16, 31, 69, 341, 342, 0, 16, 17, 33], ["+", 51, 16, 31, 69, 341, 342, 0, 16, 17, 72], ["-", 31, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["+", 31, 11, 31, 69, 341, 342, 0, 16, 17, 72], ["-", 51, 16, 31, 69, 341, 342, 0, 16, 17, 72], ["+", 51, 16, 31, 69, 341, 342, 0, 16, 17, 33], ["-", 31, 11, 31, 69, 341, 342, 0, 16, 17, 72], ["+", 31, 11, 31, 69, 341, 342, 0, 16, 17, 33]]
1
636
#include <iostream> int n, m; int chair[105]; int dist(int k) { int ret = 1000; for (int i = k; i <= n; ++i) { if (chair[i]) { ret = i - k; } } for (int i = k; i >= 1; --i) { if (chair[i]) { ret = std::min(ret, k - i); } } return ret; } int main() { while (true) { for (int i = 0; i < 105; ++i) chair[i] = 0; std::cin >> n >> m; if (n == 0 && m == 0) break; for (int i = 0; i < m; ++i) { char ch; std::cin >> ch; if (ch == 'A') { for (int j = 1; j <= n; ++j) { if (chair[j] == 0) { chair[j] = 1; break; } } } else if (ch == 'B') { bool f = false; for (int j = n; j >= 1; --j) { if (chair[j] == 0 && chair[j - 1] != 1 && chair[j + 1] != 1) { chair[j] = 2; f = true; break; } } if (!f) { for (int j = 1; j <= n; ++j) { if (chair[j] == 0) { chair[j] = 2; break; } } } } else if (ch == 'C') { if (i == 0) chair[n % 2 ? ((n + 1) / 2) : (n / 2 + 1)] = 3; else { for (int j = 1; j <= n; ++j) { if (chair[j]) { if (j + 1 <= n && chair[j + 1] == 0) { chair[j + 1] = 3; break; } if (j - 1 >= 1 && chair[j - 1] == 0) { chair[j - 1] = 3; break; } } } } } else if (ch == 'D') { if (i == 0) chair[1] = 4; else { int maxV = -1000, maxI; for (int j = 1; j <= n; ++j) { if (chair[j] == 0) { if (dist(j) > maxV) maxV = dist(j), maxI = j; } } chair[maxI] = 4; } } } for (int i = 1; i <= n; ++i) { if (chair[i] == 0) std::cout << "#"; else std::cout << (char)('A' + chair[i] - 1); } std::cout << std::endl; } return 0; }
#include <iostream> int n, m; int chair[105]; int dist(int k) { int ret = 1000; for (int i = k; i <= n; ++i) { if (chair[i]) { ret = i - k; break; } } for (int i = k; i >= 1; --i) { if (chair[i]) { ret = std::min(ret, k - i); break; } } return ret; } int main() { while (true) { for (int i = 0; i < 105; ++i) chair[i] = 0; std::cin >> n >> m; if (n == 0 && m == 0) break; for (int i = 0; i < m; ++i) { char ch; std::cin >> ch; if (ch == 'A') { for (int j = 1; j <= n; ++j) { if (chair[j] == 0) { chair[j] = 1; break; } } } else if (ch == 'B') { bool f = false; for (int j = n; j >= 1; --j) { if (chair[j] == 0 && chair[j - 1] != 1 && chair[j + 1] != 1) { chair[j] = 2; f = true; break; } } if (!f) { for (int j = 1; j <= n; ++j) { if (chair[j] == 0) { chair[j] = 2; break; } } } } else if (ch == 'C') { if (i == 0) chair[n % 2 ? ((n + 1) / 2) : (n / 2 + 1)] = 3; else { for (int j = 1; j <= n; ++j) { if (chair[j]) { if (j + 1 <= n && chair[j + 1] == 0) { chair[j + 1] = 3; break; } if (j - 1 >= 1 && chair[j - 1] == 0) { chair[j - 1] = 3; break; } } } } } else if (ch == 'D') { if (i == 0) chair[1] = 4; else { int maxV = -1000, maxI; for (int j = 1; j <= n; ++j) { if (chair[j] == 0) { if (dist(j) > maxV) maxV = dist(j), maxI = j; } } chair[maxI] = 4; } } } for (int i = 1; i <= n; ++i) { if (chair[i] == 0) std::cout << "#"; else std::cout << (char)('A' + chair[i] - 1); } std::cout << std::endl; } return 0; }
[["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 35], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35]]
1
624
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; const ld eps = 1e-9; //// < "d:\d_download\visual studio ///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual ///studio 2015\projects\programing_contest_c++\debug\b.txt" void sit(vector<int> &seats, int who) { if (who == 1) { for (int i = 0; i < seats.size(); ++i) { if (!seats[i]) { seats[i] = who; return; } } } else if (who == 2) { for (int i = seats.size() - 1; i >= 0; --i) { if (!seats[i]) { bool ok = true; for (int k = i + 1; k >= i - 1; k -= 2) { if (k >= 0 && k < seats.size()) { if (seats[k] == 1) ok = false; } } if (ok) { seats[i] = who; return; } } } for (int i = 0; i < seats.size(); ++i) { if (!seats[i]) { seats[i] = who; return; } } } else if (who == 3) { for (int i = 0; i < seats.size(); ++i) { if (seats[i]) { if (i != seats.size() - 1 && !seats[i + 1]) { seats[i + 1] = who; return; } else if (i != 0 && !seats[i - 1]) { seats[i - 1] = who; return; } } } seats[seats.size() / 2] = who; return; } else { vector<int> diss(seats.size(), 1000); for (int i = 0; i < seats.size(); ++i) { if (seats[i]) { diss[i] = 0; for (int j = i - 1; j >= 0; --j) { diss[j] = min(diss[j], i - j); } for (int j = i + 1; j < seats.size(); ++j) { diss[j] = min(diss[j], j - i); } } } int maxdis = -1; int ans = -1; for (int i = 0; i < seats.size(); ++i) { if (maxdis < diss[i]) { maxdis = diss[i]; ans = i; } } seats[ans] = who; return; } assert(false); } int main() { while (1) { int N, M; cin >> N >> M; if (!N) break; vector<int> seats(N); for (int i = 0; i < M; ++i) { char c; cin >> c; int num = c - 'A' + 1; sit(seats, num); for (int i = 0; i < N; ++i) { if (seats[i]) { cout << char('A' + seats[i] - 1); } else { cout << '#'; } } cout << endl; } } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; const ld eps = 1e-9; //// < "d:\d_download\visual studio ///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual ///studio 2015\projects\programing_contest_c++\debug\b.txt" void sit(vector<int> &seats, int who) { if (who == 1) { for (int i = 0; i < seats.size(); ++i) { if (!seats[i]) { seats[i] = who; return; } } } else if (who == 2) { for (int i = seats.size() - 1; i >= 0; --i) { if (!seats[i]) { bool ok = true; for (int k = i + 1; k >= i - 1; k -= 2) { if (k >= 0 && k < seats.size()) { if (seats[k] == 1) ok = false; } } if (ok) { seats[i] = who; return; } } } for (int i = 0; i < seats.size(); ++i) { if (!seats[i]) { seats[i] = who; return; } } } else if (who == 3) { for (int i = 0; i < seats.size(); ++i) { if (seats[i]) { if (i != seats.size() - 1 && !seats[i + 1]) { seats[i + 1] = who; return; } else if (i != 0 && !seats[i - 1]) { seats[i - 1] = who; return; } } } seats[seats.size() / 2] = who; return; } else { vector<int> diss(seats.size(), 1000); for (int i = 0; i < seats.size(); ++i) { if (seats[i]) { diss[i] = 0; for (int j = i - 1; j >= 0; --j) { diss[j] = min(diss[j], i - j); } for (int j = i + 1; j < seats.size(); ++j) { diss[j] = min(diss[j], j - i); } } } int maxdis = -1; int ans = -1; for (int i = 0; i < seats.size(); ++i) { if (maxdis < diss[i]) { maxdis = diss[i]; ans = i; } } seats[ans] = who; return; } assert(false); } int main() { while (1) { int N, M; cin >> N >> M; if (!N) break; vector<int> seats(N); for (int i = 0; i < M; ++i) { char c; cin >> c; int num = c - 'A' + 1; sit(seats, num); } for (int i = 0; i < N; ++i) { if (seats[i]) { cout << char('A' + seats[i] - 1); } else { cout << '#'; } } cout << endl; } return 0; }
[["+", 0, 52, 8, 9, 0, 7, 8, 9, 0, 46], ["-", 0, 52, 8, 9, 0, 7, 8, 9, 0, 46]]
1
686
#include <algorithm> #include <cassert> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; int ans[111]; int calc(int x, int n) { int l = 0; for (; l <= x; l++) { if (ans[x - l] != -1) break; } if (l == x + 1) l = n; int r = 0; for (; r + x < n; r++) { if (ans[r + x] != -1) break; } if (r + x == n) r = n; return min(l, r); } int main() { int N, M; while (cin >> N >> M) { if (N == 0 && M == 0) break; memset(ans, -1, sizeof(ans)); for (int t = 0; t < M; t++) { char c; cin >> c; if (c == 'A') { for (int i = 0; i < N; i++) { if (ans[i] == -1) { ans[i] = 0; break; } } } if (c == 'B') { bool done = false; for (int i = N - 1; i >= 0; i--) if (ans[i] == -1) { if (ans[i + 1] != 0) { if (i == 0 || ans[i - 1] != 0) { done = true; ans[i] = 1; break; } } } if (!done) { for (int i = 0; i < N; i++) { if (ans[i] == -1) { ans[i] = 1; break; } } } } if (c == 'C') { if (t == 0) { ans[(N + 1) / 2] = 2; } else { for (int i = 0; i < N; i++) { if (ans[i] != -1) { if (i + 1 < N && ans[i + 1] == -1) { ans[i + 1] = 2; break; } else if (i > 0 && ans[i - 1] == -1) { ans[i - 1] = 2; break; } } } } } if (c == 'D') { if (t == 0) { ans[0] = 3; } else { int index = 0, maxi = 0; for (int i = 0; i < N; i++) if (ans[i] == -1) { int tmp = calc(i, N); if (tmp > maxi) { maxi = tmp; index = i; } } ans[index] = 3; } } } for (int i = 0; i < N; i++) { if (ans[i] == 0) cout << 'A'; else if (ans[i] == 1) cout << 'B'; else if (ans[i] == 2) cout << 'C'; else if (ans[i] == 3) cout << 'D'; else cout << '#'; } cout << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; int ans[111]; int calc(int x, int n) { int l = 0; for (; l <= x; l++) { if (ans[x - l] != -1) break; } if (l == x + 1) l = n; int r = 0; for (; r + x < n; r++) { if (ans[r + x] != -1) break; } if (r + x == n) r = n; return min(l, r); } int main() { int N, M; while (cin >> N >> M) { if (N == 0 && M == 0) break; memset(ans, -1, sizeof(ans)); for (int t = 0; t < M; t++) { char c; cin >> c; if (c == 'A') { for (int i = 0; i < N; i++) { if (ans[i] == -1) { ans[i] = 0; break; } } } if (c == 'B') { bool done = false; for (int i = N - 1; i >= 0; i--) if (ans[i] == -1) { if (ans[i + 1] != 0) { if (i == 0 || ans[i - 1] != 0) { done = true; ans[i] = 1; break; } } } if (!done) { for (int i = 0; i < N; i++) { if (ans[i] == -1) { ans[i] = 1; break; } } } } if (c == 'C') { if (t == 0) { ans[N / 2] = 2; } else { for (int i = 0; i < N; i++) { if (ans[i] != -1) { if (i + 1 < N && ans[i + 1] == -1) { ans[i + 1] = 2; break; } else if (i > 0 && ans[i - 1] == -1) { ans[i - 1] = 2; break; } } } } } if (c == 'D') { if (t == 0) { ans[0] = 3; } else { int index = 0, maxi = 0; for (int i = 0; i < N; i++) if (ans[i] == -1) { int tmp = calc(i, N); if (tmp > maxi) { maxi = tmp; index = i; } } ans[index] = 3; } } } for (int i = 0; i < N; i++) { if (ans[i] == 0) cout << 'A'; else if (ans[i] == 1) cout << 'B'; else if (ans[i] == 2) cout << 'C'; else if (ans[i] == 3) cout << 'D'; else cout << '#'; } cout << endl; } return 0; }
[["-", 31, 69, 341, 342, 0, 16, 31, 23, 0, 24], ["-", 341, 342, 0, 16, 31, 23, 0, 16, 17, 72], ["-", 341, 342, 0, 16, 31, 23, 0, 16, 12, 13], ["-", 31, 69, 341, 342, 0, 16, 31, 23, 0, 25]]
1
712
#include <algorithm> #include <iostream> #include <string> using namespace std; int N, M; int a[100]; void sit_from_left(int id) { for (int i = 0; i < N; ++i) { if (a[i] == -1) { a[i] = id; break; } } } void sit_a() { sit_from_left(0); } void sit_b() { bool sat = false; ; for (int i = N - 1; i >= 0; --i) { if (a[i] != -1) { continue; } if (i > 0 && a[i - 1] == 0) { continue; } if (i < N - 1 && a[i + 1] == 0) { continue; } a[i] = 1; sat = true; break; } if (sat) { return; } sit_from_left(1); } void sit_c() { bool sat = false; for (int i = 0; i < N; ++i) { if (a[i] == -1) { continue; } if (i < N - 1 && a[i + 1] == -1) { a[i + 1] = 2; sat = true; break; } if (i > 0 && a[i - 1] == -1) { a[i - 1] = 2; sat = true; break; } } if (sat) { return; } a[N / 2 + 1] = 2; } void sit_d() { int m = 0; int mi = 0; for (int i = 0; i < N; ++i) { if (a[i] != -1) { continue; } int l = N; int r = N; for (int j = i; j >= 0; --j) { if (a[j] != -1) { l = i - j; break; } } for (int j = i; j < N; ++j) { if (a[j] != -1) { r = j - i; break; } } if (m < min(l, r)) { m = min(l, r); mi = i; } } a[mi] = 3; } int main() { while (1) { cin >> N >> M; if (!N && !M) { break; } fill(a, a + N, -1); for (int i = 0; i < M; ++i) { string s; cin >> s; if (s == "A") { sit_a(); } else if (s == "B") { sit_b(); } else if (s == "C") { sit_c(); } else if (s == "D") { sit_d(); } } for (int i = 0; i < N; ++i) { if (a[i] < 0) { cout << (char)'#'; } else { cout << (char)('A' + a[i]); } } cout << endl; } return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; int N, M; int a[100]; void sit_from_left(int id) { for (int i = 0; i < N; ++i) { if (a[i] == -1) { a[i] = id; break; } } } void sit_a() { sit_from_left(0); } void sit_b() { bool sat = false; ; for (int i = N - 1; i >= 0; --i) { if (a[i] != -1) { continue; } if (i > 0 && a[i - 1] == 0) { continue; } if (i < N - 1 && a[i + 1] == 0) { continue; } a[i] = 1; sat = true; break; } if (sat) { return; } sit_from_left(1); } void sit_c() { bool sat = false; for (int i = 0; i < N; ++i) { if (a[i] == -1) { continue; } if (i < N - 1 && a[i + 1] == -1) { a[i + 1] = 2; sat = true; break; } if (i > 0 && a[i - 1] == -1) { a[i - 1] = 2; sat = true; break; } } if (sat) { return; } a[N / 2] = 2; } void sit_d() { int m = 0; int mi = 0; for (int i = 0; i < N; ++i) { if (a[i] != -1) { continue; } int l = N; int r = N; for (int j = i; j >= 0; --j) { if (a[j] != -1) { l = i - j; break; } } for (int j = i; j < N; ++j) { if (a[j] != -1) { r = j - i; break; } } if (m < min(l, r)) { m = min(l, r); mi = i; } } a[mi] = 3; } int main() { while (1) { cin >> N >> M; if (!N && !M) { break; } fill(a, a + N, -1); for (int i = 0; i < M; ++i) { string s; cin >> s; if (s == "A") { sit_a(); } else if (s == "B") { sit_b(); } else if (s == "C") { sit_c(); } else if (s == "D") { sit_d(); } } for (int i = 0; i < N; ++i) { if (a[i] < 0) { cout << (char)'#'; } else { cout << (char)('A' + a[i]); } } cout << endl; } return 0; }
[["-", 0, 11, 31, 69, 341, 342, 0, 16, 17, 72], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
1
653
#include <algorithm> #include <cmath> #include <queue> #include <stack> #include <stdio.h> #include <vector> using namespace std; struct Info { Info() { country = '#'; } char country; }; void func(int n, int m) { Info info[n]; char tmp[2]; int maximum, most_left_index, pre_index, div, left_dist; bool FLG; for (int loop = 0; loop < m; loop++) { scanf("%s", tmp); switch (tmp[0]) { case 'A': for (int i = 0; i < n; i++) { if (info[i].country == '#') { info[i].country = 'A'; break; } } break; case 'B': FLG = false; if (info[n - 1].country == '#' && info[n - 2].country != 'A') { info[n - 1].country = 'B'; break; } for (int i = n - 2; i >= 1; i--) { if (info[i].country == '#' && info[i - 1].country != 'A' && info[i + 1].country != 'A') { info[i].country = 'B'; FLG = true; break; } } if (FLG) break; if (info[0].country == '#' && info[1].country != 'A') { info[0].country = 'B'; break; } for (int i = 0; i < n; i++) { if (info[i].country == '#') { info[i].country = 'B'; break; } } break; case 'C': if (loop == 0) { if (n % 2 == 1) { info[(n + 1) / 2].country = 'C'; } else { info[(n / 2) + 1].country = 'C'; } } else { FLG = false; if (info[0].country != '#' && info[1].country == '#') { info[1].country = 'C'; break; } for (int i = 1; i <= n - 2; i++) { if (info[i].country != '#') { if (info[i + 1].country == '#') { info[i + 1].country = 'C'; FLG = true; break; } else if (info[i - 1].country == '#') { info[i - 1].country = 'C'; FLG = true; break; } } if (FLG) break; } if (FLG) break; if (info[n - 1].country != '#' && info[n - 2].country == '#') { info[n - 2].country = 'C'; break; } } break; case 'D': if (loop == 0) { info[0].country = 'D'; } else { maximum = 0; most_left_index = -1; pre_index = -1; if (info[0].country == '#') { most_left_index = 0; for (div = 1; div <= n - 1; div++) { if (info[div].country != '#') { break; } } maximum = div; } else { pre_index = 0; } for (int i = 1; i <= n - 1; i++) { if (info[i].country == '#') { left_dist = i - pre_index; if (i <= n - 2) { for (div = 1; i + div <= n - 1; div++) { if (info[i + div].country != '#') { break; } } } else { // i == n-1 div = left_dist; } div = min(left_dist, div); if (div > maximum) { maximum = div; most_left_index = i; } } else { pre_index = i; } } info[most_left_index].country = 'D'; } break; } } for (int i = 0; i < n; i++) printf("%c", info[i].country); printf("\n"); } int main() { int n, m; while (true) { scanf("%d %d", &n, &m); if (n == 0 && m == 0) break; func(n, m); } return 0; }
#include <algorithm> #include <cmath> #include <queue> #include <stack> #include <stdio.h> #include <vector> using namespace std; struct Info { Info() { country = '#'; } char country; }; void func(int n, int m) { Info info[n]; char tmp[2]; int maximum, most_left_index, pre_index, div, left_dist; bool FLG; for (int loop = 0; loop < m; loop++) { scanf("%s", tmp); switch (tmp[0]) { case 'A': for (int i = 0; i < n; i++) { if (info[i].country == '#') { info[i].country = 'A'; break; } } break; case 'B': FLG = false; if (info[n - 1].country == '#' && info[n - 2].country != 'A') { info[n - 1].country = 'B'; break; } for (int i = n - 2; i >= 1; i--) { if (info[i].country == '#' && info[i - 1].country != 'A' && info[i + 1].country != 'A') { info[i].country = 'B'; FLG = true; break; } } if (FLG) break; if (info[0].country == '#' && info[1].country != 'A') { info[0].country = 'B'; break; } for (int i = 0; i < n; i++) { if (info[i].country == '#') { info[i].country = 'B'; break; } } break; case 'C': if (loop == 0) { if (n % 2 == 1) { info[((n + 1) / 2) - 1].country = 'C'; } else { info[(n / 2)].country = 'C'; } } else { FLG = false; if (info[0].country != '#' && info[1].country == '#') { info[1].country = 'C'; break; } for (int i = 1; i <= n - 2; i++) { if (info[i].country != '#') { if (info[i + 1].country == '#') { info[i + 1].country = 'C'; FLG = true; break; } else if (info[i - 1].country == '#') { info[i - 1].country = 'C'; FLG = true; break; } } if (FLG) break; } if (FLG) break; if (info[n - 1].country != '#' && info[n - 2].country == '#') { info[n - 2].country = 'C'; break; } } break; case 'D': if (loop == 0) { info[0].country = 'D'; } else { maximum = 0; most_left_index = -1; pre_index = -1; if (info[0].country == '#') { most_left_index = 0; for (div = 1; div <= n - 1; div++) { if (info[div].country != '#') { break; } } maximum = div; } else { pre_index = 0; } for (int i = 1; i <= n - 1; i++) { if (info[i].country == '#') { left_dist = i - pre_index; if (i <= n - 2) { for (div = 1; i + div <= n - 1; div++) { if (info[i + div].country != '#') { break; } } } else { // i == n-1 div = left_dist; } div = min(left_dist, div); if (div > maximum) { maximum = div; most_left_index = i; } } else { pre_index = i; } } info[most_left_index].country = 'D'; } break; } } for (int i = 0; i < n; i++) printf("%c", info[i].country); printf("\n"); } int main() { int n, m; while (true) { scanf("%d %d", &n, &m); if (n == 0 && m == 0) break; func(n, m); } return 0; }
[["+", 0, 16, 31, 23, 0, 16, 31, 23, 0, 24], ["+", 28, 69, 341, 342, 0, 16, 31, 23, 0, 25], ["+", 31, 118, 28, 69, 341, 342, 0, 16, 17, 33], ["+", 31, 118, 28, 69, 341, 342, 0, 16, 12, 13], ["-", 31, 118, 28, 69, 341, 342, 0, 16, 17, 72], ["-", 31, 118, 28, 69, 341, 342, 0, 16, 12, 13]]
1
979
#include <algorithm> #include <cstdio> #define N 11 #define inf 10000000 using namespace std; int d[N][N], V; void warshall_floyd(void) { for (int k = 0; k <= V; k++) { for (int i = 0; i <= V; i++) { for (int j = 0; j <= V; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } return; } void init(void) { for (int i = 0; i < N; i++) fill(d[i], d[i] + N, inf); for (int i = 0; i < N; i++) d[i][i] = 0; return; } int main(void) { int n; while (scanf("%d", &n) == 1) { if (n == 0) break; init(); int s, e, c; V = 0; for (int i = 0; i < n; i++) { scanf("%d%d%d", &s, &e, &c); d[s][e] = d[e][s] = c; V = max(s, e); } warshall_floyd(); int ans = inf; int sum = 0; int id = 0; for (int i = 0; i <= V; i++) { sum = 0; for (int j = 0; j <= V; j++) sum += d[i][j]; if (sum < ans) { ans = sum; id = i; } } printf("%d %d\n", id, ans); } return 0; }
#include <algorithm> #include <cstdio> #define N 11 #define inf 10000000 using namespace std; int d[N][N], V; void warshall_floyd(void) { for (int k = 0; k <= V; k++) { for (int i = 0; i <= V; i++) { for (int j = 0; j <= V; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } return; } void init(void) { for (int i = 0; i < N; i++) fill(d[i], d[i] + N, inf); for (int i = 0; i < N; i++) d[i][i] = 0; return; } int main(void) { int n; while (scanf("%d", &n) == 1) { if (n == 0) break; init(); int s, e, c; V = 0; for (int i = 0; i < n; i++) { scanf("%d%d%d", &s, &e, &c); d[s][e] = d[e][s] = c; V = max(V, max(s, e)); } warshall_floyd(); int ans = inf; int sum = 0; int id = 0; for (int i = 0; i <= V; i++) { sum = 0; for (int j = 0; j <= V; j++) sum += d[i][j]; if (sum < ans) { ans = sum; id = i; } } printf("%d %d\n", id, ans); } return 0; }
[["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 22], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 21], ["+", 0, 11, 12, 2, 3, 4, 0, 2, 63, 22], ["+", 12, 2, 3, 4, 0, 2, 3, 4, 0, 24], ["+", 12, 2, 3, 4, 0, 2, 3, 4, 0, 25]]
1
381
#include <algorithm> #include <cstring> #include <iostream> using namespace std; int n, m, a, b, c, _end; int x[10][10]; int bfs(int p, int a) { int s[10], minx = 114514, sum; sum = 0; memset(s, 127, sizeof(s)); s[a] = 0; for (int l = 0; l < p; l++) { for (int o = 0; o < p; o++) { if (s[o] < 114514) { for (int q = 0; q < p; q++) { if (x[o][q] < 114514) { s[q] = min(s[q], s[o] + x[o][q]); } } } } } for (int l = 0; l < p; l++) { sum += s[l]; } minx = min(minx, sum); return minx; } int search(int p) { int t[10], maxn = 114514, maxid; for (int k = 0; k < p; k++) { t[k] = bfs(p, k); } for (int k = 0; k < p; k++) { if (maxn > t[k]) { maxn = t[k]; maxid = k; } } return maxn * 100 + maxid; } void _cin() { memset(x, 51, sizeof(x)); n = 0; _end = 0; cin >> m; if (m == 0) { _end = 1; return; } for (int i = 0; i < m; i++) { cin >> a >> b >> c; n = max(n, max(a, b)); x[a][b] = c; x[b][a] = c; } n += 1; } int main() { while (true) { _cin(); if (_end == 1) { break; } cout << search(n) % 10 << ' ' << search(n) / 10 << endl; } }
#include <algorithm> #include <cstring> #include <iostream> using namespace std; int n, m, a, b, c, _end; int x[10][10]; int bfs(int p, int a) { int s[10], minx = 114514, sum; sum = 0; memset(s, 127, sizeof(s)); s[a] = 0; for (int l = 0; l < p; l++) { for (int o = 0; o < p; o++) { if (s[o] < 114514) { for (int q = 0; q < p; q++) { if (x[o][q] < 114514) { s[q] = min(s[q], s[o] + x[o][q]); } } } } } for (int l = 0; l < p; l++) { sum += s[l]; } minx = min(minx, sum); return minx; } int search(int p) { int t[10], maxn = 114514, maxid; for (int k = 0; k < p; k++) { t[k] = bfs(p, k); } for (int k = 0; k < p; k++) { if (maxn > t[k]) { maxn = t[k]; maxid = k; } } return maxn * 100 + maxid; } void _cin() { memset(x, 51, sizeof(x)); n = 0; _end = 0; cin >> m; if (m == 0) { _end = 1; return; } for (int i = 0; i < m; i++) { cin >> a >> b >> c; n = max(n, max(a, b)); x[a][b] = c; x[b][a] = c; } n += 1; } int main() { while (true) { _cin(); if (_end == 1) { break; } cout << search(n) % 100 << ' ' << search(n) / 100 << endl; } }
[["-", 31, 16, 31, 16, 31, 16, 12, 16, 12, 13], ["+", 31, 16, 31, 16, 31, 16, 12, 16, 12, 13], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
459
#include <iostream> using namespace std; const int INF = 1000000000; int main() { while (true) { int n; cin >> n; if (n == 0) break; int T[10][10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) { T[i][j] = 0; } else { T[i][j] = INF; } } } int m = 0; for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; T[a][b] = T[b][a] = c; if (a + 1 > m) { m = a + 1; } if (b + 1 > m) { m = b + 1; } } for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (T[i][j] > T[i][k] + T[k][j]) { T[i][j] = T[i][k] + T[k][j]; } } } } int count1[10]; for (int k = 0; k < m; k++) { int count = 0; for (int i = 0; i < m; i++) { count = T[k][i] + count; } count1[k] = count; } int count2 = count1[0]; for (int k = 0; k < m; k++) { if (count1[k] < count2) count2 = count1[k]; } for (int k = 0; k < m; k++) { if (count1[k] == count2) cout << k << " " << count2 << endl; } } return 0; }
#include <iostream> using namespace std; const int INF = 1000000000; int main() { while (true) { int n; cin >> n; if (n == 0) break; int T[10][10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) { T[i][j] = 0; } else { T[i][j] = INF; } } } int m = 0; for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; T[a][b] = T[b][a] = c; if (a + 1 > m) { m = a + 1; } if (b + 1 > m) { m = b + 1; } } for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (T[i][j] > T[i][k] + T[k][j]) { T[i][j] = T[i][k] + T[k][j]; } } } } int count1[10]; for (int k = 0; k < m; k++) { int count = 0; for (int i = 0; i < m; i++) { count = T[k][i] + count; } count1[k] = count; } int count2 = count1[0]; for (int k = 0; k < m; k++) { if (count1[k] < count2) count2 = count1[k]; } for (int k = 0; k < m; k++) { if (count1[k] == count2) { cout << k << " " << count2 << endl; break; } } } return 0; }
[["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35], ["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46]]
1
439
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <string> #include <vector> #define rep(i, a, b) for (int i = (a); i < b; i++) #define INF 1000000000 #define MAX_N 305 using namespace std; bool debug = false; int d[10][10]; void init() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) d[i][j] = 0; else d[i][j] = INF; } } } void make(int n) { int a, b, c; for (int i = 0; i < n; i++) { cin >> a >> b >> c; d[a][b] = c; d[b][a] = c; } } void wf() { for (int k = 0; k < 10; k++) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } } void solve() { int mini = INF, num; for (int i = 0; i < 10; i++) { int sum = 0; for (int j = 0; j < 10; j++) { if (d[i][j] != INF) sum += d[i][j]; } if (sum <= mini && sum != 0) { mini = sum; num = i; } } cout << num << " " << mini << endl; } int main() { int n; while (cin >> n) { if (n == 0) break; init(); make(n); wf(); solve(); } return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <string> #include <vector> #define rep(i, a, b) for (int i = (a); i < b; i++) #define INF 1000000000 #define MAX_N 305 using namespace std; bool debug = false; int d[12][12]; void init() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) d[i][j] = 0; else d[i][j] = INF; } } } void make(int n) { int a, b, c; for (int i = 0; i < n; i++) { cin >> a >> b >> c; d[a][b] = c; d[b][a] = c; } } void wf() { for (int k = 0; k < 10; k++) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } } void solve() { int mini = INF, num; for (int i = 0; i <= 10; i++) { int sum = 0; for (int j = 0; j <= 10; j++) { if (d[i][j] != INF) sum += d[i][j]; } if (sum < mini && sum != 0) { mini = sum; num = i; } } cout << num << " " << mini << endl; } int main() { int n; while (cin >> n) { if (n == 0) break; init(); make(n); wf(); solve(); } return 0; }
[["-", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 57, 15, 339, 51, 16, 31, 16, 17, 19], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 18]]
1
412
#include <cstdio> using namespace std; static const int MAX_V = 10; static const int MAX_E = 45; 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_V][MAX_V]; int dijkstra(int s) { int mincost; int d[MAX_V], color[MAX_V]; for (int i = 0; i < MAX_V; i++) { d[i] = INFTY; color[i] = WHITE; } d[s] = 0; color[s] = GRAY; while (true) { int u; mincost = INFTY; for (int i = 0; i < MAX_V; i++) { if (mincost > d[i] && color[i] != BLACK) { u = i; mincost = d[i]; } } if (mincost == INFTY) break; color[u] = BLACK; for (int v = 0; v < MAX_V; 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; } } } } int sum_d = 0; for (int i = 0; i < MAX_V; i++) { if (d[i] != INFTY) { sum_d += d[i]; } } return sum_d; } int main(void) { while (true) { scanf("%d", &n); if (n == 0) break; for (int i = 0; i < MAX_V; i++) { for (int j = 0; j < MAX_V; j++) { M[i][j] = INFTY; } } for (int i = 1; i <= n; i++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); M[a][b] = M[b][a] = c; } int min_v, min_d = INFTY; for (int i = 0; i < MAX_V; i++) { int dist = dijkstra(i); // printf("%d %d\n", i, dist); if (dist < min_d && dist != 0) { min_d = dist; min_v = i; } } printf("%d %d", min_v, min_d); } return 0; }
#include <cstdio> using namespace std; static const int MAX_V = 10; static const int MAX_E = 45; 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_V][MAX_V]; int dijkstra(int s) { int mincost; int d[MAX_V], color[MAX_V]; for (int i = 0; i < MAX_V; i++) { d[i] = INFTY; color[i] = WHITE; } d[s] = 0; color[s] = GRAY; while (true) { int u; mincost = INFTY; for (int i = 0; i < MAX_V; i++) { if (mincost > d[i] && color[i] != BLACK) { u = i; mincost = d[i]; } } if (mincost == INFTY) break; color[u] = BLACK; for (int v = 0; v < MAX_V; 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; } } } } int sum_d = 0; for (int i = 0; i < MAX_V; i++) { if (d[i] != INFTY) { sum_d += d[i]; } } return sum_d; } int main(void) { while (true) { scanf("%d", &n); if (n == 0) break; for (int i = 0; i < MAX_V; i++) { for (int j = 0; j < MAX_V; j++) { M[i][j] = INFTY; } } for (int i = 1; i <= n; i++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); M[a][b] = M[b][a] = c; } int min_v, min_d = INFTY; for (int i = 0; i < MAX_V; i++) { int dist = dijkstra(i); // printf("%d %d\n", i, dist); if (dist < min_d && dist != 0) { min_d = dist; min_v = i; } } printf("%d %d\n", min_v, min_d); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
524
// 2014/06/16 Tazoe #include <iostream> using namespace std; const int INF = 1000000000; int main() { while (true) { int n; cin >> n; if (n == 0) break; int T[10][10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) { T[i][j] = 0; } else { T[i][j] = INF; } } } int m = 0; // ???????????° for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; T[a][b] = T[b][a] = c; if (a + 1 > m) { m = a + 1; } if (b + 1 > m) { m = b + 1; } } for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (T[i][j] > T[i][k] + T[k][j]) { T[i][j] = T[i][k] + T[k][j]; } } } } int town = m - 1; int value = INF; for (int i = m - 1; i >= 0; i--) { int tempValue = 0; for (int j = 0; j < m; j++) { tempValue += T[i][j]; } if (value > tempValue) { value = tempValue; town = i; } } cout << town << ' ' << value << endl; } return 0; }
// 2014/06/16 Tazoe #include <iostream> using namespace std; const int INF = 1000000000; int main() { while (true) { int n; cin >> n; if (n == 0) break; int T[10][10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) { T[i][j] = 0; } else { T[i][j] = INF; } } } int m = 0; // ???????????° for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; T[a][b] = T[b][a] = c; if (a + 1 > m) { m = a + 1; } if (b + 1 > m) { m = b + 1; } } for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (T[i][j] > T[i][k] + T[k][j]) { T[i][j] = T[i][k] + T[k][j]; } } } } int town = m - 1; int value = INF; for (int i = m - 1; i >= 0; i--) { int tempValue = 0; for (int j = 0; j < m; j++) { tempValue += T[i][j]; } if (value >= tempValue) { value = tempValue; town = i; } } cout << town << ' ' << value << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 20]]
1
391
#include <iostream> using namespace std; const int INF = 1000000000; int main() { while (true) { int n; cin >> n; if (n == 0) break; int T[10][10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) { T[i][j] = 0; } else { T[i][j] = INF; } } } int m = 0; for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; T[a][b] = T[b][a] = c; if (a + 1 > m) { m = a + 1; } if (b + 1 > m) { m = b + 1; } } for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (T[i][j] > T[i][k] + T[k][j]) { T[i][j] = T[i][k] + T[k][j]; } } } } int town; int ti = INF; for (int i = m; i >= 0; i--) { int t = 0; for (int j = 0; j < m; j++) { t += T[i][j]; } if (ti >= t) { ti = t; town = i; } } cout << town << " " << ti << endl; } return 0; }
#include <iostream> using namespace std; const int INF = 1000000000; int main() { while (true) { int n; cin >> n; if (n == 0) break; int T[10][10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) { T[i][j] = 0; } else { T[i][j] = INF; } } } int m = 0; for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; T[a][b] = T[b][a] = c; if (a + 1 > m) { m = a + 1; } if (b + 1 > m) { m = b + 1; } } for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (T[i][j] > T[i][k] + T[k][j]) { T[i][j] = T[i][k] + T[k][j]; } } } } int town; int ti = INF; for (int i = m - 1; i >= 0; i--) { int t = 0; for (int j = 0; j < m; j++) { t += T[i][j]; } if (ti >= t) { ti = t; town = i; } } cout << town << " " << ti << endl; } return 0; }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 33], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
383
#include <algorithm> #include <iostream> using namespace std; const int INF = 1000000000; int main() { while (true) { int n; cin >> n; if (n == 0) break; int T[10][10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) { T[i][j] = 0; } else { T[i][j] = INF; } } } int m = 0; // ????????° for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; T[a][b] = T[b][a] = c; if (a + 1 > m) { m = a + 1; } if (b + 1 > m) { m = b + 1; } } for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (T[i][j] > T[i][k] + T[k][j]) { T[i][j] = T[i][k] + T[k][j]; } } } } int Count[m]; for (int i = 0; i < m; i++) { Count[i] = 0; for (int j = 0; j < m; j++) { Count[i] = Count[i] + T[i][j]; } } int mnum = 0; for (int i = 0; i < m; i++) { if (Count[0] > Count[i]) { Count[0] = Count[i]; mnum++; } } cout << mnum << ' ' << Count[0] << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; const int INF = 1000000000; int main() { while (true) { int n; cin >> n; if (n == 0) break; int T[10][10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) { T[i][j] = 0; } else { T[i][j] = INF; } } } int m = 0; // ????????° for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; T[a][b] = T[b][a] = c; if (a + 1 > m) { m = a + 1; } if (b + 1 > m) { m = b + 1; } } for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (T[i][j] > T[i][k] + T[k][j]) { T[i][j] = T[i][k] + T[k][j]; } } } } int Count[m]; for (int i = 0; i < m; i++) { Count[i] = 0; for (int j = 0; j < m; j++) { Count[i] = Count[i] + T[i][j]; } } int mnum = 0; for (int i = 0; i < m; i++) { if (Count[0] > Count[i]) { Count[0] = Count[i]; mnum = i; } } cout << mnum << ' ' << Count[0] << endl; } return 0; }
[["-", 0, 57, 64, 9, 0, 1, 0, 27, 17, 29], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 12, 22]]
1
429
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <vector> #define MAXN 2555 #define INF 0x3f3f3f3f #define v first #define b second using namespace std; int fp[15][15]; int m; void floyd() { for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) for (int k = 0; k < m; k++) { fp[i][k] = min(fp[i][k], fp[i][j] + fp[j][k]); } } int main() { int n; while (scanf("%d", &n), n) { m = 0; memset(fp, INF, sizeof(fp)); for (int i = 0; i < n; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); fp[a][b] = fp[b][a] = c; m = max(m, max(a, b) + 1); } for (int i = 0; i < m; i++) fp[i][i] = 0; floyd(); int ans, sum = INF; for (int i = 0; i < m; i++) { int res = 0; for (int j = 0; j < m; j++) { res += fp[i][j]; } if (res < sum) { sum = res; ans = i; } } cout << ans << " " << sum << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <vector> #define MAXN 2555 #define INF 0x3f3f3f3f #define v first #define b second using namespace std; int fp[15][15]; int m; void floyd() { for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) for (int k = 0; k < m; k++) { fp[j][k] = min(fp[j][k], fp[j][i] + fp[i][k]); } } int main() { int n; while (scanf("%d", &n), n) { m = 0; memset(fp, INF, sizeof(fp)); for (int i = 0; i < n; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); fp[a][b] = fp[b][a] = c; m = max(m, max(a, b) + 1); } for (int i = 0; i < m; i++) fp[i][i] = 0; floyd(); int ans, sum = INF; for (int i = 0; i < m; i++) { int res = 0; for (int j = 0; j < m; j++) { res += fp[i][j]; } if (res < sum) { sum = res; ans = i; } } cout << ans << " " << sum << endl; } }
[["-", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 3, 4, 0, 69, 28, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 3, 4, 0, 16, 31, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 16, 31, 69, 341, 342, 0, 22], ["-", 0, 16, 12, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 16, 12, 69, 28, 69, 341, 342, 0, 22]]
1
357
#include <bits/stdc++.h> #define SIZE 100005 #define MAX_V 100001 #define INF (1e9 + 1) using namespace std; typedef long long ll; typedef pair<int, int> P; int g[10][10]; int main() { int a; while (scanf("%d", &a), a) { for (int i = 0; i, 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) g[i][j] = 0; else g[i][j] = 999999999; } } int t = 0; for (int i = 0; i < a; i++) { int c, d, e; scanf("%d%d%d", &c, &d, &e); if (t < c) t = c; if (t < d) t = d; g[c][d] = e; g[d][c] = e; } t++; for (int k = 0; k < t; k++) { for (int i = 0; i < t; i++) { for (int j = 0; j < t; j++) { if (g[i][k] + g[k][j] < g[i][j]) g[i][j] = g[i][k] + g[k][j]; } } } int n = 0; ll ans = 9999999999; for (int i = 0; i < t; i++) { ll now = 0; for (int j = 0; j < t; j++) now += g[i][j]; if (ans > now) { ans = now; n = i; } } printf("%d %lld\n", n, ans); } }
#include <bits/stdc++.h> #define SIZE 100005 #define MAX_V 100001 #define INF (1e9 + 1) using namespace std; typedef long long ll; typedef pair<int, int> P; int g[10][10]; int main() { int a; while (scanf("%d", &a), a) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) g[i][j] = 0; else g[i][j] = 999999999; } } int t = 0; for (int i = 0; i < a; i++) { int c, d, e; scanf("%d%d%d", &c, &d, &e); if (t < c) t = c; if (t < d) t = d; g[c][d] = e; g[d][c] = e; } t++; for (int k = 0; k < t; k++) { for (int i = 0; i < t; i++) { for (int j = 0; j < t; j++) { if (g[i][k] + g[k][j] < g[i][j]) g[i][j] = g[i][k] + g[k][j]; } } } int n = 0; ll ans = 9999999999; for (int i = 0; i < t; i++) { ll now = 0; for (int j = 0; j < t; j++) now += g[i][j]; if (ans > now) { ans = now; n = i; } } printf("%d %lld\n", n, ans); } }
[["-", 0, 52, 8, 9, 0, 7, 15, 34, 0, 21], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18]]
1
390
#include <algorithm> #include <iostream> #define INF 1000000 using namespace std; int main(void) { int n; while (cin >> n && n) { int f[10][10]; fill((int *)f, (int *)f + n * n, INF); int s, t, cost; int maxi = 0; for (int i = 0; i < n; i++) { cin >> s >> t >> cost; maxi = max(max(maxi, s), t); f[s][t] = cost; f[t][s] = cost; } for (int i = 0; i <= maxi; i++) f[i][i] = 0; // WF for (int k = 0; k <= maxi; k++) { for (int i = 0; i <= maxi; i++) { for (int j = 0; j <= maxi; j++) { f[i][j] = min(f[i][j], f[i][k] + f[k][j]); } } } int tmp, index; int ret = INF; for (int i = 0; i <= maxi; i++) { tmp = 0; for (int j = 0; j <= maxi; j++) { tmp += f[i][j]; } if (tmp < ret) { ret = tmp; index = i; } } cout << index << " " << ret << endl; } }
#include <algorithm> #include <iostream> #define INF 1000000 using namespace std; int main(void) { int n; while (cin >> n && n) { int f[10][10]; fill((int *)f, (int *)f + 10 * 10, INF); int s, t, cost; int maxi = 0; for (int i = 0; i < n; i++) { cin >> s >> t >> cost; maxi = max(max(maxi, s), t); f[s][t] = cost; f[t][s] = cost; } for (int i = 0; i <= maxi; i++) f[i][i] = 0; // WF for (int k = 0; k <= maxi; k++) { for (int i = 0; i <= maxi; i++) { for (int j = 0; j <= maxi; j++) { f[i][j] = min(f[i][j], f[i][k] + f[k][j]); } } } int tmp, index; int ret = INF; for (int i = 0; i <= maxi; i++) { tmp = 0; for (int j = 0; j <= maxi; j++) { tmp += f[i][j]; } if (tmp < ret) { ret = tmp; index = i; } } cout << index << " " << ret << endl; } }
[["-", 0, 2, 3, 4, 0, 16, 12, 16, 31, 22], ["+", 0, 2, 3, 4, 0, 16, 12, 16, 31, 13], ["-", 0, 2, 3, 4, 0, 16, 12, 16, 12, 22], ["+", 0, 2, 3, 4, 0, 16, 12, 16, 12, 13]]
1
323
#include <iostream> using namespace std; int main() { int n; while (cin >> n, n) { int map[10][10]; int m = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) map[i][j] = 0; else map[i][j] = 10000000; } } for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; m = max(max(a, b), m); map[a][b] = map[b][a] = c; } for (int k = 0; k <= m; k++) { for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { map[i][j] = min(map[i][j], map[i][k] + map[k][j]); } } } int ans[10] = {}; for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) ans[i] += map[i][j]; int a = m; for (int i = 0; i <= m; i++) { if (ans[i] < ans[a]) a = i; } cout << a << " " << ans[a] << endl; } }
#include <iostream> using namespace std; int main() { int n; while (cin >> n, n) { int map[10][10]; int m = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == j) map[i][j] = 0; else map[i][j] = 10000000; } } for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; m = max(max(a, b), m); map[a][b] = map[b][a] = c; } for (int k = 0; k <= m; k++) { for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { map[i][j] = min(map[i][j], map[i][k] + map[k][j]); } } } int ans[10] = {}; for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) ans[i] += map[i][j]; int a = 0; for (int i = 0; i <= m; i++) { if (ans[i] < ans[a]) a = i; } cout << a << " " << ans[a] << endl; } }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 22], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
346
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef vector<vector<int>> Matrix; const int INF = 100000000; Matrix d; // グラフの距離を格納した2次元配列(隣接行列) // d[u][v]は辺e=(u,v)のコスト(辺が存在しない場合はINF、ただしd[i][i]=0) void warshall_floyd(int n) { // nは頂点数 for (int i = 0; i < n; i++) // 経由する頂点 for (int j = 0; j < n; j++) // 開始頂点 for (int k = 0; k < n; k++) // 終端 d[j][k] = min(d[j][k], d[j][i] + d[i][k]); } int main() { while (1) { int n = 0, m; d = Matrix(10, vector<int>(10, INF)); for (int i = 0; i < 10; i++) d[i][i] = 0; cin >> m; if (m == 0) break; for (int i = 0; i < m; i++) { int from, to, cost; cin >> from >> to >> cost; n = max(n, from); n = max(n, to); d[from][to] = cost; d[to][from] = cost; } n++; warshall_floyd(n); int sum[100]; int a; for (int i = 0; i < n; i++) { sum[i] = 0; for (int j = 0; j < n; j++) { sum[i] += d[i][j]; } } int ans = sum[0]; int num = 0; for (int i = 0; i < n - 1; i++) { if (sum[i] > sum[i + 1]) { ans = sum[i + 1]; num = i + 1; } } cout << num << " " << ans << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef vector<vector<int>> Matrix; const int INF = 100000000; Matrix d; // グラフの距離を格納した2次元配列(隣接行列) // d[u][v]は辺e=(u,v)のコスト(辺が存在しない場合はINF、ただしd[i][i]=0) void warshall_floyd(int n) { // nは頂点数 for (int i = 0; i < n; i++) // 経由する頂点 for (int j = 0; j < n; j++) // 開始頂点 for (int k = 0; k < n; k++) // 終端 d[j][k] = min(d[j][k], d[j][i] + d[i][k]); } int main() { while (1) { int n = 0, m; d = Matrix(10, vector<int>(10, INF)); for (int i = 0; i < 10; i++) d[i][i] = 0; cin >> m; if (m == 0) break; for (int i = 0; i < m; i++) { int from, to, cost; cin >> from >> to >> cost; n = max(n, from); n = max(n, to); d[from][to] = cost; d[to][from] = cost; } n++; warshall_floyd(n); int sum[100]; int a; for (int i = 0; i < n; i++) { sum[i] = 0; for (int j = 0; j < n; j++) { sum[i] += d[i][j]; } } int ans = sum[0]; int num = 0; for (int i = 0; i < n - 1; i++) { if (ans > sum[i + 1]) { ans = sum[i + 1]; num = i + 1; } } cout << num << " " << ans << endl; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 31, 69, 28, 22], ["-", 15, 339, 51, 16, 31, 69, 341, 342, 0, 70], ["-", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22], ["-", 15, 339, 51, 16, 31, 69, 341, 342, 0, 73], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22]]
1
407
#include <algorithm> #include <iostream> const int MAX_V = 10, INF = 1 << 24; int V; int d[MAX_V], used[MAX_V], cost[MAX_V][MAX_V]; void dijkstra(int s) { std::fill(d, d + MAX_V, INF); std::fill(used, used + MAX_V, 0); d[s] = 0; while (true) { int v = -1; for (int u = 0; u < V; u++) { if (!used[u] && (v == -1 || d[u] < d[v])) { v = u; } } if (v == -1) { break; } used[v] = 1; for (int u = 0; u < V; u++) { d[u] = std::min(d[u], d[v] + cost[v][u]); } } } int getCost(int s) { dijkstra(s); int res = 0; for (int i = 0; i < V; i++) { if (i != s) { res += d[i]; } } return res; } int main() { int n = 0; while (std::cin >> n, n) { for (int i = 0; i < MAX_V; i++) { std::fill(cost[i], cost[i] + MAX_V, INF); } V = 0; for (int i = 0; i < n; i++) { int a, b, c; std::cin >> a >> b >> c; cost[a][b] = c; cost[b][a] = c; V = std::max(V, std::max(a + 1, b + 1)); } int res = INF, town = 0; for (int i = V - 1; i >= 0; i--) { int v = getCost(i); if (v < res) { res = v; town = i; } } std::cout << town << " " << res << std::endl; } }
#include <algorithm> #include <iostream> const int MAX_V = 10, INF = 1 << 24; int V; int d[MAX_V], used[MAX_V], cost[MAX_V][MAX_V]; void dijkstra(int s) { std::fill(d, d + MAX_V, INF); std::fill(used, used + MAX_V, 0); d[s] = 0; while (true) { int v = -1; for (int u = 0; u < V; u++) { if (!used[u] && (v == -1 || d[u] < d[v])) { v = u; } } if (v == -1) { break; } used[v] = 1; for (int u = 0; u < V; u++) { d[u] = std::min(d[u], d[v] + cost[v][u]); } } } int getCost(int s) { dijkstra(s); int res = 0; for (int i = 0; i < V; i++) { if (i != s) { res += d[i]; } } return res; } int main() { int n = 0; while (std::cin >> n, n) { for (int i = 0; i < MAX_V; i++) { std::fill(cost[i], cost[i] + MAX_V, INF); } V = 0; for (int i = 0; i < n; i++) { int a, b, c; std::cin >> a >> b >> c; cost[a][b] = c; cost[b][a] = c; V = std::max(V, std::max(a + 1, b + 1)); } int res = INF, town = 0; for (int i = V - 1; i >= 0; i--) { int v = getCost(i); if (v <= res) { res = v; town = i; } } std::cout << town << " " << res << std::endl; } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19]]
1
453
#include <algorithm> #include <cstdio> using namespace std; #define INF 1000000 int C[15][15]; int main() { int n; while (scanf("%d", &n), n) { for (int i = 0; i < 15; ++i) { for (int j = 0; j < 15; ++j) { C[i][j] = i == j ? 0 : INF; } } int m = 0; for (int i = 0; i < n; ++i) { int a, b, c; scanf("%d%d%d", &a, &b, &c); m = max(m, max(a, b)); C[a][b] = C[b][a] = c; } m++; for (int k = 0; k < m; ++k) { for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { C[i][j] = min(C[i][j], C[i][k] + C[k][j]); } } } int ln = INF, lc = INF; for (int i = m - 1; i >= 0; --i) { int c = 0; for (int j = 0; j < m; ++j) { c += C[i][j]; } if (c < lc) { ln = i; lc = c; } } printf("%d %d\n", ln, lc); } return 0; }
#include <algorithm> #include <cstdio> using namespace std; #define INF 1000000 int C[15][15]; int main() { int n; while (scanf("%d", &n), n) { for (int i = 0; i < 15; ++i) { for (int j = 0; j < 15; ++j) { C[i][j] = i == j ? 0 : INF; } } int m = 0; for (int i = 0; i < n; ++i) { int a, b, c; scanf("%d%d%d", &a, &b, &c); m = max(m, max(a, b)); C[a][b] = C[b][a] = c; } m++; for (int k = 0; k < m; ++k) { for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { C[i][j] = min(C[i][j], C[i][k] + C[k][j]); } } } int ln = INF, lc = INF; for (int i = m - 1; i >= 0; --i) { int c = 0; for (int j = 0; j < m; ++j) { c += C[i][j]; } if (c <= lc) { ln = i; lc = c; } } printf("%d %d\n", ln, lc); } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19]]
1
344
// 2013/11/15(Fri) #include <iostream> using namespace std; int main() { while (true) { int n; int a, b, c; int INF = 12001; int time[10][10]; int m = 0; int min = 12000, imin = 0; cin >> n; if (n == 0) break; for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) { if (i == j) time[i][j] = 0; else time[i][j] = INF; } for (int i = 0; i < n; i++) { cin >> a >> b >> c; time[a][b] = c; time[b][a] = c; if (m < a) m = a; if (m < b) m = b; } for (int h = 0; h < m + 1; h++) for (int i = 0; i < m + 1; i++) for (int j = 0; j < m + 1; j++) if (time[i][h] == INF || time[h][j] == INF) continue; else if (time[i][j] == INF) time[i][j] = time[i][h] + time[h][j]; else if (time[i][j] > time[i][h] + time[h][j]) time[i][j] = time[i][h] + time[h][j]; for (int i = 1; i < m + 1; i++) min += time[0][i]; for (int i = 1; i < m + 1; i++) { int sum = 0; for (int j = 0; j < m + 1; j++) sum += time[i][j]; if (sum < min) { imin = i; min = sum; } } cout << imin << ' ' << min << endl; } return 0; }
// 2013/11/15(Fri) #include <iostream> using namespace std; int main() { while (true) { int n; int a, b, c; int INF = -1; int time[10][10]; int m = 0; int min = 0, imin = 0; cin >> n; if (n == 0) break; for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) { if (i == j) time[i][j] = 0; else time[i][j] = INF; } for (int i = 0; i < n; i++) { cin >> a >> b >> c; time[a][b] = c; time[b][a] = c; if (m < a) m = a; if (m < b) m = b; } for (int h = 0; h < m + 1; h++) for (int i = 0; i < m + 1; i++) for (int j = 0; j < m + 1; j++) if (time[i][h] == INF || time[h][j] == INF) continue; else if (time[i][j] == INF) time[i][j] = time[i][h] + time[h][j]; else if (time[i][j] > time[i][h] + time[h][j]) time[i][j] = time[i][h] + time[h][j]; for (int i = 1; i < m + 1; i++) min += time[0][i]; for (int i = 1; i < m + 1; i++) { int sum = 0; for (int j = 0; j < m + 1; j++) sum += time[i][j]; if (sum < min) { imin = i; min = sum; } } cout << imin << ' ' << min << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
456
# -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0189 AC """ import sys from sys import stdin input = stdin.readline def warshallFloyd(V, dp): for k in range(V): for i in range(V): for j in range(V): dp_ik = dp[i][k] dp_kj = dp[k][j] if dp[i][j] < dp_ik + dp_kj: dp[i][j] = dp_ik + dp_kj def main(args): while True: n_max = 9+1 n = int(input()) if n == 0: break dp = [[float('inf')] * n_max for _ in range(n_max)] for i in range(n_max): dp[i][i] = 0 max_town = 0 for _ in range(n): a, b, c = map(int, input().split()) dp[a][b] = c dp[b][a] = c max_town = max(max_town, a, b) warshallFloyd(max_town + 1, dp) min_dist = float('inf') town_live = -1 for i, d in enumerate(dp[:max_town+1]): sum_dist = 0 for ele in d[:max_town+1]: sum_dist += ele if sum_dist < min_dist: min_dist = sum_dist town_live = i print('{} {}'.format(town_live, min_dist)) if __name__ == '__main__': main(sys.argv[1:])
# -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0189 AC """ import sys from sys import stdin input = stdin.readline def warshallFloyd(V, dp): for k in range(V): for i in range(V): for j in range(V): dp_ik = dp[i][k] dp_kj = dp[k][j] if dp[i][j] > dp_ik + dp_kj: dp[i][j] = dp_ik + dp_kj def main(args): while True: n_max = 9+1 n = int(input()) if n == 0: break dp = [[float('inf')] * n_max for _ in range(n_max)] for i in range(n_max): dp[i][i] = 0 max_town = 0 for _ in range(n): a, b, c = map(int, input().split()) dp[a][b] = c dp[b][a] = c max_town = max(max_town, a, b) warshallFloyd(max_town + 1, dp) min_dist = float('inf') town_live = -1 for i, d in enumerate(dp[:max_town+1]): sum_dist = 0 for ele in d[:max_town+1]: sum_dist += ele if sum_dist < min_dist: min_dist = sum_dist town_live = i print('{} {}'.format(town_live, min_dist)) if __name__ == '__main__': main(sys.argv[1:])
[["-", 0, 7, 8, 196, 0, 57, 15, 666, 667, 18], ["+", 0, 7, 8, 196, 0, 57, 15, 666, 667, 47]]
5
306
NULL = 10000 TOWN_COUNT = 10 while True: input_count = int(input()) if input_count == 0: break adjacency_matrix = [[NULL] * TOWN_COUNT for _ in range(TOWN_COUNT)] for lp in range(TOWN_COUNT): adjacency_matrix[lp][lp] = 0 need_range = 0 for _ in range(input_count): start, end, time = [int(item) for item in input().split(" ")] adjacency_matrix[start][end] = time adjacency_matrix[end][start] = time need_range = max(need_range, start, end) for a in range(need_range + 1): for b in range(need_range + 1): for c in range(need_range + 1): adjacency_matrix[a][c] = min(adjacency_matrix[a][b] + adjacency_matrix[b][c], adjacency_matrix[a][c]) min_time = NULL min_index = -1 for index, item in enumerate(adjacency_matrix): need_element = [item2 for item2 in item if 0 < item2 < NULL] if 0 < len(need_element): time = sum(need_element) if time < min_time: min_time = time min_index = index print(min_index, min_time)
NULL = 10000 TOWN_COUNT = 10 while True: input_count = int(input()) if input_count == 0: break adjacency_matrix = [[NULL] * TOWN_COUNT for _ in range(TOWN_COUNT)] for lp in range(TOWN_COUNT): adjacency_matrix[lp][lp] = 0 need_range = 0 for _ in range(input_count): start, end, time = [int(item) for item in input().split(" ")] adjacency_matrix[start][end] = time adjacency_matrix[end][start] = time need_range = max(need_range, start, end) for a in range(need_range + 1): for b in range(need_range + 1): for c in range(need_range + 1): adjacency_matrix[b][c] = min(adjacency_matrix[b][a] + adjacency_matrix[a][c], adjacency_matrix[b][c]) min_time = NULL min_index = -1 for index, item in enumerate(adjacency_matrix): need_element = [item2 for item2 in item if 0 < item2 < NULL] if 0 < len(need_element): time = sum(need_element) if time < min_time: min_time = time min_index = index print(min_index, min_time)
[["-", 0, 1, 0, 662, 31, 206, 51, 206, 206, 22], ["+", 0, 1, 0, 662, 31, 206, 51, 206, 206, 22], ["-", 3, 4, 0, 657, 31, 206, 51, 206, 206, 22], ["+", 3, 4, 0, 657, 31, 206, 51, 206, 206, 22], ["-", 12, 652, 3, 4, 0, 657, 31, 206, 206, 22], ["+", 12, 652, 3, 4, 0, 657, 31, 206, 206, 22], ["-", 3, 4, 0, 657, 12, 206, 51, 206, 206, 22], ["+", 3, 4, 0, 657, 12, 206, 51, 206, 206, 22], ["-", 12, 652, 3, 4, 0, 206, 51, 206, 206, 22], ["+", 12, 652, 3, 4, 0, 206, 51, 206, 206, 22]]
5
247
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define repa(i, s, e) for (int i = s; i <= e; i++) #define repd(i, s, e) for (int i = s; i >= e; i--) #define PB(a) push_back(a) #define MP(i, s) make_pair(i, s) typedef long long ll; // geometry library start const double EPS = 1.0e-10; class P { public: double x, y; P() {} P(int x_, int y_) : x(x_), y(y_) {} P(double x_, double y_) : x(x_), y(y_) {} P(const P &p) : x(p.x), y(p.y) {} P operator+(const P &p) const { return P(x + p.x, y + p.y); } P operator-(const P &p) const { return P(x - p.x, y - p.y); } P operator-() const { return P(-x, -y); } P operator*(double s) const { return P(x * s, y * s); } P operator/(double s) const { return P(x / s, y / s); } P &operator=(const P &p) { x = p.x; y = p.y; return (*this); } double dot(const P &p) const { return x * p.x + y * p.y; } double det(const P &p) const { return x * p.y - y * p.x; } double norm() const { return sqrt(x * x + y * y); } double norm2() const { return x * x + y * y; } P proj(const P &p) const { double k = det(p) / norm2(); return P(x * k, y * k); } }; double tri(P a, P b, P c) { return (b - a).det(c - a); } int sign(double r) { return r < -EPS ? -1 : r > EPS ? 1 : 0; } class L { public: P s, t; L() {} L(P s_, P t_) : s(s_), t(t_) {} L(const L &l) : s(l.s), t(l.t) {} L &operator=(const L &l) { s = l.s; t = l.t; return (*this); } double length() { return (s - t).norm(); } P vec() const { return t - s; } P proj(const P &p) { return s + vec().proj(p - s); } int iLL(const L &l) const { if (sign(vec().det(l.vec()))) return 1; if (sign(vec().det(l.s - s))) return 0; return -1; } bool iLS(const L &l) const { return sign(tri(s, t, l.s)) * sign(tri(s, t, l.t)) <= 0; } bool iSS(const L &l) const { return iLS(l) && l.iLS(*this); } P pLL(const L &l) const { return s + vec() * (l.s - s).det(l.vec()) / t.det(l.vec()); } double dLP(const P &p) const { return abs(tri(s, t, p)) / vec().norm(); } double dSP(const P &p) const { if (sign(vec().dot(p - s)) <= 0) return (p - s).norm(); if (sign(vec().dot(p - t)) >= 0) return (p - t).norm(); return dLP(p); } }; class C { public: P p; double r; C() {} C(P p_, double r_) : p(p_), r(r_) {} C(const C &c) : p(c.p), r(c.r) {} C &operator=(const C &c) { p = c.p; r = c.r; return (*this); } }; // geometry library end int x1, z1, x2, z2; L E[3]; P T[3]; int area[4] = {1900000, 1000000, 100000, 0}; string fort[4] = {"dai-kichi", "chu-kichi", "kichi", "syo-kichi"}; void solve() { rep(i, 3) { if (!E[i].iSS(E[(i + 1) % 3])) { printf("kyo\n"); return; } T[i] = E[i].pLL(E[(i + 1) % 3]); } double S = abs((T[1] - T[0]).det(T[2] - T[1])) * 0.5; rep(i, 4) { if (i == 3) { if (sign(S - area[i]) > 0) { cout << fort[i] << endl; return; } } else if (sign(S - area[i]) >= 0) { cout << fort[i] << endl; return; } } printf("kyo\n"); } int main() { for (;;) { scanf("%d%d%d%d", &x1, &z1, &x2, &z2); if (x1 == 0 && z1 == 0 && x2 == 0 && z2 == 0) break; E[0] = L(P(x1, z1), P(x2, z2)); scanf("%d%d%d%d", &x1, &z1, &x2, &z2); E[1] = L(P(x1, z1), P(x2, z2)); scanf("%d%d%d%d", &x1, &z1, &x2, &z2); E[2] = L(P(x1, z1), P(x2, z2)); solve(); } }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define repa(i, s, e) for (int i = s; i <= e; i++) #define repd(i, s, e) for (int i = s; i >= e; i--) #define PB(a) push_back(a) #define MP(i, s) make_pair(i, s) typedef long long ll; // geometry library start const double EPS = 1.0e-10; class P { public: double x, y; P() {} P(int x_, int y_) : x(x_), y(y_) {} P(double x_, double y_) : x(x_), y(y_) {} P(const P &p) : x(p.x), y(p.y) {} P operator+(const P &p) const { return P(x + p.x, y + p.y); } P operator-(const P &p) const { return P(x - p.x, y - p.y); } P operator-() const { return P(-x, -y); } P operator*(double s) const { return P(x * s, y * s); } P operator/(double s) const { return P(x / s, y / s); } P &operator=(const P &p) { x = p.x; y = p.y; return (*this); } double dot(const P &p) const { return x * p.x + y * p.y; } double det(const P &p) const { return x * p.y - y * p.x; } double norm() const { return sqrt(x * x + y * y); } double norm2() const { return x * x + y * y; } P proj(const P &p) const { double k = det(p) / norm2(); return P(x * k, y * k); } }; double tri(P a, P b, P c) { return (b - a).det(c - a); } int sign(double r) { return r < -EPS ? -1 : r > EPS ? 1 : 0; } class L { public: P s, t; L() {} L(P s_, P t_) : s(s_), t(t_) {} L(const L &l) : s(l.s), t(l.t) {} L &operator=(const L &l) { s = l.s; t = l.t; return (*this); } double length() { return (s - t).norm(); } P vec() const { return t - s; } P proj(const P &p) { return s + vec().proj(p - s); } int iLL(const L &l) const { if (sign(vec().det(l.vec()))) return 1; if (sign(vec().det(l.s - s))) return 0; return -1; } bool iLS(const L &l) const { return sign(tri(s, t, l.s)) * sign(tri(s, t, l.t)) <= 0; } bool iSS(const L &l) const { return iLS(l) && l.iLS(*this); } P pLL(const L &l) const { return s + vec() * (l.s - s).det(l.vec()) / vec().det(l.vec()); } double dLP(const P &p) const { return abs(tri(s, t, p)) / vec().norm(); } double dSP(const P &p) const { if (sign(vec().dot(p - s)) <= 0) return (p - s).norm(); if (sign(vec().dot(p - t)) >= 0) return (p - t).norm(); return dLP(p); } }; class C { public: P p; double r; C() {} C(P p_, double r_) : p(p_), r(r_) {} C(const C &c) : p(c.p), r(c.r) {} C &operator=(const C &c) { p = c.p; r = c.r; return (*this); } }; // geometry library end int x1, z1, x2, z2; L E[3]; P T[3]; int area[4] = {1900000, 1000000, 100000, 0}; string fort[4] = {"dai-kichi", "chu-kichi", "kichi", "syo-kichi"}; void solve() { rep(i, 3) { if (!E[i].iSS(E[(i + 1) % 3])) { printf("kyo\n"); return; } T[i] = E[i].pLL(E[(i + 1) % 3]); } double S = abs((T[1] - T[0]).det(T[2] - T[1])) * 0.5; rep(i, 4) { if (i == 3) { if (sign(S - area[i]) > 0) { cout << fort[i] << endl; return; } } else if (sign(S - area[i]) >= 0) { cout << fort[i] << endl; return; } } printf("kyo\n"); } int main() { for (;;) { scanf("%d%d%d%d", &x1, &z1, &x2, &z2); if (x1 == 0 && z1 == 0 && x2 == 0 && z2 == 0) break; E[0] = L(P(x1, z1), P(x2, z2)); scanf("%d%d%d%d", &x1, &z1, &x2, &z2); E[1] = L(P(x1, z1), P(x2, z2)); scanf("%d%d%d%d", &x1, &z1, &x2, &z2); E[2] = L(P(x1, z1), P(x2, z2)); solve(); } }
[["-", 0, 16, 12, 16, 12, 2, 63, 118, 28, 22], ["+", 12, 16, 12, 2, 63, 118, 28, 2, 63, 22], ["+", 12, 2, 63, 118, 28, 2, 3, 4, 0, 24], ["+", 12, 2, 63, 118, 28, 2, 3, 4, 0, 25]]
1
1,405
import java.util.Scanner; // Search public class Main { static int c; static int[] a; static int comp(int k, int x) { c++; return a[k] - x; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); int x = sc.nextInt(); c = 0; int l = 0; int r = n - 1; int k = (l + r) / 2; while (true) { // System.out.println("K: " + k + " L:" + l + " //R:" + r); int com = comp(k, x); if (r <= l) break; if (com < 0) l = k + 1; else if (com > 0) r = k - 1; else break; k = (l + r) / 2; } System.out.println(c); } } }
import java.util.Scanner; // Search public class Main { static int c; static int[] a; static int comp(int k, int x) { c++; return a[k] - x; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); int x = sc.nextInt(); c = 0; int l = 0; int r = n - 1; int k = (l + r) / 2; while (l <= r) { // System.out.println("K: " + k + " L:" + l + " //R:" + r); int com = comp(k, x); if (r <= l) break; if (com < 0) l = k + 1; else if (com > 0) r = k - 1; else break; k = (l + r) / 2; } System.out.println(c); } } }
[["-", 0, 52, 8, 196, 0, 52, 15, 15, 0, 146], ["+", 8, 196, 0, 52, 15, 15, 0, 16, 31, 22], ["+", 8, 196, 0, 52, 15, 15, 0, 16, 17, 19], ["+", 8, 196, 0, 52, 15, 15, 0, 16, 12, 22]]
3
234
#include <stdio.h> int main(void) { int n, target, count; int data[100]; int lo, hi, ce; int i; while (scanf("%d", &n)) { if (n == 0) break; for (i = 0; i < n; i++) { scanf("%d", &data[i]); } scanf("%d", &target); count = 0; hi = n; lo = 0; while (hi >= lo) { count++; ce = (lo + hi) / 2; if (data[ce] == target) break; if (data[ce] < target) lo = ce + 1; else hi = ce - 1; } printf("%d\n", count); } return 0; }
#include <stdio.h> int main(void) { int n, target, count; int data[100]; int lo, hi, ce; int i; while (scanf("%d", &n)) { if (n == 0) break; for (i = 0; i < n; i++) { scanf("%d", &data[i]); } scanf("%d", &target); count = 0; hi = n - 1; lo = 0; while (hi >= lo) { count++; ce = (lo + hi) / 2; if (data[ce] == target) break; if (data[ce] < target) lo = ce + 1; else hi = ce - 1; } printf("%d\n", count); } return 0; }
[["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 33], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
0
171
#include <stdio.h> int list[10000]; int n, t; int bin_search(int l, int r, int c) { int m = (l + r) / 2; if (list[m] == t) { return c; } else if (r < l) { return c; } else if (list[m] < t) { return bin_search(m + 1, r, c + 1); } else if (list[m] > t) { return bin_search(l, m - 1, c + 1); } } int main() { int i; while (1) { scanf("%d", &n); if (n == 0) break; for (i = 0; i < n; i++) { scanf("%d", &list[i]); } scanf("%d", &t); printf("%d\n", bin_search(0, n, 1)); } return 0; }
#include <stdio.h> int list[100]; int n, t; int bin_search(int l, int r, int c) { int m = (l + r) / 2; if (list[m] == t) { return c; } else if (r < l) { return c - 1; } else if (list[m] < t) { return bin_search(m + 1, r, c + 1); } else if (list[m] > t) { return bin_search(l, m - 1, c + 1); } } int main() { int i; while (1) { scanf("%d", &n); if (n == 0) break; for (i = 0; i < n; i++) { scanf("%d", &list[i]); } scanf("%d", &t); printf("%d\n", bin_search(0, n - 1, 1)); } return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 0, 57, 64, 9, 0, 37, 0, 16, 17, 33], ["+", 0, 57, 64, 9, 0, 37, 0, 16, 12, 13], ["+", 3, 4, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 3, 4, 0, 2, 3, 4, 0, 16, 12, 13]]
0
207
#include <stdio.h> int a, times, ind[105]; void solve(int l, int r) { times++; if (a == ind[(l + r) / 2]) { return; } if (l > r) { return; } if (ind[(l + r) / 2] > a) { solve(l, (l + r) / 2 - 1); } else { solve((l + r) / 2 + 1, r); } } int main(void) { int n; int i; while (1) { scanf("%d", &n); if (n == 0) { break; } for (i = 0; i < n; i++) { scanf("%d", &ind[i]); } scanf("%d", &a); solve(0, n); printf("%d\n", times); times = 0; } return (0); }
#include <stdio.h> int a, times, ind[105]; void solve(int l, int r) { times++; if (a == ind[(l + r) / 2]) { return; } if (l > r) { times--; return; } if (ind[(l + r) / 2] > a) { solve(l, (l + r) / 2 - 1); } else { solve((l + r) / 2 + 1, r); } } int main(void) { int n; int i; while (1) { scanf("%d", &n); if (n == 0) { break; } for (i = 0; i < n; i++) { scanf("%d", &ind[i]); } scanf("%d", &a); solve(0, n - 1); printf("%d\n", times); times = 0; } return (0); }
[["+", 0, 57, 64, 9, 0, 1, 0, 27, 28, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 27, 17, 68], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
0
208
#include <iostream> using namespace std; int main() { while (1) { int n; cin >> n; if (n == 0) break; int A[n]; for (int i = 0; i < n; i++) cin >> A[i]; int x; cin >> x; int ans = 1; int r = n - 1, l = 0; while (l < r) { ans++; int mid = (r + l) / 2; if (A[mid] == x || l == r) break; else if (A[mid] < x) l = mid + 1; else if (A[mid] > x) r = mid - 1; } cout << ans << endl; } return 0; }
#include <iostream> using namespace std; int main() { while (1) { int n; cin >> n; if (n == 0) break; int A[n]; for (int i = 0; i < n; i++) cin >> A[i]; int x; cin >> x; int ans = 0; int r = n - 1, l = 0; while (l <= r) { ans++; int mid = (r + l) / 2; if (A[mid] == x || l > r) break; else if (A[mid] < x) l = mid + 1; else if (A[mid] > x) r = mid - 1; } cout << ans << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["-", 8, 9, 0, 52, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 19], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 60], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47]]
1
161
#include <iostream> using namespace std; int a[100]; int n, k; main() { while (cin >> n, n) { for (int i = 0; i < n; i++) cin >> a[i]; int l = 0, f = n - 1; cin >> k; int cnt = 0; while (f > l) { int m = (f + l) / 2; cnt++; if (a[m] > k) l = m + 1; else if (a[m] < k) f = m - 1; else break; } cout << cnt << endl; } }
#include <iostream> using namespace std; int a[100]; int n, k; main() { while (cin >> n, n) { for (int i = 0; i < n; i++) cin >> a[i]; int l = 0, f = n - 1; cin >> k; int cnt = 0; while (f >= l) { int m = (f + l) / 2; cnt++; if (a[m] > k) f = m - 1; else if (a[m] < k) l = m + 1; else break; } cout << cnt << endl; } }
[["-", 8, 9, 0, 52, 15, 339, 51, 16, 17, 47], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 20], ["-", 8, 9, 0, 57, 64, 1, 0, 11, 31, 22], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 31, 22], ["-", 0, 57, 64, 1, 0, 11, 12, 16, 17, 72], ["+", 0, 57, 64, 1, 0, 11, 12, 16, 17, 33], ["-", 75, 76, 0, 57, 64, 1, 0, 11, 31, 22], ["+", 75, 76, 0, 57, 64, 1, 0, 11, 31, 22], ["-", 0, 57, 64, 1, 0, 11, 12, 16, 17, 33], ["+", 0, 57, 64, 1, 0, 11, 12, 16, 17, 72]]
1
135
#include <iostream> #include <vector> using namespace std; int main() { int n; while (cin >> n, n) { vector<int> v; int f; for (int i = 0; i < n; i++) { int tmp; cin >> tmp; v.push_back(tmp); } cin >> f; int l, r; l = 0; r = v.size() - 1; int ans = 0; while (l <= r) { int m; m = (l + r) / 2; ans++; if (v[m] == m) break; else if (v[m] < f) l = m + 1; else r = m - 1; } cout << ans << endl; } }
#include <iostream> #include <vector> using namespace std; int main() { int n; while (cin >> n, n) { vector<int> v; int f; for (int i = 0; i < n; i++) { int tmp; cin >> tmp; v.push_back(tmp); } cin >> f; int l, r; l = 0; r = v.size() - 1; int ans = 0; while (l <= r) { int m; m = (l + r) / 2; ans++; if (v[m] == f) break; else if (v[m] < f) l = m + 1; else r = m - 1; } cout << ans << endl; } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22]]
1
158
#include <iostream> using namespace std; int main() { int num[101], r, l, c; int kazu, target, t; int i; while (1) { t = 0; cin >> kazu; if (kazu == 0) { break; } for (i = 0; i < kazu; i++) { cin >> num[i]; } cin >> target; l = 0; r = (kazu - 1); t = 0; while (1) { c = (l + r) / 2; t++; if (num[c] == target) { break; } else if (r <= l) { break; } else if (num[c] < target) { l = c + 1; } else { r = c - 1; } } cout << t << endl; } }
#include <iostream> using namespace std; int main() { int num[101], r, l, c; int kazu, target, t; int i; while (1) { t = 0; cin >> kazu; if (kazu == 0) { break; } for (i = 0; i < kazu; i++) { cin >> num[i]; } cin >> target; l = 0; r = (kazu - 1); t = 0; while (r >= l) { c = (l + r) / 2; t++; if (num[c] == target) { break; } else if (r <= l) { break; } else if (num[c] < target) { l = c + 1; } else { r = c - 1; } } cout << t << endl; } }
[["-", 0, 52, 8, 9, 0, 52, 15, 339, 51, 13], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 31, 22], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 20], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 12, 22]]
1
176
#include <iostream> using namespace std; int box[10000]; int num = 0; int findn; bool check(int begin, int end) { if (begin > end) return true; num++; if (box[(begin + end) / 2] > findn) check(begin, (begin + end) / 2 - 1); else if (box[(begin + end) / 2] < findn) check((begin + end) / 2 + 1, end); else if (box[(begin + end) / 2] == findn) return true; return false; } int main() { int n; while (1) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { cin >> box[i]; } cin >> findn; bool TF = check(0, n - 1); cout << num << endl; } return 0; }
#include <iostream> using namespace std; int box[10000]; int num = 0; int findn; bool check(int begin, int end) { if (begin > end) return true; num++; if (box[(begin + end) / 2] > findn) check(begin, (begin + end) / 2 - 1); else if (box[(begin + end) / 2] < findn) check((begin + end) / 2 + 1, end); else if (box[(begin + end) / 2] == findn) return true; return false; } int main() { int n; while (1) { num = 0; cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { cin >> box[i]; } cin >> findn; bool TF = check(0, n - 1); cout << num << endl; } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
201
#include <iostream> #include <string> #include <vector> using namespace std; int v[100]; int main() { int n; while (cin >> n, n) { for (int i = 0; i < n; ++i) { cin >> v[i]; } int search; cin >> search; int l = 0, h = n - 1, res = 0; while (l <= h) { int mid = (l + h) / 2; ++res; if (mid == search) { break; } else if (search < mid) { h = mid - 1; } else { l = mid + 1; } } cout << res << endl; } return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; int v[100]; int main() { int n; while (cin >> n, n) { for (int i = 0; i < n; ++i) { cin >> v[i]; } int search; cin >> search; int l = 0, h = n - 1, res = 0; while (l <= h) { int mid = (l + h) / 2; ++res; if (v[mid] == search) { break; } else if (search < v[mid]) { h = mid - 1; } else { l = mid + 1; } } cout << res << endl; } return 0; }
[["+", 0, 57, 15, 339, 51, 16, 31, 69, 28, 22], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 70], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 73], ["+", 0, 57, 15, 339, 51, 16, 12, 69, 28, 22], ["+", 15, 339, 51, 16, 12, 69, 341, 342, 0, 70], ["+", 15, 339, 51, 16, 12, 69, 341, 342, 0, 73]]
1
145
#include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> ////////////////*My library*//////////////////////// #define rep_to(a, i, n) for (i = a; i < n; i++) #define rep(i, n) for (i = 0; i < n; i++) #define rep_r(i, n) for (i = n - 1; i >= 0; i--) #define debug_i(x) printf("x:%d\n", x) #define debug_f(x) printf("x:%f\n", x) #define debug_s(str) printf("str:%s\n", str) #define debug_c(c) printf("c:%c\n", c) #define s_i1(x) scanf("%d", &x) #define s_i2(x, y) scanf("%d %d", &x, &y) #define s_i3(x, y, z) scanf("%d %d %d", &x, &y, &z) //////////////////////////////////////////////////// //////////////////////// /*prime()*/ /*primecount()*/ /*sort()*/ /////////////////////// int main() { int i, j; int n, num[100000] = {}, key; while (1) { s_i1(n); if (n == 0) break; rep(i, n) s_i1(num[i]); s_i1(key); /////????????¢?´¢////////////////// int min = 0; int max = n - 1; int mid = (min + max) / 2; int count = 1; while (min < max) { count++; mid = (min + max) / 2; if (key == num[mid]) break; else if (key > num[mid]) min = mid + 1; else if (key < num[mid]) max = mid - 1; } /////////////////////////////// printf("%d\n", count + 1); } return 0; }
#include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> ////////////////*My library*//////////////////////// #define rep_to(a, i, n) for (i = a; i < n; i++) #define rep(i, n) for (i = 0; i < n; i++) #define rep_r(i, n) for (i = n - 1; i >= 0; i--) #define debug_i(x) printf("x:%d\n", x) #define debug_f(x) printf("x:%f\n", x) #define debug_s(str) printf("str:%s\n", str) #define debug_c(c) printf("c:%c\n", c) #define s_i1(x) scanf("%d", &x) #define s_i2(x, y) scanf("%d %d", &x, &y) #define s_i3(x, y, z) scanf("%d %d %d", &x, &y, &z) //////////////////////////////////////////////////// //////////////////////// /*prime()*/ /*primecount()*/ /*sort()*/ /////////////////////// int main() { int i, j; int n, num[100000] = {}, key; while (1) { s_i1(n); if (n == 0) break; rep(i, n) s_i1(num[i]); s_i1(key); /////????????¢?´¢////////////////// int min = 0; int max = n - 1; int mid = (min + max) / 2; int count = 0; while (min <= max) { count++; mid = (min + max) / 2; if (key == num[mid]) break; else if (key > num[mid]) min = mid + 1; else if (key < num[mid]) max = mid - 1; } /////////////////////////////// printf("%d\n", count); } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["-", 8, 9, 0, 52, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 19], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
263
#include <algorithm> #include <iostream> using namespace std; int T[100010]; int find(int m, int x) { int r = 0, l = m, ans = 0; while (1) { ans++; if (r >= l) break; if (T[(r + l) / 2] == x) return ans; if (T[(r + l) / 2] > x) l = (r + l) / 2 - 1; else if (T[(r + l) / 2] < x) r = (r + l) / 2 + 1; if (r > l) break; } return ans; } int main() { int n, k, num, ans; while (1) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { cin >> T[i]; } cin >> k; ans = find(n, k); cout << ans << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int T[100010]; int find(int m, int x) { int r = 0, l = m - 1, ans = 0; while (1) { ans++; if (r >= l) break; if (T[(r + l) / 2] == x) return ans; if (T[(r + l) / 2] > x) l = (r + l) / 2 - 1; else if (T[(r + l) / 2] < x) r = (r + l) / 2 + 1; if (r > l) break; } return ans; } int main() { int n, k, num, ans; while (1) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { cin >> T[i]; } cin >> k; ans = find(n, k); cout << ans << endl; } return 0; }
[["+", 8, 9, 0, 43, 49, 50, 51, 16, 17, 33], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 12, 13]]
1
217
#include <iostream> using namespace std; int a[1000], n, cnt, L, R, M, p; int main() { while (true) { cin >> n; if (n == 0) { break; } for (int i = 0; i < n; i++) { cin >> a[i]; } cin >> p; L = 0; R = n - 1; cnt = 0; while (R - L >= 1) { cnt++; M = (R + L) / 2; if (p == a[M]) { break; } if (p > a[M]) { L = M + 1; } if (p < a[M]) { R = M - 1; } } cout << cnt + 1 << endl; } }
#include <iostream> using namespace std; int a[1000], n, cnt, L, R, M, p; int main() { while (true) { cin >> n; if (n == 0) { break; } for (int i = 0; i < n; i++) { cin >> a[i]; } cin >> p; L = 0; R = n - 1; cnt = 0; while (R >= L) { cnt++; M = (R + L) / 2; if (p == a[M]) { break; } if (p > a[M]) { L = M + 1; } if (p < a[M]) { R = M - 1; } } cout << cnt << endl; } }
[["-", 0, 52, 15, 339, 51, 16, 31, 16, 17, 33], ["-", 0, 52, 15, 339, 51, 16, 31, 16, 12, 22], ["-", 8, 9, 0, 52, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 12, 22], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 17, 72], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
169
#include <iostream> using namespace std; int a[100]; int main() { int n, key; while (true) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) cin >> a[i]; cin >> key; int ans = 0; int left = 0, right = n - 1, mid; while (true) { if (left > right) break; mid = (left + right) / 2; ans++; if (a[mid] == key) break; if (a[mid] < key) left = mid + 1; if (a[mid] > key) right = mid - 1; ans++; } cout << ans << endl; } return 0; }
#include <iostream> using namespace std; int a[100]; int main() { int n, key; while (true) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) cin >> a[i]; cin >> key; int ans = 0; int left = 0, right = n - 1, mid; while (true) { if (left > right) break; mid = (left + right) / 2; ans++; if (a[mid] == key) break; if (a[mid] < key) left = mid + 1; if (a[mid] > key) right = mid - 1; } cout << ans << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 1, 0, 27, 28, 22], ["-", 0, 52, 8, 9, 0, 1, 0, 27, 17, 29], ["-", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
164
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <sstream> #include <vector> using namespace std; int a[100000]; int lef; int rig; int ans; bool binary_search_2(int num) { if (lef == rig) { ans++; return false; } int middle = (lef + rig) / 2; ans++; if (a[middle] > num) { rig = middle - 1; binary_search_2(num); } else if (a[middle] < num) { lef = middle + 1; binary_search_2(num); } else if (a[middle] == num) { return true; } } bool binary_search(int num, int n) { lef = 0; rig = n - 1; binary_search_2(num); } int main() { int n, k; while (true) { cin >> n; if (n == 0) { break; } for (int i = 0; i < n; i++) { cin >> a[i]; } cin >> k; ans = 0; binary_search(k, n); cout << ans << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <sstream> #include <vector> using namespace std; int a[100000]; int lef; int rig; int ans; bool binary_search_2(int num) { if (lef > rig) { return false; } int middle = (lef + rig) / 2; ans++; if (a[middle] > num) { rig = middle - 1; binary_search_2(num); } else if (a[middle] < num) { lef = middle + 1; binary_search_2(num); } else if (a[middle] == num) { return true; } } bool binary_search(int num, int n) { lef = 0; rig = n - 1; binary_search_2(num); } int main() { int n, k; while (true) { cin >> n; if (n == 0) { break; } for (int i = 0; i < n; i++) { cin >> a[i]; } cin >> k; ans = 0; binary_search(k, n); cout << ans << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["-", 0, 57, 64, 9, 0, 1, 0, 27, 28, 22], ["-", 0, 57, 64, 9, 0, 1, 0, 27, 17, 29], ["-", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35]]
1
233
#include <iostream> using namespace std; int nibn(int a[], int s, int e, int k); int nibn(int a[], int s, int e, int k) { if (s > e) return 1; if (a[(s + e) / 2] == k) { return 1; } else if (a[(s + e) / 2] < k) { return 1 + nibn(a, (s + e) / 2 + 1, e, k); } else if (a[(s + e) / 2] > k) { return 1 + nibn(a, s, (s + e) / 2 - 1, k); } } int main() { int n, a[150], k; while (cin >> n) { if (!n) break; for (int i = 0; i < n; i++) { cin >> a[i]; } cin >> k; cout << nibn(a, 0, n - 1, k) << endl; } return 0; }
#include <iostream> using namespace std; int nibn(int a[], int s, int e, int k); int nibn(int a[], int s, int e, int k) { if (s > e) return 0; if (a[(s + e) / 2] == k) { return 1; } else if (a[(s + e) / 2] < k) { return 1 + nibn(a, (s + e) / 2 + 1, e, k); } else if (a[(s + e) / 2] > k) { return 1 + nibn(a, s, (s + e) / 2 - 1, k); } } int main() { int n, a[150], k; while (cin >> n) { if (!n) break; for (int i = 0; i < n; i++) { cin >> a[i]; } cin >> k; cout << nibn(a, 0, n - 1, k) << endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 57, 64, 37, 0, 13], ["+", 0, 14, 8, 9, 0, 57, 64, 37, 0, 13]]
1
230
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(v) (v).begin(), (v).end() #define fi first #define se second template <typename A, typename B> inline bool chmax(A &a, B b) { if (a < b) { a = b; return 1; } return 0; } template <typename A, typename B> inline bool chmin(A &a, B b) { if (a > b) { a = b; return 1; } return 0; } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, pll> pip; const ll INF = 1ll << 60; const ll MOD = 1000000007; const double EPS = 1e-9; int n; int a[112345]; int main() { while (cin >> n, n) { REP(i, n) scanf("%d", a + i); int k; cin >> k; int l = 0, r = n - 1; int c = 0; while (r - l >= 0) { int m = (l + r) / 2; c++; if (r == l) break; if (a[m] < k) l = m + 1; else r = m - 1; } cout << c << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(v) (v).begin(), (v).end() #define fi first #define se second template <typename A, typename B> inline bool chmax(A &a, B b) { if (a < b) { a = b; return 1; } return 0; } template <typename A, typename B> inline bool chmin(A &a, B b) { if (a > b) { a = b; return 1; } return 0; } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, pll> pip; const ll INF = 1ll << 60; const ll MOD = 1000000007; const double EPS = 1e-9; int n; int a[112345]; int main() { while (cin >> n, n) { REP(i, n) scanf("%d", a + i); int k; cin >> k; int l = 0, r = n - 1; int c = 0; while (r - l >= 0) { int m = (l + r) / 2; c++; if (a[m] == k) break; if (a[m] < k) l = m + 1; else r = m - 1; } cout << c << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 31, 69, 28, 22], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 70], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 73], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22]]
1
294
#include <stdio.h> int main() { int n, i, a[100], l, m, r, x, c; while (scanf("%d", &n), n) { for (i = 0; i < n; ++i) scanf("%d", &a[i]); scanf("%d", &x); for (c = l = 0, r = n; l < r;) { ++c; m = (l + r) / 2; if (a[m] == x) break; else if (a[m] < x) l = m + 1; else r = m; } printf("%d\n", c); } return 0; }
#include <stdio.h> int main() { int n, i, a[100], l, m, r, x, c; while (scanf("%d", &n), n) { for (i = 0; i < n; ++i) scanf("%d", &a[i]); scanf("%d", &x); for (c = l = 0, r = n - 1; l <= r;) { ++c; m = (l + r) / 2; if (a[m] == x) break; else if (a[m] < x) l = m + 1; else r = m - 1; } printf("%d\n", c); } return 0; }
[["+", 0, 7, 10, 34, 12, 11, 12, 16, 17, 33], ["+", 0, 7, 10, 34, 12, 11, 12, 16, 12, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 75, 76, 0, 1, 0, 11, 12, 16, 17, 33], ["+", 75, 76, 0, 1, 0, 11, 12, 16, 12, 13]]
1
157
#include <iostream> #include <vector> using namespace std; int main() { int n, i; int m; int left, right, count; vector<int> v; while (cin >> n, n) { v.clear(); v.resize(n); for (i = 0; i < n; i++) cin >> v[i]; cin >> m; count = 1; left = 0; right = n - 1; while (left < right) { count++; if (v[(right - left) / 2 + left] > m) right = (right - left) / 2 + left - 1; else if (v[(right - left) / 2 + left] < m) left = (right - left) / 2 + left + 1; else { left = right = (right - left) / 2 + left; count--; } } cout << count << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; int main() { int n, i; int m; int left, right, count; vector<int> v; while (cin >> n, n) { v.clear(); v.resize(n); for (i = 0; i < n; i++) cin >> v[i]; cin >> m; count = 0; left = 0; right = n - 1; while (left <= right) { count++; if (v[(right - left) / 2 + left] > m) right = (right - left) / 2 + left - 1; else if (v[(right - left) / 2 + left] < m) left = (right - left) / 2 + left + 1; else { left = right = (right - left) / 2 + left; break; } } cout << count << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["-", 8, 9, 0, 52, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 19], ["-", 75, 76, 0, 9, 0, 1, 0, 27, 28, 22], ["-", 75, 76, 0, 9, 0, 1, 0, 27, 17, 68], ["+", 0, 57, 75, 76, 0, 9, 0, 93, 0, 94]]
1
199
#include <cmath> #include <cstdio> int n, x; int a[100]; int main() { while (scanf("%d", &n)) { if (!n) break; for (int i = 0; i < n; i++) scanf("%d", &a[i]); scanf("%d", &x); int lb = 0, ub = n - 1, res = 0; while (ub - lb >= 1) { res++; int mid = floor((lb + ub) / 2); if (a[mid] < x) { lb = mid + 1; } else if (a[mid] > x) { ub = mid - 1; } else { break; } } printf("%d\n", res + 1); } }
#include <cmath> #include <cstdio> int n, x; int a[100]; int main() { while (scanf("%d", &n)) { if (!n) break; for (int i = 0; i < n; i++) scanf("%d", &a[i]); scanf("%d", &x); int lb = 0, ub = n - 1, res = 0; while (ub >= lb) { res++; int mid = floor((lb + ub) / 2); if (a[mid] < x) { lb = mid + 1; } else if (a[mid] > x) { ub = mid - 1; } else { break; } } printf("%d\n", res); } }
[["-", 0, 52, 15, 339, 51, 16, 31, 16, 17, 33], ["-", 0, 52, 15, 339, 51, 16, 31, 16, 12, 22], ["-", 8, 9, 0, 52, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 12, 22], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
173
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const double EPS = 1e-9; typedef vector<int> vint; typedef pair<int, int> pint; #define rep(i, n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for (int i = x; i < n; i++) template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); } template <class T, class C> void chmax(T &a, C b) { if (a < b) a = b; } template <class T, class C> void chmin(T &a, C b) { if (b < a) a = b; } template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); } int main() { for (int n; cin >> n;) { int target, ans = 0; vint v(n); rep(i, n) cin >> v[i]; cin >> target; for (int lb = 0, ub = n - 1; lb <= ub; ans++) { int mid = (ub + lb) / 2; if (v[mid] < target) lb = mid + 1; if (v[mid] > target) ub = mid - 1; if (v[mid] == target) { ans++; break; } } cout << ans << endl; } }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const double EPS = 1e-9; typedef vector<int> vint; typedef pair<int, int> pint; #define rep(i, n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for (int i = x; i < n; i++) template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); } template <class T, class C> void chmax(T &a, C b) { if (a < b) a = b; } template <class T, class C> void chmin(T &a, C b) { if (b < a) a = b; } template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); } int main() { for (int n; cin >> n && n;) { int target, ans = 0; vint v(n); rep(i, n) cin >> v[i]; cin >> target; for (int lb = 0, ub = n - 1; lb <= ub; ans++) { int mid = (ub + lb) / 2; if (v[mid] < target) lb = mid + 1; if (v[mid] > target) ub = mid - 1; if (v[mid] == target) { ans++; break; } } cout << ans << endl; } }
[["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 98], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22]]
1
360
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <string> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define INF 1 << 27 #define all(n) n.begin(), n.end() #define insert(a, b, c, d) PP(P(a, b), P(c, d)) #define F first #define S second #define pb push_back #define pf push_front #define LIM 100000 using namespace std; typedef pair<int, int> P; typedef pair<P, P> PP; typedef long long ll; typedef unsigned long long ull; int main() { while (true) { int n; cin >> n; if (n == 0) break; vector<int> vec; rep(i, n) { int in; cin >> in; vec.pb(in); } int key, cnt = 0; cin >> key; int l, r, m; l = 0, r = n - 1; while (l < r) { m = (l + r) / 2; cnt++; if (vec[m] == key) break; if (vec[m] > key) r = m; else l = m + 1; } cout << cnt << endl; } return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <string> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define INF 1 << 27 #define all(n) n.begin(), n.end() #define insert(a, b, c, d) PP(P(a, b), P(c, d)) #define F first #define S second #define pb push_back #define pf push_front #define LIM 100000 using namespace std; typedef pair<int, int> P; typedef pair<P, P> PP; typedef long long ll; typedef unsigned long long ull; int main() { while (true) { int n; cin >> n; if (n == 0) break; vector<int> vec; rep(i, n) { int in; cin >> in; vec.pb(in); } int key, cnt = 0; cin >> key; int l, r, m; l = 0, r = n - 1; while (l <= r) { m = (l + r) / 2; cnt++; if (vec[m] == key) break; if (vec[m] > key) r = m - 1; else l = m + 1; } cout << cnt << endl; } return 0; }
[["-", 8, 9, 0, 52, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 19], ["+", 0, 57, 64, 1, 0, 11, 12, 16, 17, 33], ["+", 0, 57, 64, 1, 0, 11, 12, 16, 12, 13]]
1
273
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int to_int(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string to_str(T x) { ostringstream sout; sout << x; return sout.str(); } typedef long long ll; const int INF = 1 << 30; int main() { int n; while (cin >> n) { vector<int> T; int res = 0, num, up = n - 1, low = 0; for (int i = 0; i < n; i++) { int a; cin >> a; T.push_back(a); } cin >> num; while (low <= up) { int mid = (up + low) / 2; res++; if (T[mid] > num) { up = mid - 1; } else if (T[mid] < num) { low = mid + 1; } else { break; } } cout << res << endl; } }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int to_int(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string to_str(T x) { ostringstream sout; sout << x; return sout.str(); } typedef long long ll; const int INF = 1 << 30; int main() { int n; while (cin >> n, n) { vector<int> T; int res = 0, num, up = n - 1, low = 0; for (int i = 0; i < n; i++) { int a; cin >> a; T.push_back(a); } cin >> num; while (low <= up) { int mid = (up + low) / 2; res++; if (T[mid] > num) { up = mid - 1; } else if (T[mid] < num) { low = mid + 1; } else { break; } } cout << res << endl; } }
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
1
248
import java.util.*; class Main { public static void main(String[] z) { Scanner s = new Scanner(System.in); for (int a[][] = new int[10][10], m, n, i, j, u, v, r = Integer.MAX_VALUE / 2; (m = s.nextInt()) > 0; System.out.println(u + " " + m)) { for (int[] b : a) Arrays.fill(b, r); for (i = u = n = 0; m-- > 0; n = n > i ? n : i, n = n > j ? n : j) a[i = s.nextInt()][j = s.nextInt()] = a[j][i] = s.nextInt(); for (++n; ++m < n;) for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) a[i][j] = Math.min(a[i][j], a[i][m] + a[m][j]); for (m = r; i-- > 0;) { for (j = v = 0; j < n; ++j) v += i != j ? a[i][j] : 0; if (m > v) { u = i; m = v; } } } } }
import java.util.*; class Main { public static void main(String[] z) { Scanner s = new Scanner(System.in); for (int a[][] = new int[10][10], m, n, i, j, u, v, r = Integer.MAX_VALUE / 2; (m = s.nextInt()) > 0; System.out.println(u + " " + m)) { for (int[] b : a) Arrays.fill(b, r); for (i = u = n = 0; m-- > 0; n = n > i ? n : i, n = n > j ? n : j) a[i = s.nextInt()][j = s.nextInt()] = a[j][i] = s.nextInt(); for (++n; ++m < n;) for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) a[i][j] = Math.min(a[i][j], a[i][m] + a[m][j]); for (m = r; i-- > 0;) { for (j = v = 0; j < n; ++j) v += i != j ? a[i][j] : 0; if (m >= v) { u = i; m = v; } } } } }
[["-", 8, 196, 0, 57, 15, 15, 0, 16, 17, 47], ["+", 8, 196, 0, 57, 15, 15, 0, 16, 17, 20]]
3
323
import java.util.Arrays; import java.util.Scanner; public class Main { Scanner sc = new Scanner(System.in); void run() { for (;;) { int n = sc.nextInt(); if (n == 0) { break; } int[][] wf = new int[11][11]; boolean[] used = new boolean[11]; for (int i = 0; i < wf.length; i++) { Arrays.fill(wf[i], Integer.MAX_VALUE >> 4); } for (int i = 0; i < n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); int c = sc.nextInt(); wf[x][y] = c; wf[y][x] = c; wf[x][x] = 0; wf[y][y] = 0; used[x] = true; used[y] = true; } for (int k = 0; k < 11; k++) { for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { wf[i][j] = Math.min(wf[i][j], wf[i][k] + wf[k][j]); } } } int ans = -1; int min = Integer.MAX_VALUE >> 2; for (int i = 0; i < 11; i++) { int sum = 0; for (int j = 0; j < 11; j++) { if (used[j]) { sum += wf[i][j]; } } if (min > sum) { min = sum; ans = 1; } } System.out.println(ans + " " + min); } } public static void main(String[] args) { Main m = new Main(); m.run(); } }
import java.util.Arrays; import java.util.Scanner; public class Main { Scanner sc = new Scanner(System.in); void run() { for (;;) { int n = sc.nextInt(); if (n == 0) { break; } int[][] wf = new int[11][11]; boolean[] used = new boolean[11]; for (int i = 0; i < wf.length; i++) { Arrays.fill(wf[i], Integer.MAX_VALUE >> 4); } for (int i = 0; i < n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); int c = sc.nextInt(); wf[x][y] = c; wf[y][x] = c; wf[x][x] = 0; wf[y][y] = 0; used[x] = true; used[y] = true; } for (int k = 0; k < 11; k++) { for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { wf[i][j] = Math.min(wf[i][j], wf[i][k] + wf[k][j]); } } } int ans = -1; int min = Integer.MAX_VALUE >> 2; for (int i = 0; i < 11; i++) { int sum = 0; for (int j = 0; j < 11; j++) { if (used[j]) { sum += wf[i][j]; } } if (min > sum) { min = sum; ans = i; } } System.out.println(ans + " " + min); } } public static void main(String[] args) { Main m = new Main(); m.run(); } }
[["-", 0, 57, 64, 196, 0, 1, 0, 11, 12, 499], ["+", 0, 57, 64, 196, 0, 1, 0, 11, 12, 22]]
3
433
import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.util.regex.*; // AOJ public class Main { final Scanner sc = new Scanner(System.in); final int[] vx = {0, 1, 0, -1}, vy = {-1, 0, 1, 0}; static class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } Point() { this.x = 0; this.y = 0; } @Override public String toString() { return "(" + this.x + "," + this.y + ")"; } static boolean ok(int x, int y, int X, int Y, int min) { return min <= x && x < X && min <= y && y < Y; } static boolean ok(int x, int y, int X, int Y) { return ok(x, y, X, Y, 0); } static void DE(char[][] c, int sx, int sy, int ex, int ey) { out.println("--------------------"); for (int y = sy; y < ey; y++) { for (int x = sx; x < ex; x++) out.print(c[x][y]); out.println(); } out.println("--------------------"); } static void DE(int[][] c, int sx, int sy, int ex, int ey) { out.println("--------------------"); for (int y = sy; y < ey; y++) { for (int x = sx; x < ex; x++) out.print(c[x][y] + " "); out.println(); } out.println("--------------------"); } } public static void main(String[] args) { new Main().AOJ0189(); } void AOJ0189() { while (sc.hasNext()) { int n = sc.nextInt(); final int MAX = 10; if (n == 0) break; int[][] g = new int[MAX][MAX]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) g[i][j] = (i == j ? 0 : 1 << 24); } for (int i = 0; i < n; i++) { int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(); g[a][b] = c; g[b][a] = c; } for (int k = 0; k < MAX; k++) { for (int j = 0; j < MAX; j++) { for (int i = 0; i < n; i++) g[i][j] = min(g[i][j], g[i][k] + g[k][j]); } } /* for(int i=0; i<MAX; i++){ for(int j=0; j<MAX; j++) out.print(g[j][i]+" "); out.println(); } */ int m = Integer.MAX_VALUE, t = Integer.MAX_VALUE; for (int i = 0; i < MAX; i++) { if (g[i][0] >= 1 << 24) break; int temp = 0; for (int j = 0; j < MAX; j++) { temp += (g[i][j] < 1 << 24 ? g[i][j] : 0); } m = (t > temp ? i : m); t = (t > temp ? temp : t); } out.println(m + " " + t); } } void AOJ0184() { while (sc.hasNext()) { int n = sc.nextInt(); if (n == 0) break; int[] ans = new int[7]; for (int i = 0; i < n; i++) { int in = sc.nextInt(), p = 0; while (true) { in -= 10; if (in < 0) { ans[p]++; break; } if (++p >= 6) { ans[6]++; break; } } } for (int i = 0; i < 7; i++) out.println(ans[i]); } } void AOJ0183() { while (sc.hasNext()) { int[][] b = new int[3][3], w = new int[3][3]; for (int y = 0; y < 3; y++) { String line = sc.next(); if (line.equals("0")) System.exit(0); for (int x = 0; x < 3; x++) { b[x][y] = (line.charAt(x) == 'b' ? 1 : 0); w[x][y] = (line.charAt(x) == 'w' ? 1 : 0); } } String ans = "NA"; for (int i = 0; i < 3; i++) { if (b[i][0] + b[i][1] + b[i][2] == 3) { ans = "b"; break; } if (w[i][0] + w[i][1] + w[i][2] == 3) { ans = "w"; break; } if (b[0][i] + b[1][i] + b[2][i] == 3) { ans = "b"; break; } if (w[0][i] + w[1][i] + w[2][i] == 3) { ans = "w"; break; } } if (!ans.equals("NA")) { out.println(ans); continue; } if (b[0][0] + b[1][1] + b[2][2] == 3 || b[2][0] + b[1][1] + b[0][2] == 3) ans = "b"; if (w[0][0] + w[1][1] + w[2][2] == 3 || w[2][0] + w[1][1] + w[0][2] == 3) ans = "w"; out.println(ans); } } // DP void AOJ1126() { while (sc.hasNext()) { int W = sc.nextInt(), H = sc.nextInt(); if ((W | H) == 0) break; char[][] c = new char[W][H]; for (int y = 0; y < H; y++) { String line = sc.next(); for (int x = 0; x < W; x++) c[x][y] = line.charAt(x); } String[][] dp = new String[W][H]; for (int i = 0; i < W; i++) { for (int j = 0; j < H; j++) dp[i][j] = ""; } String ans = ""; for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { if (Character.isDigit(c[x][y])) { dp[x][y] = max1126((x - 1 >= 0 ? dp[x - 1][y] : ""), (y - 1 >= 0 ? dp[x][y - 1] : "")) + Character.digit(c[x][y], 10); ans = max1126(ans, dp[x][y]); } } } out.println(ans); } } String max1126(String a, String b) { if (a == "") return b; if (b == "") return a; while (a.charAt(0) == '0') { if (a.length() <= 1) { a = ""; break; } else a = a.substring(1); } while (b.charAt(0) == '0') { if (b.length() <= 1) { b = ""; break; } else b = b.substring(1); } if (a == "") return b; if (b == "") return a; if (a.length() > b.length()) return a; if (b.length() > a.length()) return b; for (int i = 0; i < a.length(); i++) { if (a.charAt(i) > b.charAt(i)) return a; if (a.charAt(i) < b.charAt(i)) return b; } return a; } // TODO TLE void AOJ0185() { ArrayList<Integer> prime = SieveList(1000000); while (sc.hasNext()) { int n = sc.nextInt(), ans = 0; if (n == 0) break; for (int i = 0; i < prime.size(); i++) { if (prime.get(i) >= n) break; for (int j = i; j < prime.size(); j++) { int temp = prime.get(i) + prime.get(j); if (temp >= n) { // if(temp==n) out.println(" "+prime.get(i)+" + "+prime.get(j)+" = // "+temp); ans += (temp == n ? 1 : 0); break; } } } out.println(ans); } } void AOJ0174() { while (sc.hasNext()) { char[] c = sc.next().toCharArray(); if (c[0] == '0') break; int a = 0, b = 0; for (int i = 1; i < c.length; i++) { if (c[i] == 'A') a++; else if (c[i] == 'B') b++; } a += (a > b ? 1 : 0); b += (b > a ? 1 : 0); out.println(a + " " + b); } } void AOJ0173() { for (int i = 0; i < 9; i++) { String s = sc.next(); int a = sc.nextInt(), b = sc.nextInt(); out.println(s + " " + (a + b) + " " + (a * 200 + b * 300)); } } // 途中 void AOJ0121() { while (sc.hasNext()) { int[] b = new int[8]; for (int i = 0; i < 8; i++) b[i] = sc.nextInt(); LinkedList<int[]> open = new LinkedList<int[]>(); open.add(b); HashSet<int[]> close = new HashSet<int[]>(); close.add(b); int ans = 0; while (!open.isEmpty()) { int[] now = open.poll(); if (solve121(now)) break; for (int i = 0; i < 7; i++) { int[] temp = now.clone(); int temp2 = temp[i + 1]; temp[i + 1] = temp[i]; temp[i] = temp2; if (close.contains(temp)) continue; if (solve121(temp)) { open.clear(); break; } open.add(temp); close.add(temp); } ans++; } out.println(ans); } } boolean solve121(int[] b) { for (int i = 0; i < 8; i++) if (b[i] != i) return false; return true; } boolean cont121(int[] a, int[] b) { for (int i = 0; i < 8; i++) if (a[i] != b[i]) return false; return true; } void AOJ1104() { while (sc.hasNext()) { int W = sc.nextInt(), H = sc.nextInt(); if ((W | H) == 0) break; int x = 1, y = 1, d = 2; // 右-1 左+1 while (true) { String s = sc.next(); if (s.equals("STOP")) break; else if (s.equals("FORWARD")) { int n = sc.nextInt(); x = max(1, min(W, x + n * vx[d])); y = max(1, min(H, y + n * vy[d])); } else if (s.equals("BACKWARD")) { int n = sc.nextInt(); x = max(1, min(W, x + n * -1 * vx[d])); y = max(1, min(H, y + n * -1 * vy[d])); } else if (s.equals("RIGHT")) d = (d == 0 ? 3 : d - 1); else if (s.equals("LEFT")) d = (d == 3 ? 0 : d + 1); // out.println("D"+x+" "+y+" "+d); } out.println(x + " " + y); } } void AOJ1114() { int N = sc.nextInt(); for (int I = 1; I <= N; I++) { int[][] b = new int[5][5]; for (int y = 0; y < 5; y++) { for (int x = 0; x < 5; x++) b[x][y] = sc.nextInt(); } int ans = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (b[i][j] == 1) ans = max(ans, solve1114(i, j, b)); } } out.println(ans); } } int solve1114(int x, int y, int[][] b) { int ans = 0; for (int i = 0; i + x < 5; i++) { if (b[i + x][y] == 0) break; boolean flag = true; for (int j = 0; j + y < 5; j++) { for (int k = 0; k <= i; k++) { for (int l = 0; l <= j; l++) { if (b[k + x][l + y] == 0) { flag = false; break; } } if (!flag) break; } if (!flag) break; ans = max(ans, (i + 1) * (j + 1)); } } return ans; } int step = 0; // バックトラック(TLE) void Old2AOJ1126() { while (sc.hasNext()) { int W = sc.nextInt(), H = sc.nextInt(); if ((W | H) == 0) break; char[][] c = new char[W][H]; for (int y = 0; y < H; y++) { String line = sc.next(); for (int x = 0; x < W; x++) c[x][y] = line.charAt(x); } String ans = ""; for (int x = 0; x < W; x++) { for (int y = 0; y < H; y++) { if (Character.isDigit(c[x][y])) { if (x > 0) if (Character.isDigit(c[x - 1][y]) && c[x - 1][y] != '0') continue; if (y > 0) if (Character.isDigit(c[x][y - 1]) && c[x][y - 1] != '0') continue; if (c[x][y] == '0') continue; String temp = solve1126(x, y, "" + c[x][y], c, W, H); ans = compara1126(temp, ans) ? temp : ans; } } } out.println(ans); } // debug out.println(step + "steps"); } String solve1126(int x, int y, String ans, char[][] c, int W, int H) { String r = ans; // debug step++; // out.println(ans); for (int i = 1; i < 3; i++) { int xx = x + vx[i], yy = y + vy[i]; if (!Point.ok(xx, yy, W, H)) continue; if (Character.isDigit(c[xx][yy])) { String temp = solve1126(xx, yy, ans + c[xx][yy], c, W, H); r = compara1126(r, temp) ? r : temp; out.println(temp); } } return r; } // 第一引数aの方が大きい時のみtrue boolean compara1126(String a, String b) { if (a.length() > b.length()) return true; if (a.length() == b.length()) { for (int i = 0; i < a.length(); i++) { if (a.charAt(i) > b.charAt(i)) return true; if (a.charAt(i) < b.charAt(i)) return false; } } return false; } // 横型探索(MLE) void OldAOJ1126() { int step = 0; while (sc.hasNext()) { int W = sc.nextInt(), H = sc.nextInt(); String ans = ""; if ((W | H) == 0) break; char[][] c = new char[W][H]; String[][] close = new String[W][H]; LinkedList<C1126> open = new LinkedList<C1126>(); for (int y = 0; y < H; y++) { String line = sc.next(); for (int x = 0; x < W; x++) { c[x][y] = line.charAt(x); if (Character.isDigit(line.charAt(x))) { if (x > 0) if (Character.isDigit(c[x - 1][y])) continue; if (y > 0) if (Character.isDigit(c[x][y - 1])) continue; open.add(new C1126(x, y, "" + c[x][y])); close[x][y] = "" + c[x][y]; } } } while (!open.isEmpty()) { C1126 now = open.poll(); // debug out.println("ANS" + ans + " NOW" + now.ans); ans = now.compara(ans) ? new String(now.ans) : ans; for (int i = 1; i < 3; i++) { int xx = now.x + vx[i], yy = now.y + vy[i]; if (!Point.ok(xx, yy, W, H)) continue; if (Character.isDigit(c[xx][yy])) { C1126 temp = new C1126(xx, yy, now.ans + c[xx][yy]); if (close[xx][yy] == null) { open.add(temp); close[xx][yy] = temp.ans; step++; } else if (temp.compara(close[xx][yy])) { open.add(temp); close[xx][yy] = temp.ans; step++; } } } } out.println(ans); } out.println(step + "step"); } class C1126 { int x, y; String ans; C1126(int x, int y, String ans) { this.x = x; this.y = y; if (ans.length() > 0) { int i = 0; while (ans.charAt(i) == '0') { if (++i >= ans.length() - 1) break; ans = ans.substring(i); } } this.ans = ans; } boolean compara(String ans) { if (this.ans.length() > ans.length()) return true; if (this.ans.length() == ans.length()) { for (int i = 0; i < ans.length(); i++) { if (ans.charAt(i) < this.ans.charAt(i)) return true; if (ans.charAt(i) > this.ans.charAt(i)) return false; } } return false; } } void AOJ0165() { final int MAX = 1000000; boolean[] prime = Sieve(MAX); int[] prime2 = new int[MAX + 1]; for (int i = 2; i <= MAX; i++) prime2[i] = (prime[i] ? prime2[i - 1] + 1 : prime2[i - 1]); while (sc.hasNext()) { int N = sc.nextInt(); if (N == 0) break; int ans = 0; for (int i = 1; i <= N; i++) { int p = sc.nextInt(), m = sc.nextInt(); int min = max(1, p - m), max = min(MAX, p + m); ans += prime2[max] - prime2[min - 1] - 1; } out.println(max(0, ans)); } } void AOJ1144() { while (sc.hasNext()) { int X = sc.nextInt(), Y = sc.nextInt(); if ((X | Y) == 0) break; char[][] c = new char[X][Y]; for (int y = 0; y < Y; y++) { String line = sc.next(); for (int x = 0; x < X; x++) { c[x][y] = line.charAt(x); } } } } // TODO 果樹園 RE 横型 void AOJ0118() { while (sc.hasNext()) { int X = sc.nextInt(), Y = sc.nextInt(), ans = 0; if ((X | Y) == 0) break; char[][] c = new char[X + 2][Y + 2]; for (int y = 0; y < Y; y++) { String line = sc.next(); for (int x = 0; x < X; x++) { c[x][y] = line.charAt(x); } } // debug // Point.DE(c, 0, 0, X, Y); for (int x = 0; x < X; x++) { for (int y = 0; y < Y; y++) { if (c[x][y] == '&') continue; ans++; LinkedList<Point> open = new LinkedList<Point>(); open.add(new Point(x, y)); c[x][y] = '&'; char mark = c[x][y]; while (!open.isEmpty()) { Point now = open.poll(); for (int i = 0; i < 4; i++) { int xx = now.x + vx[i], yy = now.y + vy[i]; if (!Point.ok(xx, yy, X, Y)) continue; if (c[xx][yy] == mark) { open.add(new Point(xx, yy)); c[xx][yy] = '&'; } } } // Point.DE(c, 0, 0, X, Y); // out.println("ANS"+ans); } } out.println(ans); } } // Red and Black 横型探索 void AOJ1130() { while (sc.hasNext()) { int X = sc.nextInt(), Y = sc.nextInt(), sx = 0, sy = 0, ans = 0; if (X == 0) break; char[][] c = new char[X][Y]; for (int y = 0; y < Y; y++) { char[] line = sc.next().toCharArray(); for (int x = 0; x < X; x++) { c[x][y] = line[x]; if (line[x] == '@') { sx = x; sy = y; } } } LinkedList<Point> open = new LinkedList<Point>(); open.add(new Point(sx, sy)); boolean[][] close = new boolean[X][Y]; while (!open.isEmpty()) { ans++; Point now = open.poll(); close[now.x][now.y] = true; for (int i = 0; i < 4; i++) { if (now.x + vx[i] < 0 || now.x + vx[i] >= X || now.y + vy[i] < 0 || now.y + vy[i] >= Y) continue; if (c[now.x + vx[i]][now.y + vy[i]] == '.' && !close[now.x + vx[i]][now.y + vy[i]]) { open.add(new Point(now.x + vx[i], now.y + vy[i])); close[now.x + vx[i]][now.y + vy[i]] = true; } } } out.println(ans); } } // TODO デパート TLE MLE WA void AOJ0223() { while (sc.hasNext()) { int X = sc.nextInt(), Y = sc.nextInt(); if (X == 0) break; int tx = sc.nextInt(), ty = sc.nextInt(), kx = sc.nextInt(), ky = sc.nextInt(); int[][] b = new int[X + 1][Y + 1]; for (int y = 1; y <= Y; y++) { for (int x = 1; x <= X; x++) b[x][y] = sc.nextInt(); } LinkedList<C0223> open = new LinkedList<C0223>(); open.add(new C0223(tx, ty, kx, ky, 0)); int[][][][] close2 = new int[X + 1][Y + 1][X + 1][Y + 1]; close2[tx][ty][kx][ky] = -1; int ans = -1; while (!open.isEmpty()) { C0223 now = open.poll(); if (now.ans()) { ans = now.sec; break; } if (now.sec >= 99) break; for (int i = 0; i < 4; i++) { if (0 < now.tx + vx[i] && now.tx + vx[i] <= X && 0 < now.ty + vy[i] && now.ty + vy[i] <= Y && 0 < now.kx - vx[i] && now.kx - vx[i] <= X && 0 < now.ky - vy[i] && now.ky - vy[i] <= Y) { tx = now.tx; ty = now.ty; kx = now.kx; ky = now.ky; if (b[tx + vx[i]][ty + vy[i]] == 0) { tx += vx[i]; ty += vy[i]; } if (b[kx - vx[i]][ky - vy[i]] == 0) { kx -= vx[i]; ky -= vy[i]; } // out.println("TEMP:"+tx+" "+ty+" "+kx+" "+ky); // if(tx==kx && ty==ky) ans=ans<0?now.sec+1:min(ans,now.sec+1); if (close2[tx][ty][kx][ky] == 0 || close2[tx][ty][kx][ky] > now.sec + 1) { // out.println("ADD:"+tx+" "+ty+" "+kx+" "+ky); open.add(new C0223(tx, ty, kx, ky, now.sec + 1)); close2[tx][ty][kx][ky] = now.sec + 1; } } } } out.println(ans < 0 ? "NA" : ans); } } class C0223 { int tx, ty, kx, ky, sec; C0223(int tx, int ty, int kx, int ky, int sec) { this.tx = tx; this.ty = ty; this.kx = kx; this.ky = ky; this.sec = sec; } boolean ans() { if (this.tx == this.kx && this.ty == this.ky) return true; else return false; } } // TODO WA void AOJ0142() { while (sc.hasNext()) { int n = sc.nextInt(); if (n == 0) break; int nn = (n - 1) / 2; boolean[] b = new boolean[n]; for (int i = 1; i < n; i++) b[(i * i) % n] = true; ArrayList<Integer> ar = new ArrayList<Integer>(); for (int i = 1; i < n; i++) { if (b[i]) ar.add(i); } int[] ans = new int[nn + 1]; for (int i = 0; i < ar.size(); i++) { for (int j = 0; j < ar.size(); j++) { if (i == j) continue; int temp = ar.get(i) - ar.get(j); temp += temp < 0 ? n : 0; temp = temp > nn ? n - temp : temp; ans[temp]++; } } for (int i = 1; i <= nn; i++) out.println(ans[i]); } } // レゴ 横型探索 void AOJ0207() { while (sc.hasNext()) { int W = sc.nextInt(), H = sc.nextInt(); if ((W | H) == 0) break; int sx = sc.nextInt(), sy = sc.nextInt(), gx = sc.nextInt(), gy = sc.nextInt(), n = sc.nextInt(); int[][] b = new int[W + 1][H + 1]; for (int i = 0; i < n; i++) { int color = sc.nextInt(), d = sc.nextInt(), x = sc.nextInt(), y = sc.nextInt(); if (d == 0) { // 横 4x2 for (int j = y; j < y + 2; j++) { for (int k = x; k < x + 4; k++) b[k][j] = color; } } else { // 縦 2x4 for (int j = y; j < y + 4; j++) { for (int k = x; k < x + 2; k++) b[k][j] = color; } } } // debug // de0207(b,W,H); int c = b[sx][sy]; if (c == 0) { out.println("NG"); continue; } LinkedList<Point> open = new LinkedList<Point>(); boolean[][] close = new boolean[W + 1][H + 1]; open.add(new Point(sx, sy)); close[sx][sy] = true; String ans = "NG"; while (!open.isEmpty()) { Point now = open.poll(); for (int i = 0; i < 4; i++) { int xx = now.x + vx[i], yy = now.y + vy[i]; if (!Point.ok(xx, yy, W + 1, H + 1, 1)) continue; if (b[xx][yy] != c) continue; if (close[xx][yy]) continue; open.add(new Point(xx, yy)); close[xx][yy] = true; if (xx == gx && yy == gy) { ans = "OK"; open.clear(); } } } out.println(ans); } } void de0207(int[][] b, int w, int h) { for (int j = 0; j <= h; j++) { for (int i = 0; i <= w; i++) out.print(b[i][j] + " "); out.println(); } for (int i = 0; i <= w; i++) out.print("-"); out.println(); } void AOJ0139() { int N = sc.nextInt(); final Pattern ap = Pattern.compile("^>'(=+)#(=+)~$"); for (int i = 1; i <= N; i++) { String s = sc.next(); Matcher m = ap.matcher(s); if (m.matches()) { if (m.group(1).length() == m.group(2).length()) { out.println("A"); continue; } } else if (Pattern.compile("^>\\^(Q=)+~~$").matcher(s).matches()) { out.println("B"); continue; } out.println("NA"); } } ArrayList<Integer> SieveList(int N) { ArrayList<Integer> prime = new ArrayList<Integer>(); boolean[] list = new boolean[N + 1]; Arrays.fill(list, true); list[0] = false; list[1] = false; prime.add(2); for (int j = 4; j <= N; j += 2) list[j] = false; for (int i = 3; i <= N; i += 2) { if (list[i]) { prime.add(i); for (int j = i + i; j <= N; j += i) list[j] = false; } } return prime; } boolean[] Sieve(int N) { boolean[] list = new boolean[N + 1]; Arrays.fill(list, true); list[0] = false; list[1] = false; for (int j = 4; j <= N; j += 2) list[j] = false; for (int i = 3; i * i <= N; i += 2) if (list[i]) for (int j = i + i; j <= N; j += i) list[j] = false; return list; } boolean isPrime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } int gcd(int x, int y) { if (y == 0) return x; else return gcd(y, x % y); } }
import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.util.regex.*; // AOJ public class Main { final Scanner sc = new Scanner(System.in); final int[] vx = {0, 1, 0, -1}, vy = {-1, 0, 1, 0}; static class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } Point() { this.x = 0; this.y = 0; } @Override public String toString() { return "(" + this.x + "," + this.y + ")"; } static boolean ok(int x, int y, int X, int Y, int min) { return min <= x && x < X && min <= y && y < Y; } static boolean ok(int x, int y, int X, int Y) { return ok(x, y, X, Y, 0); } static void DE(char[][] c, int sx, int sy, int ex, int ey) { out.println("--------------------"); for (int y = sy; y < ey; y++) { for (int x = sx; x < ex; x++) out.print(c[x][y]); out.println(); } out.println("--------------------"); } static void DE(int[][] c, int sx, int sy, int ex, int ey) { out.println("--------------------"); for (int y = sy; y < ey; y++) { for (int x = sx; x < ex; x++) out.print(c[x][y] + " "); out.println(); } out.println("--------------------"); } } public static void main(String[] args) { new Main().AOJ0189(); } void AOJ0189() { while (sc.hasNext()) { int n = sc.nextInt(); final int MAX = 10; if (n == 0) break; int[][] g = new int[MAX][MAX]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) g[i][j] = (i == j ? 0 : 1 << 24); } for (int i = 0; i < n; i++) { int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(); g[a][b] = c; g[b][a] = c; } for (int k = 0; k < MAX; k++) { for (int j = 0; j < MAX; j++) { for (int i = 0; i < MAX; i++) g[i][j] = min(g[i][j], g[i][k] + g[k][j]); } } /* for(int i=0; i<MAX; i++){ for(int j=0; j<MAX; j++) out.print(g[j][i]+" "); out.println(); } */ int m = Integer.MAX_VALUE, t = Integer.MAX_VALUE; for (int i = 0; i < MAX; i++) { if (g[i][0] >= 1 << 24) break; int temp = 0; for (int j = 0; j < MAX; j++) { temp += (g[i][j] < 1 << 24 ? g[i][j] : 0); } m = (t > temp ? i : m); t = (t > temp ? temp : t); } out.println(m + " " + t); } } void AOJ0184() { while (sc.hasNext()) { int n = sc.nextInt(); if (n == 0) break; int[] ans = new int[7]; for (int i = 0; i < n; i++) { int in = sc.nextInt(), p = 0; while (true) { in -= 10; if (in < 0) { ans[p]++; break; } if (++p >= 6) { ans[6]++; break; } } } for (int i = 0; i < 7; i++) out.println(ans[i]); } } void AOJ0183() { while (sc.hasNext()) { int[][] b = new int[3][3], w = new int[3][3]; for (int y = 0; y < 3; y++) { String line = sc.next(); if (line.equals("0")) System.exit(0); for (int x = 0; x < 3; x++) { b[x][y] = (line.charAt(x) == 'b' ? 1 : 0); w[x][y] = (line.charAt(x) == 'w' ? 1 : 0); } } String ans = "NA"; for (int i = 0; i < 3; i++) { if (b[i][0] + b[i][1] + b[i][2] == 3) { ans = "b"; break; } if (w[i][0] + w[i][1] + w[i][2] == 3) { ans = "w"; break; } if (b[0][i] + b[1][i] + b[2][i] == 3) { ans = "b"; break; } if (w[0][i] + w[1][i] + w[2][i] == 3) { ans = "w"; break; } } if (!ans.equals("NA")) { out.println(ans); continue; } if (b[0][0] + b[1][1] + b[2][2] == 3 || b[2][0] + b[1][1] + b[0][2] == 3) ans = "b"; if (w[0][0] + w[1][1] + w[2][2] == 3 || w[2][0] + w[1][1] + w[0][2] == 3) ans = "w"; out.println(ans); } } // DP void AOJ1126() { while (sc.hasNext()) { int W = sc.nextInt(), H = sc.nextInt(); if ((W | H) == 0) break; char[][] c = new char[W][H]; for (int y = 0; y < H; y++) { String line = sc.next(); for (int x = 0; x < W; x++) c[x][y] = line.charAt(x); } String[][] dp = new String[W][H]; for (int i = 0; i < W; i++) { for (int j = 0; j < H; j++) dp[i][j] = ""; } String ans = ""; for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { if (Character.isDigit(c[x][y])) { dp[x][y] = max1126((x - 1 >= 0 ? dp[x - 1][y] : ""), (y - 1 >= 0 ? dp[x][y - 1] : "")) + Character.digit(c[x][y], 10); ans = max1126(ans, dp[x][y]); } } } out.println(ans); } } String max1126(String a, String b) { if (a == "") return b; if (b == "") return a; while (a.charAt(0) == '0') { if (a.length() <= 1) { a = ""; break; } else a = a.substring(1); } while (b.charAt(0) == '0') { if (b.length() <= 1) { b = ""; break; } else b = b.substring(1); } if (a == "") return b; if (b == "") return a; if (a.length() > b.length()) return a; if (b.length() > a.length()) return b; for (int i = 0; i < a.length(); i++) { if (a.charAt(i) > b.charAt(i)) return a; if (a.charAt(i) < b.charAt(i)) return b; } return a; } // TODO TLE void AOJ0185() { ArrayList<Integer> prime = SieveList(1000000); while (sc.hasNext()) { int n = sc.nextInt(), ans = 0; if (n == 0) break; for (int i = 0; i < prime.size(); i++) { if (prime.get(i) >= n) break; for (int j = i; j < prime.size(); j++) { int temp = prime.get(i) + prime.get(j); if (temp >= n) { // if(temp==n) out.println(" "+prime.get(i)+" + "+prime.get(j)+" = // "+temp); ans += (temp == n ? 1 : 0); break; } } } out.println(ans); } } void AOJ0174() { while (sc.hasNext()) { char[] c = sc.next().toCharArray(); if (c[0] == '0') break; int a = 0, b = 0; for (int i = 1; i < c.length; i++) { if (c[i] == 'A') a++; else if (c[i] == 'B') b++; } a += (a > b ? 1 : 0); b += (b > a ? 1 : 0); out.println(a + " " + b); } } void AOJ0173() { for (int i = 0; i < 9; i++) { String s = sc.next(); int a = sc.nextInt(), b = sc.nextInt(); out.println(s + " " + (a + b) + " " + (a * 200 + b * 300)); } } // 途中 void AOJ0121() { while (sc.hasNext()) { int[] b = new int[8]; for (int i = 0; i < 8; i++) b[i] = sc.nextInt(); LinkedList<int[]> open = new LinkedList<int[]>(); open.add(b); HashSet<int[]> close = new HashSet<int[]>(); close.add(b); int ans = 0; while (!open.isEmpty()) { int[] now = open.poll(); if (solve121(now)) break; for (int i = 0; i < 7; i++) { int[] temp = now.clone(); int temp2 = temp[i + 1]; temp[i + 1] = temp[i]; temp[i] = temp2; if (close.contains(temp)) continue; if (solve121(temp)) { open.clear(); break; } open.add(temp); close.add(temp); } ans++; } out.println(ans); } } boolean solve121(int[] b) { for (int i = 0; i < 8; i++) if (b[i] != i) return false; return true; } boolean cont121(int[] a, int[] b) { for (int i = 0; i < 8; i++) if (a[i] != b[i]) return false; return true; } void AOJ1104() { while (sc.hasNext()) { int W = sc.nextInt(), H = sc.nextInt(); if ((W | H) == 0) break; int x = 1, y = 1, d = 2; // 右-1 左+1 while (true) { String s = sc.next(); if (s.equals("STOP")) break; else if (s.equals("FORWARD")) { int n = sc.nextInt(); x = max(1, min(W, x + n * vx[d])); y = max(1, min(H, y + n * vy[d])); } else if (s.equals("BACKWARD")) { int n = sc.nextInt(); x = max(1, min(W, x + n * -1 * vx[d])); y = max(1, min(H, y + n * -1 * vy[d])); } else if (s.equals("RIGHT")) d = (d == 0 ? 3 : d - 1); else if (s.equals("LEFT")) d = (d == 3 ? 0 : d + 1); // out.println("D"+x+" "+y+" "+d); } out.println(x + " " + y); } } void AOJ1114() { int N = sc.nextInt(); for (int I = 1; I <= N; I++) { int[][] b = new int[5][5]; for (int y = 0; y < 5; y++) { for (int x = 0; x < 5; x++) b[x][y] = sc.nextInt(); } int ans = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (b[i][j] == 1) ans = max(ans, solve1114(i, j, b)); } } out.println(ans); } } int solve1114(int x, int y, int[][] b) { int ans = 0; for (int i = 0; i + x < 5; i++) { if (b[i + x][y] == 0) break; boolean flag = true; for (int j = 0; j + y < 5; j++) { for (int k = 0; k <= i; k++) { for (int l = 0; l <= j; l++) { if (b[k + x][l + y] == 0) { flag = false; break; } } if (!flag) break; } if (!flag) break; ans = max(ans, (i + 1) * (j + 1)); } } return ans; } int step = 0; // バックトラック(TLE) void Old2AOJ1126() { while (sc.hasNext()) { int W = sc.nextInt(), H = sc.nextInt(); if ((W | H) == 0) break; char[][] c = new char[W][H]; for (int y = 0; y < H; y++) { String line = sc.next(); for (int x = 0; x < W; x++) c[x][y] = line.charAt(x); } String ans = ""; for (int x = 0; x < W; x++) { for (int y = 0; y < H; y++) { if (Character.isDigit(c[x][y])) { if (x > 0) if (Character.isDigit(c[x - 1][y]) && c[x - 1][y] != '0') continue; if (y > 0) if (Character.isDigit(c[x][y - 1]) && c[x][y - 1] != '0') continue; if (c[x][y] == '0') continue; String temp = solve1126(x, y, "" + c[x][y], c, W, H); ans = compara1126(temp, ans) ? temp : ans; } } } out.println(ans); } // debug out.println(step + "steps"); } String solve1126(int x, int y, String ans, char[][] c, int W, int H) { String r = ans; // debug step++; // out.println(ans); for (int i = 1; i < 3; i++) { int xx = x + vx[i], yy = y + vy[i]; if (!Point.ok(xx, yy, W, H)) continue; if (Character.isDigit(c[xx][yy])) { String temp = solve1126(xx, yy, ans + c[xx][yy], c, W, H); r = compara1126(r, temp) ? r : temp; out.println(temp); } } return r; } // 第一引数aの方が大きい時のみtrue boolean compara1126(String a, String b) { if (a.length() > b.length()) return true; if (a.length() == b.length()) { for (int i = 0; i < a.length(); i++) { if (a.charAt(i) > b.charAt(i)) return true; if (a.charAt(i) < b.charAt(i)) return false; } } return false; } // 横型探索(MLE) void OldAOJ1126() { int step = 0; while (sc.hasNext()) { int W = sc.nextInt(), H = sc.nextInt(); String ans = ""; if ((W | H) == 0) break; char[][] c = new char[W][H]; String[][] close = new String[W][H]; LinkedList<C1126> open = new LinkedList<C1126>(); for (int y = 0; y < H; y++) { String line = sc.next(); for (int x = 0; x < W; x++) { c[x][y] = line.charAt(x); if (Character.isDigit(line.charAt(x))) { if (x > 0) if (Character.isDigit(c[x - 1][y])) continue; if (y > 0) if (Character.isDigit(c[x][y - 1])) continue; open.add(new C1126(x, y, "" + c[x][y])); close[x][y] = "" + c[x][y]; } } } while (!open.isEmpty()) { C1126 now = open.poll(); // debug out.println("ANS" + ans + " NOW" + now.ans); ans = now.compara(ans) ? new String(now.ans) : ans; for (int i = 1; i < 3; i++) { int xx = now.x + vx[i], yy = now.y + vy[i]; if (!Point.ok(xx, yy, W, H)) continue; if (Character.isDigit(c[xx][yy])) { C1126 temp = new C1126(xx, yy, now.ans + c[xx][yy]); if (close[xx][yy] == null) { open.add(temp); close[xx][yy] = temp.ans; step++; } else if (temp.compara(close[xx][yy])) { open.add(temp); close[xx][yy] = temp.ans; step++; } } } } out.println(ans); } out.println(step + "step"); } class C1126 { int x, y; String ans; C1126(int x, int y, String ans) { this.x = x; this.y = y; if (ans.length() > 0) { int i = 0; while (ans.charAt(i) == '0') { if (++i >= ans.length() - 1) break; ans = ans.substring(i); } } this.ans = ans; } boolean compara(String ans) { if (this.ans.length() > ans.length()) return true; if (this.ans.length() == ans.length()) { for (int i = 0; i < ans.length(); i++) { if (ans.charAt(i) < this.ans.charAt(i)) return true; if (ans.charAt(i) > this.ans.charAt(i)) return false; } } return false; } } void AOJ0165() { final int MAX = 1000000; boolean[] prime = Sieve(MAX); int[] prime2 = new int[MAX + 1]; for (int i = 2; i <= MAX; i++) prime2[i] = (prime[i] ? prime2[i - 1] + 1 : prime2[i - 1]); while (sc.hasNext()) { int N = sc.nextInt(); if (N == 0) break; int ans = 0; for (int i = 1; i <= N; i++) { int p = sc.nextInt(), m = sc.nextInt(); int min = max(1, p - m), max = min(MAX, p + m); ans += prime2[max] - prime2[min - 1] - 1; } out.println(max(0, ans)); } } void AOJ1144() { while (sc.hasNext()) { int X = sc.nextInt(), Y = sc.nextInt(); if ((X | Y) == 0) break; char[][] c = new char[X][Y]; for (int y = 0; y < Y; y++) { String line = sc.next(); for (int x = 0; x < X; x++) { c[x][y] = line.charAt(x); } } } } // TODO 果樹園 RE 横型 void AOJ0118() { while (sc.hasNext()) { int X = sc.nextInt(), Y = sc.nextInt(), ans = 0; if ((X | Y) == 0) break; char[][] c = new char[X + 2][Y + 2]; for (int y = 0; y < Y; y++) { String line = sc.next(); for (int x = 0; x < X; x++) { c[x][y] = line.charAt(x); } } // debug // Point.DE(c, 0, 0, X, Y); for (int x = 0; x < X; x++) { for (int y = 0; y < Y; y++) { if (c[x][y] == '&') continue; ans++; LinkedList<Point> open = new LinkedList<Point>(); open.add(new Point(x, y)); c[x][y] = '&'; char mark = c[x][y]; while (!open.isEmpty()) { Point now = open.poll(); for (int i = 0; i < 4; i++) { int xx = now.x + vx[i], yy = now.y + vy[i]; if (!Point.ok(xx, yy, X, Y)) continue; if (c[xx][yy] == mark) { open.add(new Point(xx, yy)); c[xx][yy] = '&'; } } } // Point.DE(c, 0, 0, X, Y); // out.println("ANS"+ans); } } out.println(ans); } } // Red and Black 横型探索 void AOJ1130() { while (sc.hasNext()) { int X = sc.nextInt(), Y = sc.nextInt(), sx = 0, sy = 0, ans = 0; if (X == 0) break; char[][] c = new char[X][Y]; for (int y = 0; y < Y; y++) { char[] line = sc.next().toCharArray(); for (int x = 0; x < X; x++) { c[x][y] = line[x]; if (line[x] == '@') { sx = x; sy = y; } } } LinkedList<Point> open = new LinkedList<Point>(); open.add(new Point(sx, sy)); boolean[][] close = new boolean[X][Y]; while (!open.isEmpty()) { ans++; Point now = open.poll(); close[now.x][now.y] = true; for (int i = 0; i < 4; i++) { if (now.x + vx[i] < 0 || now.x + vx[i] >= X || now.y + vy[i] < 0 || now.y + vy[i] >= Y) continue; if (c[now.x + vx[i]][now.y + vy[i]] == '.' && !close[now.x + vx[i]][now.y + vy[i]]) { open.add(new Point(now.x + vx[i], now.y + vy[i])); close[now.x + vx[i]][now.y + vy[i]] = true; } } } out.println(ans); } } // TODO デパート TLE MLE WA void AOJ0223() { while (sc.hasNext()) { int X = sc.nextInt(), Y = sc.nextInt(); if (X == 0) break; int tx = sc.nextInt(), ty = sc.nextInt(), kx = sc.nextInt(), ky = sc.nextInt(); int[][] b = new int[X + 1][Y + 1]; for (int y = 1; y <= Y; y++) { for (int x = 1; x <= X; x++) b[x][y] = sc.nextInt(); } LinkedList<C0223> open = new LinkedList<C0223>(); open.add(new C0223(tx, ty, kx, ky, 0)); int[][][][] close2 = new int[X + 1][Y + 1][X + 1][Y + 1]; close2[tx][ty][kx][ky] = -1; int ans = -1; while (!open.isEmpty()) { C0223 now = open.poll(); if (now.ans()) { ans = now.sec; break; } if (now.sec >= 99) break; for (int i = 0; i < 4; i++) { if (0 < now.tx + vx[i] && now.tx + vx[i] <= X && 0 < now.ty + vy[i] && now.ty + vy[i] <= Y && 0 < now.kx - vx[i] && now.kx - vx[i] <= X && 0 < now.ky - vy[i] && now.ky - vy[i] <= Y) { tx = now.tx; ty = now.ty; kx = now.kx; ky = now.ky; if (b[tx + vx[i]][ty + vy[i]] == 0) { tx += vx[i]; ty += vy[i]; } if (b[kx - vx[i]][ky - vy[i]] == 0) { kx -= vx[i]; ky -= vy[i]; } // out.println("TEMP:"+tx+" "+ty+" "+kx+" "+ky); // if(tx==kx && ty==ky) ans=ans<0?now.sec+1:min(ans,now.sec+1); if (close2[tx][ty][kx][ky] == 0 || close2[tx][ty][kx][ky] > now.sec + 1) { // out.println("ADD:"+tx+" "+ty+" "+kx+" "+ky); open.add(new C0223(tx, ty, kx, ky, now.sec + 1)); close2[tx][ty][kx][ky] = now.sec + 1; } } } } out.println(ans < 0 ? "NA" : ans); } } class C0223 { int tx, ty, kx, ky, sec; C0223(int tx, int ty, int kx, int ky, int sec) { this.tx = tx; this.ty = ty; this.kx = kx; this.ky = ky; this.sec = sec; } boolean ans() { if (this.tx == this.kx && this.ty == this.ky) return true; else return false; } } // TODO WA void AOJ0142() { while (sc.hasNext()) { int n = sc.nextInt(); if (n == 0) break; int nn = (n - 1) / 2; boolean[] b = new boolean[n]; for (int i = 1; i < n; i++) b[(i * i) % n] = true; ArrayList<Integer> ar = new ArrayList<Integer>(); for (int i = 1; i < n; i++) { if (b[i]) ar.add(i); } int[] ans = new int[nn + 1]; for (int i = 0; i < ar.size(); i++) { for (int j = 0; j < ar.size(); j++) { if (i == j) continue; int temp = ar.get(i) - ar.get(j); temp += temp < 0 ? n : 0; temp = temp > nn ? n - temp : temp; ans[temp]++; } } for (int i = 1; i <= nn; i++) out.println(ans[i]); } } // レゴ 横型探索 void AOJ0207() { while (sc.hasNext()) { int W = sc.nextInt(), H = sc.nextInt(); if ((W | H) == 0) break; int sx = sc.nextInt(), sy = sc.nextInt(), gx = sc.nextInt(), gy = sc.nextInt(), n = sc.nextInt(); int[][] b = new int[W + 1][H + 1]; for (int i = 0; i < n; i++) { int color = sc.nextInt(), d = sc.nextInt(), x = sc.nextInt(), y = sc.nextInt(); if (d == 0) { // 横 4x2 for (int j = y; j < y + 2; j++) { for (int k = x; k < x + 4; k++) b[k][j] = color; } } else { // 縦 2x4 for (int j = y; j < y + 4; j++) { for (int k = x; k < x + 2; k++) b[k][j] = color; } } } // debug // de0207(b,W,H); int c = b[sx][sy]; if (c == 0) { out.println("NG"); continue; } LinkedList<Point> open = new LinkedList<Point>(); boolean[][] close = new boolean[W + 1][H + 1]; open.add(new Point(sx, sy)); close[sx][sy] = true; String ans = "NG"; while (!open.isEmpty()) { Point now = open.poll(); for (int i = 0; i < 4; i++) { int xx = now.x + vx[i], yy = now.y + vy[i]; if (!Point.ok(xx, yy, W + 1, H + 1, 1)) continue; if (b[xx][yy] != c) continue; if (close[xx][yy]) continue; open.add(new Point(xx, yy)); close[xx][yy] = true; if (xx == gx && yy == gy) { ans = "OK"; open.clear(); } } } out.println(ans); } } void de0207(int[][] b, int w, int h) { for (int j = 0; j <= h; j++) { for (int i = 0; i <= w; i++) out.print(b[i][j] + " "); out.println(); } for (int i = 0; i <= w; i++) out.print("-"); out.println(); } void AOJ0139() { int N = sc.nextInt(); final Pattern ap = Pattern.compile("^>'(=+)#(=+)~$"); for (int i = 1; i <= N; i++) { String s = sc.next(); Matcher m = ap.matcher(s); if (m.matches()) { if (m.group(1).length() == m.group(2).length()) { out.println("A"); continue; } } else if (Pattern.compile("^>\\^(Q=)+~~$").matcher(s).matches()) { out.println("B"); continue; } out.println("NA"); } } ArrayList<Integer> SieveList(int N) { ArrayList<Integer> prime = new ArrayList<Integer>(); boolean[] list = new boolean[N + 1]; Arrays.fill(list, true); list[0] = false; list[1] = false; prime.add(2); for (int j = 4; j <= N; j += 2) list[j] = false; for (int i = 3; i <= N; i += 2) { if (list[i]) { prime.add(i); for (int j = i + i; j <= N; j += i) list[j] = false; } } return prime; } boolean[] Sieve(int N) { boolean[] list = new boolean[N + 1]; Arrays.fill(list, true); list[0] = false; list[1] = false; for (int j = 4; j <= N; j += 2) list[j] = false; for (int i = 3; i * i <= N; i += 2) if (list[i]) for (int j = i + i; j <= N; j += i) list[j] = false; return list; } boolean isPrime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } int gcd(int x, int y) { if (y == 0) return x; else return gcd(y, x % y); } }
[["-", 0, 7, 8, 196, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 12, 22]]
3
7,716
#include <stdio.h> #include <string.h> int map[10][10]; int min(int a, int b) { return a < b ? a : b; } int cmp(int i, int j, int k) { if (map[i][j] == -1) { if (map[i][k] >= 0 && map[k][j] >= 0) return map[i][k] + map[k][j]; else return -1; } else if (map[i][k] == -1 || map[k][j] == -1) return map[i][j]; else return min(map[i][j], map[i][k] + map[k][j]); } int main() { int n; for (; scanf("%d", &n), n;) { int m = 0, i, j, k; memset(map, -1, sizeof(map)); for (; n--;) { int a, b, c; scanf("%d %d %d", &a, &b, &c); m = m > a ? m : a; m = n > b ? m : b; map[a][b] = map[b][a] = c; } for (i = 0; i <= m; i++) map[i][i] = 0; for (k = 0; k <= m; k++) for (i = 0; i <= m; i++) for (j = 0; j <= m; j++) map[i][j] = cmp(i, j, k); /* for(i=0; i<=m; i++){ */ /* for(j=0; j<=m; j++) */ /* printf("%3d ",map[i][j]); */ /* putchar('\n'); */ /* } */ int num, sum = -1; for (i = 0; i <= m; i++) { int tsum = 0; for (j = 0; j <= m; j++) tsum += map[i][j]; if (sum == -1 || tsum < sum) { num = i; sum = tsum; } } printf("%d %d\n", num, sum); } }
#include <stdio.h> #include <string.h> int map[10][10]; int min(int a, int b) { return a < b ? a : b; } int cmp(int i, int j, int k) { if (map[i][j] == -1) { if (map[i][k] >= 0 && map[k][j] >= 0) return map[i][k] + map[k][j]; else return -1; } else if (map[i][k] == -1 || map[k][j] == -1) return map[i][j]; else return min(map[i][j], map[i][k] + map[k][j]); } int main() { int n; for (; scanf("%d", &n), n;) { int m = 0, i, j, k; memset(map, -1, sizeof(map)); for (; n--;) { int a, b, c; scanf("%d %d %d", &a, &b, &c); m = m > a ? m : a; m = m > b ? m : b; map[a][b] = map[b][a] = c; } for (i = 0; i <= m; i++) map[i][i] = 0; for (k = 0; k <= m; k++) for (i = 0; i <= m; i++) for (j = 0; j <= m; j++) map[i][j] = cmp(i, j, k); int num, sum = -1; for (i = 0; i <= m; i++) { int tsum = 0; for (j = 0; j <= m; j++) tsum += map[i][j]; if (sum == -1 || tsum < sum) { num = i; sum = tsum; } } printf("%d %d\n", num, sum); } }
[["-", 0, 1, 0, 11, 12, 41, 15, 16, 31, 22], ["+", 0, 1, 0, 11, 12, 41, 15, 16, 31, 22]]
0
451
#include <cstdio> #include <iostream> using namespace std; const int INF = 1 << 30; const int MAX_V = 10; const int MAX_E = 45; int d[MAX_V][MAX_V]; int n; int V; void show_d() { cout << "===" << endl; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { printf("%3d ", d[i][j]); } cout << endl; } } void warshall_floyd() { for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } // show_d(); // debug } } int main() { while (true) { cin >> n; if (!n) break; int f, t, c; V = 0; for (int i = 0; i < MAX_V; i++) { for (int j = 0; j < MAX_V; j++) { d[i][j] = INF; if (i == j) d[i][j] = 0; } } for (int i = 0; i < n; i++) { cin >> f >> t >> c; d[f][t] = c; d[t][f] = c; V = max(V, f + 1); V = max(V, t + 1); } warshall_floyd(); int ix = 0, s = INF; for (int i = 0; i < V; i++) { int tmp = 0; for (int j = 0; j < V; j++) { tmp += d[i][j]; } if (tmp < s) { ix = i; s = tmp; } } cout << ix << " " << s << endl; } return 0; }
#include <cstdio> #include <iostream> using namespace std; const int INF = 1 << 20; const int MAX_V = 10; const int MAX_E = 45; int d[MAX_V][MAX_V]; int n; int V; void show_d() { cout << "===" << endl; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { printf("%3d ", d[i][j]); } cout << endl; } } void warshall_floyd() { for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } // show_d(); // debug } } int main() { while (true) { cin >> n; if (!n) break; int f, t, c; V = 0; for (int i = 0; i < MAX_V; i++) { for (int j = 0; j < MAX_V; j++) { d[i][j] = INF; if (i == j) d[i][j] = 0; } } for (int i = 0; i < n; i++) { cin >> f >> t >> c; d[f][t] = c; d[t][f] = c; V = max(V, f + 1); V = max(V, t + 1); } warshall_floyd(); int ix = 0, s = INF; for (int i = 0; i < V; i++) { int tmp = 0; for (int j = 0; j < V; j++) { tmp += d[i][j]; } if (tmp < s) { ix = i; s = tmp; } } cout << ix << " " << s << endl; } return 0; }
[["-", 0, 30, 0, 43, 49, 50, 51, 16, 12, 13], ["+", 0, 30, 0, 43, 49, 50, 51, 16, 12, 13]]
1
447
#include <algorithm> #include <cmath> #include <iostream> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define INF 100000 using namespace std; int main() { // int town[11][11]; int to, from, cost; int n; while (1) { cin >> n; if (!n) break; int maxsize = 0; int dt[11][11]; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { // town[i][j]=INF; if (i != j) dt[i][j] = INF; else dt[i][j] = 0; // out<<"yeah "<<i<<" "<<j<<endl; } } for (int i = 0; i < n; i++) { cin >> to >> from >> cost; // cout<<"analize"<<endl; dt[to][from] = cost; dt[from][to] = cost; maxsize = max(maxsize, to); maxsize = max(maxsize, from); } for (int k = 0; k < maxsize + 1; k++) { for (int i = 0; i < maxsize + 1; i++) { for (int j = 0; j < maxsize + 1; j++) { dt[i][j] = min(dt[i][j], dt[i][k] + dt[k][j]); } } } int sum[maxsize + 1]; rep(i, maxsize + 1) { sum[i] = 0; rep(j, maxsize + 1) { sum[i] += dt[i][j]; } // cout<<sum[i]<<" "; } int ans[maxsize + 1]; rep(i, maxsize + 1) ans[i] = sum[i]; sort(sum, sum + maxsize + 1); // cout<<sum[0]<<endl; for (int i = 0; i < maxsize + 1; i++) { if (ans[i] == sum[0]) { cout << i << " " << sum[0]; } } cout << endl; // cout<<"finish"<<endl; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define INF 100000 using namespace std; int main() { // int town[11][11]; int to, from, cost; int n; while (1) { cin >> n; if (!n) break; int maxsize = 0; int dt[11][11]; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { // town[i][j]=INF; if (i != j) dt[i][j] = INF; else dt[i][j] = 0; // out<<"yeah "<<i<<" "<<j<<endl; } } for (int i = 0; i < n; i++) { cin >> to >> from >> cost; // cout<<"analize"<<endl; dt[to][from] = cost; dt[from][to] = cost; maxsize = max(maxsize, to); maxsize = max(maxsize, from); } for (int k = 0; k < maxsize + 1; k++) { for (int i = 0; i < maxsize + 1; i++) { for (int j = 0; j < maxsize + 1; j++) { dt[i][j] = min(dt[i][j], dt[i][k] + dt[k][j]); } } } int sum[maxsize + 1]; rep(i, maxsize + 1) { sum[i] = 0; rep(j, maxsize + 1) { sum[i] += dt[i][j]; } // cout<<sum[i]<<" "; } // cout<<endl; int ans[maxsize + 1]; rep(i, maxsize + 1) ans[i] = sum[i]; sort(sum, sum + maxsize + 1); // cout<<sum[0]<<endl; for (int i = 0; i < maxsize + 1; i++) { if (ans[i] == sum[0]) { cout << i << " " << sum[0]; break; } } cout << endl; // cout<<"finish"<<endl; } return 0; }
[["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 35]]
1
436
#include <algorithm> #include <iostream> using namespace std; #define INF 1e+9; int E, V; int cost[50][50]; int d[100]; int main() { while (cin >> E, E) { V = 0; fill(d, d + 100, 0); for (int i = 0; i < 50; i++) { for (int j = 0; j < 50; j++) { cost[i][j] = (i == j) ? 0 : INF } } for (int i = 0; i < E; i++) { int a, b, c; cin >> a >> b >> c; cost[a][b] = c; cost[b][a] = c; V = max(V, max(a, b)); } for (int k = 0; k <= V; k++) { for (int i = 0; i <= V; i++) { for (int j = 0; j <= V; j++) { cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]); } } } int minCost = INF; for (int i = 0; i <= V; i++) { for (int j = 0; j <= V; j++) { d[i] += cost[i][j]; } minCost = min(minCost, d[i]); } for (int i = 0; i <= V; i++) { if (minCost == d[i]) cout << i << " " << d[i] << endl; } } }
#include <algorithm> #include <iostream> using namespace std; #define INF 1e+9; int E, V; int cost[50][50]; int d[100]; int main() { while (cin >> E, E) { V = 0; fill(d, d + 100, 0); for (int i = 0; i < 50; i++) { for (int j = 0; j < 50; j++) { cost[i][j] = (i == j) ? 0 : INF } } for (int i = 0; i < E; i++) { int a, b, c; cin >> a >> b >> c; cost[a][b] = c; cost[b][a] = c; V = max(V, max(a, b)); } for (int k = 0; k <= V; k++) { for (int i = 0; i <= V; i++) { for (int j = 0; j <= V; j++) { cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]); } } } int minCost = INF; for (int i = 0; i <= V; i++) { for (int j = 0; j <= V; j++) { d[i] += cost[i][j]; } minCost = min(minCost, d[i]); } for (int i = 0; i <= V; i++) { if (minCost == d[i]) { cout << i << " " << d[i] << endl; break; } } } }
[["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35], ["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46]]
1
362
#include <bits/stdc++.h> using namespace std; int main() { int n, m; while (cin >> n >> m, n) { string s; for (int i = 0; i < n; i++) s += '#'; while (m--) { string p; cin >> p; if (p == "A") { for (int i = 0; i < n; i++) if (s[i] == '#') { s[i] = 'A'; break; } } if (p == "B") { int pp = 0; for (int i = n - 1; i >= 0; i--) { if (i == n - 1) { if (s[i - 1] != 'A' && s[i] == '#') { pp++, s[i] = 'B'; break; } } else if (!i) { if (s[i + 1] != 'A' && s[i] == '#') { pp++, s[i] = 'B'; break; } } else { if (s[i - 1] != 'A' && s[i] == '#' && s[i + 1] != 'A') { pp++, s[i] = 'B'; break; } } } if (!pp) for (int i = 0; i < n; i++) if (s[i] == '#') { s[i] = 'B'; break; } } if (p == "C") { int pp = 0; for (int i = 0; i < n; i++) if (s[i] != '#') { if (i != n - 1) { if (s[i + 1] == '#') { s[i + 1] = 'C'; pp++; break; } } if (i) { if (s[i - 1] == '#') { s[i - 1] = 'C'; pp++; break; } } } if (!pp) { if (n % 2) s[(n + 1) / 2 - 1] = 'C'; else s[n / 2] = 'C'; } } else if (p == "D") { int d[n] = {}, p = 0, sp = 0; for (int i = 0; i < n; i++) { int p1 = 0, p2 = 0; if (i == n - 1) p1 = 1000; if (i == 0) p2 = 1000; for (int j = i; j < n; j++) if (s[j] == '#') p1++; else break; for (int j = i; j >= 0; j--) if (s[j] == '#') p2++; else break; p1 = min(p1, p2); p = max(p, p1); d[i] = p1; } for (int i = 0; i < n; i++) if (s[i] != '#') sp++; if (sp) s[0] = 'D'; else for (int i = 0; i < n; i++) if (d[i] == p) { s[i] = 'D'; break; } } } cout << s << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; while (cin >> n >> m, n) { string s; for (int i = 0; i < n; i++) s += '#'; while (m--) { string p; cin >> p; if (p == "A") { for (int i = 0; i < n; i++) if (s[i] == '#') { s[i] = 'A'; break; } } if (p == "B") { int pp = 0; for (int i = n - 1; i >= 0; i--) { if (i == n - 1) { if (s[i - 1] != 'A' && s[i] == '#') { pp++, s[i] = 'B'; break; } } else if (!i) { if (s[i + 1] != 'A' && s[i] == '#') { pp++, s[i] = 'B'; break; } } else { if (s[i - 1] != 'A' && s[i] == '#' && s[i + 1] != 'A') { pp++, s[i] = 'B'; break; } } } if (!pp) for (int i = 0; i < n; i++) if (s[i] == '#') { s[i] = 'B'; break; } } if (p == "C") { int pp = 0; for (int i = 0; i < n; i++) if (s[i] != '#') { if (i != n - 1) { if (s[i + 1] == '#') { s[i + 1] = 'C'; pp++; break; } } if (i) { if (s[i - 1] == '#') { s[i - 1] = 'C'; pp++; break; } } } if (!pp) { if (n % 2) s[(n + 1) / 2 - 1] = 'C'; else s[n / 2] = 'C'; } } else if (p == "D") { int d[n] = {}, p = 0, sp = 0; for (int i = 0; i < n; i++) { int p1 = 0, p2 = 0; if (i == n - 1) p1 = 1000; if (i == 0) p2 = 1000; for (int j = i; j < n; j++) if (s[j] == '#') p1++; else break; for (int j = i; j >= 0; j--) if (s[j] == '#') p2++; else break; p1 = min(p1, p2); p = max(p, p1); d[i] = p1; // cout<<d[i]; } // cout<<endl; for (int i = 0; i < n; i++) if (s[i] != '#') sp++; if (!sp) s[0] = 'D'; else for (int i = 0; i < n; i++) if (d[i] == p) { s[i] = 'D'; break; } } } cout << s << endl; } }
[["+", 64, 9, 0, 57, 15, 339, 51, 91, 17, 111]]
1
744
#line 0 "cmacro.cpp" #include <bits/stdc++.h> using namespace std; #define in , #define esc_paren(...) __VA_ARGS__ #define pp_empty(...) #define pp_cat_i(x, y) x##y #define pp_cat(x, y) pp_cat_i(x, y) #define pp_inc0 1 #define pp_inc1 2 #define pp_inc2 3 #define pp_inc3 4 #define pp_inc4 5 #define pp_inc5 6 #define pp_inc6 7 #define pp_inc7 8 #define pp_inc8 9 #define pp_inc(i) pp_cat(pp_inc, i) #define pp_arg10(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, ...) _9 #define pp_has_comma(...) pp_arg10(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 0) #define pp_trigger_paren(...) , #define pp_is_empty(...) \ pp_is_empty1(pp_has_comma(__VA_ARGS__), \ pp_has_comma(pp_trigger_paren __VA_ARGS__), \ pp_has_comma(__VA_ARGS__()), \ pp_has_comma(pp_trigger_paren __VA_ARGS__())) #define pp_paste5(_0, _1, _2, _3, _4) _0##_1##_2##_3##_4 #define pp_is_empty1(_0, _1, _2, _3) \ pp_has_comma(pp_paste5(pp_is_empty_case_, _0, _1, _2, _3)) #define pp_is_empty_case_0001 , #define pp_is_one(...) pp_is_one_(__VA_ARGS__, pp_one_seq) #define pp_is_one_(...) pp_is_one_n(__VA_ARGS__) #define pp_is_one_n(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N #define pp_one_seq 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 #define pp_narg(...) pp_narg_(__VA_ARGS__, pp_narg_seq) #define pp_narg_(...) pp_narg_n(__VA_ARGS__) #define pp_narg_n(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, \ _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, \ _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \ _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, \ _63, n, ...) \ n #define pp_narg_seq \ 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, \ 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, \ 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, \ 8, 7, 6, 5, 4, 3, 2, 1, 0 #define pp_if_0(x, y) y #define pp_if_1(x, y) x #define pp_if(cond, x, y) pp_cat(pp_if_, cond)(x, y) #define pp_foreach_i9(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i8(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i7(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i6(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i5(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i4(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i3(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i2(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i1(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i0(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i(i, f, ...) \ pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach(f, ...) pp_foreach_i(0, f, __VA_ARGS__) typedef long long ll; #define down_queue(x) priority_queue<x> #define up_queue(x) priority_queue<x, vector<x>, greater<x>> #define all(x) x.begin(), x.end() #define split_str(str, sp_word) \ istringstream stream(str); \ string res; \ for (int cnt = 0; getline(stream, res, sp_word); cnt++) #define digit(x) ((int)log10((double)(x)) + 1) #define mp(x, y) make_pair(x, y) #define pb(x) push_back(x) #define pf(x) push_front(x) #define vec_cpy(to, from) copy(all(to), back_inserter(from)) #define ary_cpy(to, from) memcpy(to, from, sizeof(from)) #define MOD 1000000007 #define INF 999999999 template <typename T> struct RepIterator { T i; RepIterator() : i(0) {} RepIterator(T n) : i(n) {} bool operator==(const RepIterator<T> &rhs) { return i == rhs.i; } bool operator!=(const RepIterator<T> &rhs) { return i != rhs.i; } T operator*() { return i; } RepIterator<T> operator++() { i++; return *this; }; }; template <typename T> struct RepLoop { T i; RepLoop(T n) : i(n) {} RepIterator<T> begin() { return RepIterator<T>(); } RepIterator<T> end() { return RepIterator<T>(i); } }; template <typename T> struct rep_range { static T range(T v) { return v; } }; #define def_range(t) \ template <> struct rep_range<t> { \ static RepLoop<t> range(t n) { return RepLoop<t>(n); } \ }; def_range(int); def_range(size_t); #define rep(...) rep1(rep2, (__VA_ARGS__)) #define rep1(X, A) X A #define rep2(i, ...) \ pp_if(pp_is_one(__VA_ARGS__), rep_len1(i, __VA_ARGS__), rep3(i, __VA_ARGS__)) #define rep_len1(i, end, ...) \ for (auto i : rep_range<decltype(end)>::range(end)) #define rep3(i, iter, ...) rep4(rep5, (iter, i, esc_paren __VA_ARGS__)) #define rep4(X, A) X A #define rep5(iter, ...) iter(__VA_ARGS__) #define up up_iter, #define up_iter3(i, start, end) for (auto i = start; i < end; i++) #define up_iter4(i, start, end, up) for (auto i = start; i < end; i += up) #define up_iter(...) pp_cat(up_iter, pp_narg(__VA_ARGS__))(__VA_ARGS__) #define down down_iter, #define down_iter2(i, end) for (auto i = end - 1; i >= 0; i--) #define down_iter3(i, start, end) for (auto i = end - 1; i >= start; i--) #define down_iter4(i, start, end, down) \ for (auto i = end - 1; i >= start; i -= down) #define down_iter(...) pp_cat(down_iter, pp_narg(__VA_ARGS__))(__VA_ARGS__) #define pp_init2(cnt, body) \ struct _InitStruct##cnt { \ _InitStruct##cnt() { body } \ } _initstruct##cnt; #define pp_init1(f, cnt, body) f(cnt, body) #define pp_init(body) pp_init1(pp_init2, __COUNTER__, body) #define init_array(arr, N, val) pp_init(rep(i in N) { arr[i] = val; }) #define iota_array(arr, N, start) pp_init(rep(i in N) { arr[i] = start + i; }) #define debug_var_elem(v) << #v << "=" << v << " " #ifdef DEBUG #define debug_echo(e) cout << "L" << __LINE__ << ": " << e << endl #define debug_var(...) \ cout << "L" << __LINE__ << ": " pp_foreach(debug_var_elem, __VA_ARGS__) \ << endl #define debug_time2(i, e) \ auto start##i = clock(); \ e; \ auto end##i = clock(); \ cout << "L" << __LINE__ << ": " << (double)(end##i - start##i) << "ms" \ << endl; #define debug_time1(f, i, e) f(i, e) #define debug_time(e) debug_time1(debug_time2, __COUNTER__, e) #else #define debug_echo(e) #define debug_var(...) #define debug_time(e) e; #endif #define pp_gen_field(list) pp_gen_field1 list #define pp_gen_field1(name, val) decltype(val) name; #define pp_gen_init(list) pp_gen_init1 list #define pp_gen_init1(name, val) , val #define pp_gen_find_set_wrap(ufnodename) ufnodename pp_gen_find_set #define pp_gen_find_set(list) pp_gen_find_set1 list #define pp_gen_find_set1(name, val) \ find_set_##name(int x, decltype(val) v) { \ if (nodes[x].parent == x) { \ nodes[x].name = v; \ return nodes[x]; \ } else { \ return nodes[x] = find_set_##name(nodes[x].parent, v); \ } \ } #define pp_gen_unite_set(list) pp_gen_unite_set1 list #define pp_gen_unite_set1(name, val) \ if (nodes[x].name == val) { \ find_set_##name(x, find(y).name); \ } \ if (nodes[y].name == val) { \ find_set_##name(y, find(x).name); \ } #define def_unionfind(ufname, ufnodename, ...) \ struct ufnodename { \ int parent; \ pp_foreach(pp_gen_field, __VA_ARGS__) \ }; \ struct ufname { \ vector<ufnodename> nodes; \ vector<int> rank; \ ufname(int n) : nodes(n + 1), rank(n + 1) { \ rep(i in n) { \ nodes[i] = ufnodename{i pp_foreach(pp_gen_init, __VA_ARGS__)}; \ rank[i] = 0; \ } \ } \ ufnodename find(int x) { \ if (nodes[x].parent == x) { \ return nodes[x]; \ } else { \ return nodes[x] = find(nodes[x].parent); \ } \ } \ pp_foreach(pp_gen_find_set_wrap(ufnodename), \ __VA_ARGS__) void unite(int x, int y) { \ x = find(x).parent; \ y = find(y).parent; \ if (x == y) \ return; \ if (rank[x] < rank[y]) { \ pp_foreach(pp_gen_unite_set, __VA_ARGS__); \ nodes[x].parent = y; \ } else { \ pp_foreach(pp_gen_unite_set, __VA_ARGS__); \ nodes[y].parent = x; \ if (rank[x] == rank[y]) \ rank[x]++; \ } \ } \ bool same(int x, int y) { return find(x).parent == find(y).parent; } \ } ll gcd(ll a, ll b) { while (b) { ll tmp = a % b; a = b; b = tmp; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } bool is_prime(ll n) { switch (n) { case 0: case 1: return false; case 2: return true; } if (n % 2 == 0) return false; for (ll i = 3; i * i <= n; i += 2) if (n % i == 0) return false; return true; } #define def_varargs(name, f) \ template <typename First> inline First name(First first) { return first; } \ template <typename First, typename... Rest> \ inline First name(First first, Rest... rest) { \ return f(first, name(rest...)); \ } #line 1 "procon.cpp" bool is_sideA(vector<char> &chairs, int i) { if (chairs[i] != 0) return true; if (i != 0) { if (chairs[i - 1] == 'A') return true; } if (i != (int)chairs.size() - 1) { if (chairs[i + 1] == 'A') return true; } return false; } int get_space(vector<char> &chairs, int i) { if (i != (int)chairs.size() - 1) { if (chairs[i + 1] == 0) return 1; } if (i != 0) { if (chairs[i - 1] == 0) return -1; } return 0; } bool is_notshit(vector<char> &chairs) { rep(i in(int)chairs.size()) { if (chairs[i] != 0) return false; } return true; } int get_distance(vector<char> &chairs, int i) { if (chairs[i] != 0) return 0; int dist = INF; rep(j in up(i + 1, (int)chairs.size())) { if (chairs[j] != 0) { dist = min(dist, j - i); break; } } rep(j in down(0, i)) { if (chairs[j] != 0) { dist = min(dist, i - j); break; } } return dist; } int main() { int n, m; while (cin >> n >> m && n && m) { vector<char> chairs(n, (char)0); rep(_ in m) { char c; cin >> c; if (c == 'A') { rep(i in(int)chairs.size()) { if (chairs[i] == 0) { chairs[i] = c; break; } } } else if (c == 'B') { bool shit = false; rep(i in down(0, (int)chairs.size())) { if (!is_sideA(chairs, i)) { chairs[i] = c; shit = true; break; } } if (shit) continue; rep(i in(int)chairs.size()) { if (chairs[i] == 0) { chairs[i] = c; break; } } } else if (c == 'C') { bool shit = false; rep(i in(int)chairs.size()) { if (chairs[i] != 0) { int space = get_space(chairs, i); if (space != 0) { chairs[i + space] = c; shit = true; break; } } } if (shit) continue; int h; if (n % 2 == 0) { h = n / 2 + 1; } else { h = n / 2; } chairs[h] = c; } else if (c == 'D') { if (is_notshit(chairs)) { chairs[0] = c; continue; } vector<int> distance(chairs.size()); rep(i in(int)chairs.size()) { distance[i] = get_distance(chairs, i); } int ans = -1; int ansi = -1; rep(i in(int)distance.size()) { if (ans < distance[i]) { ans = distance[i]; ansi = i; } } chairs[ansi] = c; } } rep(c in chairs) { if (c == 0) { cout << "#"; } else { cout << c; } } cout << endl; } }
#line 0 "cmacro.cpp" #include <bits/stdc++.h> using namespace std; #define in , #define esc_paren(...) __VA_ARGS__ #define pp_empty(...) #define pp_cat_i(x, y) x##y #define pp_cat(x, y) pp_cat_i(x, y) #define pp_inc0 1 #define pp_inc1 2 #define pp_inc2 3 #define pp_inc3 4 #define pp_inc4 5 #define pp_inc5 6 #define pp_inc6 7 #define pp_inc7 8 #define pp_inc8 9 #define pp_inc(i) pp_cat(pp_inc, i) #define pp_arg10(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, ...) _9 #define pp_has_comma(...) pp_arg10(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 0) #define pp_trigger_paren(...) , #define pp_is_empty(...) \ pp_is_empty1(pp_has_comma(__VA_ARGS__), \ pp_has_comma(pp_trigger_paren __VA_ARGS__), \ pp_has_comma(__VA_ARGS__()), \ pp_has_comma(pp_trigger_paren __VA_ARGS__())) #define pp_paste5(_0, _1, _2, _3, _4) _0##_1##_2##_3##_4 #define pp_is_empty1(_0, _1, _2, _3) \ pp_has_comma(pp_paste5(pp_is_empty_case_, _0, _1, _2, _3)) #define pp_is_empty_case_0001 , #define pp_is_one(...) pp_is_one_(__VA_ARGS__, pp_one_seq) #define pp_is_one_(...) pp_is_one_n(__VA_ARGS__) #define pp_is_one_n(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N #define pp_one_seq 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 #define pp_narg(...) pp_narg_(__VA_ARGS__, pp_narg_seq) #define pp_narg_(...) pp_narg_n(__VA_ARGS__) #define pp_narg_n(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, \ _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, \ _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, \ _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \ _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, \ _63, n, ...) \ n #define pp_narg_seq \ 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, \ 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, \ 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, \ 8, 7, 6, 5, 4, 3, 2, 1, 0 #define pp_if_0(x, y) y #define pp_if_1(x, y) x #define pp_if(cond, x, y) pp_cat(pp_if_, cond)(x, y) #define pp_foreach_i9(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i8(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i7(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i6(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i5(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i4(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i3(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i2(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i1(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i0(i, f, x, ...) \ f(x) pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach_i(i, f, ...) \ pp_if(pp_is_empty(__VA_ARGS__), pp_empty, \ pp_cat(pp_foreach_i, i))(pp_inc(i), f, __VA_ARGS__) #define pp_foreach(f, ...) pp_foreach_i(0, f, __VA_ARGS__) typedef long long ll; #define down_queue(x) priority_queue<x> #define up_queue(x) priority_queue<x, vector<x>, greater<x>> #define all(x) x.begin(), x.end() #define split_str(str, sp_word) \ istringstream stream(str); \ string res; \ for (int cnt = 0; getline(stream, res, sp_word); cnt++) #define digit(x) ((int)log10((double)(x)) + 1) #define mp(x, y) make_pair(x, y) #define pb(x) push_back(x) #define pf(x) push_front(x) #define vec_cpy(to, from) copy(all(to), back_inserter(from)) #define ary_cpy(to, from) memcpy(to, from, sizeof(from)) #define MOD 1000000007 #define INF 999999999 template <typename T> struct RepIterator { T i; RepIterator() : i(0) {} RepIterator(T n) : i(n) {} bool operator==(const RepIterator<T> &rhs) { return i == rhs.i; } bool operator!=(const RepIterator<T> &rhs) { return i != rhs.i; } T operator*() { return i; } RepIterator<T> operator++() { i++; return *this; }; }; template <typename T> struct RepLoop { T i; RepLoop(T n) : i(n) {} RepIterator<T> begin() { return RepIterator<T>(); } RepIterator<T> end() { return RepIterator<T>(i); } }; template <typename T> struct rep_range { static T range(T v) { return v; } }; #define def_range(t) \ template <> struct rep_range<t> { \ static RepLoop<t> range(t n) { return RepLoop<t>(n); } \ }; def_range(int); def_range(size_t); #define rep(...) rep1(rep2, (__VA_ARGS__)) #define rep1(X, A) X A #define rep2(i, ...) \ pp_if(pp_is_one(__VA_ARGS__), rep_len1(i, __VA_ARGS__), rep3(i, __VA_ARGS__)) #define rep_len1(i, end, ...) \ for (auto i : rep_range<decltype(end)>::range(end)) #define rep3(i, iter, ...) rep4(rep5, (iter, i, esc_paren __VA_ARGS__)) #define rep4(X, A) X A #define rep5(iter, ...) iter(__VA_ARGS__) #define up up_iter, #define up_iter3(i, start, end) for (auto i = start; i < end; i++) #define up_iter4(i, start, end, up) for (auto i = start; i < end; i += up) #define up_iter(...) pp_cat(up_iter, pp_narg(__VA_ARGS__))(__VA_ARGS__) #define down down_iter, #define down_iter2(i, end) for (auto i = end - 1; i >= 0; i--) #define down_iter3(i, start, end) for (auto i = end - 1; i >= start; i--) #define down_iter4(i, start, end, down) \ for (auto i = end - 1; i >= start; i -= down) #define down_iter(...) pp_cat(down_iter, pp_narg(__VA_ARGS__))(__VA_ARGS__) #define pp_init2(cnt, body) \ struct _InitStruct##cnt { \ _InitStruct##cnt() { body } \ } _initstruct##cnt; #define pp_init1(f, cnt, body) f(cnt, body) #define pp_init(body) pp_init1(pp_init2, __COUNTER__, body) #define init_array(arr, N, val) pp_init(rep(i in N) { arr[i] = val; }) #define iota_array(arr, N, start) pp_init(rep(i in N) { arr[i] = start + i; }) #define debug_var_elem(v) << #v << "=" << v << " " #ifdef DEBUG #define debug_echo(e) cout << "L" << __LINE__ << ": " << e << endl #define debug_var(...) \ cout << "L" << __LINE__ << ": " pp_foreach(debug_var_elem, __VA_ARGS__) \ << endl #define debug_time2(i, e) \ auto start##i = clock(); \ e; \ auto end##i = clock(); \ cout << "L" << __LINE__ << ": " << (double)(end##i - start##i) << "ms" \ << endl; #define debug_time1(f, i, e) f(i, e) #define debug_time(e) debug_time1(debug_time2, __COUNTER__, e) #else #define debug_echo(e) #define debug_var(...) #define debug_time(e) e; #endif #define pp_gen_field(list) pp_gen_field1 list #define pp_gen_field1(name, val) decltype(val) name; #define pp_gen_init(list) pp_gen_init1 list #define pp_gen_init1(name, val) , val #define pp_gen_find_set_wrap(ufnodename) ufnodename pp_gen_find_set #define pp_gen_find_set(list) pp_gen_find_set1 list #define pp_gen_find_set1(name, val) \ find_set_##name(int x, decltype(val) v) { \ if (nodes[x].parent == x) { \ nodes[x].name = v; \ return nodes[x]; \ } else { \ return nodes[x] = find_set_##name(nodes[x].parent, v); \ } \ } #define pp_gen_unite_set(list) pp_gen_unite_set1 list #define pp_gen_unite_set1(name, val) \ if (nodes[x].name == val) { \ find_set_##name(x, find(y).name); \ } \ if (nodes[y].name == val) { \ find_set_##name(y, find(x).name); \ } #define def_unionfind(ufname, ufnodename, ...) \ struct ufnodename { \ int parent; \ pp_foreach(pp_gen_field, __VA_ARGS__) \ }; \ struct ufname { \ vector<ufnodename> nodes; \ vector<int> rank; \ ufname(int n) : nodes(n + 1), rank(n + 1) { \ rep(i in n) { \ nodes[i] = ufnodename{i pp_foreach(pp_gen_init, __VA_ARGS__)}; \ rank[i] = 0; \ } \ } \ ufnodename find(int x) { \ if (nodes[x].parent == x) { \ return nodes[x]; \ } else { \ return nodes[x] = find(nodes[x].parent); \ } \ } \ pp_foreach(pp_gen_find_set_wrap(ufnodename), \ __VA_ARGS__) void unite(int x, int y) { \ x = find(x).parent; \ y = find(y).parent; \ if (x == y) \ return; \ if (rank[x] < rank[y]) { \ pp_foreach(pp_gen_unite_set, __VA_ARGS__); \ nodes[x].parent = y; \ } else { \ pp_foreach(pp_gen_unite_set, __VA_ARGS__); \ nodes[y].parent = x; \ if (rank[x] == rank[y]) \ rank[x]++; \ } \ } \ bool same(int x, int y) { return find(x).parent == find(y).parent; } \ } ll gcd(ll a, ll b) { while (b) { ll tmp = a % b; a = b; b = tmp; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } bool is_prime(ll n) { switch (n) { case 0: case 1: return false; case 2: return true; } if (n % 2 == 0) return false; for (ll i = 3; i * i <= n; i += 2) if (n % i == 0) return false; return true; } #define def_varargs(name, f) \ template <typename First> inline First name(First first) { return first; } \ template <typename First, typename... Rest> \ inline First name(First first, Rest... rest) { \ return f(first, name(rest...)); \ } #line 1 "procon.cpp" bool is_sideA(vector<char> &chairs, int i) { if (chairs[i] != 0) return true; if (i != 0) { if (chairs[i - 1] == 'A') return true; } if (i != (int)chairs.size() - 1) { if (chairs[i + 1] == 'A') return true; } return false; } int get_space(vector<char> &chairs, int i) { if (i != (int)chairs.size() - 1) { if (chairs[i + 1] == 0) return 1; } if (i != 0) { if (chairs[i - 1] == 0) return -1; } return 0; } bool is_notshit(vector<char> &chairs) { rep(i in(int)chairs.size()) { if (chairs[i] != 0) return false; } return true; } int get_distance(vector<char> &chairs, int i) { if (chairs[i] != 0) return 0; int dist = INF; rep(j in up(i + 1, (int)chairs.size())) { if (chairs[j] != 0) { dist = min(dist, j - i); break; } } rep(j in down(0, i)) { if (chairs[j] != 0) { dist = min(dist, i - j); break; } } return dist; } int main() { int n, m; while (cin >> n >> m && n && m) { vector<char> chairs(n, (char)0); rep(_ in m) { char c; cin >> c; if (c == 'A') { rep(i in(int)chairs.size()) { if (chairs[i] == 0) { chairs[i] = c; break; } } } else if (c == 'B') { bool shit = false; rep(i in down(0, (int)chairs.size())) { if (!is_sideA(chairs, i)) { chairs[i] = c; shit = true; break; } } if (shit) continue; rep(i in(int)chairs.size()) { if (chairs[i] == 0) { chairs[i] = c; break; } } } else if (c == 'C') { bool shit = false; rep(i in(int)chairs.size()) { if (chairs[i] != 0) { int space = get_space(chairs, i); if (space != 0) { chairs[i + space] = c; shit = true; break; } } } if (shit) continue; int h; if (n % 2 == 0) { h = n / 2; } else { h = n / 2; } chairs[h] = c; } else if (c == 'D') { if (is_notshit(chairs)) { chairs[0] = c; continue; } vector<int> distance(chairs.size()); rep(i in(int)chairs.size()) { distance[i] = get_distance(chairs, i); } int ans = -1; int ansi = -1; rep(i in(int)distance.size()) { if (ans < distance[i]) { ans = distance[i]; ansi = i; } } chairs[ansi] = c; } } rep(c in chairs) { if (c == 0) { cout << "#"; } else { cout << c; } } cout << endl; } }
[["-", 64, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["-", 64, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
1
2,044
#include <bits/stdc++.h> using namespace std; int main() { while (1) { int n, m; cin >> n >> m; if (n == 0 && m == 0) break; char ch[n]; for (int i = 0; i < n; i++) { ch[i] = '#'; } for (int k = 0; k < m; k++) { char c; cin >> c; if (c == 'A') { for (int i = 0; i < n; i++) { if (ch[i] == '#') { ch[i] = 'A'; break; } } } else if (c == 'B') { bool flag = true; for (int i = n - 1; i != 0; i--) { if (ch[i] == '#') { if (i == n - 1 && ch[i - 1] != 'A') { ch[i] = 'B'; flag = false; break; } else if (i == 0 && ch[i + 1] != 'A') { ch[i] = 'B'; flag = false; break; } if ((ch[i + 1] != 'A') && (ch[i - 1] != 'A')) { ch[i] = 'B'; flag = false; break; } } } if (flag) { for (int i = 0; i < n; i++) { if (ch[i] == '#') { ch[i] = 'B'; break; } } } } else if (c == 'C') { if (k == 0) { if (n % 2 == 0) { ch[n / 2 + 1 - 1] = 'C'; } else { ch[(n + 1) / 2 - 1] = 'C'; } } else { for (int i = 0; i < n; i++) { if (ch[i] != '#') { if (ch[i + 1] == '#') { ch[i + 1] = 'C'; break; } else if (i != 0 && ch[i - 1] == '#') { ch[i - 1] = 'C'; break; } else { for (int j = i; j < n; j++) { if (ch[j] == '#') { ch[j] = 'C'; break; } } } } } } } else { int dis[n]; int mdis = 0, pos = 0; if (k == 0) { ch[0] = 'D'; } else { for (int i = 0; i < n; i++) { if (ch[i] != '#') { dis[0] = i; break; } } for (int i = 1; i < n; i++) { if (ch[i] != '#') { dis[i] = 0; continue; } dis[i] = dis[i - 1] + 1; for (int j = i + 1; j < n; j++) { if (ch[j] != '#') { dis[i] = min(dis[i], j - i); } } } for (int i = 0; i < n; i++) { if (dis[i] > mdis) { mdis = dis[i]; pos = i; } } ch[pos] = 'D'; } } } for (int i = 0; i < n; i++) { cout << ch[i]; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { while (1) { int n, m; cin >> n >> m; if (n == 0 && m == 0) break; char ch[n]; for (int i = 0; i < n; i++) { ch[i] = '#'; } for (int k = 0; k < m; k++) { char c; cin >> c; if (c == 'A') { for (int i = 0; i < n; i++) { if (ch[i] == '#') { ch[i] = 'A'; break; } } } else if (c == 'B') { bool flag = true; for (int i = n - 1; i != 0; i--) { if (ch[i] == '#') { if (i == n - 1 && ch[i - 1] != 'A') { ch[i] = 'B'; flag = false; break; } else if (i == 0 && ch[i + 1] != 'A') { ch[i] = 'B'; flag = false; break; } if ((ch[i + 1] != 'A') && (ch[i - 1] != 'A')) { ch[i] = 'B'; flag = false; break; } } } if (flag) { for (int i = 0; i < n; i++) { if (ch[i] == '#') { ch[i] = 'B'; break; } } } } else if (c == 'C') { if (k == 0) { if (n % 2 == 0) { ch[n / 2 + 1 - 1] = 'C'; } else { ch[(n + 1) / 2 - 1] = 'C'; } } else { for (int i = 0; i < n; i++) { if (ch[i] != '#') { if (ch[i + 1] == '#') { ch[i + 1] = 'C'; break; } else if (i != 0 && ch[i - 1] == '#') { ch[i - 1] = 'C'; break; } else { for (int j = i; j < n; j++) { if (ch[j] == '#') { ch[j] = 'C'; break; } } break; } } } } } else { int dis[n]; int mdis = 0, pos = 0; if (k == 0) { ch[0] = 'D'; } else { for (int i = 0; i < n; i++) { if (ch[i] != '#') { dis[0] = i; break; } } for (int i = 1; i < n; i++) { if (ch[i] != '#') { dis[i] = 0; continue; } dis[i] = dis[i - 1] + 1; for (int j = i + 1; j < n; j++) { if (ch[j] != '#') { dis[i] = min(dis[i], j - i); } } } for (int i = 0; i < n; i++) { if (dis[i] > mdis) { mdis = dis[i]; pos = i; } } ch[pos] = 'D'; } } } for (int i = 0; i < n; i++) { cout << ch[i]; } cout << endl; } }
[["+", 0, 57, 75, 76, 0, 9, 0, 93, 0, 94], ["+", 0, 57, 75, 76, 0, 9, 0, 93, 0, 35]]
1
804
#include <bits/stdc++.h> using namespace std; const int INF = (1 << 30) - 1; int N, M; char seat[105]; int left_insert() { int k = 1; while (k <= N && seat[k] != '#') { k++; } return (k); } int main() { while (cin >> N >> M, N || M) { fill_n(seat, 105, '#'); for (int i = 0; i < M; i++) { char c; bool flag = false; int d[105]; int k; cin >> c; fill_n(d, 105, INF); switch (c) { case 'A': seat[left_insert()] = 'A'; break; case 'B': k = N; while (k > 0 && seat[k] != '#' && (seat[k - 1] == 'A' || seat[k + 1] == 'A')) { k--; } if (k > 0) { seat[k] = 'B'; } else { seat[left_insert()] = 'B'; } break; case 'C': for (int i = 1; i <= N; i++) { if (seat[i] != '#') { if (i < N && seat[i + 1] == '#') { seat[i + 1] = 'C'; flag = true; break; } if (i > 1 && seat[i - 1] == '#') { seat[i - 1] = 'C'; flag = true; break; } } } if (!flag) { seat[N / 2 + 1] = 'C'; } break; case 'D': for (int i = 1; i <= N; i++) { if (seat[i] != '#') { d[i] = 0; } d[i] = min(d[i - 1] + 1, d[i]); } for (int i = N; i > 0; i--) { d[i] = min(d[i], d[i + 1] + 1); } int maxp = 1; for (int i = 2; i <= N; i++) { if (d[i] > d[maxp]) { maxp = i; } } seat[maxp] = 'D'; break; } } for (int i = 1; i <= N; i++) { cout << seat[i]; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; const int INF = (1 << 30) - 1; int N, M; char seat[105]; int left_insert() { int k = 1; while (k <= N && seat[k] != '#') { k++; } return (k); } int main() { while (cin >> N >> M, N || M) { fill_n(seat, 105, '#'); for (int i = 0; i < M; i++) { char c; bool flag = false; int d[105]; int k; cin >> c; fill_n(d, 105, INF); switch (c) { case 'A': seat[left_insert()] = 'A'; break; case 'B': k = N; while (k > 0 && (seat[k] != '#' || seat[k - 1] == 'A' || seat[k + 1] == 'A')) { k--; } if (k > 0) { seat[k] = 'B'; } else { seat[left_insert()] = 'B'; } break; case 'C': for (int i = 1; i <= N; i++) { if (seat[i] != '#') { if (i < N && seat[i + 1] == '#') { seat[i + 1] = 'C'; flag = true; break; } if (i > 1 && seat[i - 1] == '#') { seat[i - 1] = 'C'; flag = true; break; } } } if (!flag) { seat[N / 2 + 1] = 'C'; } break; case 'D': for (int i = 1; i <= N; i++) { if (seat[i] != '#') { d[i] = 0; } d[i] = min(d[i - 1] + 1, d[i]); } for (int i = N; i > 0; i--) { d[i] = min(d[i], d[i + 1] + 1); } int maxp = 1; for (int i = 2; i <= N; i++) { if (d[i] > d[maxp]) { maxp = i; } } seat[maxp] = 'D'; break; } } for (int i = 1; i <= N; i++) { cout << seat[i]; } cout << endl; } }
[["+", 0, 52, 15, 339, 51, 16, 12, 23, 0, 24], ["-", 0, 100, 0, 52, 15, 339, 51, 16, 17, 98], ["-", 0, 52, 15, 339, 51, 16, 12, 23, 0, 24], ["+", 51, 16, 12, 23, 0, 16, 31, 16, 17, 106]]
1
558
#include <iostream> using namespace std; #define REP(i, b, n) for (int i = b; i < n; i++) #define rep(i, n) REP(i, 0, n) int findlm(int n, char *in) { rep(i, n) if (in[i] == 0) return i; } int findNotA(int n, char *in) { for (int i = n - 1; i >= 0; i--) { if (in[i] == 0 && (i == 0 || in[i - 1] != 'A') && (i == n - 1 || in[i + 1] != 'A')) return i; } return -1; } int nextP(int n, char *in) { rep(i, n) { if (in[i] != 0) { if (i < n - 1 && in[i + 1] == 0) return i + 1; if (i > 0 && in[i - 1] == 0) return i - 1; } } return -1; } int notNextP(int n, char *in) { int ret = -1, maxdist = 0; rep(i, n) { if (in[i] != 0) continue; int tmpdist = 0; for (int j = i - 1; j >= 0; j--) { if (in[j] != 0) { tmpdist = i - j; break; } } for (int j = i + 1; j < n; j++) { if (in[j] != 0) { tmpdist = min(tmpdist, j - i); break; } } if (tmpdist > maxdist) ret = i, maxdist = tmpdist; } return ret; } void put(int n, char *in, char inp) { int index = -1; if (inp == 'A') { index = findlm(n, in); } else if (inp == 'B') { index = findNotA(n, in); if (index == -1) index = findlm(n, in); } else if (inp == 'C') { index = nextP(n, in); if (index == -1) index = n / 2; } else if (inp == 'D') { index = notNextP(n, in); if (index == -1) index = 0; } in[index] = inp; } main() { char op[255]; op['A'] = 'A'; op['B'] = 'B'; op['C'] = 'C'; op['D'] = 'D'; op[0] = '#'; int n, m; char in[100]; while (cin >> n >> m && n) { rep(i, n) in[i] = 0; rep(i, m) { char inp; cin >> inp; put(n, in, inp); } rep(i, n) cout << op[in[i]]; cout << endl; } }
#include <iostream> using namespace std; #define REP(i, b, n) for (int i = b; i < n; i++) #define rep(i, n) REP(i, 0, n) int findlm(int n, char *in) { rep(i, n) if (in[i] == 0) return i; } int findNotA(int n, char *in) { for (int i = n - 1; i >= 0; i--) { if (in[i] == 0 && (i == 0 || in[i - 1] != 'A') && (i == n - 1 || in[i + 1] != 'A')) return i; } return -1; } int nextP(int n, char *in) { rep(i, n) { if (in[i] != 0) { if (i < n - 1 && in[i + 1] == 0) return i + 1; if (i > 0 && in[i - 1] == 0) return i - 1; } } return -1; } int notNextP(int n, char *in) { int ret = -1, maxdist = 0; rep(i, n) { if (in[i] != 0) continue; int tmpdist = 100000; for (int j = i - 1; j >= 0; j--) { if (in[j] != 0) { tmpdist = i - j; break; } } for (int j = i + 1; j < n; j++) { if (in[j] != 0) { tmpdist = min(tmpdist, j - i); break; } } if (tmpdist > maxdist) ret = i, maxdist = tmpdist; } return ret; } void put(int n, char *in, char inp) { int index = -1; if (inp == 'A') { index = findlm(n, in); } else if (inp == 'B') { index = findNotA(n, in); if (index == -1) index = findlm(n, in); } else if (inp == 'C') { index = nextP(n, in); if (index == -1) index = n / 2; } else if (inp == 'D') { index = notNextP(n, in); if (index == -1) index = 0; } in[index] = inp; } main() { char op[255]; op['A'] = 'A'; op['B'] = 'B'; op['C'] = 'C'; op['D'] = 'D'; op[0] = '#'; int n, m; char in[100]; while (cin >> n >> m && n) { rep(i, n) in[i] = 0; rep(i, m) { char inp; cin >> inp; put(n, in, inp); } rep(i, n) cout << op[in[i]]; cout << endl; } }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
1
639
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <string> #include <vector> using namespace std; int main() { int i, j, k; int a[102], m, n; string s; for (;;) { cin >> m >> n; if (m + n == 0) break; memset(a, 0, sizeof(a)); for (i = 0; i < n; i++) { cin >> s; if (s[0] == 'A') { for (j = 1; a[j]; j++) ; a[j] = 1; } else if (s[0] == 'B') { for (j = m; m > 0; m--) { if (a[j - 1] != 1 && a[j] == 0 && a[j + 1] != 1) { a[j] = 2; break; } } if (j == 0) { for (j = 1; a[j]; j++) ; a[j] = 2; } } else if (s[0] == 'C') { for (j = 1; j < m + 1 && a[j] == 0; j++) ; if (j == m + 1) a[m / 2 + 1] = 3; else { for (j = 1;; j++) { for (; a[j] == 0; j++) ; if (j != m && a[j + 1] == 0) { a[j + 1] = 3; break; } else if (j != 1 && a[j - 1] == 0) { a[j - 1] = 3; break; } } } } else if (s[0] == 'D') { for (j = 1; j < m + 1 && a[j] == 0; j++) ; if (j == m + 1) a[1] = 4; else { int p = 0, d; for (j = 1; a[j] == 0; j++) ; if (j > 2) { d = j - 2; p = 1; } for (;;) { for (k = j + 1; k < m + 1 && a[k] == 0; k++) ; if (k == m + 1) { if (k - j > 2) { if (p == 0 || d < k - j - 2) { d = k - j - 2; p = m; } } break; } else { if (k - j > 3) { if (p == 0 || d < (k - j) / 2 - 1) { d = (k - j) / 2 - 1; p = j + (k - j) / 2; } } } j = k; } if (p) a[p] = 4; else { for (j = 1; a[j]; j++) ; a[j] = 4; } } } } for (i = 1; i < m + 1; i++) { if (a[i]) cout << (char)(a[i] - 1 + 'A'); else cout << "#"; } cout << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <string> #include <vector> using namespace std; int main() { int i, j, k; int a[102], m, n; string s; for (;;) { cin >> m >> n; if (m + n == 0) break; memset(a, 0, sizeof(a)); for (i = 0; i < n; i++) { cin >> s; if (s[0] == 'A') { for (j = 1; a[j]; j++) ; a[j] = 1; } else if (s[0] == 'B') { for (j = m; j > 0; j--) { if (a[j - 1] != 1 && a[j] == 0 && a[j + 1] != 1) { a[j] = 2; break; } } if (j == 0) { for (j = 1; a[j]; j++) ; a[j] = 2; } } else if (s[0] == 'C') { for (j = 1; j < m + 1 && a[j] == 0; j++) ; if (j == m + 1) a[m / 2 + 1] = 3; else { for (j = 1;; j++) { for (; a[j] == 0; j++) ; if (j != m && a[j + 1] == 0) { a[j + 1] = 3; break; } else if (j != 1 && a[j - 1] == 0) { a[j - 1] = 3; break; } } } } else if (s[0] == 'D') { for (j = 1; j < m + 1 && a[j] == 0; j++) ; if (j == m + 1) a[1] = 4; else { int p = 0, d; for (j = 1; a[j] == 0; j++) ; if (j > 2) { d = j - 2; p = 1; } for (;;) { for (k = j + 1; k < m + 1 && a[k] == 0; k++) ; if (k == m + 1) { if (k - j > 2) { if (p == 0 || d < k - j - 2) { d = k - j - 2; p = m; } } break; } else { if (k - j > 3) { if (p == 0 || d < (k - j) / 2 - 1) { d = (k - j) / 2 - 1; p = j + (k - j) / 2; } } } j = k; } if (p) a[p] = 4; else { for (j = 1; a[j]; j++) ; a[j] = 4; } } } } for (i = 1; i < m + 1; i++) { if (a[i]) cout << (char)(a[i] - 1 + 'A'); else cout << "#"; } cout << endl; } return 0; }
[["-", 0, 57, 64, 9, 0, 7, 15, 16, 31, 22], ["+", 0, 57, 64, 9, 0, 7, 15, 16, 31, 22], ["-", 0, 57, 64, 9, 0, 7, 26, 27, 28, 22], ["+", 0, 57, 64, 9, 0, 7, 26, 27, 28, 22]]
1
708
#define _USE_MATH_DEFINES #include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <math.h> #include <memory.h> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; typedef complex<double> xy_t; typedef pair<xy_t, xy_t> line; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; typedef pair<int, string> Ps; typedef vector<int> vec; typedef vector<vec> mat; const int INF = 1 << 27; const double EPS = 1e-9; const double PI = 3.1415926535897932384626433832795; const int CLK = CLOCKS_PER_SEC; #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, m, n) for (int i = m; i < n; i++) int main() { int n, m; while (cin >> n >> m && (n || m)) { string chair(n, '#'); char ch; rep(i, m) { cin >> ch; if (ch == 'A') { rep(j, n) { if (chair[j] == '#') { chair[j] = ch; break; } } } else if (ch == 'B') { bool ok = false; rep(j, n) { if (chair[n - j - 1] == '#' && (j == 0 || chair[n - j] != 'A' && j == n - 1 || chair[n - j - 2] != 'A')) { chair[n - j - 1] = ch; ok = true; break; } } if (!ok) { rep(j, n) { if (chair[j] == '#') { chair[j] = ch; break; } } } } else if (ch == 'C') { if (i == 0) { chair[n / 2 + 1] = ch; } else { rep(j, n) { if (chair[j] != '#') { if (chair[j + 1] == '#') { chair[j + 1] = ch; break; } else if (j > 0 && chair[j - 1] == '#') { chair[j - 1] = ch; break; } } } } } else { if (i == 0) { chair[0] = ch; } else { int best = 0; int dis = 0; rep(i, n) { if (chair[i] != '#') continue; int d = INF; rep(j, n) { if (chair[j] != '#') { d = min(abs(j - i), d); } } if (d > dis) { dis = d; best = i; } } chair[best] = ch; } } } cout << chair << endl; } return 0; }
#define _USE_MATH_DEFINES #include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <math.h> #include <memory.h> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; typedef complex<double> xy_t; typedef pair<xy_t, xy_t> line; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; typedef pair<int, string> Ps; typedef vector<int> vec; typedef vector<vec> mat; const int INF = 1 << 27; const double EPS = 1e-9; const double PI = 3.1415926535897932384626433832795; const int CLK = CLOCKS_PER_SEC; #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, m, n) for (int i = m; i < n; i++) int main() { int n, m; while (cin >> n >> m && (n || m)) { string chair(n, '#'); char ch; rep(i, m) { cin >> ch; if (ch == 'A') { rep(j, n) { if (chair[j] == '#') { chair[j] = ch; break; } } } else if (ch == 'B') { bool ok = false; rep(j, n) { if (chair[n - j - 1] == '#' && (j == 0 || chair[n - j] != 'A' && j == n - 1 || chair[n - j - 2] != 'A')) { chair[n - j - 1] = ch; ok = true; break; } } if (!ok) { rep(j, n) { if (chair[j] == '#') { chair[j] = ch; break; } } } } else if (ch == 'C') { if (i == 0) { chair[n / 2] = ch; } else { rep(j, n) { if (chair[j] != '#') { if (chair[j + 1] == '#') { chair[j + 1] = ch; break; } else if (j > 0 && chair[j - 1] == '#') { chair[j - 1] = ch; break; } } } } } else { if (i == 0) { chair[0] = ch; } else { int best = 0; int dis = 0; rep(i, n) { if (chair[i] != '#') continue; int d = INF; rep(j, n) { if (chair[j] != '#') { d = min(abs(j - i), d); } } if (d > dis) { dis = d; best = i; } } chair[best] = ch; } } } cout << chair << endl; } return 0; }
[["-", 0, 11, 31, 69, 341, 342, 0, 16, 17, 72], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
1
620
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, m; bool en = false; while (cin >> n >> m && !(n == 0 && m == 0)) { char chair[101]; char ch; fill(chair, chair + 101, '#'); for (int i = 0; i < m; i++) { cin >> ch; if (ch == 'A') { for (int j = 0; j < n; j++) { if (chair[j] == '#') { chair[j] = ch; break; } } } else if (ch == 'B') { bool f = false; for (int j = n - 1; j >= 0; j--) { if (chair[j] == '#' && ((j - 1 < 0 || chair[j - 1] != 'A') && (j + 1 >= n || chair[j + 1] != 'A'))) { chair[j] = ch; f = true; break; } } // À‚ê‚È‚©‚Á‚½ê‡ if (!f) { for (int j = 0; j < n; j++) { if (chair[j] == '#') { chair[j] = ch; break; } } } } else if (ch == 'C') { bool f = false; for (int j = 0; j < n; j++) { if (chair[j] != '#') { f = true; break; } } if (!f) { chair[n / 2] == ch; } else { for (int j = 0; j < n; j++) { if (chair[j] != '#') { if (j + 1 < n && chair[j + 1] == '#') { chair[j + 1] = ch; f = true; break; } else if ((j - 1 >= 0) && chair[j - 1] == '#') { chair[j - 1] = ch; f = true; break; } } } } } else if (ch == 'D') { int dists[101]; fill(dists, dists + 101, 1000000); bool fff = false; for (int j = 0; j < n; j++) { if (chair[j] != '#') { fff = true; break; } } if (fff == false) { chair[0] = ch; } else { for (int j = 0; j < n; j++) { if (chair[j] != '#') { for (int k = 0; k < n; k++) { dists[k] = min(dists[k], abs(j - k)); } } } int maxCost = -1; int idx; for (int j = 0; j < n; j++) { if (chair[j] == '#') { if (maxCost < dists[j]) { maxCost = dists[j]; idx = j; } } } chair[idx] = ch; } } } for (int i = 0; i < n; i++) { cout << chair[i]; } cout << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, m; bool en = false; while (cin >> n >> m && !(n == 0 && m == 0)) { char chair[101]; char ch; fill(chair, chair + 101, '#'); for (int i = 0; i < m; i++) { cin >> ch; if (ch == 'A') { for (int j = 0; j < n; j++) { if (chair[j] == '#') { chair[j] = ch; break; } } } else if (ch == 'B') { bool f = false; for (int j = n - 1; j >= 0; j--) { if (chair[j] == '#' && ((j - 1 < 0 || chair[j - 1] != 'A') && (j + 1 >= n || chair[j + 1] != 'A'))) { chair[j] = ch; f = true; break; } } // À‚ê‚È‚©‚Á‚½ê‡ if (!f) { for (int j = 0; j < n; j++) { if (chair[j] == '#') { chair[j] = ch; break; } } } } else if (ch == 'C') { bool f = false; for (int j = 0; j < n; j++) { if (chair[j] != '#') { f = true; break; } } if (!f) { chair[n / 2] = ch; } else { for (int j = 0; j < n; j++) { if (chair[j] != '#') { if (j + 1 < n && chair[j + 1] == '#') { chair[j + 1] = ch; f = true; break; } else if ((j - 1 >= 0) && chair[j - 1] == '#') { chair[j - 1] = ch; f = true; break; } } } } } else if (ch == 'D') { int dists[101]; fill(dists, dists + 101, 1000000); bool fff = false; for (int j = 0; j < n; j++) { if (chair[j] != '#') { fff = true; break; } } if (fff == false) { chair[0] = ch; } else { for (int j = 0; j < n; j++) { if (chair[j] != '#') { for (int k = 0; k < n; k++) { dists[k] = min(dists[k], abs(j - k)); } } } int maxCost = -1; int idx; for (int j = 0; j < n; j++) { if (chair[j] == '#') { if (maxCost < dists[j]) { maxCost = dists[j]; idx = j; } } } chair[idx] = ch; } } } for (int i = 0; i < n; i++) { cout << chair[i]; } cout << endl; } return 0; }
[["-", 0, 57, 64, 9, 0, 1, 0, 16, 17, 60], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 17, 32]]
1
704
#include <algorithm> #include <iostream> using namespace std; int main() { int n, m; for (;;) { cin >> n >> m; if (!n && !m) break; string c; for (int i = 0; i < n; i++) c += '#'; for (int i = 0; i < m; i++) { char g; cin >> g; int pos; if (g == 'A') { pos = 0; while (c[pos] != '#') pos++; } else if (g == 'B') { pos = n - 1; while (pos >= 0) { if (c[pos] == '#') { if ((pos == n - 1 || c[pos + 1] != 'A') && (pos == 0 || c[pos - 1] != 'A')) break; } pos--; } if (pos < 0) { pos = 0; while (c[pos] != '#') pos++; } } else if (g == 'C') { if (!i) pos = n / 2; else { pos = 0; while (pos < n) { if (c[pos] != '#') { if (pos < n - 1 && c[pos + 1] == '#') { pos++; break; } if (pos > 0 && c[pos - 1] == '#') { pos--; break; } } pos++; } } } else { pos = 0; if (i) { int dis = 0; int tmp = -1; while (pos < n) { if (c[pos] == '#') { int l = 1, r = 1; while (pos - l >= 0 && c[pos - l] != '#') l++; while (pos + r < n && c[pos + r] != '#') r++; if (pos - l < 0) { if (dis < r) { dis = r; tmp = pos; } } else if (pos + r >= n) { if (dis < l) { dis = l; tmp = pos; } } else { if (dis < min(l, r)) { dis = min(l, r); tmp = pos; } } } pos++; } pos = tmp; } } c[pos] = g; } cout << c << endl; } }
#include <algorithm> #include <iostream> using namespace std; int main() { int n, m; for (;;) { cin >> n >> m; if (!n && !m) break; string c; for (int i = 0; i < n; i++) c += '#'; for (int i = 0; i < m; i++) { char g; cin >> g; int pos; if (g == 'A') { pos = 0; while (c[pos] != '#') pos++; } else if (g == 'B') { pos = n - 1; while (pos >= 0) { if (c[pos] == '#') { if ((pos == n - 1 || c[pos + 1] != 'A') && (pos == 0 || c[pos - 1] != 'A')) break; } pos--; } if (pos < 0) { pos = 0; while (c[pos] != '#') pos++; } } else if (g == 'C') { if (!i) pos = n / 2; else { pos = 0; while (pos < n) { if (c[pos] != '#') { if (pos < n - 1 && c[pos + 1] == '#') { pos++; break; } if (pos > 0 && c[pos - 1] == '#') { pos--; break; } } pos++; } } } else { pos = 0; if (i) { int dis = 0; int tmp = -1; while (pos < n) { if (c[pos] == '#') { int l = 1, r = 1; while (pos - l >= 0 && c[pos - l] == '#') l++; while (pos + r < n && c[pos + r] == '#') r++; if (pos - l < 0) { if (dis < r) { dis = r; tmp = pos; } } else if (pos + r >= n) { if (dis < l) { dis = l; tmp = pos; } } else { if (dis < min(l, r)) { dis = min(l, r); tmp = pos; } } } pos++; } pos = tmp; } } c[pos] = g; } cout << c << endl; } }
[["-", 0, 52, 15, 339, 51, 16, 12, 16, 17, 79], ["+", 0, 52, 15, 339, 51, 16, 12, 16, 17, 60]]
1
528
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int, int> pint; #define mp make_pair template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; } template <class T> void chmin(T &t, T f) { if (t > f) t = f; } template <class T> void chmax(T &t, T f) { if (t < f) t = f; } int in() { int x; scanf("%d", &x); return x; } int chairs[110]; int n, m; void fa() { int i; for (i = 0; i < n; i++) { if (chairs[i] == 0) { chairs[i] = 1; return; } } } void fb() { int i; for (i = n - 1; i >= 0; i--) { if (chairs[i] == 0 && (i == n - 1 || chairs[i + 1] != 1) && (i == 0 || chairs[i - 1] != 1)) { chairs[i] = 2; return; } } for (i = 0; i < n; i++) { if (chairs[i] == 0) { chairs[i] = 2; return; } } } void fc() { int i; for (i = 0; i < n; i++) { if (chairs[i] == 0) continue; if (i != n - 1 && chairs[i + 1] == 0) { chairs[i + 1] = 3; return; } if (i != 0 && chairs[i - 1] == 0) { chairs[i - 1] = 3; return; } } if (n % 2 == 0) { chairs[n / 2 + 1] = 3; return; } else { chairs[(n + 1) / 2] = 3; return; } } void fd() { int i, j; int c = 0, d = 0, dd; for (i = 0; i < n; i++) { if (chairs[i] == 0) { for (j = 1; i - j >= 0; j++) { if (chairs[i - j] != 0) break; } if (i - j < 0) j = n; dd = j; for (j = 1; i + j < n; j++) { if (chairs[i + j] != 0) break; } if (i + j == n) j = n; chmin(dd, j); if (d < dd) { c = i; d = dd; } } } chairs[c] = 4; return; } int main() { while (cin >> n >> m) { int i; string str; for (i = 0; i < n; i++) chairs[i] = 0; for (i = 0; i < m; i++) { cin >> str; if (str[0] == 'A') fa(); if (str[0] == 'B') fb(); if (str[0] == 'C') fc(); if (str[0] == 'D') fd(); } for (i = 0; i < n; i++) { if (chairs[i] == 0) cout << "#"; else if (chairs[i] == 1) cout << "A"; else if (chairs[i] == 2) cout << "B"; else if (chairs[i] == 3) cout << "C"; else if (chairs[i] == 4) cout << "D"; } cout << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int, int> pint; #define mp make_pair template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; } template <class T> void chmin(T &t, T f) { if (t > f) t = f; } template <class T> void chmax(T &t, T f) { if (t < f) t = f; } int in() { int x; scanf("%d", &x); return x; } int chairs[110]; int n, m; void fa() { int i; for (i = 0; i < n; i++) { if (chairs[i] == 0) { chairs[i] = 1; return; } } } void fb() { int i; for (i = n - 1; i >= 0; i--) { if (chairs[i] == 0 && (i == n - 1 || chairs[i + 1] != 1) && (i == 0 || chairs[i - 1] != 1)) { chairs[i] = 2; return; } } for (i = 0; i < n; i++) { if (chairs[i] == 0) { chairs[i] = 2; return; } } } void fc() { int i; for (i = 0; i < n; i++) { if (chairs[i] == 0) continue; if (i != n - 1 && chairs[i + 1] == 0) { chairs[i + 1] = 3; return; } if (i != 0 && chairs[i - 1] == 0) { chairs[i - 1] = 3; return; } } if (n % 2 == 0) { chairs[n / 2] = 3; return; } else { chairs[(n + 1) / 2 - 1] = 3; return; } } void fd() { int i, j; int c = 0, d = 0, dd; for (i = 0; i < n; i++) { if (chairs[i] == 0) { for (j = 1; i - j >= 0; j++) { if (chairs[i - j] != 0) break; } if (i - j < 0) j = n; dd = j; for (j = 1; i + j < n; j++) { if (chairs[i + j] != 0) break; } if (i + j == n) j = n; chmin(dd, j); if (d < dd) { c = i; d = dd; } } } chairs[c] = 4; return; } int main() { while (cin >> n >> m, n) { int i; string str; for (i = 0; i < n; i++) chairs[i] = 0; for (i = 0; i < m; i++) { cin >> str; // cout<<str; if (str[0] == 'A') fa(); if (str[0] == 'B') fb(); if (str[0] == 'C') fc(); if (str[0] == 'D') fd(); } // cout<<" : "; for (i = 0; i < n; i++) { if (chairs[i] == 0) cout << "#"; else if (chairs[i] == 1) cout << "A"; else if (chairs[i] == 2) cout << "B"; else if (chairs[i] == 3) cout << "C"; else if (chairs[i] == 4) cout << "D"; } cout << endl; } return 0; }
[["-", 0, 11, 31, 69, 341, 342, 0, 16, 17, 72], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
1
873
#include <iostream> #include <string.h> #include <vector> using namespace std; void sit_seat(char c, vector<char> &seat) { if (c == 'A') { for (int i = 0; i < seat.size(); i++) { if (seat[i] == 0) { seat[i] = c; break; } } } else if (c == 'B') { bool is_sit = false; for (int i = seat.size() - 1; i >= 0; i--) { if (seat[i] != 0) continue; int r = i + 1; if (r >= 0 && r < seat.size()) { if (seat[r] == 'A') continue; } int l = i - 1; if (l >= 0 && l < seat.size()) { if (seat[l] == 'A') continue; } seat[i] = c; is_sit = true; break; } if (!is_sit) { for (int i = 0; i < seat.size(); i++) { if (seat[i] == 0) seat[i] = c; } } } else if (c == 'C') { bool is_sit = false; for (int i = 0; i < seat.size(); i++) { if (seat[i] != 0) continue; int r = i + 1; if (r >= 0 && r < seat.size()) { if (seat[r] != 0) { seat[i] = c; is_sit = true; break; } } int l = i - 1; if (l >= 0 && l < seat.size()) { if (seat[l] != 0) { seat[i] = c; is_sit = true; break; } } } if (!is_sit) { if (seat.size() % 2 == 0) { seat[seat.size() / 2] = c; } else { seat[(seat.size() - 1) / 2] = c; } } } else if (c == 'D') { bool is_people = false; for (int i = 0; i < seat.size(); i++) { if (seat[i] != 0) { is_people = true; break; } } bool is_sit = false; if (!is_people) { seat[0] = c; is_sit = true; } if (!is_sit) { int mmd = 0; int p_sit = 0; for (int i = 0; i < seat.size(); i++) { if (seat[i] != 0) continue; int r = i; while (1) { r++; if (r < 0 || r >= seat.size()) { r = 10000; break; } if (seat[r] != 0) break; } int rd = r - i; int l = i; while (1) { l--; if (l < 0 || l >= seat.size()) { l = -10000; break; } if (seat[l] != 0) break; } int ld = i - l; int d = min(rd, ld); if (mmd < d) { mmd = d; p_sit = i; } } seat[p_sit] = c; } } else { cout << "unknown nationality!" << endl; return; } } int main() { int n, np; char c; while (cin >> n >> np, n | np) { vector<char> seat(n); memset(&seat[0], 0, sizeof(char) * n); for (int i = 0; i < np; i++) { cin >> c; sit_seat(c, seat); } for (int i = 0; i < n; i++) { if (seat[i] != 0) printf("%c", seat[i]); else printf("#"); } printf("\n"); } }
#include <iostream> #include <string.h> #include <vector> using namespace std; void sit_seat(char c, vector<char> &seat) { if (c == 'A') { for (int i = 0; i < seat.size(); i++) { if (seat[i] == 0) { seat[i] = c; break; } } } else if (c == 'B') { bool is_sit = false; for (int i = seat.size() - 1; i >= 0; i--) { if (seat[i] != 0) continue; int r = i + 1; if (r >= 0 && r < seat.size()) { if (seat[r] == 'A') continue; } int l = i - 1; if (l >= 0 && l < seat.size()) { if (seat[l] == 'A') continue; } seat[i] = c; is_sit = true; break; } if (!is_sit) { for (int i = 0; i < seat.size(); i++) { if (seat[i] == 0) seat[i] = c; } } } else if (c == 'C') { bool is_sit = false; for (int i = 0; i < seat.size(); i++) { if (seat[i] == 0) continue; int r = i + 1; if (r >= 0 && r < seat.size()) { if (seat[r] == 0) { seat[r] = c; is_sit = true; break; } } int l = i - 1; if (l >= 0 && l < seat.size()) { if (seat[l] == 0) { seat[l] = c; is_sit = true; break; } } } if (!is_sit) { if (seat.size() % 2 == 0) { seat[seat.size() / 2] = c; } else { seat[(seat.size() - 1) / 2] = c; } } } else if (c == 'D') { bool is_people = false; for (int i = 0; i < seat.size(); i++) { if (seat[i] != 0) { is_people = true; break; } } bool is_sit = false; if (!is_people) { seat[0] = c; is_sit = true; } if (!is_sit) { int mmd = 0; int p_sit = 0; for (int i = 0; i < seat.size(); i++) { if (seat[i] != 0) continue; int r = i; while (1) { r++; if (r < 0 || r >= seat.size()) { r = 10000; break; } if (seat[r] != 0) break; } int rd = r - i; int l = i; while (1) { l--; if (l < 0 || l >= seat.size()) { l = -10000; break; } if (seat[l] != 0) break; } int ld = i - l; int d = min(rd, ld); if (mmd < d) { mmd = d; p_sit = i; } } seat[p_sit] = c; } } else { cout << "unknown nationality!" << endl; return; } } int main() { int n, np; char c; while (cin >> n >> np, n | np) { vector<char> seat(n); memset(&seat[0], 0, sizeof(char) * n); for (int i = 0; i < np; i++) { cin >> c; sit_seat(c, seat); } for (int i = 0; i < n; i++) { if (seat[i] != 0) printf("%c", seat[i]); else printf("#"); } printf("\n"); } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 79], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60], ["-", 64, 9, 0, 57, 15, 339, 51, 16, 17, 79], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 17, 60], ["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22]]
1
859
#include <algorithm> #include <cstring> #include <iostream> using namespace std; int chair[100], pos, n, m; int d(char c) { if (c == 'A') { for (int i = 0; i < n; i++) { if (chair[i] == -1) return i; } } else if (c == 'B') { for (int i = n - 1; i >= 0; i--) { if (chair[i] == -1) { if (i == 0) { if (chair[i + 1] != 'A') return i; } else if (i == n - 1) { if (chair[i - 1] != 'A') return i; } else { if (chair[i - 1] != 'A' && chair[i + 1] != 'A') return i; } } } for (int i = 0; i < n; i++) { if (chair[i] == -1) return i; } } else if (c == 'C') { for (int i = 0; i < n; i++) { if (chair[i] != -1) { if (i + 1 < n && chair[i + 1] == -1) return i + 1; if (0 <= i - 1 && chair[i - 1] == -1) return i - 1; } } return n / 2; } else { int d_min_max = 0, d_pos = -1, tmp; for (int i = 0; i < n; i++) { //‹ó‚¢‚Ä‚¢‚éˆÖŽq‚ƍłà‹ß‚¢l‚Ì‹——£‚ð‹‚ß‚é if (chair[i] == -1) { int dist_min = 1 << 28; if (i != n - 1) { tmp = 1; while (i + tmp < n && chair[i + tmp] == -1) tmp++; if (i + tmp < n) { dist_min = min(dist_min, tmp); } } if (i != 0) { tmp = 1; while (i - tmp >= 0 && chair[i - tmp] == -1) tmp++; if (i - tmp >= 0) { dist_min = min(dist_min, tmp); } } if (d_min_max < dist_min && dist_min != 1 << 28) { d_min_max = dist_min; d_pos = i; } } } if (d_pos != -1) return d_pos; for (int i = 0; i < n; i++) { if (chair[i] == -1) return i; } } } int main() { while (cin >> n >> m) { if (!(n | m)) break; char c; memset(chair, -1, sizeof(chair)); for (int i = 0; i < m; i++) { cin >> c; chair[d(c)] = c; } for (int i = 0; i < n; i++) { if (chair[i] == -1) { cout << "#"; } else { cout << (char)chair[i]; } cout << endl; } } }
#include <algorithm> #include <cstring> #include <iostream> using namespace std; int chair[100], pos, n, m; int d(char c) { if (c == 'A') { for (int i = 0; i < n; i++) { if (chair[i] == -1) return i; } } else if (c == 'B') { for (int i = n - 1; i >= 0; i--) { if (chair[i] == -1) { if (i == 0) { if (chair[i + 1] != 'A') return i; } else if (i == n - 1) { if (chair[i - 1] != 'A') return i; } else { if (chair[i - 1] != 'A' && chair[i + 1] != 'A') return i; } } } for (int i = 0; i < n; i++) { if (chair[i] == -1) return i; } } else if (c == 'C') { for (int i = 0; i < n; i++) { if (chair[i] != -1) { if (i + 1 < n && chair[i + 1] == -1) return i + 1; if (0 <= i - 1 && chair[i - 1] == -1) return i - 1; } } return n / 2; } else { int d_min_max = 0, d_pos = -1, tmp; for (int i = 0; i < n; i++) { //‹ó‚¢‚Ä‚¢‚éˆÖŽq‚ƍłà‹ß‚¢l‚Ì‹——£‚ð‹‚ß‚é if (chair[i] == -1) { int dist_min = 1 << 28; if (i != n - 1) { tmp = 1; while (i + tmp < n && chair[i + tmp] == -1) tmp++; if (i + tmp < n) { dist_min = min(dist_min, tmp); } } if (i != 0) { tmp = 1; while (i - tmp >= 0 && chair[i - tmp] == -1) tmp++; if (i - tmp >= 0) { dist_min = min(dist_min, tmp); } } if (d_min_max < dist_min && dist_min != 1 << 28) { d_min_max = dist_min; d_pos = i; } } } if (d_pos != -1) return d_pos; for (int i = 0; i < n; i++) { if (chair[i] == -1) return i; } } } int main() { while (cin >> n >> m) { if (!(n | m)) break; char c; memset(chair, -1, sizeof(chair)); for (int i = 0; i < m; i++) { cin >> c; chair[d(c)] = c; } for (int i = 0; i < n; i++) { if (chair[i] == -1) { cout << "#"; } else { cout << (char)chair[i]; } } cout << endl; } }
[["+", 0, 52, 8, 9, 0, 7, 8, 9, 0, 46], ["-", 0, 52, 8, 9, 0, 7, 8, 9, 0, 46]]
1
638
// 08 #include <algorithm> #include <iostream> #include <string> using namespace std; int main() { for (int n, m; cin >> n >> m, n | m;) { string c(n, '#'); for (int i = 0; i < m; i++) { char p; cin >> p; switch (p) { case 'B': int j; for (j = c.size() - 1; j >= 0; j--) { if (c[j] == '#' && (j == 0 || c[j - 1] != 'A') && (j == c.size() - 1 || c[j + 1] != 'A')) { c[j] = p; break; } } if (j >= 0) break; case 'A': for (int j = 0; j < c.size(); j++) { if (c[j] == '#') { c[j] = p; break; } } break; case 'C': if (i == 0) { c[n / 2] = p; } else { for (int j = 0; j < c.size(); j++) { if (c[j] != '#') { if (j != c.size() - 1 && c[j + 1] == '#') { c[j + 1] = p; break; } else if (j != 0 && c[j - 1] == '#') { c[j - 1] = p; break; } } } } break; case 'D': if (i == 0) { c[0] = p; } else { int b; int d = -1; for (int j = 0; j < c.size(); j++) { if (c[j] == '#') { int cd; int l; for (l = j; l >= 0 && c[l] != '#'; l--) ; cd = (l < 0) ? 1000 : j - l; int r; for (r = j; r < n && c[r] != '#'; r++) ; cd = min(cd, (r == n) ? 1000 : r - j); if (d < cd) { d = cd; b = j; } } } c[b] = p; } break; } } cout << c << endl; } return 0; }
// 08 #include <algorithm> #include <iostream> #include <string> using namespace std; int main() { for (int n, m; cin >> n >> m, n | m;) { string c(n, '#'); for (int i = 0; i < m; i++) { char p; cin >> p; switch (p) { case 'B': int j; for (j = c.size() - 1; j >= 0; j--) { if (c[j] == '#' && (j == 0 || c[j - 1] != 'A') && (j == c.size() - 1 || c[j + 1] != 'A')) { c[j] = p; break; } } if (j >= 0) break; case 'A': for (int j = 0; j < c.size(); j++) { if (c[j] == '#') { c[j] = p; break; } } break; case 'C': if (i == 0) { c[n / 2] = p; } else { for (int j = 0; j < c.size(); j++) { if (c[j] != '#') { if (j != c.size() - 1 && c[j + 1] == '#') { c[j + 1] = p; break; } else if (j != 0 && c[j - 1] == '#') { c[j - 1] = p; break; } } } } break; case 'D': if (i == 0) { c[0] = p; } else { int b; int d = -1; for (int j = 0; j < c.size(); j++) { if (c[j] == '#') { int cd; int l; for (l = j; l >= 0 && c[l] == '#'; l--) ; cd = (l < 0) ? 1000 : j - l; int r; for (r = j; r < n && c[r] == '#'; r++) ; cd = min(cd, (r == n) ? 1000 : r - j); if (d < cd) { d = cd; b = j; } } } c[b] = p; } break; } } cout << c << endl; } return 0; }
[["-", 64, 9, 0, 7, 15, 16, 12, 16, 17, 79], ["+", 64, 9, 0, 7, 15, 16, 12, 16, 17, 60]]
1
537
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; #define INF (1 << 29) int main() { char chair[105]; int n, m; char c; while (cin >> n >> m, n) { fill(chair, chair + n, '#'); chair[n] = 0; for (int i = 0; i < m; i++) { cin >> c; switch (c) { case 'A': left: for (int j = 0; j < n; j++) { if (chair[j] == '#') { chair[j] = c; break; } } break; case 'B': for (int j = n - 1; j >= 0; j--) { if ((j == 0 || chair[j - 1] != 'A') && chair[j + 1] != 'A' && chair[j] == '#') { chair[j] = c; break; } if (j == 0) goto left; } break; case 'C': for (int j = 0; j < n; j++) { if (chair[j] != '#') { if (chair[j + 1] == '#') { chair[j + 1] = c; break; } if (j != 0 && chair[j - 1] == '#') { chair[j - 1] = c; break; } } if (j == n - 1) { chair[n / 2] = c; } } break; case 'D': int p = 0, s = -1; for (int j = 0; j < n; j++) { if (chair[j] != '#') continue; int t = INF; for (int k = j; k < n; k++) { if (chair[k] != '#') { t = k - j; } } for (int k = j; k >= 0; k--) { if (chair[k] != '#') { t = min(t, j - k); } } if (t > s) { p = j; s = t; } } if (p == INF) { chair[n - 1] = c; } else { chair[p] = c; } break; } } printf("%s\n", chair); } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; #define INF (1 << 29) int main() { char chair[105]; int n, m; char c; while (cin >> n >> m, n) { fill(chair, chair + n, '#'); chair[n] = 0; for (int i = 0; i < m; i++) { cin >> c; switch (c) { case 'A': left: for (int j = 0; j < n; j++) { if (chair[j] == '#') { chair[j] = c; break; } } break; case 'B': for (int j = n - 1; j >= 0; j--) { if ((j == 0 || chair[j - 1] != 'A') && chair[j + 1] != 'A' && chair[j] == '#') { chair[j] = c; break; } if (j == 0) goto left; } break; case 'C': for (int j = 0; j < n; j++) { if (chair[j] != '#') { if (chair[j + 1] == '#') { chair[j + 1] = c; break; } if (j != 0 && chair[j - 1] == '#') { chair[j - 1] = c; break; } } if (j == n - 1) { chair[n / 2] = c; } } break; case 'D': int p = 0, s = -1; for (int j = 0; j < n; j++) { if (chair[j] != '#') continue; int t = INF; for (int k = j; k < n; k++) { if (chair[k] != '#') { t = k - j; break; } } for (int k = j; k >= 0; k--) { if (chair[k] != '#') { t = min(t, j - k); break; } } if (t > s) { p = j; s = t; } } if (p == INF) { chair[n - 1] = c; } else { chair[p] = c; } break; } } printf("%s\n", chair); } return 0; }
[["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 35], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35]]
1
514
#include <iostream> #include <string> #include <vector> using namespace std; const int N_MAX = 100; void sitA(char *seat, int n) { int i = 0; while (seat[i] != '#') { ++i; } seat[i] = 'A'; } void sitB(char *seat, int n) { int i = n - 1; while (i > 0) { if (seat[i] == '#') { if (seat[i - 1] != 'A' && seat[i + 1] != 'A') { seat[i] = 'B'; return; } } --i; } while (seat[i] != '#') { ++i; } seat[i] = 'B'; } void sitC(char *seat, int n) { int i = 0; while (i < n) { if (seat[i] != '#') { if (seat[i + 1] == '#') { seat[i + 1] = 'C'; return; } if (i >= 0 && seat[i - 1] == '#') { seat[i - 1] = 'C'; return; } } ++i; } seat[n / 2] = 'C'; } void sitD(char *seat, int n) { int est[N_MAX]; int s, e; vector<int> seated; for (int i = 0; i < n; ++i) { if (seat[i] != '#') { seated.push_back(i); } } if (seated.size() == 0) { seat[0] = 'D'; return; } e = seated[0]; for (int i = e; i >= 0; --i) { est[i] = e - i; } for (int i = 1; i < seated.size(); ++i) { s = e; e = seated[i]; for (int j = 0; j <= (e - s - 1) / 2; ++j) { est[s + j] = est[e - j] = j; } } for (int i = e; i < n; ++i) { est[i] = i - e; } int estMax = 0, estIdx = 0; for (int i = 0; i < n; ++i) { if (est[i] > estMax) { estMax = est[i]; estIdx = i; } } seat[estIdx] = 'D'; } void sit(char a, char *seat, int n) { switch (a) { case 'A': sitA(seat, n); break; case 'B': sitB(seat, n); break; case 'C': sitC(seat, n); break; case 'D': sitD(seat, n); break; } } int main() { int n, m; char seat[N_MAX + 1]; while (cin >> n >> m && (n || m)) { for (int i = 0; i < n; ++i) { seat[i] = '#'; } seat[n] = '\0'; for (int i = 0; i < m; ++i) { char a; cin >> a; sit(a, seat, n); } cout << seat << endl; } return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; const int N_MAX = 100; void sitA(char *seat, int n) { int i = 0; while (seat[i] != '#') { ++i; } seat[i] = 'A'; } void sitB(char *seat, int n) { int i = n - 1; while (i > 0) { if (seat[i] == '#') { if (seat[i - 1] != 'A' && seat[i + 1] != 'A') { seat[i] = 'B'; return; } } --i; } while (seat[i] != '#') { ++i; } seat[i] = 'B'; } void sitC(char *seat, int n) { int i = 0; while (i < n) { if (seat[i] != '#') { if (seat[i + 1] == '#') { seat[i + 1] = 'C'; return; } if (i >= 0 && seat[i - 1] == '#') { seat[i - 1] = 'C'; return; } } ++i; } seat[n / 2] = 'C'; } void sitD(char *seat, int n) { int est[N_MAX]; int s, e; vector<int> seated; for (int i = 0; i < n; ++i) { if (seat[i] != '#') { seated.push_back(i); } } if (seated.size() == 0) { seat[0] = 'D'; return; } e = seated[0]; for (int i = e; i >= 0; --i) { est[i] = e - i; } for (int i = 1; i < seated.size(); ++i) { s = e; e = seated[i]; for (int j = 0; j <= (e - s) / 2; ++j) { est[s + j] = est[e - j] = j; } } for (int i = e; i < n; ++i) { est[i] = i - e; } int estMax = 0, estIdx = 0; for (int i = 0; i < n; ++i) { if (est[i] > estMax) { estMax = est[i]; estIdx = i; } } seat[estIdx] = 'D'; } void sit(char a, char *seat, int n) { switch (a) { case 'A': sitA(seat, n); break; case 'B': sitB(seat, n); break; case 'C': sitC(seat, n); break; case 'D': sitD(seat, n); break; } } int main() { int n, m; char seat[N_MAX + 1]; while (cin >> n >> m && (n || m)) { for (int i = 0; i < n; ++i) { seat[i] = '#'; } seat[n] = '\0'; for (int i = 0; i < m; ++i) { char a; cin >> a; sit(a, seat, n); } cout << seat << endl; } return 0; }
[["-", 15, 16, 12, 16, 31, 23, 0, 16, 17, 33], ["-", 15, 16, 12, 16, 31, 23, 0, 16, 12, 13]]
1
734
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, j) REP((i), 0, (j)) #define REP(i, j, k) for (int i = (j); (i) < (k); ++i) #define BW(a, x, b) ((a) <= (x) && (x) <= (b)) #define F first #define S second #define INF 1 << 30 typedef pair<int, int> pi; typedef pair<int, pi> pii; typedef vector<int> vi; typedef queue<int> qi; typedef long long ll; int n, m, f, dis, pos; char c[100]; int main() { while (scanf("%d%d", &n, &m) && n) { rep(i, n) c[i] = '#'; char p[2]; rep(k, m) { scanf("%s", p); switch (p[0]) { case 'A': rep(i, n) if (c[i] == '#') { c[i] = 'A'; break; ; } break; case 'B': if (c[n - 2] != 'A' && c[n - 1] == '#') { c[n - 1] = 'B'; break; } for (int i = n - 2; i > 0; i--) if (c[i + 1] != 'A' && c[i - 1] != 'A' && c[i] == '#') { c[i] = 'B'; goto e1; } if (c[1] != 'A' && c[0] == '#') { c[0] = 'B'; break; } rep(i, n) if (c[i] == '#') { c[i] = 'B'; break; } e1: break; case 'C': rep(i, n) if (c[i] != '#') { if (i + 1 < n && c[i + 1] == '#') { c[i + 1] = 'C'; goto e2; } else if (i - 1 >= 0 && c[i - 1] == '#') { c[i + 1] = 'C'; goto e2; } } if (n % 2) c[(n + 1) / 2 - 1] = 'C'; else c[n / 2] = 'C'; e2: break; case 'D': dis = 0, pos = 0; rep(i, n) { if (c[i] != '#') continue; int j; f = 0; for (j = 1; j < n; j++) { if (i - j >= 0 && c[i - j] != '#') f = 1; // printf("%d ", f); if (i + j < n && c[i + j] != '#') f = 1; // printf("%d ", f); if (f) { if (dis < j) { dis = j; pos = i; } break; } } // printf("%d %d %d\n",i, dis, pos); } c[pos] = 'D'; break; } } rep(i, n) putchar(c[i]); puts(""); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, j) REP((i), 0, (j)) #define REP(i, j, k) for (int i = (j); (i) < (k); ++i) #define BW(a, x, b) ((a) <= (x) && (x) <= (b)) #define F first #define S second #define INF 1 << 30 typedef pair<int, int> pi; typedef pair<int, pi> pii; typedef vector<int> vi; typedef queue<int> qi; typedef long long ll; int n, m, f, dis, pos; char c[100]; int main() { while (scanf("%d%d", &n, &m) && n) { rep(i, n) c[i] = '#'; char p[2]; rep(k, m) { scanf("%s", p); switch (p[0]) { case 'A': rep(i, n) if (c[i] == '#') { c[i] = 'A'; break; ; } break; case 'B': if (c[n - 2] != 'A' && c[n - 1] == '#') { c[n - 1] = 'B'; break; } for (int i = n - 2; i > 0; i--) if (c[i + 1] != 'A' && c[i - 1] != 'A' && c[i] == '#') { c[i] = 'B'; goto e1; } if (c[1] != 'A' && c[0] == '#') { c[0] = 'B'; break; } rep(i, n) if (c[i] == '#') { c[i] = 'B'; break; } e1: break; case 'C': rep(i, n) if (c[i] != '#') { if (i + 1 < n && c[i + 1] == '#') { c[i + 1] = 'C'; goto e2; } else if (i - 1 >= 0 && c[i - 1] == '#') { c[i - 1] = 'C'; goto e2; } } if (n % 2) c[(n + 1) / 2 - 1] = 'C'; else c[n / 2] = 'C'; e2: break; case 'D': dis = 0, pos = 0; rep(i, n) { if (c[i] != '#') continue; int j; f = 0; for (j = 1; j < n; j++) { if (i - j >= 0 && c[i - j] != '#') f = 1; // printf("%d ", f); if (i + j < n && c[i + j] != '#') f = 1; // printf("%d ", f); if (f) { if (dis < j) { dis = j; pos = i; } break; } } // printf("%d %d %d\n",i, dis, pos); } c[pos] = 'D'; break; } } rep(i, n) putchar(c[i]); puts(""); } return 0; }
[["-", 0, 11, 31, 69, 341, 342, 0, 16, 17, 72], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33]]
1
703
#include <iostream> using namespace std; int n, m; char t[100], ch; void putA(); void putB(); void putC(); void putD(); int main() { while (cin >> n >> m) { if (n == 0 && m == 0) break; for (int i = 0; i < n; i++) t[i] = '#'; for (int i = 0; i < m; i++) { cin >> ch; if (ch == 'A') putA(); else if (ch == 'B') putB(); else if (ch == 'C') putC(); else putD(); } for (int i = 0; i < n; i++) cout << t[i]; cout << endl; } return 0; } void putA() { for (int i = 0; i < n; i++) { if (t[i] != '#') continue; t[i] = 'A'; return; } } void putB() { for (int i = n - 1; i >= 0; i--) { if (t[i] != '#') continue; if (i - 1 >= 0 && t[i - 1] == 'A') continue; if (i + 1 < n && t[i + 1] == 'A') continue; t[i] = 'B'; return; } for (int i = 0; i < n; i++) { if (t[i] == '#') { t[i] = 'B'; return; } } } void putC() { for (int i = 0; i < n; i++) { if (t[i] == '#') continue; if (i + 1 < n && t[i + 1] == '#') { t[i + 1] = 'C'; return; } else if (i - 1 >= 0 && t[i - 1] == '#') { t[i - 1] = 'C'; return; } } t[n / 2] = 'C'; } void putD() { bool flg = true; for (int i = 0; i < n; i++) if (t[i] != '#') flg = false; if (flg) { t[0] = 'D'; return; } int l[100], r[100], m[100]; l[0] = (t[0] == '#' ? 7 : 0); for (int i = 1; i < n; i++) { if (t[i] == '#') l[i] = l[i - 1] + 1; else l[i] = 0; } r[n - 1] = (t[n - 1] == '#' ? 7 : 0); ; for (int i = n - 2; i >= 0; i--) { if (t[i] == '#') r[i] = r[i + 1] + 1; else r[i] = 0; } for (int i = 0; i < n; i++) { m[i] = min(l[i], r[i]); } int pos = -1, maxm = 0; for (int i = 0; i < n; i++) { if (t[i] != '#') continue; if (m[i] > maxm) { pos = i; maxm = m[i]; } } if (pos != -1) { t[pos] = 'D'; } }
#include <iostream> using namespace std; int n, m; char t[100], ch; void putA(); void putB(); void putC(); void putD(); int main() { while (cin >> n >> m) { if (n == 0 && m == 0) break; for (int i = 0; i < n; i++) t[i] = '#'; for (int i = 0; i < m; i++) { cin >> ch; if (ch == 'A') putA(); else if (ch == 'B') putB(); else if (ch == 'C') putC(); else putD(); } for (int i = 0; i < n; i++) cout << t[i]; cout << endl; } return 0; } void putA() { for (int i = 0; i < n; i++) { if (t[i] != '#') continue; t[i] = 'A'; return; } } void putB() { for (int i = n - 1; i >= 0; i--) { if (t[i] != '#') continue; if (i - 1 >= 0 && t[i - 1] == 'A') continue; if (i + 1 < n && t[i + 1] == 'A') continue; t[i] = 'B'; return; } for (int i = 0; i < n; i++) { if (t[i] == '#') { t[i] = 'B'; return; } } } void putC() { for (int i = 0; i < n; i++) { if (t[i] == '#') continue; if (i + 1 < n && t[i + 1] == '#') { t[i + 1] = 'C'; return; } else if (i - 1 >= 0 && t[i - 1] == '#') { t[i - 1] = 'C'; return; } } t[n / 2] = 'C'; } void putD() { bool flg = true; for (int i = 0; i < n; i++) if (t[i] != '#') flg = false; if (flg) { t[0] = 'D'; return; } int l[100], r[100], m[100]; l[0] = (t[0] == '#' ? 100 : 0); for (int i = 1; i < n; i++) { if (t[i] == '#') l[i] = l[i - 1] + 1; else l[i] = 0; } r[n - 1] = (t[n - 1] == '#' ? 100 : 0); for (int i = n - 2; i >= 0; i--) { if (t[i] == '#') r[i] = r[i + 1] + 1; else r[i] = 0; } // for(int i=0;i<n;i++)cout<<t[i];cout<<endl; for (int i = 0; i < n; i++) { m[i] = min(l[i], r[i]); // cout<<m[i]; } // cout<<endl; int pos = -1, maxm = 0; for (int i = 0; i < n; i++) { if (t[i] != '#') continue; if (m[i] > maxm) { pos = i; maxm = m[i]; } } if (pos != -1) { t[pos] = 'D'; } }
[["-", 0, 1, 0, 11, 12, 23, 0, 41, 64, 13], ["+", 0, 1, 0, 11, 12, 23, 0, 41, 64, 13], ["-", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35]]
1
801
while(n,m=gets.split.map &:to_i)[0]>0 s=?#*n+?0 m.times{(c=gets[0])<?B?(i=s.index ?#):c<?C?(i=n-1 i-=1 while i>=0&&(s[i]!=?#||s[j=i+1]==?A||s[i-1]==?A) i<0&&i=j-1):c<?D?(j=0 j+=1 while j<n&&(s[j]==?#||s[i=j+1]!=?#&&s[i=j-1]!=?#) j>=n&&i=n/2):(w=z=1e20 t=[] u=[] (0..n-1).map{|k|s[k]!=?#&&w=0;t<<w+=1;s[n-1-k]!=?#&&z=0;u.unshift z+=1} v=t.zip(u).map &:min i=v.index{|k|k==v.max}) s[i]=c puts s.chop } puts s.chop end
while(n,m=gets.split.map &:to_i)[0]>0 s=?#*n+?0 m.times{(c=gets[0])<?B?(i=s.index ?#):c<?C?(i=n-1 i-=1 while i>=0&&(s[i]!=?#||s[j=i+1]==?A||s[i-1]==?A) i<0&&i=j-1):c<?D?(j=0 j+=1 while j<n&&(s[j]==?#||s[i=j+1]!=?#&&s[i=j-1]!=?#) j>=n&&i=n/2):(w=z=1e20 t=[] u=[] (0..n-1).map{|k|s[k]!=?#&&w=0;t<<w+=1;s[n-1-k]!=?#&&z=0;u.unshift z+=1} v=t.zip(u).map &:min i=v.index{|k|k==v.max}) s[i]=c} puts s.chop end
[["-", 0, 42, 0, 754, 75, 739, 0, 768, 0, 769], ["+", 0, 42, 0, 754, 75, 739, 0, 768, 0, 769]]
4
233
import java.util.*; public class Main { int n, m, k, start, goal, r; int[][] cost, time; final int INF = Integer.MAX_VALUE; int[] min; int dijkstra(int[][] V) { int[] min = new int[m + 1]; Arrays.fill(min, INF); min[start] = 0; Queue<int[]> que = new LinkedList<int[]>(); que.offer(new int[] {start, 0}); while (!que.isEmpty()) { int[] p = que.poll(); int from = p[0]; if (min[from] < p[1]) continue; for (int to = 1; to <= m; to++) { int v = V[from][to]; if (min[from] + v < min[to]) { min[to] = min[from] + v; que.offer(new int[] {to, min[to]}); } } } return min[goal]; } int solve() { if (r == 0) { return dijkstra(cost); } else { return dijkstra(time); } } void run() { Scanner sc = new Scanner(System.in); String output = ""; while (sc.hasNext()) { n = sc.nextInt(); m = sc.nextInt(); if (n == 0 && m == 0) { break; } else { cost = new int[m + 1][m + 1]; time = new int[m + 1][m + 1]; for (int i = 1; i <= m; i++) { Arrays.fill(cost[i], INF); Arrays.fill(time[i], INF); } for (int i = 0; i < n; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int t = sc.nextInt(); cost[a][b] = c; cost[b][a] = c; time[a][b] = t; time[b][a] = t; } k = sc.nextInt(); for (int i = 0; i < k; i++) { start = sc.nextInt(); goal = sc.nextInt(); r = sc.nextInt(); output += solve() + "\n"; } } } sc.close(); System.out.print(output); } public static void main(String[] args) { new Main().run(); } }
import java.util.*; public class Main { int n, m, k, start, goal, r; int[][] cost, time; final int INF = Integer.MAX_VALUE; int[] min; int dijkstra(int[][] V) { int[] min = new int[m + 1]; Arrays.fill(min, INF); min[start] = 0; Queue<int[]> que = new LinkedList<int[]>(); que.offer(new int[] {start, 0}); while (!que.isEmpty()) { int[] p = que.poll(); int from = p[0]; if (min[from] < p[1]) continue; for (int to = 1; to <= m; to++) { int v = V[from][to]; if (v != INF && min[from] + v < min[to]) { min[to] = min[from] + v; que.offer(new int[] {to, min[to]}); } } } return min[goal]; } int solve() { if (r == 0) { return dijkstra(cost); } else { return dijkstra(time); } } void run() { Scanner sc = new Scanner(System.in); String output = ""; while (sc.hasNext()) { n = sc.nextInt(); m = sc.nextInt(); if (n == 0 && m == 0) { break; } else { cost = new int[m + 1][m + 1]; time = new int[m + 1][m + 1]; for (int i = 1; i <= m; i++) { Arrays.fill(cost[i], INF); Arrays.fill(time[i], INF); } for (int i = 0; i < n; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int t = sc.nextInt(); cost[a][b] = c; cost[b][a] = c; time[a][b] = t; time[b][a] = t; } k = sc.nextInt(); for (int i = 0; i < k; i++) { start = sc.nextInt(); goal = sc.nextInt(); r = sc.nextInt(); output += solve() + "\n"; } } } sc.close(); System.out.print(output); } public static void main(String[] args) { new Main().run(); } }
[["+", 0, 57, 15, 15, 0, 16, 31, 16, 31, 22], ["+", 0, 57, 15, 15, 0, 16, 31, 16, 17, 79], ["+", 0, 57, 15, 15, 0, 16, 31, 16, 12, 22], ["+", 8, 196, 0, 57, 15, 15, 0, 16, 17, 98]]
3
595
/* ?????????????????? ????????\?????????????¬???¨??? ??? ??? ???????????´??????????´\????????????????????????????????§????????????????¨?????????????????????????????????? ????????????????????§??????????????????????????????????????????????????§???????????????????????????????????? ??????????????\??????????????????????????¨?????\???????????????????????????????????????????????????????????° ??????????¨????????????????????????????????????????????´?????????????????????§?????????????????????????????? ?¨??????????????????????????????°??????????????£???????????????????????? ??????????????±????§??????°?????\?????¨?????????????????????????????????????????????????????????????°??????? ???????????????????????°????????????????????????????????????????§???? 1 ??\??? 1000 ??\????????´??°??§ ??¨??????????§??????° m ??? 1 ??\??? 1000 ??\?????§????????????????????±?????????????????????????????? ????§???? a b ???????§?????????? time ????????? cost ??§??¨????????? time cost ??????????????? 1 ??\??? 1000 ??\?????¨????????????????????????????????????????§? p ?????°????§? q ???????¨???? r ??§??¨????????? r ??? 0 ?????¨???????????????????????? 1 ?????????????°??????????????????????????????? ?????????????????° k ??? 1 ??\??? 1000 ??\?????§?????????????????????????????????????????????????????? ???????????¨???????????? ??\ ??? ?????°???????????????????????????????????\?????¨?????????????????????????????\?????????????????????????????? ???????????§????????????????????????????????????????????\????????¨????????§?????? 1 ?????? ??????????????±?????° n????§??????° m ?????´??° ??´??°???????§??????????????????? 2 ?????? ?¬¬ 1 ?????????????????± a b cost time?????´??° ??´??° ??´??° ??´??°???????§??????????????????? 3 ?????? ?¬¬ 2 ?????????????????± ??? n+1 ?????? ?¬¬ n ?????????????????± n+2 ?????? ????????????????????° k?????´??°??? n+3 ?????? ?¬¬ 1 ?????????????????? p q r ?????´??° ??´??° ??´??°???????§??????????????????? n+4 ?????? ?¬¬ 2 ?????????????????? ??? n+2+k ??? ?¬¬ k ?????????????????? ??? ??? ??\??????????????????????????¨??????????????????????????????????°???????????????????????????? */ /* ???????????£????????????????????????????§£??? ??? ?????????????????????????§£?????????????????????????????????????????§???????????£?????´?????????????????¶????????¨?????£????????????????????? ???2009??´????????§???????????????????????§??????????§£??¬??§??????????§£???????????£????§£????????????????????????... */ #include <stdio.h> #define NUM 1000 // ?????????????´?????????° #define MAX 1000000000 // ?????????????´????????????????????????????????????§??? int min(int a, int b) { if (a > b) { return b; } else { return a; } } int main(void) { int i, j; int c, t; int a, b; // ??????????????± ?§???? a????????? b int n, m; // ??????????????±?????° n????§??????° m int r; // ???????????????????¨????(0:??????????????? 1:????°???????) int cost[NUM][NUM]; // ????????????????????? int time[NUM][NUM]; // ????????????????????? while (1) { scanf("%d %d", &n, &m); if ((n == 0) || (m == 0)) { break; // ????????°??????????????? } for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { if (i != j) { cost[i][j] = MAX; time[i][j] = MAX; } else { cost[i][j] = 0; time[i][j] = 0; } } } for (i = 0; i < n; i++) { scanf("%d %d %d %d", &a, &b, &c, &t); //??????????????\???????????????????????±?????¨?????????????????¨?????????????????????????????? cost[a - 1][b - 1] = c; time[a - 1][b - 1] = t; cost[b - 1][a - 1] = c; // ?????????????????????????????????????????? time[b - 1][a - 1] = t; } // ?¶?????????????????????±????£?????????? for (i = 0; i < m; i++) { for (a = 0; a < m; a++) { if (a == i) { continue; } else { for (b = 0; b < i; b++) { if ((a == i) || (b == i) || (a == b)) { continue; } else { cost[a][b] = min(cost[a][i] + cost[i][b], cost[a][b]); cost[b][a] = min(cost[b][i] + cost[i][a], cost[b][a]); time[a][b] = min(time[a][i] + time[i][b], time[a][b]); time[b][a] = min(time[b][i] + time[i][a], time[b][a]); } } } } } /* for ( a = 0 ; a < m ; a++ ) { for ( b = 0 ; b < m ; b++ ) { printf( "%3d ", cost[ a ][ b ] ); } printf( "\n" ); } */ scanf("%d", &n); // ????????????????????°?????\??? for (i = 0; i < n; i++) { scanf("%d %d %d", &a, &b, &r); if (a > b) { j = a; a = b; b = j; } if (r != 0) { printf("%d\n", time[a - 1][b - 1]); } else { printf("%d\n", cost[a - 1][b - 1]); } } } return 0; }
/* ?????????????????? ????????\?????????????¬???¨??? ??? ??? ???????????´??????????´\????????????????????????????????§????????????????¨?????????????????????????????????? ????????????????????§??????????????????????????????????????????????????§???????????????????????????????????? ??????????????\??????????????????????????¨?????\???????????????????????????????????????????????????????????° ??????????¨????????????????????????????????????????????´?????????????????????§?????????????????????????????? ?¨??????????????????????????????°??????????????£???????????????????????? ??????????????±????§??????°?????\?????¨?????????????????????????????????????????????????????????????°??????? ???????????????????????°????????????????????????????????????????§???? 1 ??\??? 1000 ??\????????´??°??§ ??¨??????????§??????° m ??? 1 ??\??? 1000 ??\?????§????????????????????±?????????????????????????????? ????§???? a b ???????§?????????? time ????????? cost ??§??¨????????? time cost ??????????????? 1 ??\??? 1000 ??\?????¨????????????????????????????????????????§? p ?????°????§? q ???????¨???? r ??§??¨????????? r ??? 0 ?????¨???????????????????????? 1 ?????????????°??????????????????????????????? ?????????????????° k ??? 1 ??\??? 1000 ??\?????§?????????????????????????????????????????????????????? ???????????¨???????????? ??\ ??? ?????°???????????????????????????????????\?????¨?????????????????????????????\?????????????????????????????? ???????????§????????????????????????????????????????????\????????¨????????§?????? 1 ?????? ??????????????±?????° n????§??????° m ?????´??° ??´??°???????§??????????????????? 2 ?????? ?¬¬ 1 ?????????????????± a b cost time?????´??° ??´??° ??´??° ??´??°???????§??????????????????? 3 ?????? ?¬¬ 2 ?????????????????± ??? n+1 ?????? ?¬¬ n ?????????????????± n+2 ?????? ????????????????????° k?????´??°??? n+3 ?????? ?¬¬ 1 ?????????????????? p q r ?????´??° ??´??° ??´??°???????§??????????????????? n+4 ?????? ?¬¬ 2 ?????????????????? ??? n+2+k ??? ?¬¬ k ?????????????????? ??? ??? ??\??????????????????????????¨??????????????????????????????????°???????????????????????????? */ /* ???????????£????????????????????????????§£??? ??? ?????????????????????????§£?????????????????????????????????????????§???????????£?????´??? ??????????????¶????????¨?????£????????????????????? ???2009??´????????§???????????????????????§??????????§£??¬??§??????????§£???????????£????§£??? ?????????????????????... */ #include <stdio.h> #define NUM 1000 // ?????????????´?????????° #define MAX 1000000000 // ?????????????´????????????????????????????????????§??? int min(int a, int b) { if (a > b) { return b; } else { return a; } } int main(void) { int i, j; int c, t; int a, b; // ??????????????± ?§???? a????????? b int n, m; // ??????????????±?????° n????§??????° m int r; // ???????????????????¨????(0:??????????????? 1:????°???????) int cost[NUM][NUM]; // ????????????????????? int time[NUM][NUM]; // ????????????????????? while (1) { scanf("%d %d", &n, &m); if ((n == 0) || (m == 0)) { break; // ????????°??????????????? } for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { if (i != j) { cost[i][j] = MAX; time[i][j] = MAX; } else { cost[i][j] = 0; time[i][j] = 0; } } } for (i = 0; i < n; i++) { scanf("%d %d %d %d", &a, &b, &c, &t); //??????????????\???????????????????????±?????¨?????????????????¨?????????????????????????????? cost[a - 1][b - 1] = c; time[a - 1][b - 1] = t; cost[b - 1][a - 1] = c; // ?????????????????????????????????????????? time[b - 1][a - 1] = t; } // ?¶?????????????????????±????£?????????? for (i = 0; i < m; i++) { for (a = 0; a < m; a++) { if (a == i) { continue; } else { for (b = 0; b < m; b++) { if ((a == i) || (b == i) || (a == b)) { continue; } else { cost[a][b] = min(cost[a][i] + cost[i][b], cost[a][b]); cost[b][a] = min(cost[b][i] + cost[i][a], cost[b][a]); time[a][b] = min(time[a][i] + time[i][b], time[a][b]); time[b][a] = min(time[b][i] + time[i][a], time[b][a]); } } } } } /* for ( a = 0 ; a < m ; a++ ) { for ( b = 0 ; b < m ; b++ ) { printf( "%3d ", cost[ a ][ b ] ); } printf( "\n" ); } */ scanf("%d", &n); // ????????????????????°?????\??? for (i = 0; i < n; i++) { scanf("%d %d %d", &a, &b, &r); if (a > b) { j = a; a = b; b = j; } if (r == 0) { printf("%d\n", cost[a - 1][b - 1]); } else { printf("%d\n", time[a - 1][b - 1]); } } } return 0; }
[["-", 75, 76, 0, 9, 0, 7, 15, 16, 12, 22], ["+", 75, 76, 0, 9, 0, 7, 15, 16, 12, 22], ["-", 8, 9, 0, 57, 15, 23, 0, 16, 17, 79], ["+", 8, 9, 0, 57, 15, 23, 0, 16, 17, 60], ["-", 0, 2, 3, 4, 0, 69, 28, 69, 28, 22], ["+", 0, 2, 3, 4, 0, 69, 28, 69, 28, 22]]
0
645
#include <stdio.h> #define INF 100000000 int main(void) { int n, m, a, b, cost, time, l, p, q, r; int i, j, k; int d[100][100]; int exp[100][100]; while (1) { scanf("%d %d", &n, &m); if (n == 0 && m == 0) break; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { d[i][j] = INF; exp[i][j] = INF; } } for (i = 0; i < n; i++) { d[i][i] = 0; exp[i][i] = 0; } for (i = 0; i < n; i++) { scanf("%d %d %d %d", &a, &b, &cost, &time); d[a - 1][b - 1] = time; d[b - 1][a - 1] = time; exp[a - 1][b - 1] = cost; exp[b - 1][a - 1] = cost; } for (k = 0; k < m; k++) { for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { if (d[i][j] > d[i][k] + d[k][j]) { d[i][j] = d[i][k] + d[k][j]; } if (exp[i][j] > exp[i][k] + exp[k][j]) { exp[i][j] = exp[i][k] + exp[k][j]; } } } } scanf("%d", &l); for (i = 0; i < l; i++) { scanf("%d %d %d", &p, &q, &r); if (r == 0) { printf("%d\n", exp[p - 1][q - 1]); } else if (r == 1) { printf("%d\n", d[p - 1][q - 1]); } } } return 0; }
#include <stdio.h> #define INF 100000000 int main(void) { int n, m, a, b, cost, time, l, p, q, r; int i, j, k; int d[100][100]; int exp[100][100]; while (1) { scanf("%d %d", &n, &m); if (n == 0 && m == 0) break; for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { d[i][j] = INF; exp[i][j] = INF; } } for (i = 0; i < m; i++) { d[i][i] = 0; exp[i][i] = 0; } for (i = 0; i < n; i++) { scanf("%d %d %d %d", &a, &b, &cost, &time); d[a - 1][b - 1] = time; d[b - 1][a - 1] = time; exp[a - 1][b - 1] = cost; exp[b - 1][a - 1] = cost; } for (k = 0; k < m; k++) { for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { if (d[i][j] > d[i][k] + d[k][j]) { d[i][j] = d[i][k] + d[k][j]; } if (exp[i][j] > exp[i][k] + exp[k][j]) { exp[i][j] = exp[i][k] + exp[k][j]; } } } } scanf("%d", &l); for (i = 0; i < l; i++) { scanf("%d %d %d", &p, &q, &r); if (r == 0) { printf("%d\n", exp[p - 1][q - 1]); } else if (r == 1) { printf("%d\n", d[p - 1][q - 1]); } } } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
0
514
#include <algorithm> #include <iostream> #include <queue> using namespace std; const int MAX_INT = 10000000; int n, m; int p, q, r; int costMap[101][101]; int timeMap[101][101]; int visited[101]; class Node { public: int cost, pos; Node(int p) { cost = 0; pos = p; } Node(int p, int c, const Node &pre) { cost = c + pre.cost; pos = p; } bool operator<(const Node &o) const { return cost > o.cost; } }; int memo[101][101][2]; int main() { while (cin >> n >> m, n || m) { for (int i = 0; i <= m; i++) { fill(costMap[i], costMap[i] + m + 1, MAX_INT); fill(timeMap[i], timeMap[i] + m + 1, MAX_INT); } for (int i = 0; i < n; i++) { int a, b, cost, time; cin >> a >> b >> cost >> time; costMap[a][b] = costMap[b][a] = cost; timeMap[a][b] = timeMap[b][a] = time; } int k; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { memo[i][j][0] = 0x7fffffff; memo[i][j][1] = 0x7fffffff; } } for (cin >> k; k; k--) { cin >> p >> q >> r; if (memo[p][q][r] != 0x7fffffff) { cout << memo[p][q][r] << endl; } fill(visited, visited + m + 1, MAX_INT); priority_queue<Node> que; que.push(Node(p)); visited[p] = 0; while (!que.empty()) { Node current = que.top(); que.pop(); if (current.pos == q) { cout << current.cost << endl; memo[p][q][r] = memo[q][p][r] = current.cost; break; } for (int i = 1; i <= m; i++) { if (r) { if (timeMap[current.pos][i] != MAX_INT && timeMap[current.pos][i] + current.cost < visited[i]) { visited[i] = timeMap[current.pos][i] + current.cost; que.push(Node(i, timeMap[current.pos][i], current)); // cout << current.pos << " to " << i << endl; } } else { if (costMap[current.pos][i] != MAX_INT && costMap[current.pos][i] + current.cost < visited[i]) { visited[i] = costMap[current.pos][i] + current.cost; que.push(Node(i, costMap[current.pos][i], current)); // cout << current.pos << " to " << i << endl; } } } } } } return 0; }
#include <algorithm> #include <iostream> #include <queue> using namespace std; const int MAX_INT = 10000000; int n, m; int p, q, r; int costMap[101][101]; int timeMap[101][101]; int visited[101]; class Node { public: int cost, pos; Node(int p) { cost = 0; pos = p; } Node(int p, int c, const Node &pre) { cost = c + pre.cost; pos = p; } bool operator<(const Node &o) const { return cost > o.cost; } }; int memo[101][101][2]; int main() { while (cin >> n >> m, n || m) { for (int i = 0; i <= m; i++) { fill(costMap[i], costMap[i] + m + 1, MAX_INT); fill(timeMap[i], timeMap[i] + m + 1, MAX_INT); } for (int i = 0; i < n; i++) { int a, b, cost, time; cin >> a >> b >> cost >> time; costMap[a][b] = costMap[b][a] = cost; timeMap[a][b] = timeMap[b][a] = time; } int k; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { memo[i][j][0] = 0x7fffffff; memo[i][j][1] = 0x7fffffff; } } for (cin >> k; k; k--) { cin >> p >> q >> r; if (memo[p][q][r] != 0x7fffffff) { cout << memo[p][q][r] << endl; continue; } fill(visited, visited + m + 1, MAX_INT); priority_queue<Node> que; que.push(Node(p)); visited[p] = 0; while (!que.empty()) { Node current = que.top(); que.pop(); if (current.pos == q) { cout << current.cost << endl; memo[p][q][r] = memo[q][p][r] = current.cost; break; } for (int i = 1; i <= m; i++) { if (r) { if (timeMap[current.pos][i] != MAX_INT && timeMap[current.pos][i] + current.cost < visited[i]) { visited[i] = timeMap[current.pos][i] + current.cost; que.push(Node(i, timeMap[current.pos][i], current)); // cout << current.pos << " to " << i << endl; } } else { if (costMap[current.pos][i] != MAX_INT && costMap[current.pos][i] + current.cost < visited[i]) { visited[i] = costMap[current.pos][i] + current.cost; que.push(Node(i, costMap[current.pos][i], current)); // cout << current.pos << " to " << i << endl; } } } } } } return 0; }
[["+", 8, 9, 0, 57, 64, 9, 0, 116, 0, 117], ["+", 8, 9, 0, 57, 64, 9, 0, 116, 0, 35]]
1
686
#include <cmath> #include <cstring> #include <iostream> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define pb push_back using namespace std; const int INF = 10000000; int V; int d[2][111][111]; void wf() { FOR(k, 0, V + 1) FOR(i, 0, V + 1) FOR(j, 0, V + 1) { d[0][i][j] = min(d[0][i][j], d[0][i][k] + d[0][k][j]); d[1][i][j] = min(d[1][i][j], d[1][i][k] + d[1][k][j]); } } int main() { int n; while (1) { cin >> n >> V; if (!(n || V)) break; FOR(i, 0, V + 1) FOR(j, 0, V + 1) { d[0][i][j] = d[1][j][i] = INF; } FOR(i, 0, n) { int a, b, m, t; cin >> a >> b >> m >> t; d[0][a][b] = d[0][b][a] = t; d[1][a][b] = d[1][b][a] = m; } wf(); int k; cin >> k; FOR(i, 0, k) { int p, q, r; cin >> p >> q >> r; cout << d[r][p][q] << endl; } } }
#include <cmath> #include <cstring> #include <iostream> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define pb push_back using namespace std; const int INF = 10000000; int V; int d[2][111][111]; void wf() { FOR(k, 0, V + 1) FOR(i, 0, V + 1) FOR(j, 0, V + 1) { d[0][i][j] = min(d[0][i][j], d[0][i][k] + d[0][k][j]); d[1][i][j] = min(d[1][i][j], d[1][i][k] + d[1][k][j]); } } int main() { int n; while (1) { cin >> n >> V; if (!(n || V)) break; FOR(i, 0, V + 1) FOR(j, 0, V + 1) { d[0][i][j] = d[1][j][i] = INF; } FOR(i, 0, n) { int a, b, m, t; cin >> a >> b >> m >> t; d[0][a][b] = d[0][b][a] = m; d[1][a][b] = d[1][b][a] = t; } wf(); int k; cin >> k; FOR(i, 0, k) { int p, q, r; cin >> p >> q >> r; cout << d[r][p][q] << endl; } } }
[["-", 0, 9, 0, 1, 0, 11, 12, 11, 12, 22], ["+", 0, 9, 0, 1, 0, 11, 12, 11, 12, 22]]
1
389
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <vector> using namespace std; typedef long long LL; typedef vector<int> Array; typedef pair<int, int> P; const int INF = 1 << 29; const LL MOD = 100000; #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define all(c) (c).begin(), (c).end() struct edge { int to, cost; edge(int t, int c) : to(t), cost(c) {} }; int main() { // while (cin >> n) stringstream ss; // ss << "result" << i + 1 << ".txt"; // FILE* fp_out = freopen(ss.str().c_str(), "w", stdout); int n, m; while (cin >> n >> m && n) { vector<vector<int>> times(m, vector<int>(m, INF)); vector<vector<int>> costs(m, vector<int>(m, INF)); rep(i, n) { int a, b, c, t; cin >> a >> b >> c >> t; a--; b--; costs[a][b] = costs[b][a] = c; times[a][b] = times[b][a] = t; } rep(k, m) rep(i, m) rep(j, m) { costs[i][j] = min(costs[i][j], costs[i][k] + costs[k][j]); times[i][j] = min(times[i][j], times[i][k] + times[k][j]); } int k; cin >> k; rep(i, k) { int p, q, r; cin >> p >> q >> r; p--; q--; cout << (r ? costs[p][q] : times[p][q]) << endl; } } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <vector> using namespace std; typedef long long LL; typedef vector<int> Array; typedef pair<int, int> P; const int INF = 1 << 29; const LL MOD = 100000; #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define all(c) (c).begin(), (c).end() struct edge { int to, cost; edge(int t, int c) : to(t), cost(c) {} }; int main() { // while (cin >> n) stringstream ss; // ss << "result" << i + 1 << ".txt"; // FILE* fp_out = freopen(ss.str().c_str(), "w", stdout); int n, m; while (cin >> n >> m && n) { vector<vector<int>> times(m, vector<int>(m, INF)); vector<vector<int>> costs(m, vector<int>(m, INF)); rep(i, n) { int a, b, c, t; cin >> a >> b >> c >> t; a--; b--; costs[a][b] = costs[b][a] = c; times[a][b] = times[b][a] = t; } rep(k, m) rep(i, m) rep(j, m) { costs[i][j] = min(costs[i][j], costs[i][k] + costs[k][j]); times[i][j] = min(times[i][j], times[i][k] + times[k][j]); } int k; cin >> k; rep(i, k) { int p, q, r; cin >> p >> q >> r; p--; q--; cout << (r ? times[p][q] : costs[p][q]) << endl; } } return 0; }
[["-", 12, 23, 0, 41, 64, 69, 28, 69, 28, 22], ["+", 12, 23, 0, 41, 64, 69, 28, 69, 28, 22], ["-", 12, 23, 0, 41, 75, 69, 28, 69, 28, 22], ["+", 12, 23, 0, 41, 75, 69, 28, 69, 28, 22]]
1
418
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vii; #define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++) #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--) #define vrep(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define ALL(v) (v).begin(), (v).end() #define mp make_pair #define pb push_back template <class T1, class T2> inline void minup(T1 &m, T2 x) { if (m > x) m = static_cast<T2>(x); } template <class T1, class T2> inline void maxup(T1 &m, T2 x) { if (m < x) m = static_cast<T2>(x); } #define INF 1000000000LL #define MOD 1000000009 #define EPS 1E-9 const int MAX_V = 1000; ll dp[2][MAX_V][MAX_V]; int E, V; int a, b; ll c, t; int k; int p, q, r; int main() { // while((cin >> E >> V) && E && V){ while (scanf(" %d %d", &E, &V) && E && V) { rep(i, 101) rep(j, 101) dp[0][i][j] = dp[1][i][j] = INF; rep(i, E) { // cin >> a >> b >> c >> t; scanf(" %d %d %d %d", &a, &b, &c, &t); a -= 1; b -= 1; dp[0][a][b] = dp[0][b][a] = c; dp[1][a][b] = dp[0][b][a] = t; } rep(b, 2) rep(k, V) rep(i, V) rep(j, V) minup(dp[b][i][j], dp[b][i][k] + dp[b][k][j]); // cin >> k; scanf(" %d", &k); while (k--) { // cin >> p >> q >> r; scanf(" %d %d %d", &p, &q, &r); // cout << dp[r][p-1][q-1] << endl; printf("%lld\n", dp[r][p - 1][q - 1]); } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vii; #define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++) #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--) #define vrep(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define ALL(v) (v).begin(), (v).end() #define mp make_pair #define pb push_back template <class T1, class T2> inline void minup(T1 &m, T2 x) { if (m > x) m = static_cast<T2>(x); } template <class T1, class T2> inline void maxup(T1 &m, T2 x) { if (m < x) m = static_cast<T2>(x); } #define INF 1000000000LL #define MOD 1000000009 #define EPS 1E-9 const int MAX_V = 1000; ll dp[2][MAX_V][MAX_V]; int E, V; int a, b; ll c, t; int k; int p, q, r; int main() { // while((cin >> E >> V) && E && V){ while (scanf(" %d %d", &E, &V) && E && V) { rep(i, 101) rep(j, 101) dp[0][i][j] = dp[1][i][j] = INF; rep(i, E) { // cin >> a >> b >> c >> t; scanf(" %d %d %d %d", &a, &b, &c, &t); a -= 1; b -= 1; dp[0][a][b] = dp[0][b][a] = c; dp[1][a][b] = dp[1][b][a] = t; } rep(b, 2) rep(k, V) rep(i, V) rep(j, V) minup(dp[b][i][j], dp[b][i][k] + dp[b][k][j]); // cin >> k; scanf(" %d", &k); while (k--) { // cin >> p >> q >> r; scanf(" %d %d %d", &p, &q, &r); // cout << dp[r][p-1][q-1] << endl; printf("%lld\n", dp[r][p - 1][q - 1]); } } return 0; }
[["-", 31, 69, 28, 69, 28, 69, 341, 342, 0, 13], ["+", 31, 69, 28, 69, 28, 69, 341, 342, 0, 13]]
1
488
#include <algorithm> #include <climits> #include <cstdio> #include <iostream> #include <map> #include <set> #include <vector> using namespace std; const int MAX_V = 1000; const int INF = 100000; int v; int g[MAX_V][MAX_V]; int g2[MAX_V][MAX_V]; int d[MAX_V]; bool used[MAX_V]; void dijkstra(int s) { fill(d, d + v, INF); fill(used, used + v, false); d[s] = 0; for (;;) { int i = -1; for (int j = 0; j < v; ++j) if (!used[j] && (i == -1 || d[i] > d[j])) i = j; if (i == -1) break; used[i] = true; for (int j = 0; j < v; ++j) d[j] = min(d[j], d[i] + g[i][j]); } } void dijkstra2(int s) { fill(d, d + v, INF); fill(used, used + v, false); d[s] = 0; for (;;) { int i = -1; for (int j = 0; j < v; ++j) if (!used[j] && (i == -1 || d[i] > d[j])) i = j; if (i == -1) break; used[i] = true; for (int j = 0; j < v; ++j) d[j] = min(d[j], d[i] + g2[i][j]); } } int main() { for (;;) { int gv, ge; cin >> ge >> gv; if (ge == 0) { return 0; } fill(&g[0][0], &g[MAX_V][0], 100000); fill(&g2[0][0], &g2[MAX_V][0], 100000); for (int i = 0; i < ge; ++i) { int gs, gt, gd, gd2; cin >> gs >> gt >> gd >> gd2; g[gs - 1][gt - 1] = gd; g[gt - 1][gs - 1] = gd; g2[gs - 1][gt - 1] = gd2; g2[gs - 1][gt - 1] = gd2; } v = gv; // for(int i=0;i<v;i++){for(int j=0;j<v;j++){cout << g[i][j] << " ";}cout << // endl;} int k; cin >> k; for (int i = 0; i < k; i++) { int p, q, r; cin >> p >> q >> r; if (r == 0) { dijkstra(p - 1); // for(int j=1;j<=gv;j++) cout << d[q - 1] << endl; } else { dijkstra2(p - 1); // for(int j=0;j<=gv;j++) cout << d[q - 1] << endl; } } } return 0; }
#include <algorithm> #include <climits> #include <cstdio> #include <iostream> #include <map> #include <set> #include <vector> using namespace std; const int MAX_V = 1000; const int INF = 100000; int v; int g[MAX_V][MAX_V]; int g2[MAX_V][MAX_V]; int d[MAX_V]; bool used[MAX_V]; void dijkstra(int s) { fill(d, d + v, INF); fill(used, used + v, false); d[s] = 0; for (;;) { int i = -1; for (int j = 0; j < v; ++j) if (!used[j] && (i == -1 || d[i] > d[j])) i = j; if (i == -1) break; used[i] = true; for (int j = 0; j < v; ++j) d[j] = min(d[j], d[i] + g[i][j]); } } void dijkstra2(int s) { fill(d, d + v, INF); fill(used, used + v, false); d[s] = 0; for (;;) { int i = -1; for (int j = 0; j < v; ++j) if (!used[j] && (i == -1 || d[i] > d[j])) i = j; if (i == -1) break; used[i] = true; for (int j = 0; j < v; ++j) d[j] = min(d[j], d[i] + g2[i][j]); } } int main() { for (;;) { int gv, ge; cin >> ge >> gv; if (ge == 0) { return 0; } fill(&g[0][0], &g[MAX_V][0], 100000); fill(&g2[0][0], &g2[MAX_V][0], 100000); for (int i = 0; i < ge; ++i) { int gs, gt, gd, gd2; cin >> gs >> gt >> gd >> gd2; g[gs - 1][gt - 1] = gd; g[gt - 1][gs - 1] = gd; g2[gs - 1][gt - 1] = gd2; g2[gt - 1][gs - 1] = gd2; } v = gv; // for(int i=0;i<v;i++){for(int j=0;j<v;j++){cout << g[i][j] << " ";}cout << // endl;} // for(int i=0;i<v;i++){for(int j=0;j<v;j++){cout << g2[i][j] << " ";}cout // << endl;} int k; cin >> k; for (int i = 0; i < k; i++) { int p, q, r; cin >> p >> q >> r; if (r == 0) { dijkstra(p - 1); // for(int j=1;j<=gv;j++) cout << d[q - 1] << endl; } else { dijkstra2(p - 1); // for(int j=0;j<=gv;j++) cout << d[q - 1] << endl; } } } return 0; }
[["-", 31, 69, 28, 69, 341, 342, 0, 16, 31, 22], ["+", 31, 69, 28, 69, 341, 342, 0, 16, 31, 22], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 31, 22], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 31, 22]]
1
623
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> #define _m_ 1 const int INF = 1e9; int cost[101][101]; int tim[101][101]; int d[101]; bool used[101]; int V; void dijkstra(int s, int o) { std::fill(d, d + V, INF); std::fill(used, used + V, false); d[s] = 0; while (_m_) { int v = -1; for (int u = 0; u < V; ++u) { if (!used[u] && (v == -1 || d[u] < d[v])) v = u; } if (v == -1) break; used[v] = true; if (o == 0) { for (int u = 0; u < V; ++u) { d[u] = std::min(d[u], d[v] + cost[v][u]); } } else { for (int u = 0; u < V; ++u) { d[u] = std::min(d[u], d[v] + tim[v][u]); } } } } int main() { int n, m; while (scanf("%d %d", &n, &m), n + m) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cost[i][j] = INF; tim[i][j] = INF; } } for (int i = 0; i < n; ++i) { int a, b, co, ti; scanf("%d %d %d %d", &a, &b, &co, &ti); a--, b--; cost[a][b] = co, cost[b][a] = co; tim[a][b] = ti, tim[b][a] = ti; } V = m; int k; scanf("%d", &k); for (int i = 0; i < k; ++i) { int p, q, r; scanf("%d %d %d", &p, &q, &r); p--, q--; dijkstra(p, r); printf("%d\n", d[q]); } } }
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> #define _m_ 1 const int INF = 1e9; int cost[101][101]; int tim[101][101]; int d[101]; bool used[101]; int V; void dijkstra(int s, int o) { std::fill(d, d + V, INF); std::fill(used, used + V, false); d[s] = 0; while (_m_) { int v = -1; for (int u = 0; u < V; ++u) { if (!used[u] && (v == -1 || d[u] < d[v])) v = u; } if (v == -1) break; used[v] = true; if (o == 0) { for (int u = 0; u < V; ++u) { d[u] = std::min(d[u], d[v] + cost[v][u]); } } else { for (int u = 0; u < V; ++u) { d[u] = std::min(d[u], d[v] + tim[v][u]); } } } } int main() { int n, m; while (scanf("%d %d", &n, &m), n + m) { for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { cost[i][j] = INF; tim[i][j] = INF; } } for (int i = 0; i < n; ++i) { int a, b, co, ti; scanf("%d %d %d %d", &a, &b, &co, &ti); a--, b--; cost[a][b] = co, cost[b][a] = co; tim[a][b] = ti, tim[b][a] = ti; } V = m; int k; scanf("%d", &k); for (int i = 0; i < k; ++i) { int p, q, r; scanf("%d %d %d", &p, &q, &r); p--, q--; dijkstra(p, r); printf("%d\n", d[q]); } } }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
1
519
#include <iostream> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define INF 1e9 using namespace std; typedef long long ll; ll min(ll a, ll b) { if (a > b) return b; else return a; } int main() { int n, m, a, b, cost, time, l, p, q, r; while (cin >> n >> m, n || m) { ll mt[m][m], mc[m][m]; rep(i, m) rep(j, m) mt[i][j] = mc[i][j] = INF; rep(i, n) { cin >> a >> b >> cost >> time; mt[a - 1][b - 1] = mt[b - 1][a - 1] = time; mc[a - 1][b - 1] = mt[b - 1][a - 1] = cost; } rep(k, m) { rep(i, m) { rep(j, m) { mt[i][j] = min(mt[i][j], mt[i][k] + mt[k][j]); mc[i][j] = min(mc[i][j], mc[i][k] + mc[k][j]); } } } cin >> l; rep(i, l) { cin >> p >> q >> r; if (r == 0) cout << mc[p - 1][q - 1] << endl; else cout << mt[p - 1][q - 1] << endl; } } return 0; }
#include <iostream> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define INF 1e9 using namespace std; typedef long long ll; ll min(ll a, ll b) { if (a > b) return b; else return a; } int main() { int n, m, a, b, cost, time, l, p, q, r; while (cin >> n >> m, n || m) { ll mt[m][m], mc[m][m]; rep(i, m) rep(j, m) mt[i][j] = mc[i][j] = INF; rep(i, n) { cin >> a >> b >> cost >> time; mt[a - 1][b - 1] = mt[b - 1][a - 1] = time; mc[a - 1][b - 1] = mc[b - 1][a - 1] = cost; } rep(k, m) { rep(i, m) { rep(j, m) { mt[i][j] = min(mt[i][j], mt[i][k] + mt[k][j]); mc[i][j] = min(mc[i][j], mc[i][k] + mc[k][j]); } } } cin >> l; rep(i, l) { cin >> p >> q >> r; if (r == 0) cout << mc[p - 1][q - 1] << endl; else cout << mt[p - 1][q - 1] << endl; } } return 0; }
[["-", 0, 11, 12, 11, 31, 69, 28, 69, 28, 22], ["+", 0, 11, 12, 11, 31, 69, 28, 69, 28, 22]]
1
371
#include <bits/stdc++.h> using namespace std; #define INF 1 << 10 int main() { int cost[101][101]; int times[101][101]; int n, m; int a, b, c, t; while (cin >> n >> m, n | m) { for (int i = 0; i < 101; i++) { for (int j = 0; j < 101; j++) { cost[i][j] = times[i][j] = INF; } } for (int i = 0; i < n; i++) { cin >> a >> b >> c >> t; cost[a][b] = cost[b][a] = c; times[a][b] = times[b][a] = c; } for (int i = 1; i <= m; i++) { for (int j = 1; j <= m; j++) { for (int k = 1; k <= m; k++) { cost[j][k] = min(cost[j][k], cost[j][i] + cost[i][k]); times[j][k] = min(times[j][k], times[j][i] + times[i][k]); } } } int k, p, q, r; cin >> k; for (int i = 0; i < k; i++) { cin >> p >> q >> r; cout << (r == 0 ? cost[p][q] : times[p][q]) << endl; } } }
#include <bits/stdc++.h> using namespace std; #define INF 1 << 25 int main() { int cost[101][101]; int times[101][101]; int n, m; int a, b, c, t; while (cin >> n >> m, n | m) { for (int i = 0; i < 101; i++) { for (int j = 0; j < 101; j++) { cost[i][j] = times[i][j] = INF; } } for (int i = 0; i < n; i++) { cin >> a >> b >> c >> t; cost[a][b] = cost[b][a] = c; times[a][b] = times[b][a] = t; } for (int i = 1; i <= m; i++) { for (int j = 1; j <= m; j++) { for (int k = 1; k <= m; k++) { cost[j][k] = min(cost[j][k], cost[j][i] + cost[i][k]); times[j][k] = min(times[j][k], times[j][i] + times[i][k]); } } } int k, p, q, r; cin >> k; for (int i = 0; i < k; i++) { cin >> p >> q >> r; cout << (r == 0 ? cost[p][q] : times[p][q]) << endl; } } }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["-", 8, 9, 0, 1, 0, 11, 12, 11, 12, 22], ["+", 8, 9, 0, 1, 0, 11, 12, 11, 12, 22]]
1
354
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define rep(i, l, n) for (lint i = l; i < n; i++) #define rer(i, l, n) for (lint i = l; i <= n; i++) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl using namespace std; typedef long long lint; typedef vector<int> vi; typedef vector<lint> vli; typedef vector<vi> vvi; typedef pair<int, int> pii; int c[101][101]; int t [101] [101]; // d[u][v]??????e=(u,v)????????????(?????¨???????????´??????INF,??????d[i][i]=0??¨??????) int V; //????????° const int INF = 10000000; void warshall_floyd() { for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { c[i][j] = min(c[i][j], c[i][k] + c[k][j]); t[i][j] = min(t[i][j], t[i][k] + t[k][j]); } } } } int main() { while (1) { int n, m; cin >> n >> m; V = m; if (n + m == 0) break; rep(i, 0, V) { rep(j, 0, V) { if (i == j) { c[i][j] = 0; t[i][j] = 0; } else { c[i][j] = INF; t[i][j] = INF; } } } int a, b, cost, time; rep(i, 0, n) { cin >> a >> b >> cost >> time; a--; b--; c[a][b] = cost; c[b][a] = cost; t[a][b] = time; t[a][b] = time; } warshall_floyd(); int kazu, p, q, r; cin >> kazu; rep(i, 0, kazu) { cin >> p >> q >> r; p--; q--; o((r == 0 ? c[p][q] : t[p][q])); } } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define rep(i, l, n) for (lint i = l; i < n; i++) #define rer(i, l, n) for (lint i = l; i <= n; i++) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl using namespace std; typedef long long lint; typedef vector<int> vi; typedef vector<lint> vli; typedef vector<vi> vvi; typedef pair<int, int> pii; int c[101][101]; int t [101] [101]; // d[u][v]??????e=(u,v)????????????(?????¨???????????´??????INF,??????d[i][i]=0??¨??????) int V; //????????° const int INF = 10000000; void warshall_floyd() { for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { c[i][j] = min(c[i][j], c[i][k] + c[k][j]); t[i][j] = min(t[i][j], t[i][k] + t[k][j]); } } } } int main() { while (1) { int n, m; cin >> n >> m; V = m; if (n + m == 0) break; rep(i, 0, V) { rep(j, 0, V) { if (i == j) { c[i][j] = 0; t[i][j] = 0; } else { c[i][j] = INF; t[i][j] = INF; } } } int a, b, cost, time; rep(i, 0, n) { cin >> a >> b >> cost >> time; a--; b--; c[a][b] = cost; c[b][a] = cost; t[a][b] = time; t[b][a] = time; } warshall_floyd(); int kazu, p, q, r; cin >> kazu; rep(i, 0, kazu) { cin >> p >> q >> r; p--; q--; o((r == 0 ? c[p][q] : t[p][q])); } } }
[["-", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22]]
1
503
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) #define INF 1 << 21 using namespace std; main() { int m, n; while (cin >> m >> n, n) { int a1[n][n], a2[n][n], q, w, e, r; r(i, n) r(j, n) { if (i == j) a2[i][j] = a1[i][j] = 0; else a2[n][n] = a1[i][j] = INF; } while (m--) { cin >> q >> w >> e >> r; q--; w--; a1[q][w] = a1[w][q] = e; a2[q][w] = a2[w][q] = r; } r(k, n) r(i, n) r(j, n) { a1[i][j] = min(a1[i][j], a1[i][k] + a1[k][j]); a2[i][j] = min(a2[i][j], a2[i][k] + a2[k][j]); } cin >> e; while (e--) { cin >> q >> w >> r; q--; w--; if (r) cout << a2[q][w] << endl; else cout << a1[q][w] << endl; } } }
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) #define INF 1 << 29 using namespace std; main() { int m, n; while (cin >> m >> n, n) { int a1[n][n], a2[n][n], q, w, e, r; r(i, n) r(j, n) { if (i == j) a2[i][j] = a1[i][j] = 0; else a2[i][j] = a1[i][j] = INF; } while (m--) { cin >> q >> w >> e >> r; q--; w--; a1[q][w] = a1[w][q] = e; a2[q][w] = a2[w][q] = r; } r(k, n) r(i, n) r(j, n) { a1[i][j] = min(a1[i][j], a1[i][k] + a1[k][j]); a2[i][j] = min(a2[i][j], a2[i][k] + a2[k][j]); } cin >> e; while (e--) { cin >> q >> w >> r; q--; w--; if (r) cout << a2[q][w] << endl; else cout << a1[q][w] << endl; } } }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["-", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22]]
1
327
#include <iostream> #define INF 1000000; using namespace std; int main() { int n, m; while (1) { cin >> n >> m; if (n == 0 && m == 0) { break; } int s_tim[m][m]; int c_money[m][m]; int a, b, cost, tim; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { s_tim[i][j] = INF; c_money[i][j] = INF; } } for (int i = 0; i < m; i++) { s_tim[i][i] = 0; c_money[i][i] = 0; } for (int i = 0; i < n; i++) { cin >> a >> b >> cost >> tim; s_tim[a - 1][b - 1] = tim; s_tim[b - 1][a - 1] = tim; c_money[a - 1][b - 1] = cost; c_money[b - 1][a - 1] = cost; } for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (s_tim[i][j] > s_tim[i][k] + s_tim[k][j]) { s_tim[i][j] = s_tim[i][k] + s_tim[k][j]; } if (c_money[i][j] > c_money[i][k] + c_money[k][j]) { c_money[i][j] = c_money[i][k] + c_money[k][j]; } } } } int contacts, p, q, r; cin >> contacts; for (int i = 0; i < contacts; i++) { cin >> p >> q >> r; if (r) { cout << s_tim[p - 1][q - 1]; } else { cout << c_money[p - 1][q - 1]; } cout << endl; } } }
#include <iostream> #define INF 100000; using namespace std; int main() { int n, m; while (1) { cin >> n >> m; if (n == 0 && m == 0) { break; } int s_tim[m][m]; int c_money[m][m]; int a, b, cost, tim; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { s_tim[i][j] = INF; c_money[i][j] = INF; } } for (int i = 0; i < m; i++) { s_tim[i][i] = 0; c_money[i][i] = 0; } for (int i = 0; i < n; i++) { cin >> a >> b >> cost >> tim; s_tim[a - 1][b - 1] = tim; s_tim[b - 1][a - 1] = tim; c_money[a - 1][b - 1] = cost; c_money[b - 1][a - 1] = cost; } for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (s_tim[i][j] > s_tim[i][k] + s_tim[k][j]) { s_tim[i][j] = s_tim[i][k] + s_tim[k][j]; } if (c_money[i][j] > c_money[i][k] + c_money[k][j]) { c_money[i][j] = c_money[i][k] + c_money[k][j]; } } } } int contacts, p, q, r; cin >> contacts; for (int i = 0; i < contacts; i++) { cin >> p >> q >> r; if (r) { cout << s_tim[p - 1][q - 1]; } else { cout << c_money[p - 1][q - 1]; } cout << endl; } } }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
1
473
#include <algorithm> #include <iostream> #include <math.h> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string> #include <utility> using namespace std; int n, m; long long gc[110][110]; long long gt[110][110]; void warshallfloyd() { for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]); gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]); } } } } int main(void) { while (cin >> n >> m, n || m) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { gc[i][j] = 9999999; gt[i][j] = 9999999; } } for (int i = 0; i < n; i++) { int a, b, c, t; cin >> a >> b >> c >> t; a--; b--; gc[a][b] = gc[b][a] = c; gt[a][b] = gt[b][a] = t; } warshallfloyd(); int k; cin >> k; for (int i = 0; i < k; i++) { int p, q, r; cin >> p >> q >> r; p--; q--; if (r == 0) { cout << gc[p][q] << endl; } else { cout << gt[p][q] << endl; } } } return 0; }
#include <algorithm> #include <iostream> #include <math.h> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string> #include <utility> using namespace std; int n, m; long long gc[110][110]; long long gt[110][110]; void warshallfloyd() { for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]); gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]); } } } } int main(void) { while (cin >> n >> m, n || m) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { gc[i][j] = 9999999; gt[i][j] = 9999999; } } for (int i = 0; i < n; i++) { int a, b, c, t; cin >> a >> b >> c >> t; a--; b--; gc[a][b] = gc[b][a] = c; gt[a][b] = gt[b][a] = t; } warshallfloyd(); int k; cin >> k; for (int i = 0; i < k; i++) { int p, q, r; cin >> p >> q >> r; p--; q--; if (r == 0) { cout << gc[p][q] << endl; } else { cout << gt[p][q] << endl; } } } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
1
405
//青春の片道切符::ワーシャルフロイド法 /*for(int k=0;k<N;k++){//経由箇所(N<200) for(int i=0;i<N;i++){//スタート(N<200) for(int j=0;j<N;j++){//目的(N<200) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } } }*/ #include <algorithm> #include <cctype> #include <iostream> #include <queue> #include <string> #include <utility> using namespace std; long long gc[110][110]; long long gt[110][110]; int n, m; void warshallfroid() { for (int k = 0; k < n; k++) { //経由箇所(N<200) for (int i = 0; i < n; i++) { //スタート(N<200) for (int j = 0; j < n; j++) { //目的(N<200) gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]); gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]); } } } } int main() { while (cin >> n >> m, n || m /*n,mが両方0のときお終わり*/) { for (int i = 0; i < m; i++) { //スタート(N<200) for (int j = 0; j < m; j++) { //目的(N<200) gc[i][j] = 9999999; //初期化 gt[i][j] = 9999999; //初期化 } } for (int i = 0; i < n; i++) { int a, b, c, t; cin >> a >> b >> c >> t; a--, b--; gc[a][b] = gc[b][a] = c; gt[a][b] = gt[b][a] = t; } warshallfroid(); int k; cin >> k; for (int i = 0; i < k; i++) { int p, q, r; cin >> p >> q >> r; p--, q--; if (r == 0) cout << gc[p][q] << endl; else cout << gt[p][q] << endl; } } }
//青春の片道切符::ワーシャルフロイド法 /*for(int k=0;k<N;k++){//経由箇所(N<200) for(int i=0;i<N;i++){//スタート(N<200) for(int j=0;j<N;j++){//目的(N<200) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } } }*/ #include <algorithm> #include <cctype> #include <iostream> #include <queue> #include <string> #include <utility> using namespace std; long long gc[110][110]; long long gt[110][110]; int n, m; void warshallfroid() { for (int k = 0; k < m; k++) { //経由箇所(N<200) for (int i = 0; i < m; i++) { //スタート(N<200) for (int j = 0; j < m; j++) { //目的(N<200) gc[i][j] = min(gc[i][j], gc[i][k] + gc[k][j]); gt[i][j] = min(gt[i][j], gt[i][k] + gt[k][j]); } } } } int main() { while (cin >> n >> m, n || m /*n,mが両方0のときお終わり*/) { for (int i = 0; i < m; i++) { //スタート(N<200) for (int j = 0; j < m; j++) { //目的(N<200) gc[i][j] = 9999999; //初期化 gt[i][j] = 9999999; //初期化 } } for (int i = 0; i < n; i++) { int a, b, c, t; cin >> a >> b >> c >> t; a--, b--; gc[a][b] = gc[b][a] = c; gt[a][b] = gt[b][a] = t; } warshallfroid(); int k; cin >> k; for (int i = 0; i < k; i++) { int p, q, r; cin >> p >> q >> r; p--, q--; if (r == 0) cout << gc[p][q] << endl; else cout << gt[p][q] << endl; } } }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
1
403