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 <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; struct rule { bool permit; string src; string dst; }; int main() { int n, m; while (cin >> n >> m && !(n == 0 && m == 0)) { vector<rule> v; for (int i = 0; i < n; i++) { string ord; rule r; cin >> ord >> r.src >> r.dst; if (ord == "permit") r.permit = true; else r.permit = false; v.push_back(r); } reverse(v.begin(), v.end()); vector<string> ss, ds, bs; for (int i = 0; i < m; i++) { string src, dst, body; cin >> src >> dst >> body; for (int j = 0; j < v.size(); j++) { bool b = false; for (int k = 0; k < v[j].src.size(); k++) { if (v[j].src[k] == '?') continue; if (v[j].src[k] != src[k]) { b = true; break; } } if (!b) { bool b2 = false; for (int k = 0; k < v[j].dst.size(); k++) { if (v[j].dst[k] == '?') continue; if (v[j].dst[k] != dst[k]) { b = true; break; } } if (!b2) { if (v[j].permit) { ss.push_back(src); ds.push_back(dst); bs.push_back(body); } break; } } } } cout << ss.size() << endl; for (int i = 0; i < ss.size(); i++) { cout << ss[i] << " " << ds[i] << " " << bs[i] << endl; } } return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; struct rule { bool permit; string src; string dst; }; int main() { int n, m; while (cin >> n >> m && !(n == 0 && m == 0)) { vector<rule> v; for (int i = 0; i < n; i++) { string ord; rule r; cin >> ord >> r.src >> r.dst; if (ord == "permit") r.permit = true; else r.permit = false; v.push_back(r); } reverse(v.begin(), v.end()); vector<string> ss, ds, bs; for (int i = 0; i < m; i++) { string src, dst, body; cin >> src >> dst >> body; for (int j = 0; j < v.size(); j++) { bool b = false; for (int k = 0; k < v[j].src.size(); k++) { if (v[j].src[k] == '?') continue; if (v[j].src[k] != src[k]) { b = true; break; } } if (!b) { bool b2 = false; for (int k = 0; k < v[j].dst.size(); k++) { if (v[j].dst[k] == '?') continue; if (v[j].dst[k] != dst[k]) { b2 = true; break; } } if (!b2) { if (v[j].permit) { ss.push_back(src); ds.push_back(dst); bs.push_back(body); } break; } } } } cout << ss.size() << endl; for (int i = 0; i < ss.size(); i++) { cout << ss[i] << " " << ds[i] << " " << bs[i] << endl; } } return 0; }
[["-", 0, 57, 64, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 31, 22]]
1
456
#define _USE_MATH_DEFINES #define INF 100000000 #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<string, string> P; typedef pair<int, P> PP; static const double EPS = 1e-8; int tx[] = {0, 1, 1, 1, 0, -1, -1, -1}; int ty[] = {-1, -1, 0, 1, 1, 1, 0, -1}; bool isMatch(const P &p1, const P &p2) { // p1:deny or permit for (int i = 0; i < 8; i++) { if (p1.first[i] != '?' && p2.first[i] != '?') { if (p1.first[i] != p2.first[i]) return false; } else if (p1.second[i] != '?' && p2.second[i] != '?') { if (p1.second[i] != p2.second[i]) return false; } } return true; } class Output { public: string from; string to; string msg; Output(string _from, string _to, string _msg) { from = _from; to = _to; msg = _msg; } }; class Rule { public: string from; string to; int filter; int priority; Rule(string _from, string _to, int _f, int _p) { from = _from; to = _to; filter = _f; priority = _p; } }; int main() { int n, m; while (~scanf("%d %d", &n, &m)) { if (n == m && m == 0) break; vector<Rule> r; for (int i = 0; i < n; i++) { string str; cin >> str; if (str == "permit") { string from, to; cin >> from >> to; r.push_back(Rule(from, to, 1, i)); } else { string from, to; cin >> from >> to; r.push_back(Rule(from, to, 0, i)); } } vector<Output> out; for (int i = 0; i < m; i++) { string from, to, msg; cin >> from >> to >> msg; for (int j = r.size() - 1; j >= 0; j--) { if (isMatch(P(r[j].from, r[j].to), P(from, to))) { if (r[j].filter == 1) { out.push_back(Output(from, to, msg)); break; } else { break; } } } } printf("%d\n", out.size()); for (int i = 0; i < out.size(); i++) { printf("%s %s %s\n", out[i].from.c_str(), out[i].to.c_str(), out[i].msg.c_str()); } } }
#define _USE_MATH_DEFINES #define INF 100000000 #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<string, string> P; typedef pair<int, P> PP; static const double EPS = 1e-8; int tx[] = {0, 1, 1, 1, 0, -1, -1, -1}; int ty[] = {-1, -1, 0, 1, 1, 1, 0, -1}; bool isMatch(const P &p1, const P &p2) { // p1:deny or permit for (int i = 0; i < 8; i++) { if (p1.first[i] != '?' && p2.first[i] != '?') { if (p1.first[i] != p2.first[i]) return false; } if (p1.second[i] != '?' && p2.second[i] != '?') { if (p1.second[i] != p2.second[i]) return false; } } return true; } class Output { public: string from; string to; string msg; Output(string _from, string _to, string _msg) { from = _from; to = _to; msg = _msg; } }; class Rule { public: string from; string to; int filter; int priority; Rule(string _from, string _to, int _f, int _p) { from = _from; to = _to; filter = _f; priority = _p; } }; int main() { int n, m; while (~scanf("%d %d", &n, &m)) { if (n == m && m == 0) break; vector<Rule> r; for (int i = 0; i < n; i++) { string str; cin >> str; if (str == "permit") { string from, to; cin >> from >> to; r.push_back(Rule(from, to, 1, i)); } else { string from, to; cin >> from >> to; r.push_back(Rule(from, to, 0, i)); } } vector<Output> out; for (int i = 0; i < m; i++) { string from, to, msg; cin >> from >> to >> msg; for (int j = r.size() - 1; j >= 0; j--) { if (isMatch(P(r[j].from, r[j].to), P(from, to))) { if (r[j].filter == 1) { out.push_back(Output(from, to, msg)); break; } else { break; } } } } printf("%d\n", out.size()); for (int i = 0; i < out.size(); i++) { printf("%s %s %s\n", out[i].from.c_str(), out[i].to.c_str(), out[i].msg.c_str()); } } }
[["-", 0, 7, 8, 9, 0, 57, 75, 76, 0, 95]]
1
673
until(n,m=gets.split.map(&:to_i))==[0,0]do r,p,a=[],[],[] n.times{r.unshift(gets.gsub("?",".").split)} m.times{p<<gets.split} p.each{|p|r.each{|r|if p[0]=~/#{r[1]}&&p[1]=~/#{r[2]}/ a<<p*" "if r[0]=~/p/ break end}} puts a.size,a end
while(n,m=gets.split.map(&:to_i))!=[0,0]do r,p,a=[],[],[] n.times{r.unshift(gets.gsub("?",".").split)} m.times{p<<gets.split} p.each{|p|r.each{|r|if p[0]=~/#{r[1]}/&&p[1]=~/#{r[2]}/ a<<p*" "if r[0]=~/p/ break end}} puts a.size,a end
[["-", 36, 36, 36, 36, 0, 493, 0, 750, 0, 750], ["+", 36, 36, 36, 36, 0, 493, 0, 89, 0, 89], ["-", 36, 36, 0, 493, 0, 750, 15, 738, 17, 60], ["+", 36, 36, 0, 493, 0, 89, 15, 738, 17, 79], ["-", 0, 493, 0, 750, 8, 170, 0, 768, 0, 769], ["+", 0, 493, 0, 89, 8, 170, 0, 768, 0, 769]]
4
87
#include <algorithm> #include <iostream> using namespace std; #define MAX_N 2000 int x[MAX_N][MAX_N]; int H, W, N; int main() { cin >> N; for (int i = 0; i < N; i++) { H = 5; W = 5; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> x[i][j]; } if (x[i][0] == 0) { x[i][0] = 0; } else { x[i][0] = 1; } for (int j = 1; j < W; j++) { if (x[i][j] == 1) { x[i][j] = 0; } else { x[i][j] = x[i][j - 1] + 1; } } } int maxn = 0; for (int i = 0; i < W; i++) { for (int j = 0; j < H; j++) { int X = x[j][i], cnt = 0; for (int k = j; k >= 0; k--) { cnt++; X = min(X, x[k][i]); if (X == 0) { break; } maxn = max(maxn, X * cnt); } } } cout << maxn << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; #define MAX_N 2000 int x[MAX_N][MAX_N]; int H, W, N; int main() { cin >> N; for (int i = 0; i < N; i++) { H = 5; W = 5; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> x[i][j]; } if (x[i][0] == 0) { x[i][0] = 0; } else { x[i][0] = 1; } for (int j = 1; j < W; j++) { if (x[i][j] == 0) { x[i][j] = 0; } else { x[i][j] = x[i][j - 1] + 1; } } } int maxn = 0; for (int i = 0; i < W; i++) { for (int j = 0; j < H; j++) { int X = x[j][i], cnt = 0; for (int k = j; k >= 0; k--) { cnt++; X = min(X, x[k][i]); if (X == 0) { break; } maxn = max(maxn, X * cnt); } } } cout << maxn << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
319
#include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int n, l[5][5]; cin >> n; while (n--) { int ans = 0, f; rep(i, 5) rep(j, 5) cin >> l[i][j]; rep(i, 5) rep(j, 5) if (l[i][j]) { rep(a, 5) rep(b, 5) { f = 0; rep(p, a) rep(q, b) if (i + p > 4 || j + q > 4 || !l[i + p][j + q]) f = 1; if (!f && ans < a * b) ans = a * b; } } cout << ans << endl; } return 0; }
#include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int n, l[5][5]; cin >> n; while (n--) { int ans = 0, f; rep(i, 5) rep(j, 5) cin >> l[i][j]; rep(i, 5) rep(j, 5) if (l[i][j]) { rep(a, 6) rep(b, 6) { f = 0; rep(p, a) rep(q, b) if (i + p > 4 || j + q > 4 || !l[i + p][j + q]) f = 1; if (!f && ans < a * b) ans = a * b; } } cout << ans << endl; } return 0; }
[["-", 64, 9, 0, 42, 0, 2, 3, 4, 0, 13], ["+", 64, 9, 0, 42, 0, 2, 3, 4, 0, 13]]
1
188
// 23 #include <iostream> using namespace std; int main() { int m; cin >> m; while (m--) { int g[5][5]; for (int y = 0; y < 5; y++) { for (int x = 0; x < 5; x++) { cin >> g[y][x]; } } int ans = 0; for (int l = 0; l < 5; l++) { for (int r = l + 1; r < 5; r++) { for (int t = 0; t < 5; t++) { for (int b = t; b < 5; b++) { int f = 1; int c = 0; for (int y = l; y <= r; y++) { for (int x = t; x <= b; x++) { f &= g[y][x]; c++; } } if (f && ans < c) { ans = c; } } } } } cout << ans << endl; } return 0; }
// 23 #include <iostream> using namespace std; int main() { int m; cin >> m; while (m--) { int g[5][5]; for (int y = 0; y < 5; y++) { for (int x = 0; x < 5; x++) { cin >> g[y][x]; } } int ans = 0; for (int l = 0; l < 5; l++) { for (int r = l; r < 5; r++) { for (int t = 0; t < 5; t++) { for (int b = t; b < 5; b++) { int f = 1; int c = 0; for (int y = l; y <= r; y++) { for (int x = t; x <= b; x++) { f &= g[y][x]; c++; } } if (f && ans < c) { ans = c; } } } } } cout << ans << endl; } return 0; }
[["-", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["-", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
227
import java.io.*; 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 r = sc.nextInt(); if (n == 0 && r == 0) { break; } int[] s = new int[100]; int[] s2 = new int[100]; int[] p = new int[100]; int[] c = new int[100]; int h, i, j, k; for (i = 0; i < n; i++) { s[i] = i + 1; } for (j = 0; j < r; j++) { for (h = 0; h < n; h++) { s2[h] = s[h]; } p[j] = sc.nextInt(); c[j] = sc.nextInt(); for (k = 0; k < p[j] + c[j] - 1; k++) { if (k < c[j]) { s[k] = s2[k + p[j] - 1]; } if (k >= c[j]) { s[k] = s2[k - c[j]]; } } } System.out.printf("%d\n", s[0]); } } }
import java.io.*; 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 r = sc.nextInt(); if (n == 0 && r == 0) { break; } int[] s = new int[100]; int[] s2 = new int[100]; int[] p = new int[100]; int[] c = new int[100]; int h, i, j, k; for (i = 0; i < n; i++) { s[i] = n - i; } for (j = 0; j < r; j++) { for (h = 0; h < n; h++) { s2[h] = s[h]; } p[j] = sc.nextInt(); c[j] = sc.nextInt(); for (k = 0; k < p[j] + c[j] - 1; k++) { if (k < c[j]) { s[k] = s2[k + p[j] - 1]; } if (k >= c[j]) { s[k] = s2[k - c[j]]; } } } System.out.printf("%d\n", s[0]); } } }
[["+", 8, 196, 0, 1, 0, 11, 12, 16, 31, 22], ["+", 8, 196, 0, 1, 0, 11, 12, 16, 17, 33], ["-", 8, 196, 0, 1, 0, 11, 12, 16, 17, 72], ["-", 8, 196, 0, 1, 0, 11, 12, 16, 12, 499]]
3
315
#include <stdio.h> void shuffle(int *fuda, int p, int c, int n) { int i; for (i = 0; i < n; i++) { if (*(fuda + i) < p + c - 1) { if (*(fuda + i) < p - 1) { *(fuda + i) += c; } else { *(fuda + i) -= (p - 1); } } } }; int main() { while (1) { int n, r, i, p, c; scanf("%d %d", &n, &r); if (n == 0 && r == 0) return 0; int fuda[n]; for (i = 0; i < n; i++) { fuda[i] = i + 1; } for (i = 0; i < r; i++) { scanf("%d %d", &p, &c); shuffle(fuda, p, c, n); } for (i = 0; i < n; i++) { if (fuda[i] == 1) { printf("%d\n", n - i); } } } }
#include <stdio.h> void shuffle(int *fuda, int p, int c, int n) { int i; for (i = 0; i < n; i++) { if (*(fuda + i) < p + c) { if (*(fuda + i) < p) { *(fuda + i) += c; } else { *(fuda + i) -= (p - 1); } } } }; int main() { while (1) { int n, r, i, p, c; scanf("%d %d", &n, &r); if (n == 0 && r == 0) return 0; int fuda[n]; for (i = 0; i < n; i++) { fuda[i] = i + 1; } for (i = 0; i < r; i++) { scanf("%d %d", &p, &c); shuffle(fuda, p, c, n); } for (i = 0; i < n; i++) { if (fuda[i] == 1) { printf("%d\n", n - i); } } } }
[["-", 0, 57, 15, 23, 0, 16, 12, 16, 17, 33], ["-", 0, 57, 15, 23, 0, 16, 12, 16, 12, 13]]
0
252
#include <stdio.h> int main() { int d[50]; int a[50], b[50]; int i, j, k, l, m, o, q; int n, r, p, c; int A; for (i = 0;; i++) { scanf("%d", &n); scanf("%d", &r); if (n == 0 && r == 0) break; A = n; for (m = 0; m < n; m++) { d[m] = A; A--; } for (j = 0; j < r; j++) { scanf("%d", &p); scanf("%d", &c); for (k = 0; k < p - 1; k++) { a[k] = d[k]; } for (l = 0; l < c; l++) { b[l] = d[p - 1 + l]; } for (o = 0; o < p - 1; o++) { d[o + c] = a[o]; } for (q = 0; q < c; q++) { d[q] = b[q]; } } printf("%d", d[0]); } return 0; }
#include <stdio.h> int main() { int d[50]; int a[50], b[50]; int i, j, k, l, m, o, q; int n, r, p, c; int A; for (i = 0;; i++) { scanf("%d", &n); scanf("%d", &r); if (n == 0 && r == 0) break; A = n; for (m = 0; m < n; m++) { d[m] = A; A--; } for (j = 0; j < r; j++) { scanf("%d", &p); scanf("%d", &c); for (k = 0; k < p - 1; k++) { a[k] = d[k]; } for (l = 0; l < c; l++) { b[l] = d[p - 1 + l]; } for (o = 0; o < p - 1; o++) { d[o + c] = a[o]; } for (q = 0; q < c; q++) { d[q] = b[q]; } } printf("%d\n", d[0]); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
0
285
#include <stdio.h> int main() { int i, j, n, r, p, c, A[52], B[52]; while (1) { scanf("%d %d", &n, &r); if (n == 0 && r == 0) { break; } for (i = n; i > 0; i--) { A[n - i] = i; B[n - i] = i; } for (i = 0; i < r; i++) { scanf("%d %d", &p, &c); for (j = 0; j <= p - 1; j++) B[j + c] = A[j]; for (j = p - 1; j < p - 1 + c; j++) B[j - (p - 1)] = A[j]; for (j = 0; j < n; j++) A[j] = B[j]; } printf("%d\n", B[0]); } return 0; }
#include <stdio.h> int main() { int i, j, n, r, p, c, A[52], B[52]; while (1) { scanf("%d %d", &n, &r); if (n == 0 && r == 0) { break; } for (i = n; i > 0; i--) { A[n - i] = i; B[n - i] = i; } for (i = 0; i < r; i++) { scanf("%d %d", &p, &c); for (j = 0; j < p - 1; j++) B[j + c] = A[j]; for (j = p - 1; j < p - 1 + c; j++) B[j - (p - 1)] = A[j]; for (j = 0; j < n; j++) A[j] = B[j]; } printf("%d\n", B[0]); } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18]]
0
226
#include <stdio.h> #include <string.h> int main(void) { int n, r; while (scanf("%d %d", &n, &r), n) { int card[64]; int i; for (i = 0; i < n; i++) { card[i] = n - i; } while (r-- > 0) { int p, c; int tmp[64]; scanf("%d %d", &p, &c); for (i = p - 1; i < p + c; i++) { tmp[i - p + 1] = card[i]; } for (i = p + c - 1; i > c - 1; i--) { card[i] = card[i - c]; } for (i = 0; i < c; i++) { card[i] = tmp[i]; } } printf("%d\n", card[0]); } return 0; }
#include <stdio.h> #include <string.h> int main(void) { int n, r; while (scanf("%d %d", &n, &r), n) { int card[64]; int i; for (i = 0; i < n; i++) { card[i] = n - i; } while (r-- > 0) { int p, c; int tmp[64]; scanf("%d %d", &p, &c); for (i = p - 1; i < p + c - 1; i++) { tmp[i - p + 1] = card[i]; } for (i = p + c - 2; i > c - 1; i--) { card[i] = card[i - c]; } for (i = 0; i < c; i++) { card[i] = tmp[i]; } } printf("%d\n", card[0]); } return 0; }
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13], ["-", 8, 9, 0, 7, 10, 11, 12, 16, 12, 13], ["+", 8, 9, 0, 7, 10, 11, 12, 16, 12, 13]]
0
208
#include <stdio.h> int main(void) { int i, j, n, r, p, c; while (1) { scanf("%d%d", &n, &r); if (n == 0 && r == 0) { break; } int yama[n]; for (i = 0; i < n; i++) { yama[i] = n - i; } for (i = 0; i < r; i++) { scanf("%d%d", &p, &c); int down[p - 1], cut[c]; for (j = 0; j < p - 1; j++) { down[j] = yama[j]; } for (j = 0; p - 1 + j < p - 1 + c; j++) { cut[j] = yama[p - 1 + j]; } for (j = 0; j < c; j++) { yama[j] = cut[j]; } for (j = 0; j < p - 1; j++) { yama[c + j] = down[j]; } } printf("%d", yama[0]); } return 0; }
#include <stdio.h> int main(void) { int i, j, n, r, p, c; while (1) { scanf("%d%d", &n, &r); if (n == 0 && r == 0) { break; } int yama[n]; for (i = 0; i < n; i++) { yama[i] = n - i; } for (i = 0; i < r; i++) { scanf("%d%d", &p, &c); int down[p - 1], cut[c]; for (j = 0; j < p - 1; j++) { down[j] = yama[j]; } for (j = 0; p - 1 + j < p - 1 + c; j++) { cut[j] = yama[p - 1 + j]; } for (j = 0; j < c; j++) { yama[j] = cut[j]; } for (j = 0; j < p - 1; j++) { yama[c + j] = down[j]; } } printf("%d\n", yama[0]); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
0
259
#include <stdio.h> #include <stdlib.h> int yama[51]; void init_yama(int n) { int i, j; for (i = n, j = 1; j <= n; i--, j++) { yama[j] = i; } return; } void cut(int p, int c) { int temp[50]; int i, j; for (i = 1, j = 0; i <= p - 1; i++, j++) { temp[j] = yama[i]; } for (i = p, j = 1; j <= c; i++, j++) { yama[j] = yama[i]; } for (i = c + 1, j = 0; j < p; i++, j++) { yama[i] = temp[j]; } return; } int main() { int n, r, p, c, i, j; while (1) { scanf("%d %d", &n, &r); if (n == 0 && r == 0) break; init_yama(n); for (i = 0; i < r; i++) { scanf("%d %d", &p, &c); cut(p, c); } printf("%d\n", yama[1]); } return 0; }
#include <stdio.h> #include <stdlib.h> int yama[51]; void init_yama(int n) { int i, j; for (i = n, j = 1; j <= n; i--, j++) { yama[j] = i; } return; } void cut(int p, int c) { int temp[50]; int i, j; for (i = 1, j = 0; i <= p - 1; i++, j++) { temp[j] = yama[i]; } for (i = p, j = 1; j <= c; i++, j++) { yama[j] = yama[i]; } for (i = c + 1, j = 0; j < p - 1; i++, j++) { yama[i] = temp[j]; } return; } int main() { int n, r, p, c, i, j; while (1) { scanf("%d %d", &n, &r); if (n == 0 && r == 0) break; init_yama(n); for (i = 0; i < r; i++) { scanf("%d %d", &p, &c); cut(p, c); } printf("%d\n", yama[1]); } return 0; }
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
0
284
/* ============================================================================ Name : hanahuda.c Author : Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdio.h> #include <stdlib.h> int main(void) { int n, r, i, j, p, c; int huda[50]; int tmp[50]; do { scanf("%d %d", &n, &r); if ((n == 0) && (r == 0)) { break; } for (i = 0; i < n; i++) { huda[i] = i + 1; } for (i = 0; i < r; i++) { scanf("%d %d", &p, &c); for (j = 0; j < c; j++) { tmp[j] = huda[n - p - j]; } for (j = 0; j < p; j++) { huda[n - p + 1 + j - c] = huda[n - p + 1 + j]; } for (j = 0; j < c; j++) { huda[n - 1 - j] = tmp[j]; } } printf("%d\n", huda[n - 1]); } while ((n != 0) && (r != 0)); printf("\n"); return 0; }
#include <stdio.h> #include <stdlib.h> int main(void) { int n, r, i, j, p, c; int huda[50]; int tmp[50]; do { scanf("%d %d", &n, &r); if ((n == 0) && (r == 0)) { break; } for (i = 0; i < n; i++) { huda[i] = i + 1; } for (i = 0; i < r; i++) { scanf("%d %d", &p, &c); for (j = 0; j < c; j++) { tmp[j] = huda[n - p - j]; } for (j = 0; j < p; j++) { huda[n - p + 1 + j - c] = huda[n - p + 1 + j]; } for (j = 0; j < c; j++) { huda[n - 1 - j] = tmp[j]; } } printf("%d\n", huda[n - 1]); } while ((n != 0) && (r != 0)); return 0; }
[["-", 0, 14, 8, 9, 0, 1, 0, 2, 63, 22], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 62], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["-", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35]]
0
260
#include <stdio.h> int main() { int card[50], next[50]; int n, r; int p, c; int i, j; while (1) { scanf("%d%d", &n, &r); if (n == 0 && r == 0) break; for (i = 0; i < n; i++) { card[i] = n - i; next[i] = n - 1; } for (j = 0; j < r; j++) { scanf("%d%d", &p, &c); for (i = 0; i < c; i++) { next[i] = card[p + i - 1]; } for (i = c; i < c + p - 1; i++) { next[i] = card[i - c]; } for (i = 0; i < n; i++) { card[i] = next[i]; } } printf("%d\n", card[0]); } return 0; }
#include <stdio.h> int main() { int card[50], next[50]; int n, r; int p, c; int i, j; while (1) { scanf("%d%d", &n, &r); if (n == 0 && r == 0) break; for (i = 0; i < n; i++) { card[i] = n - i; next[i] = n - i; } for (j = 0; j < r; j++) { scanf("%d%d", &p, &c); for (i = 0; i < c; i++) { next[i] = card[p + i - 1]; } for (i = c; i < c + p - 1; i++) { next[i] = card[i - c]; } for (i = 0; i < n; i++) { card[i] = next[i]; } } printf("%d\n", card[0]); } return 0; }
[["-", 8, 9, 0, 1, 0, 11, 12, 16, 12, 13], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 22]]
0
227
#include <stdio.h> int main(void) { int hanafuda[50]; int buffer[50]; int maisuu, s_kaisuu, s_now; int i; int p, c; while (1) { scanf("%d%d", &maisuu, &s_kaisuu); if (maisuu == 0 && s_kaisuu == 0) break; for (i = 0; i < maisuu; i++) hanafuda[i] = maisuu - i; for (s_now = 0; s_now < s_kaisuu; s_now++) { scanf("%d%d", &p, &c); for (i = 0; i < c; i++) buffer[i] = hanafuda[i + p - 1]; for (i = 0; i < p; i++) buffer[i + c] = hanafuda[i]; for (i = 0; i < p + c; i++) hanafuda[i] = buffer[i]; } printf("%d\n", hanafuda[0]); } return 0; }
#include <stdio.h> int main(void) { int hanafuda[50]; int buffer[50]; int maisuu, s_kaisuu, s_now; int i; int p, c; while (1) { scanf("%d%d", &maisuu, &s_kaisuu); if (maisuu == 0 && s_kaisuu == 0) break; for (i = 0; i < maisuu; i++) hanafuda[i] = maisuu - i; for (s_now = 0; s_now < s_kaisuu; s_now++) { scanf("%d%d", &p, &c); for (i = 0; i < c; i++) buffer[i] = hanafuda[i + p - 1]; for (i = 0; i < p - 1; i++) buffer[i + c] = hanafuda[i]; for (i = 0; i < p + c - 1; i++) hanafuda[i] = buffer[i]; } printf("%d\n", hanafuda[0]); } return 0; }
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
0
210
#include <iostream> using namespace std; int main() { int n, r; int card[50]; int cutp[50]; int cutc[50]; while (cin >> n >> r, n || r) { for (int i = 0; i < n; i++) { card[i] = i + 1; } for (int i = 0; i < r; i++) { int p, c; cin >> p >> c; for (int i = 0; i < p - 1; i++) { cutp[i] = card[n - p + 1 + i]; } for (int i = 0; i < c; i++) { cutc[i] = card[n - p + 1 - c + i]; } for (int i = 0; i < p - 1; i++) { card[n - p + 1 - c + i] = cutp[i]; } for (int i = 0; i < c; i++) { card[n - p + 1 + i] = cutc[i]; } } cout << card[n - 1] << endl; } return 0; }
#include <iostream> using namespace std; int main() { int n, r; int card[50]; int cutp[50]; int cutc[50]; while (cin >> n >> r, n || r) { for (int i = 0; i < n; i++) { card[i] = i + 1; } for (int j = 0; j < r; j++) { int p, c; cin >> p >> c; for (int i = 0; i < p - 1; i++) { cutp[i] = card[n - p + 1 + i]; } for (int i = 0; i < c; i++) { cutc[i] = card[n - p + 1 - c + i]; } for (int i = 0; i < p - 1; i++) { card[n - p + 1 - c + i] = cutp[i]; } for (int i = 0; i < c; i++) { card[n - c + i] = cutc[i]; } } cout << card[n - 1] << endl; } return 0; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 49, 22], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 49, 22], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 31, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 31, 22], ["-", 0, 52, 8, 9, 0, 7, 26, 27, 28, 22], ["+", 0, 52, 8, 9, 0, 7, 26, 27, 28, 22], ["-", 341, 342, 0, 16, 31, 16, 31, 16, 12, 22], ["-", 31, 69, 341, 342, 0, 16, 31, 16, 17, 72], ["-", 31, 69, 341, 342, 0, 16, 31, 16, 12, 13], ["+", 31, 69, 341, 342, 0, 16, 31, 16, 12, 22]]
1
251
#include <iostream> using namespace std; void cut(int[], int, int); int main(void) { int p, c; for (int n, r; cin >> n >> r, n || r;) { int card[n]; for (int i = 0; i < n; i++) card[i] = n - i; for (int i = 0; i < r; i++) { cin >> p >> c; if (p != 1) cut(card, p, c); } cout << card[0] << endl; } return 0; } void cut(int card[], int p, int c) { int tmp[c]; for (int i = 0; i < c; i++) tmp[i] = card[p - 1 + i]; for (int i = 1; i <= p; i++) card[p + c - i] = card[p - i]; for (int i = 0; i < c; i++) card[i] = tmp[i]; }
#include <iostream> using namespace std; void cut(int[], int, int); int main(void) { int p, c; for (int n, r; cin >> n >> r, n || r;) { int card[n]; for (int i = 0; i < n; i++) card[i] = n - i; for (int i = 0; i < r; i++) { cin >> p >> c; if (p != 1) cut(card, p, c); } cout << card[0] << endl; } return 0; } void cut(int card[], int p, int c) { int tmp[c]; for (int i = 0; i < c; i++) tmp[i] = card[p - 1 + i]; for (int i = 0; i < p; i++) card[p + c - 2 - i] = card[p - 2 - i]; for (int i = 0; i < c; i++) card[i] = tmp[i]; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 31, 69, 341, 342, 0, 16, 31, 16, 17, 33], ["+", 31, 69, 341, 342, 0, 16, 31, 16, 12, 13], ["+", 12, 69, 341, 342, 0, 16, 31, 16, 17, 33], ["+", 12, 69, 341, 342, 0, 16, 31, 16, 12, 13]]
1
232
#include <limits.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int n, m; int p[51] = {0}; int c[51] = {0}; int i; int ans; while (1) { scanf("%d %d", &n, &m); if ((n == 0) && (m == 0)) break; ans = n; for (i = 1; i <= m; i++) { scanf("%d %d", &p[i], &c[i]); } for (i = m; i >= 1; i--) { if (p[i] != 1) { if ((n - p[i] + 1 - c[i] - 1) >= ans) { } else if (ans >= (n - c[i] - 1)) { ans = ans - p[i] + 1; } else ans = ans + c[i]; } } printf("%d\n", ans); } return 0; }
#include <limits.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int n, m; int p[51] = {0}; int c[51] = {0}; int i; int ans; while (1) { scanf("%d %d", &n, &m); if ((n == 0) && (m == 0)) break; ans = n; for (i = 1; i <= m; i++) { scanf("%d %d", &p[i], &c[i]); } for (i = m; i >= 1; i--) { if (p[i] != 1) { if ((n - p[i] - c[i] + 1) >= ans) { } else if (ans >= (n - c[i] + 1)) { ans = ans - p[i] + 1; } else ans = ans + c[i]; } } printf("%d\n", ans); } return 0; }
[["-", 31, 23, 0, 16, 31, 16, 31, 16, 17, 72], ["-", 31, 23, 0, 16, 31, 16, 31, 16, 12, 13], ["-", 15, 339, 51, 16, 31, 23, 0, 16, 17, 33], ["+", 15, 339, 51, 16, 31, 23, 0, 16, 17, 72], ["-", 15, 339, 51, 16, 12, 23, 0, 16, 17, 33], ["+", 15, 339, 51, 16, 12, 23, 0, 16, 17, 72]]
1
222
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define mp make_pair #define pb push_back #define REP(i, a, n) for (int i = a; i < (n); i++) #define rep(i, n) for (int i = 0; i < (n); i++) #define all(s) s.begin(), s.end() #define rall(s) s.rbegin(), s.rend() #define range(x, min, max) ((min) <= (x) && (x) <= (max)) using namespace std; typedef long long LL; typedef vector<int> VI; typedef vector<vector<int>> VII; typedef vector<string> VS; typedef vector<bool> VB; typedef pair<int, int> PII; const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1}; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, r; VI ps, cs, cards, temp; while (1) { ps.clear(); cs.clear(); cards.clear(); cin >> n >> r; if (n == 0 && r == 0) break; rep(i, n) { cards.pb(i + 1); } reverse(all(cards)); rep(i, r) { int p, c; cin >> p >> c; ps.pb(p); cs.pb(c); } rep(i, r) { temp.clear(); REP(j, ps[i] - 1, ps[i] + cs[i] - 1) { temp.pb(cards[j]); } rep(j, ps[i] - 1) { temp.pb(cards[j]); } REP(j, ps[i] + cs[i], cards.size()) { temp.pb(cards[j]); } cards.clear(); cards = temp; } cout << cards[0] << endl; } return 0; }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define mp make_pair #define pb push_back #define REP(i, a, n) for (int i = a; i < (n); i++) #define rep(i, n) for (int i = 0; i < (n); i++) #define all(s) s.begin(), s.end() #define rall(s) s.rbegin(), s.rend() #define range(x, min, max) ((min) <= (x) && (x) <= (max)) using namespace std; typedef long long LL; typedef vector<int> VI; typedef vector<vector<int>> VII; typedef vector<string> VS; typedef vector<bool> VB; typedef pair<int, int> PII; const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1}; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, r; VI ps, cs, cards, temp; while (1) { ps.clear(); cs.clear(); cards.clear(); cin >> n >> r; if (n == 0 && r == 0) break; rep(i, n) { cards.pb(i + 1); } reverse(all(cards)); rep(i, r) { int p, c; cin >> p >> c; ps.pb(p); cs.pb(c); } rep(i, r) { temp.clear(); REP(j, ps[i] - 1, ps[i] + cs[i] - 1) { temp.pb(cards[j]); } rep(j, ps[i] - 1) { temp.pb(cards[j]); } REP(j, ps[i] + cs[i] - 1, cards.size()) { temp.pb(cards[j]); } cards.clear(); cards = temp; } cout << cards[0] << endl; } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
426
#include <algorithm> #include <iostream> using namespace std; int main() { int n, r, p, c, l[50]; while (cin >> n >> r, n | r) { for (int i = 0; i < n; i++) l[i] = n - i; for (int i = 0; i < r; i++) { cin >> p >> c; rotate(l, l + p - 1, l + p + c - 1); } cout << l[0]; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { int n, r, p, c, l[50]; while (cin >> n >> r, n | r) { for (int i = 0; i < n; i++) l[i] = n - i; for (int i = 0; i < r; i++) { cin >> p >> c; rotate(l, l + p - 1, l + p + c - 1); } cout << l[0] << endl; } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22]]
1
116
#include <iostream> using namespace std; int main() { int n, r, p, c; int card[50], tmp; while (cin >> n >> r) { if (n == 0 && r == 0) break; for (int i = 0; i < n; i++) { card[i] = n - i; } for (int i = 0; i < r; i++) { cin >> p >> c; for (int j = 0; j < c; j++) { tmp = card[p - 1 + j]; for (int k = p - 2 + j; k >= j; k--) { card[k + 1] = card[k]; } card[j] = tmp; } } cout << card[0]; } cout << "\n"; return 0; }
#include <iostream> using namespace std; int main() { int n, r, p, c; int card[50], tmp; while (cin >> n >> r) { if (n == 0 && r == 0) break; for (int i = 0; i < n; i++) { card[i] = n - i; } for (int i = 0; i < r; i++) { cin >> p >> c; for (int j = 0; j < c; j++) { tmp = card[p - 1 + j]; for (int k = p - 2 + j; k >= j; k--) { card[k + 1] = card[k]; } card[j] = tmp; } } cout << card[0] << "\n"; } return 0; }
[["-", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35], ["-", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46], ["-", 0, 14, 8, 9, 0, 1, 0, 16, 31, 22], ["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46]]
1
180
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < b; i++) #define dump(x) cout << #x << " = " << x << endl using namespace std; int card[50]; int n, r; void disp() { rep(i, 0, n) cout << card[i] << " "; cout << endl; return; } int main() { while (1) { cin >> n >> r; if (n == 0) break; rep(i, 0, n) card[i] = n - i; disp(); rep(i, 0, r) { int p, c; cin >> p >> c; int temp[p]; rep(i, 0, p - 1) temp[i] = card[i]; rep(i, 0, c) card[i] = card[p - 1 + i]; rep(i, 0, p - 1) card[c + i] = temp[i]; // disp(); } cout << card[0] << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < b; i++) #define dump(x) cout << #x << " = " << x << endl using namespace std; int card[50]; int n, r; void disp() { rep(i, 0, n) cout << card[i] << " "; cout << endl; return; } int main() { while (1) { cin >> n >> r; if (n == 0) break; rep(i, 0, n) card[i] = n - i; // disp(); rep(i, 0, r) { int p, c; cin >> p >> c; int temp[p]; rep(i, 0, p - 1) temp[i] = card[i]; rep(i, 0, c) card[i] = card[p - 1 + i]; rep(i, 0, p - 1) card[c + i] = temp[i]; // disp(); } cout << card[0] << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 1, 0, 2, 63, 22], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["-", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
216
#include <algorithm> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #define pi 3.14159 using namespace std; int main() { int n, r; while (cin >> n >> r) { if (n == 0 && r == 0) break; int a[50], b[50]; for (int i = 0; i < n; i++) { a[i] = n - i; } for (int i = 0; i < n; i++) { b[i] = n - i; } for (int i = 0; i < r; i++) { int p, c; cin >> p >> c; for (int j = 0; j < c; j++) { b[j] = a[p + j - 1]; } for (int j = 0; j < p; j++) { b[c + j] = a[j]; } for (int j = 0; j < n; j++) { a[j] = b[j]; } } cout << b[0] << endl; } return 0; }
#include <algorithm> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #define pi 3.14159 using namespace std; int main() { int n, r; while (cin >> n >> r) { if (n == 0 && r == 0) break; int a[50], b[50]; for (int i = 0; i < n; i++) { a[i] = n - i; } for (int i = 0; i < n; i++) { b[i] = n - i; } for (int i = 0; i < r; i++) { int p, c; cin >> p >> c; for (int j = 0; j < c; j++) { b[j] = a[p + j - 1]; } for (int j = 0; j < p - 1; j++) { b[c + j] = a[j]; } for (int j = 0; j < n; j++) { a[j] = b[j]; } } cout << b[0] << endl; } return 0; }
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
234
#include <bits/stdc++.h> using namespace std; // objects----------------------------------------------------------------- typedef complex<double> Point; typedef Point Vector; typedef vector<Point> Polygon; struct Segment { Point p1, p2; Segment(const Point &p1 = Point(), const Point &p2 = Point()) : p1(p1), p2(p2) {} }; struct Line { Point p1, p2; Line(const Point &p1 = Point(), const Point &p2 = Point()) : p1(p1), p2(p2) {} }; struct Circle { Point p; double r; Circle(const Point &p = Point(), double r = 0.0) : p(p), r(r) {} }; // constants--------------------------------------------------------------- const double PI = acos(-1); const double EPS = 1e-12; const double INF = 1e16; const int COUNTER_CLOCKWISE = 1; const int CLOCKWISE = -1; const int ONLINE_BACK = 2; const int ONLINE_FRONT = -2; const int ON_SEGMENT = 0; const int IN = 2; const int ON = 1; const int OUT = 0; // calculations------------------------------------------------------------ inline double square(double a) { return a * a; } inline double norm(const Point &a) { return square(a.real()) + square(a.imag()); } inline double dot(const Point &a, const Point &b) { return (conj(a) * b).real(); } inline double cross(const Point &a, const Point &b) { return (conj(a) * b).imag(); } inline double toDeg(double t) { return t / PI * 180.0; } inline double toRad(double t) { return t / 180.0 * t; } inline double arg(Vector p) { return atan2(imag(p), real(p)); } inline Vector polar(double a, double r) { return Vector(cos(r), sin(r)) * a; } #define equals(a, b) (fabs((a) - (b)) < EPS) #define equalsv(a, b) (equals(real(a), real(b)) && equals(imag(a), imag(b))) #define curr(P, i) P[(i) % P.size()] #define next(P, i) P[(i + 1) % P.size()] #define prev(P, i) P[(i - 1 + P.size()) % P.size()] int ccw(Point p0, Point p1, Point p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > EPS) return COUNTER_CLOCKWISE; if (cross(a, b) < -EPS) return CLOCKWISE; if (dot(a, b) < EPS) return ONLINE_BACK; if (norm(a) < norm(b)) return ONLINE_FRONT; return ON_SEGMENT; } // inputer----------------------------------------------------------------- void inputPoint(Point &p) { double x, y; scanf("%lf%lf", &x, &y); p = Point(x, y); } void inputSegment(Segment &s) { inputPoint(s.p1); inputPoint(s.p2); } void inputLine(Line &l) { inputPoint(l.p1); inputPoint(l.p2); } void inputCircle(Circle &c) { inputPoint(c.p); scanf("%lf", &c.r); } void inputPolygon(Polygon &g, int n) { g.resize(n); for (int i = 0; i < n; i++) inputPoint(g[i]); } // orthogonal-------------------------------------------------------------- bool orthogonal(Vector a, Vector b) { return equals(dot(a, b), 0.0); } bool orthogonal(Point a1, Point a2, Point b1, Point b2) { return orthogonal(a1 - a2, b1 - b2); } bool orthogonal(Segment s1, Segment s2) { return orthogonal(s1.p1 - s1.p2, s2.p1 - s2.p2); } bool orthogonal(Line l1, Line l2) { return orthogonal(l1.p1 - l1.p2, l2.p1 - l2.p2); } // parallel---------------------------------------------------------------- bool parallel(Vector a, Vector b) { return equals(cross(a, b), 0.0); } bool parallel(Point a1, Point a2, Point b1, Point b2) { return parallel(a1 - a2, b1 - b2); } bool parallel(Segment s1, Segment s2) { return parallel(s1.p1 - s1.p2, s2.p1 - s2.p2); } bool parallel(Line l1, Line l2) { return parallel(l1.p1 - l1.p2, l2.p1 - l2.p2); } // project&reflect--------------------------------------------------------- Point project(Line s, Point p) { Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / norm(base); return s.p1 + base * r; } Point reflect(Line s, Point p) { return p + (project(s, p) - p) * 2.0; } // distance---------------------------------------------------------------- double distancePP(Point a, Point b) { return abs(a - b); } double distanceLP(Line l, Point p) { return fabs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1)); } double distanceLL(Line l, Line m) { if (parallel(l, m)) return distanceLP(l, m.p1); return 0.0; } double distanceSP(Segment s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return distancePP(p, s.p1); if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return distancePP(p, s.p2); return distanceLP(Line(s.p1, s.p2), p); } double distanceSS(Segment s1, Segment s2) { if (ccw(s1.p1, s1.p2, s2.p1) * ccw(s1.p1, s1.p2, s2.p2) <= 0 && ccw(s2.p1, s2.p2, s1.p1) * ccw(s2.p1, s2.p2, s1.p2) <= 0) return 0; return min(min(distanceSP(s1, s2.p1), distanceSP(s1, s2.p2)), min(distanceSP(s2, s1.p1), distanceSP(s2, s1.p2))); } double distanceCS(Circle c, Segment s) { return distanceSP(s, c.p) - c.r; } // intersect---------------------------------------------------------------- bool intersectSS(Point p1, Point p2, Point p3, Point p4) { return ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0; } bool intersectSS(Segment s1, Segment s2) { return intersectSS(s1.p1, s1.p2, s2.p1, s2.p2); } bool intersectCL(Circle c, Line l) { return fabs(cross(l.p2 - l.p1, c.p - l.p1) / abs(l.p2 - l.p1)) <= c.r + EPS; } bool intersectCC(Circle c1, Circle c2) { return abs(c1.p - c2.p) <= c1.r + c2.r + EPS; } bool intersectCS(Circle c, Segment s) { return distanceSP(s, c.p) <= c.r + EPS; } bool intersectGC(Polygon g, Circle c) { for (int i = 0; i < g.size(); i++) { Segment s(g[i], next(g, i)); if (intersectCS(c, s)) return true; } return false; } // crossPoint-------------------------------------------------------------- Point crossPoint(Segment s1, Segment s2) { double crs = cross(s1.p2 - s1.p1, s2.p2 - s2.p1); return s2.p1 + (s2.p2 - s2.p1) * cross(s1.p2 - s1.p1, s1.p2 - s2.p1) * (1.0 / crs); } pair<Point, Point> crossPoints(Circle c, Line l) { assert(intersectCL(c, l)); Vector pr = project(l, c.p); Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1); double base = sqrt(square(c.r) - norm(pr - c.p)); return make_pair(pr + e * base, pr - e * base); } pair<Point, Point> crossPoints(Circle c1, Circle c2) { assert(intersectCC(c1, c2)); double d = abs(c1.p - c2.p); double a = acos((square(c1.r) + square(d) - square(c2.r)) / (2 * c1.r * d)); double t = arg(c2.p - c1.p); return make_pair(c1.p + polar(c1.r, t + a), c1.p + polar(c1.r, t - a)); } // polygon------------------------------------------------------------------ double area(Polygon &g) { const int sz = g.size(); double res = cross(g[sz - 1], g[0]); for (int i = 1; i < sz; i++) res += cross(g[i - 1], g[i]); return fabs(res) * 0.5; } bool convex(Polygon &g) { for (int i = 0; i < g.size(); i++) { if (ccw(prev(g, i), curr(g, i), next(g, i)) == CLOCKWISE) return false; } return true; } // contains------------------------------------------------------------------- int contains(Circle c, Point p) { double d = distancePP(c.p, p); if (equals(d, c.r)) return ON; return d < c.r ? IN : OUT; } int contains(Polygon g, Point p) { int n = g.size(); bool x = false; for (int i = 0; i < n; i++) { Point a = g[i] - p, b = next(g, i) - p; if (abs(cross(a, b)) < EPS && dot(a, b) < EPS) return ON; if (imag(a) > imag(b)) swap(a, b); if (imag(a) < EPS && EPS < imag(b) && cross(a, b) > EPS) x = !x; } return x ? IN : OUT; } int contains(Circle c, Polygon g) { for (int i = 0; i < g.size(); i++) { if (!contains(c, g[i])) return OUT; } return IN; } int contains(Polygon g, Circle c) { if (contains(g, c.p) != IN) return OUT; for (int i = 0; i < g.size(); i++) { if (distanceCS(c, Segment(g[i], next(g, i))) < -EPS) return OUT; } return IN; } // end--------------------------------------------------------------------- int main() { int n; int c = 1; while (cin >> n, n) { c++; Polygon g(n); for (int i = 0; i < n; i++) inputPoint(g[i]); printf("%d %.20lf\n", c, area(g)); } return 0; }
#include <bits/stdc++.h> using namespace std; // objects----------------------------------------------------------------- typedef complex<double> Point; typedef Point Vector; typedef vector<Point> Polygon; struct Segment { Point p1, p2; Segment(const Point &p1 = Point(), const Point &p2 = Point()) : p1(p1), p2(p2) {} }; struct Line { Point p1, p2; Line(const Point &p1 = Point(), const Point &p2 = Point()) : p1(p1), p2(p2) {} }; struct Circle { Point p; double r; Circle(const Point &p = Point(), double r = 0.0) : p(p), r(r) {} }; // constants--------------------------------------------------------------- const double PI = acos(-1); const double EPS = 1e-12; const double INF = 1e16; const int COUNTER_CLOCKWISE = 1; const int CLOCKWISE = -1; const int ONLINE_BACK = 2; const int ONLINE_FRONT = -2; const int ON_SEGMENT = 0; const int IN = 2; const int ON = 1; const int OUT = 0; // calculations------------------------------------------------------------ inline double square(double a) { return a * a; } inline double norm(const Point &a) { return square(a.real()) + square(a.imag()); } inline double dot(const Point &a, const Point &b) { return (conj(a) * b).real(); } inline double cross(const Point &a, const Point &b) { return (conj(a) * b).imag(); } inline double toDeg(double t) { return t / PI * 180.0; } inline double toRad(double t) { return t / 180.0 * t; } inline double arg(Vector p) { return atan2(imag(p), real(p)); } inline Vector polar(double a, double r) { return Vector(cos(r), sin(r)) * a; } #define equals(a, b) (fabs((a) - (b)) < EPS) #define equalsv(a, b) (equals(real(a), real(b)) && equals(imag(a), imag(b))) #define curr(P, i) P[(i) % P.size()] #define next(P, i) P[(i + 1) % P.size()] #define prev(P, i) P[(i - 1 + P.size()) % P.size()] int ccw(Point p0, Point p1, Point p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > EPS) return COUNTER_CLOCKWISE; if (cross(a, b) < -EPS) return CLOCKWISE; if (dot(a, b) < EPS) return ONLINE_BACK; if (norm(a) < norm(b)) return ONLINE_FRONT; return ON_SEGMENT; } // inputer----------------------------------------------------------------- void inputPoint(Point &p) { double x, y; scanf("%lf%lf", &x, &y); p = Point(x, y); } void inputSegment(Segment &s) { inputPoint(s.p1); inputPoint(s.p2); } void inputLine(Line &l) { inputPoint(l.p1); inputPoint(l.p2); } void inputCircle(Circle &c) { inputPoint(c.p); scanf("%lf", &c.r); } void inputPolygon(Polygon &g, int n) { g.resize(n); for (int i = 0; i < n; i++) inputPoint(g[i]); } // orthogonal-------------------------------------------------------------- bool orthogonal(Vector a, Vector b) { return equals(dot(a, b), 0.0); } bool orthogonal(Point a1, Point a2, Point b1, Point b2) { return orthogonal(a1 - a2, b1 - b2); } bool orthogonal(Segment s1, Segment s2) { return orthogonal(s1.p1 - s1.p2, s2.p1 - s2.p2); } bool orthogonal(Line l1, Line l2) { return orthogonal(l1.p1 - l1.p2, l2.p1 - l2.p2); } // parallel---------------------------------------------------------------- bool parallel(Vector a, Vector b) { return equals(cross(a, b), 0.0); } bool parallel(Point a1, Point a2, Point b1, Point b2) { return parallel(a1 - a2, b1 - b2); } bool parallel(Segment s1, Segment s2) { return parallel(s1.p1 - s1.p2, s2.p1 - s2.p2); } bool parallel(Line l1, Line l2) { return parallel(l1.p1 - l1.p2, l2.p1 - l2.p2); } // project&reflect--------------------------------------------------------- Point project(Line s, Point p) { Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / norm(base); return s.p1 + base * r; } Point reflect(Line s, Point p) { return p + (project(s, p) - p) * 2.0; } // distance---------------------------------------------------------------- double distancePP(Point a, Point b) { return abs(a - b); } double distanceLP(Line l, Point p) { return fabs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1)); } double distanceLL(Line l, Line m) { if (parallel(l, m)) return distanceLP(l, m.p1); return 0.0; } double distanceSP(Segment s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return distancePP(p, s.p1); if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return distancePP(p, s.p2); return distanceLP(Line(s.p1, s.p2), p); } double distanceSS(Segment s1, Segment s2) { if (ccw(s1.p1, s1.p2, s2.p1) * ccw(s1.p1, s1.p2, s2.p2) <= 0 && ccw(s2.p1, s2.p2, s1.p1) * ccw(s2.p1, s2.p2, s1.p2) <= 0) return 0; return min(min(distanceSP(s1, s2.p1), distanceSP(s1, s2.p2)), min(distanceSP(s2, s1.p1), distanceSP(s2, s1.p2))); } double distanceCS(Circle c, Segment s) { return distanceSP(s, c.p) - c.r; } // intersect---------------------------------------------------------------- bool intersectSS(Point p1, Point p2, Point p3, Point p4) { return ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0; } bool intersectSS(Segment s1, Segment s2) { return intersectSS(s1.p1, s1.p2, s2.p1, s2.p2); } bool intersectCL(Circle c, Line l) { return fabs(cross(l.p2 - l.p1, c.p - l.p1) / abs(l.p2 - l.p1)) <= c.r + EPS; } bool intersectCC(Circle c1, Circle c2) { return abs(c1.p - c2.p) <= c1.r + c2.r + EPS; } bool intersectCS(Circle c, Segment s) { return distanceSP(s, c.p) <= c.r + EPS; } bool intersectGC(Polygon g, Circle c) { for (int i = 0; i < g.size(); i++) { Segment s(g[i], next(g, i)); if (intersectCS(c, s)) return true; } return false; } // crossPoint-------------------------------------------------------------- Point crossPoint(Segment s1, Segment s2) { double crs = cross(s1.p2 - s1.p1, s2.p2 - s2.p1); return s2.p1 + (s2.p2 - s2.p1) * cross(s1.p2 - s1.p1, s1.p2 - s2.p1) * (1.0 / crs); } pair<Point, Point> crossPoints(Circle c, Line l) { assert(intersectCL(c, l)); Vector pr = project(l, c.p); Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1); double base = sqrt(square(c.r) - norm(pr - c.p)); return make_pair(pr + e * base, pr - e * base); } pair<Point, Point> crossPoints(Circle c1, Circle c2) { assert(intersectCC(c1, c2)); double d = abs(c1.p - c2.p); double a = acos((square(c1.r) + square(d) - square(c2.r)) / (2 * c1.r * d)); double t = arg(c2.p - c1.p); return make_pair(c1.p + polar(c1.r, t + a), c1.p + polar(c1.r, t - a)); } // polygon------------------------------------------------------------------ double area(Polygon &g) { const int sz = g.size(); double res = cross(g[sz - 1], g[0]); for (int i = 1; i < sz; i++) res += cross(g[i - 1], g[i]); return fabs(res) * 0.5; } bool convex(Polygon &g) { for (int i = 0; i < g.size(); i++) { if (ccw(prev(g, i), curr(g, i), next(g, i)) == CLOCKWISE) return false; } return true; } // contains------------------------------------------------------------------- int contains(Circle c, Point p) { double d = distancePP(c.p, p); if (equals(d, c.r)) return ON; return d < c.r ? IN : OUT; } int contains(Polygon g, Point p) { int n = g.size(); bool x = false; for (int i = 0; i < n; i++) { Point a = g[i] - p, b = next(g, i) - p; if (abs(cross(a, b)) < EPS && dot(a, b) < EPS) return ON; if (imag(a) > imag(b)) swap(a, b); if (imag(a) < EPS && EPS < imag(b) && cross(a, b) > EPS) x = !x; } return x ? IN : OUT; } int contains(Circle c, Polygon g) { for (int i = 0; i < g.size(); i++) { if (!contains(c, g[i])) return OUT; } return IN; } int contains(Polygon g, Circle c) { if (contains(g, c.p) != IN) return OUT; for (int i = 0; i < g.size(); i++) { if (distanceCS(c, Segment(g[i], next(g, i))) < -EPS) return OUT; } return IN; } // end--------------------------------------------------------------------- int main() { int n; int c = 0; while (cin >> n, n) { c++; Polygon g(n); for (int i = 0; i < n; i++) inputPoint(g[i]); printf("%d %.1lf\n", c, area(g)); } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
2,431
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; // ------ Classes ------ // class Point { public: long double px, py; Point() : px(0), py(0){}; Point(long double px_, long double py_) : px(px_), py(py_){}; friend bool operator==(const Point &p1, const Point &p2) { return p1.px == p2.px && p1.py == p2.py; } friend bool operator!=(const Point &p1, const Point &p2) { return p1.px != p2.px || p1.py != p2.py; } friend bool operator<(const Point &p1, const Point &p2) { return p1.px < p2.px ? true : (p1.px == p2.px && p1.py < p2.py); } friend bool operator>(const Point &p1, const Point &p2) { return p1.px > p2.px ? true : (p1.px == p2.px && p1.py > p2.py); } friend bool operator<=(const Point &p1, const Point &p2) { return !(p1 > p2); } friend bool operator>=(const Point &p1, const Point &p2) { return !(p1 < p2); } friend Point operator+(const Point &p1, const Point &p2) { return Point(p1.px + p2.px, p1.py + p2.py); } friend Point operator-(const Point &p1, const Point &p2) { return Point(p1.px - p2.px, p1.py - p2.py); } friend Point operator*(const Point &p1, long double d) { return Point(p1.px * d, p1.py + d); } friend Point operator*(long double d, const Point &p1) { return p1 * d; } friend Point operator/(const Point &p1, long double d) { return Point(p1.px / d, p1.py / d); } Point &operator+=(const Point &p1) { px += p1.px; py += p1.py; return *this; } Point &operator-=(const Point &p1) { px -= p1.px; py -= p1.py; return *this; } Point &operator*=(long double d) { px *= d; py *= d; return *this; } Point &operator/=(long double d) { px /= d; py /= d; return *this; } }; // ------ Functions ------ // long double norm(const Point &a) { return a.px * a.px + a.py * a.py; } long double abs(const Point &a) { return sqrtl(norm(a)); } long double dot(const Point &a, const Point &b) { return a.px * b.px + a.py * b.py; } long double crs(const Point &a, const Point &b) { return a.px * b.py - a.py * b.px; } long double area(vector<Point> v) { long double ret = 0.0L; for (int i = 0; i < v.size(); i++) ret += crs(v[i], v[(i + 1) % v.size()]); return ret / 2; } // ------ Main ------ // int n, c; vector<Point> v; int main() { while (scanf("%d", &n), n) { v.resize(n); for (int i = 0; i < n; i++) cin >> v[i].px >> v[i].py; printf("%d %.1Lf\n", ++c, area(v)); } }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; // ------ Classes ------ // class Point { public: long double px, py; Point() : px(0), py(0){}; Point(long double px_, long double py_) : px(px_), py(py_){}; friend bool operator==(const Point &p1, const Point &p2) { return p1.px == p2.px && p1.py == p2.py; } friend bool operator!=(const Point &p1, const Point &p2) { return p1.px != p2.px || p1.py != p2.py; } friend bool operator<(const Point &p1, const Point &p2) { return p1.px < p2.px ? true : (p1.px == p2.px && p1.py < p2.py); } friend bool operator>(const Point &p1, const Point &p2) { return p1.px > p2.px ? true : (p1.px == p2.px && p1.py > p2.py); } friend bool operator<=(const Point &p1, const Point &p2) { return !(p1 > p2); } friend bool operator>=(const Point &p1, const Point &p2) { return !(p1 < p2); } friend Point operator+(const Point &p1, const Point &p2) { return Point(p1.px + p2.px, p1.py + p2.py); } friend Point operator-(const Point &p1, const Point &p2) { return Point(p1.px - p2.px, p1.py - p2.py); } friend Point operator*(const Point &p1, long double d) { return Point(p1.px * d, p1.py + d); } friend Point operator*(long double d, const Point &p1) { return p1 * d; } friend Point operator/(const Point &p1, long double d) { return Point(p1.px / d, p1.py / d); } Point &operator+=(const Point &p1) { px += p1.px; py += p1.py; return *this; } Point &operator-=(const Point &p1) { px -= p1.px; py -= p1.py; return *this; } Point &operator*=(long double d) { px *= d; py *= d; return *this; } Point &operator/=(long double d) { px /= d; py /= d; return *this; } }; // ------ Functions ------ // long double norm(const Point &a) { return a.px * a.px + a.py * a.py; } long double abs(const Point &a) { return sqrtl(norm(a)); } long double dot(const Point &a, const Point &b) { return a.px * b.px + a.py * b.py; } long double crs(const Point &a, const Point &b) { return a.px * b.py - a.py * b.px; } long double area(vector<Point> v) { long double ret = 0.0L; for (int i = 0; i < v.size(); i++) ret += crs(v[i], v[(i + 1) % v.size()]); return ret / 2; } // ------ Main ------ // int n, c; vector<Point> v; int main() { while (scanf("%d", &n), n) { v.resize(n); for (int i = 0; i < n; i++) cin >> v[i].px >> v[i].py; printf("%d %.1Lf\n", ++c, abs(area(v))); } }
[["+", 0, 1, 0, 2, 3, 4, 0, 2, 63, 22], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 24], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 25]]
1
804
#include <algorithm> #include <iostream> #include <vector> using namespace std; class Point { public: double x, y; Point(int x, int y) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } }; typedef vector<Point> Polygon; int main() { int cnt = 0; while (1) { int i, j, n; double x, y, s = 0; Polygon p; cin >> n; cnt++; if (n == 0) break; for (i = 0; i < n; i++) { cin >> x >> y; p.push_back(Point(x, y)); } for (i = 1; i < n - 1; i++) { s -= ((p[i] - p[0]).x * (p[i + 1] - p[0]).y - (p[i] - p[0]).y * (p[i + 1] - p[0]).x) / 2.0; } // cout << cnt << " " << s << endl; printf("%d %.1f", cnt, s); } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; class Point { public: double x, y; Point(int x, int y) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } }; typedef vector<Point> Polygon; int main() { int cnt = 0; while (1) { int i, j, n; double x, y, s = 0; Polygon p; cin >> n; cnt++; if (n == 0) break; for (i = 0; i < n; i++) { cin >> x >> y; p.push_back(Point(x, y)); } for (i = 1; i < n - 1; i++) { s -= ((p[i] - p[0]).x * (p[i + 1] - p[0]).y - (p[i] - p[0]).y * (p[i + 1] - p[0]).x) / 2.0; } // cout << cnt << " " << s << endl; printf("%d %.1f\n", cnt, s); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
281
#include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; typedef complex<int> xy_t; int main() { int n, k = 1; double x, y, sum = 0.0; queue<double> answer; while (cin >> n && n > 0) { vector<xy_t> p(n); for (int i = 0; i < n; ++i) { cin >> x >> y; p[i] = xy_t(x, y); } for (int i = 0; i + 2 < n; ++i) { xy_t a = p[0], b = p[i + 1], c = p[i + 2]; sum += ((conj(b - a) * (c - a)).imag()) / 2.0; } answer.push(abs(sum)); } while (!answer.empty()) { printf("%d %.1f\n", k, answer.front()); answer.pop(); ++k; } }
#include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; typedef complex<int> xy_t; int main() { int n, k = 1; double x, y, sum = 0.0; queue<double> answer; while (cin >> n && n > 0) { vector<xy_t> p(n); for (int i = 0; i < n; ++i) { cin >> x >> y; p[i] = xy_t(x, y); } for (int i = 0; i + 2 < n; ++i) { xy_t a = p[0], b = p[i + 1], c = p[i + 2]; sum += ((conj(b - a) * (c - a)).imag()) / 2.0; } answer.push(abs(sum)); sum = 0.0; } while (!answer.empty()) { printf("%d %.1f\n", k, answer.front()); answer.pop(); ++k; } }
[["+", 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
221
#include <cmath> #include <cstdio> #include <iostream> using namespace std; int n; int x[51], y[51]; int main() { int caseId = 0; while (cin >> n) { if (n == 0) break; caseId++; int i; for (i = 0; i < n; i++) cin >> x[i] >> y[i]; x[n] = x[0]; y[n] = y[0]; int area = 0; for (i = 0; i < n; i++) { area += x[i] * y[i + 1] - y[i] * x[i + 1]; } printf("%d %.1f\n", n, abs(area) * 0.5); } return 0; }
#include <cmath> #include <cstdio> #include <iostream> using namespace std; int n; int x[51], y[51]; int main() { int caseId = 0; while (cin >> n) { if (n == 0) break; caseId++; int i; for (i = 0; i < n; i++) cin >> x[i] >> y[i]; x[n] = x[0]; y[n] = y[0]; int area = 0; for (i = 0; i < n; i++) { area += x[i] * y[i + 1] - y[i] * x[i + 1]; } printf("%d %.1f\n", caseId, abs(area) * 0.5); } return 0; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
168
#include <bits/stdc++.h> using namespace std; using db = long double; using ll = long long; using vi = vector<int>; #define op operator #define pb push_back struct poi { db x, y; poi op - (poi p) { return {x - p.x, y - p.y}; } db cross(poi p) { return x * p.y - y * p.x; } } p[53]; int main() { cout << fixed << setprecision(1); ios ::sync_with_stdio(0); int _ = 0; for (int n; cin >> n && n;) { for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y; p[n] = p[0]; db ans = 0; for (int i = 0; i < n; i++) ans += p[i].cross(p[i + 1]); cout << _++ << ' ' << -ans / 2 << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using db = long double; using ll = long long; using vi = vector<int>; #define op operator #define pb push_back struct poi { db x, y; poi op - (poi p) { return {x - p.x, y - p.y}; } db cross(poi p) { return x * p.y - y * p.x; } } p[53]; int main() { cout << fixed << setprecision(1); ios ::sync_with_stdio(0); int _ = 0; for (int n; cin >> n && n;) { for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y; p[n] = p[0]; db ans = 0; for (int i = 0; i < n; i++) ans += p[i].cross(p[i + 1]); cout << ++_ << ' ' << -ans / 2 << '\n'; } return 0; }
[["-", 31, 16, 31, 16, 31, 16, 12, 27, 28, 22], ["+", 31, 16, 31, 16, 31, 16, 12, 27, 28, 22]]
1
229
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <string> #include <utility> #include <vector> using namespace std; typedef pair<int, int> PII; typedef long long ll; int main() { int n, datacase = 1; while (true) { cin >> n; if (!n) break; double area = 0, sx, sy, x1, y1, x2, y2; cin >> x1 >> y1; sx = x1, sy = y1; for (int i = 0; i < n - 1; i++) { cin >> x2 >> y2; area += (y2 + y1) * (x2 - x1) / 2; x1 = x2; y1 = y2; } x2 = sx; y2 = sy; area += (y2 - y1) * (x2 - x1) / 2; printf("%d %f\n", datacase, abs(area)); datacase++; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <string> #include <utility> #include <vector> using namespace std; typedef pair<int, int> PII; typedef long long ll; int main() { int n, datacase = 1; while (true) { cin >> n; if (!n) break; double area = 0, sx, sy, x1, y1, x2, y2; cin >> x1 >> y1; sx = x1, sy = y1; for (int i = 0; i < n - 1; i++) { cin >> x2 >> y2; area += (y2 + y1) * (x2 - x1) / 2; x1 = x2; y1 = y2; } x2 = sx; y2 = sy; area += (y2 + y1) * (x2 - x1) / 2; printf("%d %.1f\n", datacase, abs(area)); datacase++; } return 0; }
[["-", 12, 16, 31, 16, 31, 23, 0, 16, 17, 33], ["+", 12, 16, 31, 16, 31, 23, 0, 16, 17, 72], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
188
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; typedef complex<double> Point; const double EPS = 1e-8; typedef vector<Point> Polygon; // 反時計回りを仮定 double cross(Point a, Point b) { return imag(conj(a) * b); } Point curr(const Polygon &a, int x) { return a[x]; } Point next(const Polygon &a, int x) { return a[(x + 1) % a.size()]; } double area(const Polygon &P) { double A = 0; for (int i = 0; i < P.size(); i++) { A += cross(curr(P, i), next(P, i)); } return abs(A) / 2.0; } int main() { int n; int casenum = 0; while (cin >> n && n) { vector<Point> ps(n); REP(i, n) { double x, y; cin >> x >> y; ps[i] = Point(x, y); } printf("%d %.1d\n", ++casenum, area(ps)); } return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; typedef complex<double> Point; const double EPS = 1e-8; typedef vector<Point> Polygon; // 反時計回りを仮定 double cross(Point a, Point b) { return imag(conj(a) * b); } Point curr(const Polygon &a, int x) { return a[x]; } Point next(const Polygon &a, int x) { return a[(x + 1) % a.size()]; } double area(const Polygon &P) { double A = 0; for (int i = 0; i < P.size(); i++) { A += cross(curr(P, i), next(P, i)); } return abs(A) / 2.0; } int main() { int n; int casenum = 0; while (cin >> n && n) { vector<Point> ps(n); REP(i, n) { double x, y; cin >> x >> y; ps[i] = Point(x, y); } printf("%d %.1f\n", ++casenum, area(ps)); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
250
#include <iostream> using namespace std; int main() { int n, x[100], y[100], now; while (cin >> n, n) { double ans = 0, pre; for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; for (int i = 1; i < n - 1; i++) { pre = ((y[i + 1] - y[0]) * (x[i] - x[0]) - (x[i + 1] - x[0]) * (y[i] - y[0])); ans += pre; } ans = max(ans, -ans); printf("%d %.1lf\n", now, ans / 2); } return 0; }
#include <iostream> using namespace std; int main() { int n, x[100], y[100], now = 1; while (cin >> n, n) { double ans = 0, pre; for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; for (int i = 1; i < n - 1; i++) { pre = ((y[i + 1] - y[0]) * (x[i] - x[0]) - (x[i + 1] - x[0]) * (y[i] - y[0])); ans += pre; } ans = max(ans, -ans); printf("%d %.1lf\n", now++, ans / 2); } return 0; }
[["+", 0, 14, 8, 9, 0, 43, 49, 50, 0, 32], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 27, 17, 29]]
1
175
#include <iostream> using namespace std; float abso(float a) { if (a < 0) return (-1.0f) * a; else return (1.0f) * a; } int main() { int n; int c = 0; float out = 0; float x[1000] = {0}; float y[1000] = {0}; float o[1000] = {0}; while (cin >> n, n) { for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; } for (int i = 1; i < n - 1; i++) { float x1 = x[i] - x[0]; float x2 = x[i + 1] - x[0]; float y1 = y[i] - y[0]; float y2 = y[i + 1] - y[0]; out += x1 * y2 - x2 * y1; } o[c] = 0.5 * abso(out); c++; for (int i = 0; i < 1000; i++) { x[i] = 0; y[i] = 0; } out = 0; } for (int i = 0; i < c; i++) { printf("%d %.1f\n", i, o[i]); } return 0; }
#include <iostream> using namespace std; float abso(float a) { if (a < 0) return (-1.0f) * a; else return (1.0f) * a; } int main() { int n; int c = 1; float out = 0; float x[1000] = {0}; float y[1000] = {0}; float o[1000] = {0}; while (cin >> n, n) { for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; } for (int i = 1; i < n - 1; i++) { float x1 = x[i] - x[0]; float x2 = x[i + 1] - x[0]; float y1 = y[i] - y[0]; float y2 = y[i + 1] - y[0]; out += x1 * y2 - x2 * y1; } o[c] = 0.5 * abso(out); c++; for (int i = 0; i < 1000; i++) { x[i] = 0; y[i] = 0; } out = 0; } for (int i = 1; i < c; i++) { printf("%d %.1f\n", i, o[i]); } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
289
#include <cstdio> int n, x[50], y[50]; int main() { for (int c = 0;; c++) { float a = 0; scanf("%d", &n); if (!n) break; for (int i = 0; i < n; i++) scanf("%d %d", &x[i], &y[i]); for (int i = 0; i < n; i++) a += 0.5 * (x[i] * y[(i + 1) % n] - y[i] * x[(i + 1) % n]); a *= a > 0 ? 1 : -1; printf("%d %.1f\n", c, a); } }
#include <cstdio> int n, x[50], y[50]; int main() { for (int c = 1;; c++) { float a = 0; scanf("%d", &n); if (!n) break; for (int i = 0; i < n; i++) scanf("%d %d", &x[i], &y[i]); for (int i = 0; i < n; i++) a += 0.5 * (x[i] * y[(i + 1) % n] - y[i] * x[(i + 1) % n]); a *= a > 0 ? 1 : -1; printf("%d %.1f\n", c, a); } }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
163
#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 <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<string> vstring; typedef vector<pint> vpint; typedef stringstream SS; 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; } #define rep(i, n) for (int i = 0; i < (n); ++i) #define repn(i, m, n) for (int i = (m); i < (n); ++i) #define repd(i, n) for (int i = (n)-1; i >= 0; --i) #define repnd(i, m, n) for (int i = (n)-1; i >= (m); --i) #define rep0(i, n) for (i = 0; i < (n); ++i) #define repn0(i, m, n) for (i = (m); i < (n); ++i) #define repd0(i, n) for (i = (n)-1; i >= 0; --i) #define repnd0(i, m, n) for (i = (n)-1; i >= (m); --i) #define repc(i, n) for (int i = 0; i <= (n); ++i) #define repcn(i, m, n) for (int i = (m); i <= (n); ++i) #define repcd(i, n) for (int i = (n); i >= 0; --i) #define repcnd(i, m, n) for (int i = (n); i >= (m); --i) #define repc0(i, n) for (i = 0; i <= (n); ++i) #define repcn0(i, m, n) for (i = (m); i <= (n); ++i) #define repcd0(i, n) for (i = (n); i >= 0; --i) #define repcnd0(i, m, n) for (i = (n); i >= (m); --i) #define all(n) n.begin(), n.end() #define sz(n) ((int)(n).size()) #define IL for (;;) #define MP make_pair #define PB push_back #define X second #define Y first #define p_queue(n) priority_queue<n, vector<n>, greater<n>> #define PUTLINE cout << "LINE:" << __LINE__ << endl; const int INF = 2147483647 / 3; const double EPS = 1e-10; const double PI = acos(-1.0); const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1, 0}; const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1, 0}; int x[100], y[100]; int main() { int n, res, count = 0; IL { cin >> n; if (n == 0) break; rep(i, n) cin >> x[i] >> y[i]; res = 0; rep(i, n - 1) res += (x[i + 1] - x[i]) * (y[i + 1] + y[i]); res += (x[0] - x[n - 1]) * (y[0] + y[n + 1]); res = abs(res); printf("%d %.1f\n", ++count, (double)res / 2); } 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 <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<string> vstring; typedef vector<pint> vpint; typedef stringstream SS; 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; } #define rep(i, n) for (int i = 0; i < (n); ++i) #define repn(i, m, n) for (int i = (m); i < (n); ++i) #define repd(i, n) for (int i = (n)-1; i >= 0; --i) #define repnd(i, m, n) for (int i = (n)-1; i >= (m); --i) #define rep0(i, n) for (i = 0; i < (n); ++i) #define repn0(i, m, n) for (i = (m); i < (n); ++i) #define repd0(i, n) for (i = (n)-1; i >= 0; --i) #define repnd0(i, m, n) for (i = (n)-1; i >= (m); --i) #define repc(i, n) for (int i = 0; i <= (n); ++i) #define repcn(i, m, n) for (int i = (m); i <= (n); ++i) #define repcd(i, n) for (int i = (n); i >= 0; --i) #define repcnd(i, m, n) for (int i = (n); i >= (m); --i) #define repc0(i, n) for (i = 0; i <= (n); ++i) #define repcn0(i, m, n) for (i = (m); i <= (n); ++i) #define repcd0(i, n) for (i = (n); i >= 0; --i) #define repcnd0(i, m, n) for (i = (n); i >= (m); --i) #define all(n) n.begin(), n.end() #define sz(n) ((int)(n).size()) #define IL for (;;) #define MP make_pair #define PB push_back #define X second #define Y first #define p_queue(n) priority_queue<n, vector<n>, greater<n>> #define PUTLINE cout << "LINE:" << __LINE__ << endl; const int INF = 2147483647 / 3; const double EPS = 1e-10; const double PI = acos(-1.0); const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1, 0}; const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1, 0}; int x[100], y[100]; int main() { int n, res, count = 0; IL { cin >> n; if (n == 0) break; rep(i, n) cin >> x[i] >> y[i]; res = 0; rep(i, n - 1) res += (x[i + 1] - x[i]) * (y[i + 1] + y[i]); res += (x[0] - x[n - 1]) * (y[0] + y[n - 1]); res = abs(res); printf("%d %.1f\n", ++count, (double)res / 2); } return 0; }
[["-", 0, 16, 12, 69, 341, 342, 0, 16, 17, 72], ["+", 0, 16, 12, 69, 341, 342, 0, 16, 17, 33]]
1
549
#include <cmath> #include <cstdio> #include <iostream> using namespace std; int main() { int n; double x, y; int cnt = 1; while (cin >> n, n) { double sum = 0; cin >> x >> y; double oldx = x, oldy = y; double fx = x, fy = y; for (int i = 1; i <= n; i++) { if (i == n) { sum += (oldx - fx) * (oldy + fy); break; } cin >> x >> y; sum += (oldx - x) * (oldy + y); oldx = x; oldy = y; } printf("%d %.1f", cnt, fabs(sum / 2)); cnt++; } return 0; }
#include <cmath> #include <cstdio> #include <iostream> using namespace std; int main() { int n; double x, y; int cnt = 1; while (cin >> n, n) { double sum = 0; cin >> x >> y; double oldx = x, oldy = y; double fx = x, fy = y; for (int i = 1; i <= n; i++) { if (i == n) { sum += (oldx - fx) * (oldy + fy); break; } cin >> x >> y; sum += (oldx - x) * (oldy + y); oldx = x; oldy = y; } printf("%d %.1f\n", cnt, fabs(sum / 2)); cnt++; } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
158
#include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <vector> #define pb push_back using namespace std; typedef complex<double> P; typedef vector<P> G; /* 外積 */ double cross(const P &a, const P &b) { return imag(conj(a) * b); } /* 多角形の面積 */ #define curr(g, i) g[i] #define next(g, i) g[(i + 1) % g.size()] double areaG(const G &g) { double A = 0; for (int i = 0; i < g.size(); ++i) A += cross(curr(g, i), next(g, i)); return abs(A / 2.0); } int main() { G g; double x, y; int n; int cnt = 1; while (cin >> n, n) { cout << cnt << endl; for (int i = 0; i < n; i++) { scanf("%lf %lf", &x, &y); g.pb(P(x, y)); } printf("%.8f\n", areaG(g)); g.clear(); cnt++; } }
#include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <vector> #define pb push_back using namespace std; typedef complex<double> P; typedef vector<P> G; /* 外積 */ double cross(const P &a, const P &b) { return imag(conj(a) * b); } /* 多角形の面積 */ #define curr(g, i) g[i] #define next(g, i) g[(i + 1) % g.size()] double areaG(const G &g) { double A = 0; for (int i = 0; i < g.size(); ++i) A += cross(curr(g, i), next(g, i)); return abs(A / 2.0); } int main() { G g; double x, y; int n; int cnt = 1; while (cin >> n, n) { cout << cnt << " "; for (int i = 0; i < n; i++) { scanf("%lf %lf", &x, &y); g.pb(P(x, y)); } printf("%.1f\n", areaG(g)); g.clear(); cnt++; } }
[["-", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22], ["+", 8, 9, 0, 1, 0, 16, 12, 5, 0, 62], ["+", 8, 9, 0, 1, 0, 16, 12, 5, 0, 6], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
236
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <string> #include <vector> #define rep(i, n) for (int(i) = 0; (i) < (int)n; (i)++) #define each(i, o) \ for (__typeof((o).begin()) i = (o).begin(); i != (o).end(); ++i) #define mp make_pair #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define SORT(x) sort((x).begin(), (x).end()) using namespace std; const double EPS = 1e-8; const double INF = 1e12; class Point { public: double y, x; Point() { y = x = 0.0; } Point(double iy, double ix) { y = iy; x = ix; } Point(double theta) { y = sin(theta); x = cos(theta); } Point operator+(const Point &p) const { return Point(y + p.y, x + p.x); } Point operator-(const Point &p) const { return Point(y - p.y, x - p.x); } void operator+=(const Point &p) { y += p.y; x += p.x; } void operator-=(const Point &p) { y -= p.y; x -= p.x; } Point operator*(double a) const { return Point(y * a, x * a); } Point operator/(double a) const { return Point(y / a, x / a); } bool operator<(const Point &p) const { return y != p.y ? y < p.y : x < p.x; } double length() const { return sqrt(x * x + y * y); } double dist(const Point &p) const { return sqrt(pow(y - p.y, 2) + pow(x - p.x, 2)); } double dot(const Point &p) const { return y * p.y + x * p.x; } double cross(const Point &p) const { return y * p.x - x * p.y; } double angle() const { return atan2(y, x); } }; double polyArea(const vector<Point> &vp) { int n = vp.size(); double ret = 0.0; rep(i, n) ret += vp[i].cross(vp[(i + 1) % n]); return abs(ret) * 0.5; } int main(int argc, char const *argv[]) { int n, count = 1; while (true) { cin >> n; if (n == 0) break; vector<Point> vp(n); rep(i, n) cin >> vp[i].x >> vp[i].y; printf("%d %lf\n", ++count, polyArea(vp)); } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <string> #include <vector> #define rep(i, n) for (int(i) = 0; (i) < (int)n; (i)++) #define each(i, o) \ for (__typeof((o).begin()) i = (o).begin(); i != (o).end(); ++i) #define mp make_pair #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define SORT(x) sort((x).begin(), (x).end()) using namespace std; const double EPS = 1e-8; const double INF = 1e12; class Point { public: double y, x; Point() { y = x = 0.0; } Point(double iy, double ix) { y = iy; x = ix; } Point(double theta) { y = sin(theta); x = cos(theta); } Point operator+(const Point &p) const { return Point(y + p.y, x + p.x); } Point operator-(const Point &p) const { return Point(y - p.y, x - p.x); } void operator+=(const Point &p) { y += p.y; x += p.x; } void operator-=(const Point &p) { y -= p.y; x -= p.x; } Point operator*(double a) const { return Point(y * a, x * a); } Point operator/(double a) const { return Point(y / a, x / a); } bool operator<(const Point &p) const { return y != p.y ? y < p.y : x < p.x; } double length() const { return sqrt(x * x + y * y); } double dist(const Point &p) const { return sqrt(pow(y - p.y, 2) + pow(x - p.x, 2)); } double dot(const Point &p) const { return y * p.y + x * p.x; } double cross(const Point &p) const { return y * p.x - x * p.y; } double angle() const { return atan2(y, x); } }; double polyArea(const vector<Point> &vp) { int n = vp.size(); double ret = 0.0; rep(i, n) ret += vp[i].cross(vp[(i + 1) % n]); return abs(ret) * 0.5; } int main(int argc, char const *argv[]) { int n, count = 0; while (true) { cin >> n; if (n == 0) break; vector<Point> vp(n); rep(i, n) cin >> vp[i].x >> vp[i].y; printf("%d %.1lf\n", ++count, polyArea(vp)); } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
579
#include <complex> #include <iostream> #include <vector> using namespace std; typedef complex<long long> P; string s; int p; P A(); P B(); P C(); P A() { P l = B(); while (s[p] == '+' || s[p] == '-') { char c = s[p++]; P r = B(); if (c == '+') l += r; else l -= r; if (abs(l.real()) > 10000 || abs(l.imag()) > 10000) throw -1; } if (abs(l.real()) > 10000 || abs(l.imag()) > 10000) throw -1; return l; } P B() { P l = C(); while (s[p] == '*') { char c = s[p++]; P r = B(); l *= r; if (abs(l.real()) > 10000 || abs(l.imag()) > 10000) throw -1; } if (abs(l.real()) > 10000 || abs(l.imag()) > 10000) throw -1; return l; } P C() { if (s[p] == '(') { p++; P r = A(); p++; return r; } if (s[p] == '-') { p++; return -C(); } if (s[p] == '+') { p++; return C(); } long long a = 0; int fst = 0; while (s[p] >= '0' && s[p] <= '9' || s[p] == 'i') { if (s[p] == 'i') { p++; if (!fst) a = 1; return P(0, a); } else { a = a * 10 + s[p] - '0'; p++; fst = true; } if (abs(a) > 10000) throw -1; } return P(a, 0); } int main() { while (getline(cin, s)) { p = 0; try { P r = A(); if (r.real()) { if (r.imag()) { if (r.imag() == -1) { cout << r.real() << "-i" << endl; } else if (r.imag() == 1) { cout << r.real() << "+i" << endl; } else if (r.imag() < 0) { cout << r.real() << r.imag() << "i" << endl; } else if (r.imag() > 0) { cout << r.real() << "+" << r.imag() << "i" << endl; } } else { cout << r.real() << endl; } } else { if (r.imag()) { if (r.imag() == -1) { cout << "-1i" << endl; } else if (r.imag() == 1) { cout << "1i" << endl; } else { cout << r.imag() << "i" << endl; } } else { cout << 0 << endl; } } } catch (int x) { cout << "overflow" << endl; } } }
#include <complex> #include <iostream> #include <vector> using namespace std; typedef complex<long long> P; string s; int p; P A(); P B(); P C(); P A() { P l = B(); while (s[p] == '+' || s[p] == '-') { char c = s[p++]; P r = B(); if (c == '+') l += r; else l -= r; if (abs(l.real()) > 10000 || abs(l.imag()) > 10000) throw -1; } if (abs(l.real()) > 10000 || abs(l.imag()) > 10000) throw -1; return l; } P B() { P l = C(); while (s[p] == '*') { char c = s[p++]; P r = C(); l *= r; if (abs(l.real()) > 10000 || abs(l.imag()) > 10000) throw -1; } if (abs(l.real()) > 10000 || abs(l.imag()) > 10000) throw -1; return l; } P C() { if (s[p] == '(') { p++; P r = A(); p++; return r; } if (s[p] == '-') { p++; return -C(); } if (s[p] == '+') { p++; return C(); } long long a = 0; int fst = 0; while (s[p] >= '0' && s[p] <= '9' || s[p] == 'i') { if (s[p] == 'i') { p++; if (!fst) a = 1; return P(0, a); } else { a = a * 10 + s[p] - '0'; p++; fst = true; } if (abs(a) > 10000) throw -1; } return P(a, 0); } int main() { while (getline(cin, s)) { p = 0; try { P r = A(); if (r.real()) { if (r.imag()) { if (r.imag() == -1) { cout << r.real() << "-1i" << endl; } else if (r.imag() == 1) { cout << r.real() << "+1i" << endl; } else if (r.imag() < 0) { cout << r.real() << r.imag() << "i" << endl; } else if (r.imag() > 0) { cout << r.real() << "+" << r.imag() << "i" << endl; } } else { cout << r.real() << endl; } } else { if (r.imag()) { if (r.imag() == -1) { cout << "-1i" << endl; } else if (r.imag() == 1) { cout << "1i" << endl; } else { cout << r.imag() << "i" << endl; } } else { cout << 0 << endl; } } } catch (int x) { cout << "overflow" << endl; } } }
[["-", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["+", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
747
#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 pb push_back #define mp make_pair typedef long long ll; typedef pair<int, int> pint; bool m[4][4]; int x[4], y[4]; int dfs(int cnt) { int res = 0; if (cnt > 16) return 1; int x1 = cnt % 4, y1 = cnt / 4; if (m[y1][x1]) return dfs(cnt + 1); rep(i, 4) { if (x1 + x[i] >= 0 && x1 + x[i] < 4 && y1 + y[i] >= 0 && y1 + y[i] < 4 && !m[y1 + y[i]][x1 + x[i]]) { m[y1][x1] = m[y1 + y[i]][x1 + x[i]] = true; res += dfs(cnt + 1); m[y1][x1] = m[y1 + y[i]][x1 + x[i]] = false; } } return res; } int main() { while (cin >> x[0], x[0] < 5) { cin >> y[0] >> x[1] >> y[1] >> x[2] >> y[2] >> x[3] >> y[3]; memset(m, 0, sizeof(m)); int ans = dfs(0); cout << ans << 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 pb push_back #define mp make_pair typedef long long ll; typedef pair<int, int> pint; bool m[4][4]; int x[4], y[4]; int dfs(int cnt) { int res = 0; if (cnt >= 16) return 1; int x1 = cnt % 4, y1 = cnt / 4; if (m[y1][x1]) return dfs(cnt + 1); rep(i, 4) { if (x1 + x[i] >= 0 && x1 + x[i] < 4 && y1 + y[i] >= 0 && y1 + y[i] < 4 && !m[y1 + y[i]][x1 + x[i]]) { m[y1][x1] = m[y1 + y[i]][x1 + x[i]] = true; res += dfs(cnt + 1); m[y1][x1] = m[y1 + y[i]][x1 + x[i]] = false; } } return res; } int main() { while (cin >> x[0], x[0] < 5) { cin >> y[0] >> x[1] >> y[1] >> x[2] >> y[2] >> x[3] >> y[3]; memset(m, 0, sizeof(m)); int ans = dfs(0); cout << ans << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 20]]
1
343
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<String> direc = new ArrayList<String>(); direc.add("RIGHT"); direc.add("LEFT"); int n, m; n = sc.nextInt(); while (n != 0) { int posX = 1; int posY = 1; int nowDirec = 0; m = sc.nextInt(); while (true) { String instruct = sc.next(); if (instruct.equals("STOP")) { break; } if (direc.contains(instruct)) { if (instruct.equals(direc.get(0))) { nowDirec++; nowDirec %= 4; } else { nowDirec--; if (nowDirec == -1) { nowDirec = 3; } } } else { int step = sc.nextInt(); if (instruct.equals("BACKWARD")) { step = -step; } if (nowDirec == 0) { posY += step; if (posY > m) { posY = m; } if (posY < 1) { posY = 1; } } else if (nowDirec == 1) { posX += step; if (posX > n) { posY = n; } if (posX < 1) { posY = 1; } } else if (nowDirec == 2) { posX -= step; if (posY > m) { posY = m; } if (posY < 1) { posY = 1; } } else if (nowDirec == 3) { posY -= step; if (posX > n) { posY = n; } if (posX < 1) { posY = 1; } } } } System.out.println(posX + " " + posY); n = sc.nextInt(); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<String> direc = new ArrayList<String>(); direc.add("RIGHT"); direc.add("LEFT"); int n, m; n = sc.nextInt(); while (n != 0) { int posX = 1; int posY = 1; int nowDirec = 0; m = sc.nextInt(); while (true) { String instruct = sc.next(); if (instruct.equals("STOP")) { break; } if (direc.contains(instruct)) { if (instruct.equals(direc.get(0))) { nowDirec++; nowDirec %= 4; } else { nowDirec--; if (nowDirec == -1) { nowDirec = 3; } } } else { int step = sc.nextInt(); if (instruct.equals("BACKWARD")) { step = -step; } if (nowDirec == 0) { posY += step; if (posY > m) { posY = m; } if (posY < 1) { posY = 1; } } else if (nowDirec == 1) { posX += step; if (posX > n) { posX = n; } if (posX < 1) { posX = 1; } } else if (nowDirec == 2) { posY -= step; if (posY > m) { posY = m; } if (posY < 1) { posY = 1; } } else if (nowDirec == 3) { posX -= step; if (posX > n) { posX = n; } if (posX < 1) { posX = 1; } } } } System.out.println(posX + " " + posY); n = sc.nextInt(); } } }
[["-", 0, 57, 64, 196, 0, 1, 0, 11, 31, 22], ["+", 0, 57, 64, 196, 0, 1, 0, 11, 31, 22], ["-", 75, 57, 64, 196, 0, 1, 0, 11, 31, 22], ["+", 75, 57, 64, 196, 0, 1, 0, 11, 31, 22]]
3
394
import java.util.*; public class Main { int xpos; int ypos; int w; int h; private boolean checkUpper(int pos, String state) { if (state.equals("y")) { if (pos > h) { return false; } else { return true; } } else { if (pos > w) { return false; } else { return true; } } } private boolean checkLower(int pos, String state) { if (state.equals("y")) { if (pos <= 0) { return false; } else { return true; } } else { if (pos <= 0) { return false; } else { return true; } } } private void operate(int dis, int dir) { switch (dir) { case 0: if (!checkUpper(ypos + dis, "y")) { ypos = h; } else { ypos += dis; } break; case 1: if (!checkUpper(xpos + dis, "x")) { xpos = w; } else { xpos += dis; } break; case 2: if (!checkLower(ypos - dis, "y")) { ypos = h; } else { ypos -= dis; } break; case 3: if (!checkLower(xpos - dis, "x")) { xpos = w; } else { xpos -= dis; } break; } } public void doIt() { Scanner sc = new Scanner(System.in); while (true) { w = sc.nextInt(); h = sc.nextInt(); if (w == 0 && h == 0) break; int dir = 0; xpos = 1; ypos = 1; int[][] map = new int[w + 1][h + 1]; while (true) { String str = sc.next(); if (str.equals("STOP")) { break; } else if (str.equals("RIGHT")) { dir = (dir + 1) % 4; } else if (str.equals("LEFT")) { dir = (dir + 3) % 4; } else if (str.equals("FORWARD")) { int dis = sc.nextInt(); operate(dis, dir); } else if (str.equals("BACKWARD")) { int dis = sc.nextInt(); operate(dis, (dir + 2) % 4); } } System.out.println(xpos + " " + ypos); } } public static void main(String[] args) { Main obj = new Main(); obj.doIt(); } }
import java.util.*; public class Main { int xpos; int ypos; int w; int h; private boolean checkUpper(int pos, String state) { if (state.equals("y")) { if (pos > h) { return false; } else { return true; } } else { if (pos > w) { return false; } else { return true; } } } private boolean checkLower(int pos, String state) { if (state.equals("y")) { if (pos <= 0) { return false; } else { return true; } } else { if (pos <= 0) { return false; } else { return true; } } } private void operate(int dis, int dir) { switch (dir) { case 0: if (!checkUpper(ypos + dis, "y")) { ypos = h; } else { ypos += dis; } break; case 1: if (!checkUpper(xpos + dis, "x")) { xpos = w; } else { xpos += dis; } break; case 2: if (!checkLower(ypos - dis, "y")) { ypos = 1; } else { ypos -= dis; } break; case 3: if (!checkLower(xpos - dis, "x")) { xpos = 1; } else { xpos -= dis; } break; } } public void doIt() { Scanner sc = new Scanner(System.in); while (true) { w = sc.nextInt(); h = sc.nextInt(); if (w == 0 && h == 0) break; int dir = 0; xpos = 1; ypos = 1; int[][] map = new int[w + 1][h + 1]; while (true) { String str = sc.next(); if (str.equals("STOP")) { break; } else if (str.equals("RIGHT")) { dir = (dir + 1) % 4; } else if (str.equals("LEFT")) { dir = (dir + 3) % 4; } else if (str.equals("FORWARD")) { int dis = sc.nextInt(); operate(dis, dir); } else if (str.equals("BACKWARD")) { int dis = sc.nextInt(); operate(dis, (dir + 2) % 4); } } System.out.println(xpos + " " + ypos); } } public static void main(String[] args) { Main obj = new Main(); obj.doIt(); } }
[["-", 0, 57, 64, 196, 0, 1, 0, 11, 12, 22], ["+", 0, 57, 64, 196, 0, 1, 0, 11, 12, 499]]
3
564
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); for (;;) { int w = in.nextInt(); int h = in.nextInt(); if ((w | h) == 0) return; String state = "U"; int nh = 1, nw = 1; for (;;) { String s = in.next(); if (s.equals("STOP")) break; if (s.equals("RIGHT")) { if (state.equals("U")) state = "R"; else if (state.equals("R")) state = "D"; else if (state.equals("D")) state = "L"; else if (state.equals("L")) state = "U"; } else if (s.equals("LEFT")) { if (state.equals("U")) state = "L"; else if (state.equals("L")) state = "D"; else if (state.equals("D")) state = "R"; else if (state.equals("R")) state = "U"; } else { int go = in.nextInt(); if (s.equals("FORWARD")) { if (state.equals("U")) while (nh <= h && go-- > 0) nh++; else if (state.equals("L")) while (nw > 1 && go-- > 0) nw--; else if (state.equals("D")) while (nh > 1 && go-- > 0) nh--; else if (state.equals("R")) while (nw <= w && go-- > 0) nw++; } else if (s.equals("BACKWARD")) { if (state.equals("U")) while (nh > 1 && go-- > 0) nh--; else if (state.equals("L")) while (nw <= w && go-- > 0) nw++; else if (state.equals("D")) while (nh <= h && go-- > 0) nh++; else if (state.equals("R")) while (nw > 1 && go-- > 0) nw--; } } } System.out.println(nw + " " + nh); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); for (;;) { int w = in.nextInt(); int h = in.nextInt(); if ((w | h) == 0) return; String state = "U"; int nh = 1, nw = 1; for (;;) { String s = in.next(); if (s.equals("STOP")) break; if (s.equals("RIGHT")) { if (state.equals("U")) state = "R"; else if (state.equals("R")) state = "D"; else if (state.equals("D")) state = "L"; else if (state.equals("L")) state = "U"; } else if (s.equals("LEFT")) { if (state.equals("U")) state = "L"; else if (state.equals("L")) state = "D"; else if (state.equals("D")) state = "R"; else if (state.equals("R")) state = "U"; } else { int go = in.nextInt(); if (s.equals("FORWARD")) { if (state.equals("U")) while (nh < h && go-- > 0) nh++; else if (state.equals("L")) while (nw > 1 && go-- > 0) nw--; else if (state.equals("D")) while (nh > 1 && go-- > 0) nh--; else if (state.equals("R")) while (nw < w && go-- > 0) nw++; } else if (s.equals("BACKWARD")) { if (state.equals("U")) while (nh > 1 && go-- > 0) nh--; else if (state.equals("L")) while (nw < w && go-- > 0) nw++; else if (state.equals("D")) while (nh < h && go-- > 0) nh++; else if (state.equals("R")) while (nw > 1 && go-- > 0) nw--; } } } System.out.println(nw + " " + nh); } } }
[["-", 64, 52, 15, 15, 0, 16, 31, 16, 17, 19], ["+", 64, 52, 15, 15, 0, 16, 31, 16, 17, 18]]
3
546
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int w = sc.nextInt(); int h = sc.nextInt(); if (w == 0 && h == 0) break; String s; int t; int d = 0; int x = 0; int y = 0; char c; while (true) { s = sc.next(); c = s.charAt(0); if (c == 'S') { System.out.println((x + 1) + " " + (y + 1)); break; } else if (c == 'R') { d++; if (d == 4) d = 0; } else if (c == 'L') { d--; if (d == -1) d = 3; } else if ((c == 'F' && d == 0) || (c == 'B' && d == 2)) { t = sc.nextInt(); y += t; if (y > h - 1) y = h - 1; } else if ((c == 'F' && d == 1) || (c == 'B' && d == 3)) { t = sc.nextInt(); x += t; if (x > w - 1) y = w - 1; } else if ((c == 'F' && d == 2) || (c == 'B' && d == 0)) { t = sc.nextInt(); y -= t; if (y < 0) y = 0; } else if ((c == 'F' && d == 3) || (c == 'B' && d == 1)) { t = sc.nextInt(); x -= t; if (x < 0) x = 0; } } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int w = sc.nextInt(); int h = sc.nextInt(); if (w == 0 && h == 0) break; String s; int t; int d = 0; int x = 0; int y = 0; char c; while (true) { s = sc.next(); c = s.charAt(0); if (c == 'S') { System.out.println((x + 1) + " " + (y + 1)); break; } else if (c == 'R') { d++; if (d == 4) d = 0; } else if (c == 'L') { d--; if (d == -1) d = 3; } else if ((c == 'F' && d == 0) || (c == 'B' && d == 2)) { t = sc.nextInt(); y += t; if (y > h - 1) y = h - 1; } else if ((c == 'F' && d == 1) || (c == 'B' && d == 3)) { t = sc.nextInt(); x += t; if (x > w - 1) x = w - 1; } else if ((c == 'F' && d == 2) || (c == 'B' && d == 0)) { t = sc.nextInt(); y -= t; if (y < 0) y = 0; } else if ((c == 'F' && d == 3) || (c == 'B' && d == 1)) { t = sc.nextInt(); x -= t; if (x < 0) x = 0; } } } } }
[["-", 64, 196, 0, 57, 64, 1, 0, 11, 31, 22], ["+", 64, 196, 0, 57, 64, 1, 0, 11, 31, 22]]
3
392
#include <math.h> #include <stdio.h> #include <stdlib.h> typedef struct st { int s, t; double d; } st; int N, U[100], i, j, k; double P[100][4], R; st D[10005]; int s(const void *a, const void *b) { return ((st *)a)->d - ((st *)b)->d > 0; } int pa(int n) { return U[n] != n ? pa(U[n]) : n; }; int eq(int a, int b) { return pa(a) == pa(b); } int main() { for (; scanf("%d", &N), N;) { for (i = 0; i < N; i++) scanf("%lf%lf%lf%lf", &P[i][0], &P[i][1], &P[i][2], &P[i][3]); for (i = k = 0; i < N - 1; i++) for (j = i + 1; j < N; j++) { double *p = P[i], *q = P[j], d; d = sqrt((p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]) + (p[2] - q[2]) * (p[2] - q[2])); d = d < p[3] + q[3] ? 0 : d - p[3] - q[3]; D[k].s = i; D[k].t = j; D[k].d = d; k++; } qsort(D, k, sizeof(st), s); for (i = 0; i < N; i++) U[i] = i; for (i = R = 0; i < k; i++) { if (!eq(D[i].s, D[i].t)) { U[pa(D[i].s)] = pa(D[i].t); R += D[i].d; } } printf("%lf\n", R); } return 0; }
#include <math.h> #include <stdio.h> #include <stdlib.h> typedef struct st { int s, t; double d; } st; int N, U[100], i, j, k; double P[100][4], R; st D[10005]; int s(const void *a, const void *b) { return ((st *)a)->d - ((st *)b)->d > 0; } int pa(int n) { return U[n] != n ? pa(U[n]) : n; }; int eq(int a, int b) { return pa(a) == pa(b); } int main() { for (; scanf("%d", &N), N;) { for (i = 0; i < N; i++) scanf("%lf%lf%lf%lf", &P[i][0], &P[i][1], &P[i][2], &P[i][3]); for (i = k = 0; i < N - 1; i++) for (j = i + 1; j < N; j++) { double *p = P[i], *q = P[j], d; d = sqrt((p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]) + (p[2] - q[2]) * (p[2] - q[2])); d = d < p[3] + q[3] ? 0 : d - p[3] - q[3]; D[k].s = i; D[k].t = j; D[k].d = d; k++; } qsort(D, k, sizeof(st), s); for (i = 0; i < N; i++) U[i] = i; for (i = R = 0; i < k; i++) { if (!eq(D[i].s, D[i].t)) { U[pa(D[i].s)] = pa(D[i].t); R += D[i].d; } } printf("%.3f\n", R); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
525
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, double> #define ppap pair<pa, int> #define PI 3.14159265358979323846 #define paa pair<int, char> #define mp make_pair #define pb push_back #define EPS (1e-10) int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1}; using namespace std; class pa3 { public: int x, y, z; pa3(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} bool operator<(const pa3 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; return z < p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa3 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; return z > p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa3 &p) const { return x == p.x && y == p.y && z == p.z; } bool operator!=(const pa3 &p) const { return !(x == p.x && y == p.y && z == p.z); } }; class pa4 { public: int x; int y, z, w; pa4(int x = 0, int y = 0, int z = 0, int w = 0) : x(x), y(y), z(z), w(w) {} bool operator<(const pa4 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; if (z != p.z) return z < p.z; return w < p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa4 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; if (z != p.z) return z > p.z; return w > p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa4 &p) const { return x == p.x && y == p.y && z == p.z && w == p.w; } }; class pa2 { public: int x, y; pa2(int x = 0, int y = 0) : x(x), y(y) {} pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); } bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; } bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pa2 &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } bool operator!=(const pa2 &p) const { return !(abs(x - p.x) == 0 && abs(y - p.y) == 0); } }; /* class Point{ public: double x,y; Point(double x=0,double y=0):x(x),y(y) {} Point operator + (Point p) {return Point(x+p.x,y+p.y);} Point operator - (Point p) {return Point(x-p.x,y-p.y);} Point operator * (double a) {return Point(x*a,y*a);} Point operator / (double a) {return Point(x/a,y/a);} double absv() {return sqrt(norm());} double norm() {return x*x+y*y;} bool operator < (const Point &p) const{ return x != p.x ? x<p.x: y<p.y; } bool operator == (const Point &p) const{ return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS; } }; typedef Point Vector; #define pl pair<int,pas> struct Segment{ Point p1,p2; }; double dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; } double cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } bool parareru(Point a,Point b,Point c,Point d){ // if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl; return abs(cross(a-b,d-c))<EPS; } double distance_ls_p(Point a, Point b, Point c) { if ( dot(b-a, c-a) < EPS ) return (c-a).absv(); if ( dot(a-b, c-b) < EPS ) return (c-b).absv(); return abs(cross(b-a, c-a)) / (b-a).absv(); } bool is_intersected_ls(Segment a,Segment b) { if(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return false; if(parareru((a.p2),(a.p1),(a.p1),(b.p2))&&parareru((a.p2),(a.p1),(a.p1),(b.p1))){ // cout<<"sss"<<endl; if(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true; if(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true; if(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true; if(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true; return false; } else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS ); } double segment_dis(Segment a,Segment b){ if(is_intersected_ls(a,b))return 0; double r=distance_ls_p(a.p1, a.p2, b.p1); r=min(r,distance_ls_p(a.p1, a.p2, b.p2)); r=min(r,distance_ls_p(b.p1, b.p2, a.p2)); r=min(r,distance_ls_p(b.p1, b.p2, a.p1)); return r; } Point intersection_ls(Segment a, Segment b) { Point ba = b.p2-b.p1; double d1 = abs(cross(ba, a.p1-b.p1)); double d2 = abs(cross(ba, a.p2-b.p1)); double t = d1 / (d1 + d2); return a.p1 + (a.p2-a.p1) * t; } */ string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } double distans(double x1, double y1, double x2, double y2) { double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); return sqrt(rr); } int mod; int extgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } int pr[1000010]; int inv[1000010]; int beki(int wa, int rr, int warukazu) { if (rr == 0) return 1 % warukazu; if (rr == 1) return wa % warukazu; wa %= warukazu; if (rr % 2 == 1) return ((ll)beki(wa, rr - 1, warukazu) * (ll)wa) % warukazu; ll zx = beki(wa, rr / 2, warukazu); return (zx * zx) % warukazu; } double bekid(double w, int r) { if (r == 0) return 1.0; if (r == 1) return w; if (r % 2) return bekid(w, r - 1) * w; double f = bekid(w, r / 2); return f * f; } int comb(int nn, int rr) { int r = pr[nn] * inv[rr]; r %= mod; r *= inv[nn - rr]; r %= mod; return r; } void gya(int ert) { pr[0] = 1; for (int i = 1; i < ert; i++) { pr[i] = (pr[i - 1] * i) % mod; } for (int i = 0; i < ert; i++) inv[i] = beki(pr[i], mod - 2, mod); } // cin.tie(0); // ios::sync_with_stdio(false); // priority_queue<pa3,vector<pa3>,greater<pa3>> pq; // sort(ve.begin(),ve.end(),greater<int>()); //----------------kokomade tenpure------------ int par[200100], ranks[200100], kosuu[200100]; void shoki(int n) { for (int i = 0; i < n; i++) { par[i] = i; ranks[i] = 0; kosuu[i] = 1; } } int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); int xx = kosuu[x], yy = kosuu[y]; if (x == y) return; if (ranks[x] < ranks[y]) { par[x] = y; kosuu[y] = yy + xx; } else { par[y] = x; if (ranks[x] == ranks[y]) ranks[x] = ranks[x] + 1; kosuu[x] = yy + xx; } return; } double x[100], y[100], z[100], r[100]; vector<pair<double, pa>> ve; double bi(double r) { return r * r; } double dis(int i, int j) { double d = sqrt(bi(x[i] - x[j]) + bi(y[i] - y[j]) + bi(z[i] - z[j])); double r1 = abs(r[i] - r[j]); double r2 = r[i] + r[j]; if (r2 < d) return d - r2; if (r1 < d) return 0; // return r1-d; return 0; } void solve() { int n; cin >> n; if (n == 0) exit(0); for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> z[i] >> r[i]; ve.clear(); shoki(110); for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) { // cout<<i<<" "<<j<<" "<<dis(i,j)<<endl; ve.pb({dis(i, j), {i, j}}); } sort(ve.begin(), ve.end()); double ans = 0; for (auto v : ve) { if (same(v.second.first, v.second.second)) continue; unite(v.second.first, v.second.second); ans += v.first; } printf("%.10lf\n", ans); } signed main() { cin.tie(0); ios::sync_with_stdio(false); while (1) solve(); return 0; }
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, double> #define ppap pair<pa, int> #define PI 3.14159265358979323846 #define paa pair<int, char> #define mp make_pair #define pb push_back #define EPS (1e-10) int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1}; using namespace std; class pa3 { public: int x, y, z; pa3(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} bool operator<(const pa3 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; return z < p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa3 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; return z > p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa3 &p) const { return x == p.x && y == p.y && z == p.z; } bool operator!=(const pa3 &p) const { return !(x == p.x && y == p.y && z == p.z); } }; class pa4 { public: int x; int y, z, w; pa4(int x = 0, int y = 0, int z = 0, int w = 0) : x(x), y(y), z(z), w(w) {} bool operator<(const pa4 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; if (z != p.z) return z < p.z; return w < p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa4 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; if (z != p.z) return z > p.z; return w > p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa4 &p) const { return x == p.x && y == p.y && z == p.z && w == p.w; } }; class pa2 { public: int x, y; pa2(int x = 0, int y = 0) : x(x), y(y) {} pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); } bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; } bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pa2 &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } bool operator!=(const pa2 &p) const { return !(abs(x - p.x) == 0 && abs(y - p.y) == 0); } }; /* class Point{ public: double x,y; Point(double x=0,double y=0):x(x),y(y) {} Point operator + (Point p) {return Point(x+p.x,y+p.y);} Point operator - (Point p) {return Point(x-p.x,y-p.y);} Point operator * (double a) {return Point(x*a,y*a);} Point operator / (double a) {return Point(x/a,y/a);} double absv() {return sqrt(norm());} double norm() {return x*x+y*y;} bool operator < (const Point &p) const{ return x != p.x ? x<p.x: y<p.y; } bool operator == (const Point &p) const{ return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS; } }; typedef Point Vector; #define pl pair<int,pas> struct Segment{ Point p1,p2; }; double dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; } double cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } bool parareru(Point a,Point b,Point c,Point d){ // if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl; return abs(cross(a-b,d-c))<EPS; } double distance_ls_p(Point a, Point b, Point c) { if ( dot(b-a, c-a) < EPS ) return (c-a).absv(); if ( dot(a-b, c-b) < EPS ) return (c-b).absv(); return abs(cross(b-a, c-a)) / (b-a).absv(); } bool is_intersected_ls(Segment a,Segment b) { if(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return false; if(parareru((a.p2),(a.p1),(a.p1),(b.p2))&&parareru((a.p2),(a.p1),(a.p1),(b.p1))){ // cout<<"sss"<<endl; if(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true; if(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true; if(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true; if(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true; return false; } else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS ); } double segment_dis(Segment a,Segment b){ if(is_intersected_ls(a,b))return 0; double r=distance_ls_p(a.p1, a.p2, b.p1); r=min(r,distance_ls_p(a.p1, a.p2, b.p2)); r=min(r,distance_ls_p(b.p1, b.p2, a.p2)); r=min(r,distance_ls_p(b.p1, b.p2, a.p1)); return r; } Point intersection_ls(Segment a, Segment b) { Point ba = b.p2-b.p1; double d1 = abs(cross(ba, a.p1-b.p1)); double d2 = abs(cross(ba, a.p2-b.p1)); double t = d1 / (d1 + d2); return a.p1 + (a.p2-a.p1) * t; } */ string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } double distans(double x1, double y1, double x2, double y2) { double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); return sqrt(rr); } int mod; int extgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } int pr[1000010]; int inv[1000010]; int beki(int wa, int rr, int warukazu) { if (rr == 0) return 1 % warukazu; if (rr == 1) return wa % warukazu; wa %= warukazu; if (rr % 2 == 1) return ((ll)beki(wa, rr - 1, warukazu) * (ll)wa) % warukazu; ll zx = beki(wa, rr / 2, warukazu); return (zx * zx) % warukazu; } double bekid(double w, int r) { if (r == 0) return 1.0; if (r == 1) return w; if (r % 2) return bekid(w, r - 1) * w; double f = bekid(w, r / 2); return f * f; } int comb(int nn, int rr) { int r = pr[nn] * inv[rr]; r %= mod; r *= inv[nn - rr]; r %= mod; return r; } void gya(int ert) { pr[0] = 1; for (int i = 1; i < ert; i++) { pr[i] = (pr[i - 1] * i) % mod; } for (int i = 0; i < ert; i++) inv[i] = beki(pr[i], mod - 2, mod); } // cin.tie(0); // ios::sync_with_stdio(false); // priority_queue<pa3,vector<pa3>,greater<pa3>> pq; // sort(ve.begin(),ve.end(),greater<int>()); //----------------kokomade tenpure------------ int par[200100], ranks[200100], kosuu[200100]; void shoki(int n) { for (int i = 0; i < n; i++) { par[i] = i; ranks[i] = 0; kosuu[i] = 1; } } int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); int xx = kosuu[x], yy = kosuu[y]; if (x == y) return; if (ranks[x] < ranks[y]) { par[x] = y; kosuu[y] = yy + xx; } else { par[y] = x; if (ranks[x] == ranks[y]) ranks[x] = ranks[x] + 1; kosuu[x] = yy + xx; } return; } double x[100], y[100], z[100], r[100]; vector<pair<double, pa>> ve; double bi(double r) { return r * r; } double dis(int i, int j) { double d = sqrt(bi(x[i] - x[j]) + bi(y[i] - y[j]) + bi(z[i] - z[j])); double r1 = abs(r[i] - r[j]); double r2 = r[i] + r[j]; if (r2 < d) return d - r2; if (r1 < d) return 0; // return r1-d; return 0; } void solve() { int n; cin >> n; if (n == 0) exit(0); for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> z[i] >> r[i]; ve.clear(); shoki(110); for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) { // cout<<i<<" "<<j<<" "<<dis(i,j)<<endl; ve.pb({dis(i, j), {i, j}}); } sort(ve.begin(), ve.end()); double ans = 0; for (auto v : ve) { if (same(v.second.first, v.second.second)) continue; unite(v.second.first, v.second.second); ans += v.first; } printf("%.3lf\n", ans); } signed main() { cin.tie(0); ios::sync_with_stdio(false); while (1) solve(); return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
1,873
#include <algorithm> #include <iostream> #include <math.h> #include <vector> using namespace std; struct Edge { int from, to; double cost; Edge() {} Edge(int from_, int to_, double cost_) : from(from_), to(to_), cost(cost_) {} bool operator<(const Edge &rhs) const { return cost < rhs.cost; } }; struct UnionFind { vector<int> data; UnionFind(int v) : data(v, -1){}; int root(int x) { return data[x] < 0 ? x : (data[x] = root(data[x])); } bool same(int x, int y) { return root(x) == root(y); } void merge(int x, int y) { x = root(x), y = root(y); if (x != y) { if (size(y) > size(x)) swap(x, y); data[x] += data[y]; data[y] = x; } } int size(int x) { return -data[root(x)]; } }; double kruskal(const int v, vector<Edge> es) { sort(es.begin(), es.end()); double ans = 0; UnionFind uf(v); for (int i = 0; i < es.size(); ++i) { if (uf.same(es[i].from, es[i].to)) continue; uf.merge(es[i].from, es[i].to); ans += es[i].cost; } return ans; } int main() { int n; while (cin >> n, n) { vector<double> x(n), y(n), z(n), r(n); vector<Edge> e; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { double d = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])); d -= r[i] + r[j]; d = max(0.0, d); e.push_back(Edge(i, j, d)); } } } cout.setf(ios::fixed); cout.precision(10); cout << kruskal(n, e) << endl; } return 0; }
#include <algorithm> #include <iostream> #include <math.h> #include <vector> using namespace std; struct Edge { int from, to; double cost; Edge() {} Edge(int from_, int to_, double cost_) : from(from_), to(to_), cost(cost_) {} bool operator<(const Edge &rhs) const { return cost < rhs.cost; } }; struct UnionFind { vector<int> data; UnionFind(int v) : data(v, -1){}; int root(int x) { return data[x] < 0 ? x : (data[x] = root(data[x])); } bool same(int x, int y) { return root(x) == root(y); } void merge(int x, int y) { x = root(x), y = root(y); if (x != y) { if (size(y) > size(x)) swap(x, y); data[x] += data[y]; data[y] = x; } } int size(int x) { return -data[root(x)]; } }; double kruskal(const int v, vector<Edge> es) { sort(es.begin(), es.end()); double ans = 0; UnionFind uf(v); for (int i = 0; i < es.size(); ++i) { if (uf.same(es[i].from, es[i].to)) continue; uf.merge(es[i].from, es[i].to); ans += es[i].cost; } return ans; } int main() { int n; while (cin >> n, n) { vector<double> x(n), y(n), z(n), r(n); vector<Edge> e; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (i != j) { double d = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])); d -= r[i] + r[j]; d = max(0.0, d); e.push_back(Edge(i, j, d)); } } } cout.setf(ios::fixed); cout.precision(3); cout << kruskal(n, e) << endl; } return 0; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 31, 22], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
630
#include <bits/stdc++.h> using namespace std; typedef long long int64; class DisjointSet { 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: DisjointSet(int size) { rank.resize(size, 0); p.resize(size, 0); } void build() { for (int i = 0; i < rank.size(); i++) { makeSet(i); } } void makeSet(int x) { p[x] = x, rank[x] = 0; } void Union(int x, int y) { link(findSet(x), findSet(y)); } int findSet(int x) { return (x != p[x] ? p[x] = findSet(p[x]) : p[x]); } }; struct edge { int u, v; double cost; bool operator<(const edge &hoge) const { return cost < hoge.cost; } }; typedef vector<edge> Edges; void add_edge(Edges &info, int u, int v, double cost) { info.push_back((edge){u, v, cost}); } double kruskal(Edges &edges, int V) { sort(edges.begin(), edges.end()); DisjointSet uf_tree(V); uf_tree.build(); double ret = 0.0; for (int i = 0; i < edges.size(); i++) { edge &e = edges[i]; if (uf_tree.findSet(e.u) != uf_tree.findSet(e.v)) { uf_tree.Union(e.u, e.v); ret += e.cost; } } return ret; } double GetDist(double x1, double y1, double z1, double r1, double x2, double y2, double z2, double r2) { double dx = fabs(x2 - x1), dy = fabs(y2 - y1), dz = fabs(z2 - z1); double dist = max(sqrt(dx * dx + dy * dy + dz * dz) - r1 - r2, 0.0); return dist; } int main() { int n; double x[100], y[100], z[100], r[100]; while (cin >> n, n) { for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } Edges edges; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { add_edge(edges, i, j, GetDist(x[i], y[i], z[i], r[i], x[j], y[j], z[j], r[j])); } } cout << fixed << setprecision(5) << kruskal(edges, n) << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long int64; class DisjointSet { 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: DisjointSet(int size) { rank.resize(size, 0); p.resize(size, 0); } void build() { for (int i = 0; i < rank.size(); i++) { makeSet(i); } } void makeSet(int x) { p[x] = x, rank[x] = 0; } void Union(int x, int y) { link(findSet(x), findSet(y)); } int findSet(int x) { return (x != p[x] ? p[x] = findSet(p[x]) : p[x]); } }; struct edge { int u, v; double cost; bool operator<(const edge &hoge) const { return cost < hoge.cost; } }; typedef vector<edge> Edges; void add_edge(Edges &info, int u, int v, double cost) { info.push_back((edge){u, v, cost}); } double kruskal(Edges &edges, int V) { sort(edges.begin(), edges.end()); DisjointSet uf_tree(V); uf_tree.build(); double ret = 0.0; for (int i = 0; i < edges.size(); i++) { edge &e = edges[i]; if (uf_tree.findSet(e.u) != uf_tree.findSet(e.v)) { uf_tree.Union(e.u, e.v); ret += e.cost; } } return ret; } double GetDist(double x1, double y1, double z1, double r1, double x2, double y2, double z2, double r2) { double dx = fabs(x2 - x1), dy = fabs(y2 - y1), dz = fabs(z2 - z1); double dist = max(sqrt(dx * dx + dy * dy + dz * dz) - r1 - r2, 0.0); return dist; } int main() { int n; double x[100], y[100], z[100], r[100]; while (cin >> n, n) { for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } Edges edges; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { add_edge(edges, i, j, GetDist(x[i], y[i], z[i], r[i], x[j], y[j], z[j], r[j])); } } cout << fixed << setprecision(3) << kruskal(edges, n) << endl; } }
[["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
688
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define sz size() #define pb push_back #define mp make_pair #define ALL(X) (X).begin(), (X).end() using namespace std; const double INF = 1000000000.0; const double eps = 1e-8; int root(vector<int> &ufa, int i) { if (ufa[i] != i) ufa[i] = root(ufa, ufa[i]); return ufa[i]; } void join(vector<int> &ufa, int i, int j) { int ri = root(ufa, i); int rj = root(ufa, j); ufa[ri] = rj; } struct e { int s; int t; double cst; }; bool operator<(const e &lhs, const e &rhs) { return lhs.cst < rhs.cst; } int main(void) { cout.precision(3); while (1) { int n; cin >> n; if (!n) break; vector<double> x(n); vector<double> y(n); vector<double> z(n); vector<double> r(n); vector<int> ufa(n); REP(i, n) ufa[i] = i; REP(i, n) cin >> x[i] >> y[i] >> z[i] >> r[i]; vector<vector<double>> dist_big(n, vector<double>(n, INF)); REP(i, n) REP(j, n) { if (i == j) continue; int d = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j]); dist_big[i][j] = dist_big[j][i] = sqrt(d) - (r[i] + r[j]); if (d <= (r[i] + r[j] + eps) * (r[i] + r[j] + eps)) { join(ufa, i, j); } } set<int> sec; REP(i, n) sec.insert(root(ufa, i)); int secn = sec.size(); if (secn > 1) { vector<int> roots; FORIT(i, sec) roots.push_back(*i); vector<vector<double>> dist(secn, vector<double>(secn, INF)); REP(i, n) REP(j, n) { if (ufa[i] == ufa[j]) continue; int di, dj; REP(k, secn) { if (roots[k] == ufa[i]) di = k; if (roots[k] == ufa[j]) dj = k; } dist[di][dj] = min(dist[di][dj], dist_big[i][j]); } vector<e> edge; REP(i, secn) REP(j, i) { e p = {i, j, dist[i][j]}; edge.push_back(p); } sort(ALL(edge)); vector<int> ufa2(secn); REP(i, secn) ufa2[i] = i; double cost = 0.0; REP(i, edge.size()) { e p = edge[i]; if (root(ufa2, p.s) != root(ufa2, p.t)) { join(ufa2, p.s, p.t); cost += p.cst; secn--; if (secn == 1) break; } } cout << setiosflags(ios::fixed) << cost << endl; } else cout << setiosflags(ios::fixed) << 0.0 << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define sz size() #define pb push_back #define mp make_pair #define ALL(X) (X).begin(), (X).end() using namespace std; const double INF = 1000000000.0; const double eps = 1e-8; int root(vector<int> &ufa, int i) { if (ufa[i] != i) ufa[i] = root(ufa, ufa[i]); return ufa[i]; } void join(vector<int> &ufa, int i, int j) { int ri = root(ufa, i); int rj = root(ufa, j); ufa[ri] = rj; } struct e { int s; int t; double cst; }; bool operator<(const e &lhs, const e &rhs) { return lhs.cst < rhs.cst; } int main(void) { cout.precision(3); while (1) { int n; cin >> n; if (!n) break; vector<double> x(n); vector<double> y(n); vector<double> z(n); vector<double> r(n); vector<int> ufa(n); REP(i, n) ufa[i] = i; REP(i, n) cin >> x[i] >> y[i] >> z[i] >> r[i]; vector<vector<double>> dist_big(n, vector<double>(n, INF)); REP(i, n) REP(j, n) { if (i == j) continue; double d = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j]); dist_big[i][j] = dist_big[j][i] = sqrt(d) - (r[i] + r[j]); if (d <= (r[i] + r[j] + eps) * (r[i] + r[j] + eps)) { join(ufa, i, j); } } set<int> sec; REP(i, n) sec.insert(root(ufa, i)); int secn = sec.size(); if (secn > 1) { vector<int> roots; FORIT(i, sec) roots.push_back(*i); vector<vector<double>> dist(secn, vector<double>(secn, INF)); REP(i, n) REP(j, n) { if (ufa[i] == ufa[j]) continue; int di, dj; REP(k, secn) { if (roots[k] == ufa[i]) di = k; if (roots[k] == ufa[j]) dj = k; } dist[di][dj] = min(dist[di][dj], dist_big[i][j]); } vector<e> edge; REP(i, secn) REP(j, i) { e p = {i, j, dist[i][j]}; edge.push_back(p); } sort(ALL(edge)); vector<int> ufa2(secn); REP(i, secn) ufa2[i] = i; double cost = 0.0; REP(i, edge.size()) { e p = edge[i]; if (root(ufa2, p.s) != root(ufa2, p.t)) { join(ufa2, p.s, p.t); cost += p.cst; secn--; if (secn == 1) break; } } cout << setiosflags(ios::fixed) << cost << endl; } else cout << setiosflags(ios::fixed) << 0.0 << endl; } return 0; }
[["-", 8, 9, 0, 14, 8, 9, 0, 43, 39, 40], ["+", 8, 9, 0, 14, 8, 9, 0, 43, 39, 40]]
1
895
#include <bits/stdc++.h> typedef long long LL; #define SORT(c) sort((c).begin(), (c).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) using namespace std; double dd(double a) { return a * a; } int main(void) { for (;;) { int n; cin >> n; if (!n) return 0; vector<double> x, y, z, r; x.resize(n); y.resize(n); z.resize(n); r.resize(n); REP(i, n) cin >> x[i] >> y[i] >> z[i] >> r[i]; vector<vector<double>> dist; dist.resize(n); REP(i, n) dist[i].resize(n); REP(i, n) REP(j, n) dist[i][j] = max(0.0, sqrt(dd(x[i] - x[j]) + dd(y[i] - y[j]) + dd(z[i] - z[j])) - r[i] - r[j]); priority_queue<pair<double, int>> prim; // negc,next prim.push(make_pair(0.0, 0)); set<int> already; double answer = 0.0; while (!prim.empty()) { double negc = prim.top().first; int tmp = prim.top().second; prim.pop(); if (!already.insert(tmp).second) continue; answer -= negc; REP(j, n) prim.push(make_pair(-dist[tmp][j], j)); } cout << fixed << setprecision(10) << answer << endl; } }
#include <bits/stdc++.h> typedef long long LL; #define SORT(c) sort((c).begin(), (c).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) using namespace std; double dd(double a) { return a * a; } int main(void) { for (;;) { int n; cin >> n; if (!n) return 0; vector<double> x, y, z, r; x.resize(n); y.resize(n); z.resize(n); r.resize(n); REP(i, n) cin >> x[i] >> y[i] >> z[i] >> r[i]; vector<vector<double>> dist; dist.resize(n); REP(i, n) dist[i].resize(n); REP(i, n) REP(j, n) dist[i][j] = max(0.0, sqrt(dd(x[i] - x[j]) + dd(y[i] - y[j]) + dd(z[i] - z[j])) - r[i] - r[j]); priority_queue<pair<double, int>> prim; // negc,next prim.push(make_pair(0.0, 0)); set<int> already; double answer = 0.0; while (!prim.empty()) { double negc = prim.top().first; int tmp = prim.top().second; prim.pop(); if (!already.insert(tmp).second) continue; answer -= negc; REP(j, n) prim.push(make_pair(-dist[tmp][j], j)); } cout << fixed << setprecision(3) << answer << endl; } }
[["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
388
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) begin(a), end(a) #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(10) typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> P; typedef complex<double> Point; typedef long long ll; const int INF = 114514810; const int MOD = 1000000007; const double EPS = 1e-8; const double PI = acos(-1.0); struct edge { int from, to; double cost; bool operator<(const edge &e) const { return cost < e.cost; } bool operator>(const edge &e) const { return cost > e.cost; } }; ///*************************************************************************************/// ///*************************************************************************************/// ///*************************************************************************************/// struct a { double x, y, z, r; }; vector<a> v; int par[105]; vector<edge> es; int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[y] = x; } bool same(int x, int y) { return find(x) == find(y); } void init_UFT(int n) { REP(i, n) { par[i] = i; } } int main() { int n; while (cin >> n, n) { v.clear(); REP(i, n) { double x, y, z, r; cin >> x >> y >> z >> r; v.push_back(a{x, y, z, r}); } es.clear(); init_UFT(n); REP(i, n) FOR(j, i + 1, n) { double dif = sqrt(pow((v[i].x - v[j].x), 2) + pow((v[i].y - v[j].y), 2) + pow((v[i].z - v[j].z), 2)) - v[i].r - v[j].r; if (dif > 0) { es.push_back(edge{i, j, dif}); } else { unite(i, j); } } sort(ALL(es)); double sum = 0; REP(i, es.size()) { if (!same(par[es[i].from], par[es[i].to])) { unite(es[i].from, es[i].to); sum += es[i].cost; } } cout << D10 << sum << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) begin(a), end(a) #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(3) typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> P; typedef complex<double> Point; typedef long long ll; const int INF = 114514810; const int MOD = 1000000007; const double EPS = 1e-8; const double PI = acos(-1.0); struct edge { int from, to; double cost; bool operator<(const edge &e) const { return cost < e.cost; } bool operator>(const edge &e) const { return cost > e.cost; } }; ///*************************************************************************************/// ///*************************************************************************************/// ///*************************************************************************************/// struct a { double x, y, z, r; }; vector<a> v; int par[105]; vector<edge> es; int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[y] = x; } bool same(int x, int y) { return find(x) == find(y); } void init_UFT(int n) { REP(i, n) { par[i] = i; } } int main() { int n; while (cin >> n, n) { v.clear(); REP(i, n) { double x, y, z, r; cin >> x >> y >> z >> r; v.push_back(a{x, y, z, r}); } es.clear(); init_UFT(n); REP(i, n) FOR(j, i + 1, n) { double dif = sqrt(pow((v[i].x - v[j].x), 2) + pow((v[i].y - v[j].y), 2) + pow((v[i].z - v[j].z), 2)) - v[i].r - v[j].r; if (dif > 0) { es.push_back(edge{i, j, dif}); } else { unite(i, j); } } sort(ALL(es)); double sum = 0; REP(i, es.size()) { if (!same(par[es[i].from], par[es[i].to])) { unite(es[i].from, es[i].to); sum += es[i].cost; } } cout << D10 << sum << endl; } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59]]
1
614
// AOJ 1127 #include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <vector> using namespace std; int c[100][100]; struct P { double x, y, z, r; double abs() { return sqrt(x * x + y * y + z * z); } }; P operator-(P a, P b) { a.x -= b.x; a.y -= b.y; a.z -= b.z; return a; } int uf[1000]; int get(int x) { return uf[x] = (uf[x] == x ? x : get(uf[x])); } int unite(int x, int y) { uf[get(x)] = get(y); } double calc(P a, P b) { return max((a - b).abs() - a.r - b.r, 0.); } int main() { int n; while (cin >> n && n) { for (int i = 0; i < 1000; i++) uf[i] = i; vector<P> pts(n); for (int i = 0; i < n; i++) { cin >> pts[i].x >> pts[i].y >> pts[i].z >> pts[i].r; } double ans = 0; vector<pair<double, pair<int, int>>> e; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { double dist = calc(pts[i], pts[j]); e.push_back(make_pair(dist, make_pair(i, j))); } } sort(e.begin(), e.end()); for (int i = 0; i < e.size(); i++) { if (get(e[i].second.first) != get(e[i].second.second)) { ans += e[i].first; unite(e[i].second.first, e[i].second.second); } } printf("%.10lf%c", ans, 10); } }
// AOJ 1127 #include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <vector> using namespace std; int c[100][100]; struct P { double x, y, z, r; double abs() { return sqrt(x * x + y * y + z * z); } }; P operator-(P a, P b) { a.x -= b.x; a.y -= b.y; a.z -= b.z; return a; } int uf[1000]; int get(int x) { return uf[x] = (uf[x] == x ? x : get(uf[x])); } int unite(int x, int y) { uf[get(x)] = get(y); } double calc(P a, P b) { return max((a - b).abs() - a.r - b.r, 0.); } int main() { int n; while (cin >> n && n) { for (int i = 0; i < 1000; i++) uf[i] = i; vector<P> pts(n); for (int i = 0; i < n; i++) { cin >> pts[i].x >> pts[i].y >> pts[i].z >> pts[i].r; } double ans = 0; vector<pair<double, pair<int, int>>> e; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { double dist = calc(pts[i], pts[j]); e.push_back(make_pair(dist, make_pair(i, j))); } } sort(e.begin(), e.end()); for (int i = 0; i < e.size(); i++) { if (get(e[i].second.first) != get(e[i].second.second)) { ans += e[i].first; unite(e[i].second.first, e[i].second.second); } } printf("%.3lf%c", ans, 10); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
482
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <iostream> #include <queue> #include <sstream> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; #define all(c) (c).begin(), (c).end() #define loop(i, a, b) for (ll i = a; i < ll(b); i++) #define rep(i, b) loop(i, 0, b) #define each(e, c) for (auto &e : c) #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define lb lower_bound #define ub upper_bound #ifdef DEBUG #define dump(...) \ (cerr << #__VA_ARGS__ << " = " << (DUMP(), __VA_ARGS__).str() << " [" \ << __LINE__ << "]" << endl) struct DUMP : ostringstream { template <class T> DUMP &operator,(const T &t) { if (this->tellp()) *this << ", "; *this << t; return *this; } }; #else #define dump(...) #endif template <class T> ostream &operator<<(ostream &os, vector<T> const &v) { rep(i, v.size()) os << v[i] << (i + 1 == v.size() ? "" : " "); return os; } typedef long double R; typedef tuple<R, R, R> P; typedef pair<P, R> S; R const eps = 1e-10; typedef R Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; bool operator<(const Edge &e, const Edge &f) { return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!! e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vector<Edge> Edges; typedef vector<Edges> Graph; typedef vector<Weight> Array; typedef vector<Array> Matrix; pair<Weight, Edges> minimumSpanningTree(const Graph &g, int r = 0) { int n = g.size(); Edges T; Weight total = 0; vector<bool> vis(n); priority_queue<Edge> q; q.push(Edge(-1, r, 0)); while (!q.empty()) { Edge e = q.top(); q.pop(); if (vis[e.dst]) continue; T.push_back(e); total += e.weight; vis[e.dst] = true; for (auto &f : g[e.dst]) if (!vis[f.dst]) q.push(f); } return make_pair(total, T); } int n; S ss[111]; R d[111][111]; P operator-(const P &p, const P &q) { R x, y, z, a, b, c; tie(x, y, z) = p; tie(a, b, c) = q; return P(x - a, y - b, z - c); } R abs(P p) { R x, y, z; tie(x, y, z) = p; return sqrt(x * x + y * y + z * z); } R dist(S const &c1, S const &c2) { R d = abs(c1.first - c2.first); return max<R>(0, d - (c1.second + c2.second)); } int main() { while (cin >> n && n) { Graph g(n); rep(i, n) { R x, y, z, r; cin >> x >> y >> z >> r; ss[i] = S(P(x, y, z), r); rep(j, i) { R d = dist(ss[i], ss[j]); g[i].eb(i, j, d); g[j].eb(j, i, d); } } printf("%.20lf\n", (double)minimumSpanningTree(g).first); } }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <iostream> #include <queue> #include <sstream> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; #define all(c) (c).begin(), (c).end() #define loop(i, a, b) for (ll i = a; i < ll(b); i++) #define rep(i, b) loop(i, 0, b) #define each(e, c) for (auto &e : c) #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define lb lower_bound #define ub upper_bound #ifdef DEBUG #define dump(...) \ (cerr << #__VA_ARGS__ << " = " << (DUMP(), __VA_ARGS__).str() << " [" \ << __LINE__ << "]" << endl) struct DUMP : ostringstream { template <class T> DUMP &operator,(const T &t) { if (this->tellp()) *this << ", "; *this << t; return *this; } }; #else #define dump(...) #endif template <class T> ostream &operator<<(ostream &os, vector<T> const &v) { rep(i, v.size()) os << v[i] << (i + 1 == v.size() ? "" : " "); return os; } typedef long double R; typedef tuple<R, R, R> P; typedef pair<P, R> S; R const eps = 1e-10; typedef R Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; bool operator<(const Edge &e, const Edge &f) { return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!! e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vector<Edge> Edges; typedef vector<Edges> Graph; typedef vector<Weight> Array; typedef vector<Array> Matrix; pair<Weight, Edges> minimumSpanningTree(const Graph &g, int r = 0) { int n = g.size(); Edges T; Weight total = 0; vector<bool> vis(n); priority_queue<Edge> q; q.push(Edge(-1, r, 0)); while (!q.empty()) { Edge e = q.top(); q.pop(); if (vis[e.dst]) continue; T.push_back(e); total += e.weight; vis[e.dst] = true; for (auto &f : g[e.dst]) if (!vis[f.dst]) q.push(f); } return make_pair(total, T); } int n; S ss[111]; R d[111][111]; P operator-(const P &p, const P &q) { R x, y, z, a, b, c; tie(x, y, z) = p; tie(a, b, c) = q; return P(x - a, y - b, z - c); } R abs(P p) { R x, y, z; tie(x, y, z) = p; return sqrt(x * x + y * y + z * z); } R dist(S const &c1, S const &c2) { R d = abs(c1.first - c2.first); return max<R>(0, d - (c1.second + c2.second)); } int main() { while (cin >> n && n) { Graph g(n); rep(i, n) { R x, y, z, r; cin >> x >> y >> z >> r; ss[i] = S(P(x, y, z), r); rep(j, i) { R d = dist(ss[i], ss[j]); g[i].eb(i, j, d); g[j].eb(j, i, d); } } printf("%.3lf\n", (double)minimumSpanningTree(g).first); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
875
#include <bits/stdc++.h> using namespace std; typedef pair<double, int> P; const double INF = 1e10; int main() { int n; while (cin >> n, n) { vector<double> x(n), y(n), z(n), r(n); for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } vector<vector<double>> dis(n, vector<double>(n, INF)); for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { double tdis = pow(x[i] - x[j], 2.0) + pow(y[i] - y[j], 2.0) + pow(z[i] - z[j], 2.0); tdis = sqrt(tdis) - r[i] - r[j]; if (tdis < 0.0) tdis = 0.0; dis[i][j] = dis[j][i] = tdis; } } priority_queue<P, vector<P>, greater<P>> que; vector<bool> used(n, false); double res = 0.0; for (int i = 0; i < n; i++) { que.push(P(dis[0][i], i)); } used[0] = true; while (que.size()) { P p = que.top(); que.pop(); if (used[p.second]) continue; res += p.first; used[p.second] = true; for (int i = 0; i < n; i++) { que.push(P(dis[p.second][i], i)); } } printf("%.6f\n", res); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<double, int> P; const double INF = 1e10; int main() { int n; while (cin >> n, n) { vector<double> x(n), y(n), z(n), r(n); for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } vector<vector<double>> dis(n, vector<double>(n, INF)); for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { double tdis = pow(x[i] - x[j], 2.0) + pow(y[i] - y[j], 2.0) + pow(z[i] - z[j], 2.0); tdis = sqrt(tdis) - r[i] - r[j]; if (tdis < 0.0) tdis = 0.0; dis[i][j] = dis[j][i] = tdis; } } priority_queue<P, vector<P>, greater<P>> que; vector<bool> used(n, false); double res = 0.0; for (int i = 0; i < n; i++) { que.push(P(dis[0][i], i)); } used[0] = true; while (que.size()) { P p = que.top(); que.pop(); if (used[p.second]) continue; res += p.first; used[p.second] = true; for (int i = 0; i < n; i++) { que.push(P(dis[p.second][i], i)); } } printf("%.3f\n", res); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
426
#include <bits/stdc++.h> using namespace std; #define SQ(a) ((a) * (a)) typedef tuple<double, int, int> Tp; #define at(t, i) get<i>(t) struct UnionFind { vector<int> parent; UnionFind(int n) { parent.resize(n); for (int i = 0; i < n; i++) { parent[i] = i; } } int find(int x) { if (parent[x] == x) return x; return parent[x] = find(parent[x]); } bool same(int x, int y) { return find(x) == find(y); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; parent[x] = y; return true; } }; struct Point { double x, y, z; Point() {} Point(double x, double y, double z) : x(x), y(y), z(z) {} }; struct Circle { Point p; double r; Circle() {} Circle(double x, double y, double z, double r) : p(x, y, z), r(r) {} }; inline double dist(Point &a, Point &b) { return sqrt(SQ(a.x - b.x) + SQ(a.y - b.y) + SQ(a.z - b.z)); } inline double dist(Circle &a, Circle &b) { return max(0.0, dist(a.p, b.p) - a.r - b.r); } int n; Circle cs[100]; int main() { while (scanf("%d", &n), n) { for (int i = 0; i < n; i++) { double x, y, z, r; scanf("%lf %lf %lf %lf", &x, &y, &z, &r); cs[i] = Circle(x, y, z, r); } vector<Tp> es; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; es.push_back(Tp(dist(cs[i], cs[j]), i, j)); } } sort(es.begin(), es.end()); double res = 0.0; UnionFind uf(n); for (int i = 0; i < es.size(); i++) { double cost = at(es[i], 0); int a = at(es[i], 1), b = at(es[i], 2); if (uf.unite(a, b)) res += cost; } printf("%f\n", res); } }
#include <bits/stdc++.h> using namespace std; #define SQ(a) ((a) * (a)) typedef tuple<double, int, int> Tp; #define at(t, i) get<i>(t) struct UnionFind { vector<int> parent; UnionFind(int n) { parent.resize(n); for (int i = 0; i < n; i++) { parent[i] = i; } } int find(int x) { if (parent[x] == x) return x; return parent[x] = find(parent[x]); } bool same(int x, int y) { return find(x) == find(y); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; parent[x] = y; return true; } }; struct Point { double x, y, z; Point() {} Point(double x, double y, double z) : x(x), y(y), z(z) {} }; struct Circle { Point p; double r; Circle() {} Circle(double x, double y, double z, double r) : p(x, y, z), r(r) {} }; inline double dist(Point &a, Point &b) { return sqrt(SQ(a.x - b.x) + SQ(a.y - b.y) + SQ(a.z - b.z)); } inline double dist(Circle &a, Circle &b) { return max(0.0, dist(a.p, b.p) - a.r - b.r); } int n; Circle cs[100]; int main() { while (scanf("%d", &n), n) { for (int i = 0; i < n; i++) { double x, y, z, r; scanf("%lf %lf %lf %lf", &x, &y, &z, &r); cs[i] = Circle(x, y, z, r); } vector<Tp> es; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; es.push_back(Tp(dist(cs[i], cs[j]), i, j)); } } sort(es.begin(), es.end()); double res = 0.0; UnionFind uf(n); for (int i = 0; i < es.size(); i++) { double cost = at(es[i], 0); int a = at(es[i], 1), b = at(es[i], 2); if (uf.unite(a, b)) res += cost; } printf("%.3f\n", res); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
631
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define all(c) (c).begin(), (c).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define pb(e) push_back(e) #define mp(a, b) make_pair(a, b) #define fr first #define sc second const int INF = 100000000; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; typedef pair<int, int> P; typedef long long ll; template <typename T> class UnionFind { int size_; std::vector<T> par; std::vector<T> rank; public: UnionFind(int size_) : size_(size_) { par.resize(size_); rank.resize(size_); for (int i = 0; i < size_; i++) { par[i] = i; rank[i] = 0; } } T find(T x) { return par[x] == x ? x : par[x] = find(par[x]); } void unite(T x, T y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } bool same(T x, T y) { return find(x) == find(y); } }; int n; struct Sphere { double x, y, z, r; }; struct Edge { int from; int to; double cost; bool operator<(const Edge &rth) const { return cost < rth.cost; } }; double pow2(double x) { return x * x; } double dist(Sphere s1, Sphere s2) { double t; t = pow2(s1.x - s2.x); t += pow2(s1.y - s2.y); t += pow2(s1.z - s2.z); t = sqrt(t); t = t - s1.r - s2.r; if (t < 0) t = 0; return t; } void solve() { UnionFind<int> uf(102); vector<Edge> edges; Sphere sphere[102]; rep(i, n) { double x, y, z, r; cin >> x >> y >> z >> r; sphere[i] = {x, y, z, r}; } rep(i, n) rep(j, n) if (i != j) { double d = dist(sphere[i], sphere[j]); edges.push_back({i, j, d}); } sort(all(edges)); double ans = 0; rep(i, edges.size()) { Edge e = edges[i]; if (!uf.same(e.from, e.to)) { ans += e.cost; uf.unite(e.from, e.to); } } printf("%.10f\n", ans); } int main() { while (cin >> n) { if (n == 0) break; solve(); } return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define all(c) (c).begin(), (c).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define pb(e) push_back(e) #define mp(a, b) make_pair(a, b) #define fr first #define sc second const int INF = 100000000; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; typedef pair<int, int> P; typedef long long ll; template <typename T> class UnionFind { int size_; std::vector<T> par; std::vector<T> rank; public: UnionFind(int size_) : size_(size_) { par.resize(size_); rank.resize(size_); for (int i = 0; i < size_; i++) { par[i] = i; rank[i] = 0; } } T find(T x) { return par[x] == x ? x : par[x] = find(par[x]); } void unite(T x, T y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } bool same(T x, T y) { return find(x) == find(y); } }; int n; struct Sphere { double x, y, z, r; }; struct Edge { int from; int to; double cost; bool operator<(const Edge &rth) const { return cost < rth.cost; } }; double pow2(double x) { return x * x; } double dist(Sphere s1, Sphere s2) { double t; t = pow2(s1.x - s2.x); t += pow2(s1.y - s2.y); t += pow2(s1.z - s2.z); t = sqrt(t); t = t - s1.r - s2.r; if (t < 0) t = 0; return t; } void solve() { UnionFind<int> uf(102); vector<Edge> edges; Sphere sphere[102]; rep(i, n) { double x, y, z, r; cin >> x >> y >> z >> r; sphere[i] = {x, y, z, r}; } rep(i, n) rep(j, n) if (i != j) { double d = dist(sphere[i], sphere[j]); edges.push_back({i, j, d}); } sort(all(edges)); double ans = 0; rep(i, edges.size()) { Edge e = edges[i]; if (!uf.same(e.from, e.to)) { ans += e.cost; uf.unite(e.from, e.to); } } printf("%.3f\n", ans); } int main() { while (cin >> n) { if (n == 0) break; solve(); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
726
#include <bits/stdc++.h> using namespace std; #define EPS 0.0001 typedef struct { double x; double y; double z; double r; } Cell; bool eq(double a, double b) { return abs(a - b) < EPS; } double cellLength(Cell a, Cell b) { double l = sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2)) - (a.r + b.r); if (l <= 0) return 0; else return l; } int main(void) { int n; double btw[101][101]; Cell cell[101]; while (cin >> n, n) { for (int i = 0; i < n; i++) { double x, y, z, r; cin >> x >> y >> z >> r; Cell tmp = {x, y, z, r}; cell[i] = tmp; } for (int p = 0; p < n; p++) { for (int q = 0; q < n; q++) { btw[p][q] = cellLength(cell[p], cell[q]); } } // prim double mincost[101]; bool used[101]; for (int i = 0; i < n; i++) { mincost[i] = INT_MAX - 1; used[i] = false; } mincost[0] = 0; double res = 0; while (true) { int v = -1; for (int u = 0; u < n; u++) { if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u; } if (v == -1) break; used[v] = true; res += mincost[v]; for (int u = 0; u < n; u++) { mincost[u] = min(mincost[u], btw[v][u]); } } printf("%f\n", res); } return 0; }
#include <bits/stdc++.h> using namespace std; #define EPS 0.0001 typedef struct { double x; double y; double z; double r; } Cell; bool eq(double a, double b) { return abs(a - b) < EPS; } double cellLength(Cell a, Cell b) { double l = sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2)) - (a.r + b.r); if (l <= 0) return 0; else return l; } int main(void) { int n; double btw[101][101]; Cell cell[101]; while (cin >> n, n) { for (int i = 0; i < n; i++) { double x, y, z, r; cin >> x >> y >> z >> r; Cell tmp = {x, y, z, r}; cell[i] = tmp; } for (int p = 0; p < n; p++) { for (int q = 0; q < n; q++) { btw[p][q] = cellLength(cell[p], cell[q]); } } // prim double mincost[101]; bool used[101]; for (int i = 0; i < n; i++) { mincost[i] = INT_MAX - 1; used[i] = false; } mincost[0] = 0; double res = 0; while (true) { int v = -1; for (int u = 0; u < n; u++) { if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u; } if (v == -1) break; used[v] = true; res += mincost[v]; for (int u = 0; u < n; u++) { mincost[u] = min(mincost[u], btw[v][u]); } } printf("%.3f\n", res); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
454
#include <cmath> #include <cstdio> #include <iostream> #define INF 1000000000 using namespace std; double mp[101][101]; bool flg[101]; double dis[101]; int n; double prim() { double sum = 0; for (int i = 0; i <= n; i++) flg[i] = 0, dis[i] = INF; dis[0] = 0; for (int q = 0; q < n; q++) { int nx; double dmin = INF; for (int i = 0; i < n; i++) if (dmin > dis[i] && flg[i] == 0) dmin = dis[i], nx = i; flg[nx] = 1; sum += dmin; for (int i = 0; i < n; i++) if (mp[nx][i] < dis[i]) dis[i] = mp[nx][i]; } return sum; } int main() { while (1) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mp[i][j] = INF; for (int i = 0; i < n; i++) { double x[101], y[101], z[101], r[101]; cin >> x[i] >> y[i] >> z[i] >> r[i]; for (int j = 0; j < i; j++) { mp[i][j] = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])) - r[i] - r[j]; // cout << mp[i][j] << endl; if (mp[i][j] < 0) mp[i][j] = 0; mp[j][i] = mp[i][j]; } } printf("%.8f\n", prim()); } return 0; }
#include <cmath> #include <cstdio> #include <iostream> #define INF 1000000000 using namespace std; double mp[101][101]; bool flg[101]; double dis[101]; int n; double prim() { double sum = 0; for (int i = 0; i <= n; i++) flg[i] = 0, dis[i] = INF; dis[0] = 0; for (int q = 0; q < n; q++) { int nx; double dmin = INF; for (int i = 0; i < n; i++) if (dmin > dis[i] && flg[i] == 0) dmin = dis[i], nx = i; flg[nx] = 1; sum += dmin; for (int i = 0; i < n; i++) if (mp[nx][i] < dis[i]) dis[i] = mp[nx][i]; } return sum; } int main() { while (1) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mp[i][j] = INF; for (int i = 0; i < n; i++) { double x[101], y[101], z[101], r[101]; cin >> x[i] >> y[i] >> z[i] >> r[i]; for (int j = 0; j < i; j++) { mp[i][j] = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])) - r[i] - r[j]; // cout << mp[i][j] << endl; if (mp[i][j] < 0) mp[i][j] = 0; mp[j][i] = mp[i][j]; } } printf("%.3f\n", prim()); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
488
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double Weight; const Weight INF = 1e9; struct Edge { int src, dst; Weight weight; Edge(int f, int t, Weight c) : src(f), dst(t), weight(c) {} }; bool operator<(const Edge &a, const Edge &b) { return a.weight != b.weight ? a.weight > b.weight : a.src != b.src ? a.src < b.src : a.dst < b.dst; } typedef vector<Edge> Edges; typedef vector<Edges> Graph; Weight Prim(const Graph &g, int r = 0) { int n = g.size(); Edges T; Weight total = 0; vector<bool> visited(n); priority_queue<Edge> que; que.push(Edge(-1, r, 0)); while (!que.empty()) { Edge e = que.top(); que.pop(); if (visited[e.dst]) continue; T.push_back(e); total += e.weight; visited[e.dst] = true; for (auto f = g[e.dst].begin(); f != g[e.dst].end(); ++f) if (!visited[f->dst]) que.push(*f); } return total; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(6) << setiosflags(ios::fixed); // printf("%.4f", x); int n; while (cin >> n, n) { vector<double> x(n), y(n), z(n), r(n); for (int i = 0; i < n; ++i) cin >> x[i] >> y[i] >> z[i] >> r[i]; Graph g(n); for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) { double d = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j]); if (d <= (r[i] + r[j]) * (r[i] + r[j])) d = 0.0; else d = sqrt(d) - r[i] - r[j]; g[i].push_back(Edge(i, j, d)); g[j].push_back(Edge(j, i, d)); } cout << Prim(g) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double Weight; const Weight INF = 1e9; struct Edge { int src, dst; Weight weight; Edge(int f, int t, Weight c) : src(f), dst(t), weight(c) {} }; bool operator<(const Edge &a, const Edge &b) { return a.weight != b.weight ? a.weight > b.weight : a.src != b.src ? a.src < b.src : a.dst < b.dst; } typedef vector<Edge> Edges; typedef vector<Edges> Graph; Weight Prim(const Graph &g, int r = 0) { int n = g.size(); Edges T; Weight total = 0; vector<bool> visited(n); priority_queue<Edge> que; que.push(Edge(-1, r, 0)); while (!que.empty()) { Edge e = que.top(); que.pop(); if (visited[e.dst]) continue; T.push_back(e); total += e.weight; visited[e.dst] = true; for (auto f = g[e.dst].begin(); f != g[e.dst].end(); ++f) if (!visited[f->dst]) que.push(*f); } return total; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(3) << setiosflags(ios::fixed); // printf("%.4f", x); int n; while (cin >> n, n) { vector<double> x(n), y(n), z(n), r(n); for (int i = 0; i < n; ++i) cin >> x[i] >> y[i] >> z[i] >> r[i]; Graph g(n); for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) { double d = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j]); if (d <= (r[i] + r[j]) * (r[i] + r[j])) d = 0.0; else d = sqrt(d) - r[i] - r[j]; g[i].push_back(Edge(i, j, d)); g[j].push_back(Edge(j, i, d)); } cout << Prim(g) << endl; } return 0; }
[["-", 0, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 0, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
623
#include <algorithm> #include <bitset> #include <iostream> #include <math.h> #include <stdio.h> #include <time.h> #include <vector> using namespace std; #define EPS (10e-11) int solve() { int n; cin >> n; if (n == 0) return 1; vector<vector<long double>> src(4, vector<long double>(n)); vector<vector<long double>> dist(n, vector<long double>(n, 0.0)); for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { cin >> src[j][i]; } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { long double d = sqrt((src[0][i] - src[0][j]) * (src[0][i] - src[0][j]) + (src[1][i] - src[1][j]) * (src[1][i] - src[1][j]) + (src[2][i] - src[2][j]) * (src[2][i] - src[2][j])); if (d > src[3][i] + src[3][j] + EPS) { d -= src[3][i] + src[3][j]; dist[j][i] = d; dist[i][j] = dist[j][i]; } } } long double distsum = 0.0; bitset<100> flg(0); flg.set(0); while (flg.count() < n) { long double mindist = 10e20; int p, q; for (int j = 0; j < n; j++) { if (!flg[j]) continue; // j????¨???????????????? for (int k = 0; k < n; k++) { if (flg[k]) continue; // k???????¨?????????? if (j == k) continue; if (dist[j][k] + EPS < mindist) { p = j; q = k; mindist = dist[j][k]; } } } distsum += mindist; flg.set(q); } printf("%.6llf\n", distsum); return 0; } int main(void) { while (1) { if (solve()) break; } return 0; }
#include <algorithm> #include <bitset> #include <iostream> #include <math.h> #include <stdio.h> #include <time.h> #include <vector> using namespace std; #define EPS (10e-11) int solve() { int n; cin >> n; if (n == 0) return 1; vector<vector<long double>> src(4, vector<long double>(n)); vector<vector<long double>> dist(n, vector<long double>(n, 0.0)); for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { cin >> src[j][i]; } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { long double d = sqrt((src[0][i] - src[0][j]) * (src[0][i] - src[0][j]) + (src[1][i] - src[1][j]) * (src[1][i] - src[1][j]) + (src[2][i] - src[2][j]) * (src[2][i] - src[2][j])); if (d > src[3][i] + src[3][j] + EPS) { d -= src[3][i] + src[3][j]; dist[j][i] = d; dist[i][j] = dist[j][i]; } } } long double distsum = 0.0; bitset<100> flg(0); flg.set(0); while (flg.count() < n) { long double mindist = 10e20; int p, q; for (int j = 0; j < n; j++) { if (!flg[j]) continue; // jは訪問済み頂点 for (int k = 0; k < n; k++) { if (flg[k]) continue; // kは未訪問頂点 if (j == k) continue; if (dist[j][k] + EPS < mindist) { p = j; q = k; mindist = dist[j][k]; } } } distsum += mindist; flg.set(q); } printf("%.3llf\n", distsum); return 0; } int main(void) { while (1) { if (solve()) break; } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
536
#include "bits/stdc++.h" #include <unordered_map> #pragma warning(disable : 4996) using namespace std; #include "bits/stdc++.h" #include <unordered_map> #pragma warning(disable : 4996) using namespace std; struct Point3 { long double x; long double y; long double z; Point3() : x(0), y(0), z(0) {} Point3(const long double _x, const long double _y, const long double _z) : x(_x), y(_y), z(_z) {} }; Point3 operator+(const Point3 &l, const Point3 &r) { return Point3(l.x + r.x, l.y + r.y, l.z + r.z); } Point3 operator+=(Point3 &l, const Point3 &r) { return l = Point3(l.x + r.x, l.y + r.y, l.z + r.z); } Point3 operator-(const Point3 &l, const Point3 &r) { return Point3(l.x - r.x, l.y - r.y, l.z - r.z); } Point3 operator-=(Point3 &l, const Point3 &r) { return l = Point3(l.x - r.x, l.y - r.y, l.z - r.z); } Point3 operator*(const Point3 &l, const long double r) { return Point3(l.x * r, l.y * r, l.z * r); } Point3 operator*(const long double r, const Point3 &l) { return l * r; } Point3 operator*=(Point3 &l, const long double r) { return l = Point3(l.x * r, l.y * r, l.z * r); } Point3 operator/(const Point3 &l, const long double r) { return Point3(l.x / r, l.y / r, l.z / r); } Point3 operator/=(Point3 &l, const long double r) { return l = Point3(l.x / r, l.y / r, l.z / r); } const long double eps = 1e-9, pi = acos(-1.0); const long double dtop = pi / 180.; const long double ptod = 1. / dtop; namespace std { bool operator<(const Point3 &lhs, const Point3 &rhs) { if (lhs.x < rhs.x - eps) return true; if (lhs.x > rhs.x + eps) return false; if (lhs.y < rhs.y - eps) return true; if (lhs.y > rhs.y + eps) return false; return lhs.z < rhs.z; } } // namespace std // 点の入力 Point3 input_point() { long double x, y, z; cin >> x >> y >> z; return Point3(x, y, z); } // 誤差つき等号判定 bool eq(const long double a, const long double b) { return (abs(a - b) < eps); } // 内積 long double dot(const Point3 &a, const Point3 &b) { return a.x * b.x + a.y * b.y + a.z * b.z; } // 外積 Point3 cross(const Point3 &a, const Point3 &b) { return Point3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); } //ノルム long double norm(const Point3 &p) { return (p.x * p.x + p.y * p.y + p.z * p.z); } long double abs(const Point3 &p) { return sqrt(p.x * p.x + p.y * p.y + p.z * p.z); } //球 struct Sphere { Point3 p; long double r; Sphere() : p(), r(0) {} Sphere(const Point3 _point, const long double _radius) : p(_point), r(_radius) {} }; // 直線の定義 class Line3 { public: Point3 a, b; Line3() : a(Point3(0, 0, 0)), b(Point3(0, 0, 0)) {} Line3(Point3 a, Point3 b) : a(a), b(b) {} Point3 operator[](const int _num) const { if (_num == 0) return a; else if (_num == 1) return b; else assert(false); } }; // 点の線分上判定 bool isis_sp(const Line3 &s, const Point3 &p) { return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps); } // 垂線の足 Point3 proj(const Line3 &l, const Point3 &p) { long double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + t * (l.a - l.b); } // 直線と点の距離 long double dist_lp3(const Line3 &l, const Point3 &p) { return abs(p - proj(l, p)); } // 球と直線の交点 vector<Point3> is_lsp(const Sphere &c, const Line3 &l) { vector<Point3> res; long double d = dist_lp3(l, c.p); if (d < c.r + eps) { long double len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety; Point3 nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } // 球と線分の交点 vector<Point3> is_ssp(const Sphere &c, const Line3 &l) { vector<Point3> res(is_lsp(c, l)); vector<Point3> nres; for (auto p : res) { if (isis_sp(l, p)) nres.emplace_back(p); } return nres; } struct UnionFind { vector<int> data; UnionFind(int size) : data(size, -1) {} bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; int main() { while (1) { int n; cin >> n; if (!n) break; vector<Sphere> sps; for (int i = 0; i < n; ++i) { long double x, y, z, r; cin >> x >> y >> z >> r; sps.emplace_back(Point3(x, y, z), r); } vector<vector<long double>> diss(n, vector<long double>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j) diss[i][j] = 0; else { if (sps[i].r + sps[j].r > abs(sps[i].p - sps[j].p)) { bool flag = true; if (sps[i].r < sps[j].r) { flag = true; swap(sps[i].r, sps[j].r); } if (sps[i].r < abs(sps[i].p - sps[j].p) + sps[j].r) { diss[i][j] = 0; } else { // diss[i][j] = sps[i].r - (abs(sps[i].p - sps[j].p) + sps[j].r); } if (flag) { swap(sps[i].r, sps[j].r); } } else { diss[i][j] = abs(sps[i].p - sps[j].p) - sps[i].r - sps[j].r; } } } } priority_queue<pair<long double, int>, vector<pair<long double, int>>, greater<pair<long double, int>>> que; vector<bool> unit(n, false); que.push(make_pair(0, 0)); long double ans = 0; while (!que.empty()) { pair<long double, int> atop(que.top()); que.pop(); if (unit[atop.second]) continue; else { unit[atop.second] = true; ans += atop.first; for (int i = 0; i < n; ++i) { if (!unit[i]) { que.push(make_pair(diss[atop.second][i], i)); } } } } cout << setprecision(5) << fixed << ans << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #pragma warning(disable : 4996) using namespace std; #include "bits/stdc++.h" #include <unordered_map> #pragma warning(disable : 4996) using namespace std; struct Point3 { long double x; long double y; long double z; Point3() : x(0), y(0), z(0) {} Point3(const long double _x, const long double _y, const long double _z) : x(_x), y(_y), z(_z) {} }; Point3 operator+(const Point3 &l, const Point3 &r) { return Point3(l.x + r.x, l.y + r.y, l.z + r.z); } Point3 operator+=(Point3 &l, const Point3 &r) { return l = Point3(l.x + r.x, l.y + r.y, l.z + r.z); } Point3 operator-(const Point3 &l, const Point3 &r) { return Point3(l.x - r.x, l.y - r.y, l.z - r.z); } Point3 operator-=(Point3 &l, const Point3 &r) { return l = Point3(l.x - r.x, l.y - r.y, l.z - r.z); } Point3 operator*(const Point3 &l, const long double r) { return Point3(l.x * r, l.y * r, l.z * r); } Point3 operator*(const long double r, const Point3 &l) { return l * r; } Point3 operator*=(Point3 &l, const long double r) { return l = Point3(l.x * r, l.y * r, l.z * r); } Point3 operator/(const Point3 &l, const long double r) { return Point3(l.x / r, l.y / r, l.z / r); } Point3 operator/=(Point3 &l, const long double r) { return l = Point3(l.x / r, l.y / r, l.z / r); } const long double eps = 1e-9, pi = acos(-1.0); const long double dtop = pi / 180.; const long double ptod = 1. / dtop; namespace std { bool operator<(const Point3 &lhs, const Point3 &rhs) { if (lhs.x < rhs.x - eps) return true; if (lhs.x > rhs.x + eps) return false; if (lhs.y < rhs.y - eps) return true; if (lhs.y > rhs.y + eps) return false; return lhs.z < rhs.z; } } // namespace std // 点の入力 Point3 input_point() { long double x, y, z; cin >> x >> y >> z; return Point3(x, y, z); } // 誤差つき等号判定 bool eq(const long double a, const long double b) { return (abs(a - b) < eps); } // 内積 long double dot(const Point3 &a, const Point3 &b) { return a.x * b.x + a.y * b.y + a.z * b.z; } // 外積 Point3 cross(const Point3 &a, const Point3 &b) { return Point3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); } //ノルム long double norm(const Point3 &p) { return (p.x * p.x + p.y * p.y + p.z * p.z); } long double abs(const Point3 &p) { return sqrt(p.x * p.x + p.y * p.y + p.z * p.z); } //球 struct Sphere { Point3 p; long double r; Sphere() : p(), r(0) {} Sphere(const Point3 _point, const long double _radius) : p(_point), r(_radius) {} }; // 直線の定義 class Line3 { public: Point3 a, b; Line3() : a(Point3(0, 0, 0)), b(Point3(0, 0, 0)) {} Line3(Point3 a, Point3 b) : a(a), b(b) {} Point3 operator[](const int _num) const { if (_num == 0) return a; else if (_num == 1) return b; else assert(false); } }; // 点の線分上判定 bool isis_sp(const Line3 &s, const Point3 &p) { return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps); } // 垂線の足 Point3 proj(const Line3 &l, const Point3 &p) { long double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + t * (l.a - l.b); } // 直線と点の距離 long double dist_lp3(const Line3 &l, const Point3 &p) { return abs(p - proj(l, p)); } // 球と直線の交点 vector<Point3> is_lsp(const Sphere &c, const Line3 &l) { vector<Point3> res; long double d = dist_lp3(l, c.p); if (d < c.r + eps) { long double len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety; Point3 nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } // 球と線分の交点 vector<Point3> is_ssp(const Sphere &c, const Line3 &l) { vector<Point3> res(is_lsp(c, l)); vector<Point3> nres; for (auto p : res) { if (isis_sp(l, p)) nres.emplace_back(p); } return nres; } struct UnionFind { vector<int> data; UnionFind(int size) : data(size, -1) {} bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; int main() { while (1) { int n; cin >> n; if (!n) break; vector<Sphere> sps; for (int i = 0; i < n; ++i) { long double x, y, z, r; cin >> x >> y >> z >> r; sps.emplace_back(Point3(x, y, z), r); } vector<vector<long double>> diss(n, vector<long double>(n, 0)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j) diss[i][j] = 0; else { if (sps[i].r + sps[j].r > abs(sps[i].p - sps[j].p)) { bool flag = false; if (sps[i].r < sps[j].r) { flag = true; swap(sps[i].r, sps[j].r); } if (sps[i].r < abs(sps[i].p - sps[j].p) + sps[j].r) { diss[i][j] = 0; } else { // diss[i][j] = sps[i].r - (abs(sps[i].p - sps[j].p) + sps[j].r); } if (flag) { swap(sps[i].r, sps[j].r); } } else { diss[i][j] = abs(sps[i].p - sps[j].p) - sps[i].r - sps[j].r; } } } } priority_queue<pair<long double, int>, vector<pair<long double, int>>, greater<pair<long double, int>>> que; vector<bool> unit(n, false); que.push(make_pair(0, 0)); long double ans = 0; while (!que.empty()) { pair<long double, int> atop(que.top()); que.pop(); if (unit[atop.second]) continue; else { unit[atop.second] = true; ans += atop.first; for (int i = 0; i < n; ++i) { if (!unit[i]) { que.push(make_pair(diss[atop.second][i], i)); } } } } cout << setprecision(3) << fixed << ans << endl; } return 0; }
[["+", 49, 50, 51, 4, 0, 2, 3, 4, 0, 21], ["+", 49, 50, 51, 4, 0, 2, 3, 4, 0, 13], ["-", 0, 57, 64, 9, 0, 43, 49, 50, 51, 146], ["+", 0, 57, 64, 9, 0, 43, 49, 50, 51, 147], ["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
2,029
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> using namespace std; #define WHITE 0 #define EPS 1e-10 #define GRAY 1 #define BLACK 2 #define MAX_N 1000 #define INF 999999999.99 long double x[MAX_N][MAX_N]; long double MIN[MAX_N], color[MAX_N]; long double X[MAX_N], Y[MAX_N], Z[MAX_N], R[MAX_N]; long double b, sum, sum_min; int n, c, a; void prim() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { MIN[j] = INF; color[j] = WHITE; } b = INF; a = n; color[i] = GRAY; MIN[i] = 0; while (a != 0) { b = INF; for (int j = 0; j < n; j++) { if (color[j] == GRAY) { if (b > MIN[j]) { b = MIN[j]; c = j; } } } color[c] = BLACK; a -= 1; for (int k = 0; k < n; k++) { if (color[k] != BLACK && x[c][k] >= -EPS) { color[k] = GRAY; MIN[k] = min(x[c][k], MIN[k]); } } } sum = 0; for (int j = 0; j < n; j++) { sum += MIN[j]; } sum_min = min(sum_min, sum); } } int main() { while (true) { sum_min = INF; cin >> n; if (n == 0) { break; } for (int i = 0; i < MAX_N; i++) { X[i] = 0; Y[i] = 0; Z[i] = 0; for (int j = 0; j < MAX_N; j++) { x[i][j] = -INF; } } for (int i = 0; i < n; i++) { cin >> X[i] >> Y[i] >> Z[i] >> R[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { long double X1 = (X[i] - X[j]) * (X[i] - X[j]); long double Y1 = (Y[i] - Y[j]) * (Y[i] - Y[j]); long double Z1 = (Z[i] - Z[j]) * (Z[i] - Z[j]); long double dist = sqrtl(X1 + Y1 + Z1); long double dist2 = dist - R[i] - R[j]; x[i][j] = max(0.0l, dist2); } } prim(); sum_min *= 10000; sum_min += 5; sum_min /= 10; int RES = sum_min; printf("%03d.%03d", RES / 1000, RES % 1000); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> using namespace std; #define WHITE 0 #define EPS 1e-10 #define GRAY 1 #define BLACK 2 #define MAX_N 1000 #define INF 999999999.99 long double x[MAX_N][MAX_N]; long double MIN[MAX_N], color[MAX_N]; long double X[MAX_N], Y[MAX_N], Z[MAX_N], R[MAX_N]; long double b, sum, sum_min; int n, c, a; void prim() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { MIN[j] = INF; color[j] = WHITE; } b = INF; a = n; color[i] = GRAY; MIN[i] = 0; while (a != 0) { b = INF; for (int j = 0; j < n; j++) { if (color[j] == GRAY) { if (b > MIN[j]) { b = MIN[j]; c = j; } } } color[c] = BLACK; a -= 1; for (int k = 0; k < n; k++) { if (color[k] != BLACK && x[c][k] >= -EPS) { color[k] = GRAY; MIN[k] = min(x[c][k], MIN[k]); } } } sum = 0; for (int j = 0; j < n; j++) { sum += MIN[j]; } sum_min = min(sum_min, sum); } } int main() { while (true) { sum_min = INF; cin >> n; if (n == 0) { break; } for (int i = 0; i < MAX_N; i++) { X[i] = 0; Y[i] = 0; Z[i] = 0; for (int j = 0; j < MAX_N; j++) { x[i][j] = -INF; } } for (int i = 0; i < n; i++) { cin >> X[i] >> Y[i] >> Z[i] >> R[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { long double X1 = (X[i] - X[j]) * (X[i] - X[j]); long double Y1 = (Y[i] - Y[j]) * (Y[i] - Y[j]); long double Z1 = (Z[i] - Z[j]) * (Z[i] - Z[j]); long double dist = sqrtl(X1 + Y1 + Z1); long double dist2 = dist - R[i] - R[j]; x[i][j] = max(0.0l, dist2); } } prim(); sum_min *= 10000; sum_min += 5; sum_min /= 10; int RES = sum_min; printf("%d.%03d\n", RES / 1000, RES % 1000); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
670
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1 << 30 #define pb push_back #define mp make_pair #define EPS 1e-8 #define equals(a, b) fabs((a) - (b)) < EPS using namespace std; typedef long long ll; typedef pair<int, int> P; struct Point3D { double x, y, z; Point3D() : x(0), y(0), z(0) {} Point3D(double x, double y, double z) : x(x), y(y), z(z) {} Point3D operator+(const Point3D &o) const { return Point3D(x + o.x, y + o.y, z + o.z); } Point3D operator-(const Point3D &o) const { return Point3D(x - o.x, y - o.y, z - o.z); } Point3D operator*(const double m) const { return Point3D(x * m, y * m, z * m); } Point3D operator/(const double d) const { return Point3D(x / d, y / d, z / d); } bool operator==(const Point3D &o) const { return fabs(x - o.x) < EPS && fabs(y - o.y) < EPS; } }; ostream &operator<<(ostream &os, const Point3D &p) { os << "(" << p.x << ", " << p.y << ", " << p.z << ")"; return os; } double dot(Point3D a, Point3D b) { return a.x * b.x + a.y * b.y + a.z * b.z; } Point3D cross(Point3D a, Point3D b) { return Point3D(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); } double norm(Point3D p) { return dot(p, p); } double abs(Point3D p) { return sqrt(norm(p)); } struct Line { Point3D a, b; Line() : a(Point3D(0, 0, 0)), b(Point3D(0, 0, 0)) {} Line(Point3D a, Point3D b) : a(a), b(b) {} }; ostream &operator<<(ostream &os, const Line &l) { os << "(" << l.a.x << ", " << l.a.y << ", " << l.a.z << ")-(" << l.b.x << "," << l.b.y << ", " << l.b.z << ")"; return os; } Point3D project(Line l, Point3D p) { Point3D base = l.b - l.a; double t = dot(base, p - l.a) / dot(base, base); return l.a + base * t; } struct Sphere { Point3D p; double r; Sphere() : p(Point3D(0, 0, 0)), r(0.0) {} Sphere(Point3D p, double r) : p(p), r(r) {} }; ostream &operator<<(ostream &os, const Sphere &s) { os << "(" << s.p.z << ", " << s.p.y << ", " << s.p.z << " :" << s.r << ")"; return os; } double distanceSphSph(Sphere s1, Sphere s2) { Point3D p = s2.p - s1.p; double dist = abs(p) - s1.r - s2.r; return max(0.0, dist); } struct UnionFind { vector<int> par, rank; int N; UnionFind(int n) { N = n; par.resize(n); rank.resize(n); rep(i, n) { par[i] = i; rank[i] = 0; } } int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } bool same(int x, int y) { return find(x) == find(y); } int size() { int cnt = 0; rep(i, N) if (find(i) == i) cnt++; return cnt; } }; struct edge { int from, to; double cost; edge(int t, int c) : to(t), cost(c) {} edge(int f, int t, double c) : from(f), to(t), cost(c) {} bool operator<(const edge &e) const { return cost < e.cost; } }; double kruskal(int n, vector<edge> v) { sort(v.begin(), v.end()); UnionFind uf(n); double ret = 0; rep(i, v.size()) { edge e = v[i]; if (!uf.same(e.from, e.to)) { uf.unite(e.from, e.to); ret += e.cost; } } return ret; } int main() { int n; while (cin >> n && n) { vector<Sphere> s(n); rep(i, n) cin >> s[i].p.x >> s[i].p.y >> s[i].p.z >> s[i].r; vector<edge> v; rep(i, n) { REP(j, i + 1, n) { double dist = distanceSphSph(s[i], s[j]); v.push_back(edge(i, j, dist)); } } cout << fixed; cout.precision(20); cout << kruskal(n, v) << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1 << 30 #define pb push_back #define mp make_pair #define EPS 1e-8 #define equals(a, b) fabs((a) - (b)) < EPS using namespace std; typedef long long ll; typedef pair<int, int> P; struct Point3D { double x, y, z; Point3D() : x(0), y(0), z(0) {} Point3D(double x, double y, double z) : x(x), y(y), z(z) {} Point3D operator+(const Point3D &o) const { return Point3D(x + o.x, y + o.y, z + o.z); } Point3D operator-(const Point3D &o) const { return Point3D(x - o.x, y - o.y, z - o.z); } Point3D operator*(const double m) const { return Point3D(x * m, y * m, z * m); } Point3D operator/(const double d) const { return Point3D(x / d, y / d, z / d); } bool operator==(const Point3D &o) const { return fabs(x - o.x) < EPS && fabs(y - o.y) < EPS; } }; ostream &operator<<(ostream &os, const Point3D &p) { os << "(" << p.x << ", " << p.y << ", " << p.z << ")"; return os; } double dot(Point3D a, Point3D b) { return a.x * b.x + a.y * b.y + a.z * b.z; } Point3D cross(Point3D a, Point3D b) { return Point3D(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); } double norm(Point3D p) { return dot(p, p); } double abs(Point3D p) { return sqrt(norm(p)); } struct Line { Point3D a, b; Line() : a(Point3D(0, 0, 0)), b(Point3D(0, 0, 0)) {} Line(Point3D a, Point3D b) : a(a), b(b) {} }; ostream &operator<<(ostream &os, const Line &l) { os << "(" << l.a.x << ", " << l.a.y << ", " << l.a.z << ")-(" << l.b.x << "," << l.b.y << ", " << l.b.z << ")"; return os; } Point3D project(Line l, Point3D p) { Point3D base = l.b - l.a; double t = dot(base, p - l.a) / dot(base, base); return l.a + base * t; } struct Sphere { Point3D p; double r; Sphere() : p(Point3D(0, 0, 0)), r(0.0) {} Sphere(Point3D p, double r) : p(p), r(r) {} }; ostream &operator<<(ostream &os, const Sphere &s) { os << "(" << s.p.z << ", " << s.p.y << ", " << s.p.z << " :" << s.r << ")"; return os; } double distanceSphSph(Sphere s1, Sphere s2) { Point3D p = s2.p - s1.p; double dist = abs(p) - s1.r - s2.r; return max(0.0, dist); } struct UnionFind { vector<int> par, rank; int N; UnionFind(int n) { N = n; par.resize(n); rank.resize(n); rep(i, n) { par[i] = i; rank[i] = 0; } } int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } bool same(int x, int y) { return find(x) == find(y); } int size() { int cnt = 0; rep(i, N) if (find(i) == i) cnt++; return cnt; } }; struct edge { int from, to; double cost; edge(int t, int c) : to(t), cost(c) {} edge(int f, int t, double c) : from(f), to(t), cost(c) {} bool operator<(const edge &e) const { return cost < e.cost; } }; double kruskal(int n, vector<edge> v) { sort(v.begin(), v.end()); UnionFind uf(n); double ret = 0; rep(i, v.size()) { edge e = v[i]; if (!uf.same(e.from, e.to)) { uf.unite(e.from, e.to); ret += e.cost; } } return ret; } int main() { int n; while (cin >> n && n) { vector<Sphere> s(n); rep(i, n) cin >> s[i].p.x >> s[i].p.y >> s[i].p.z >> s[i].r; vector<edge> v; rep(i, n) { REP(j, i + 1, n) { double dist = distanceSphSph(s[i], s[j]); v.push_back(edge(i, j, dist)); } } cout << fixed; cout.precision(3); cout << kruskal(n, v) << endl; } return 0; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
1,399
#include <bits/stdc++.h> #define loop(n, i) for (int i = 0; i < n; i++) #define all(v) v.begin(), v.end() #define HERE cout << "HERE: " << __LINE__ << endl; #define INSP(v) cout << v << " at " << __LINE__ << endl; using namespace std; typedef long long ll; struct Elem { int to; double cost; }; bool operator>(const Elem &x, const Elem &y) { return tie(x.cost, x.to) > tie(y.cost, y.to); } int main() { while (1) { int n; cin >> n; if (!n) break; vector<double> x(n), y(n), z(n), r(n); loop(n, i) cin >> x[i] >> y[i] >> z[i] >> r[i]; vector<vector<double>> dist(n, vector<double>(n)); loop(n, i) loop(n, j) { double d = pow( pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2) + pow(z[i] - z[j], 2), 0.5); dist[i][j] = max(0.0, d - r[i] - r[j]); } priority_queue<Elem, vector<Elem>, greater<Elem>> Q; vector<int> vis(n); Q.push({0, 0}); double ans = 0; while (Q.size()) { Elem e = Q.top(); Q.pop(); if (vis[e.to]) continue; vis[e.to] = 1; ans += e.cost; loop(n, i) if (i != e.to && !vis[i]) { Q.push({i, dist[e.to][i]}); } } cout << setprecision(8) << fixed << ans << endl; } return 0; }
#include <bits/stdc++.h> #define loop(n, i) for (int i = 0; i < n; i++) #define all(v) v.begin(), v.end() #define HERE cout << "HERE: " << __LINE__ << endl; #define INSP(v) cout << v << " at " << __LINE__ << endl; using namespace std; typedef long long ll; struct Elem { int to; double cost; }; bool operator>(const Elem &x, const Elem &y) { return tie(x.cost, x.to) > tie(y.cost, y.to); } int main() { while (1) { int n; cin >> n; if (!n) break; vector<double> x(n), y(n), z(n), r(n); loop(n, i) cin >> x[i] >> y[i] >> z[i] >> r[i]; vector<vector<double>> dist(n, vector<double>(n)); loop(n, i) loop(n, j) { double d = pow( pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2) + pow(z[i] - z[j], 2), 0.5); dist[i][j] = max(0.0, d - r[i] - r[j]); } priority_queue<Elem, vector<Elem>, greater<Elem>> Q; vector<int> vis(n); Q.push({0, 0}); double ans = 0; while (Q.size()) { Elem e = Q.top(); Q.pop(); if (vis[e.to]) continue; vis[e.to] = 1; ans += e.cost; loop(n, i) if (i != e.to && !vis[i]) { Q.push({i, dist[e.to][i]}); } } cout << setprecision(3) << fixed << ans << endl; } return 0; }
[["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
426
#include <bits/stdc++.h> using namespace std; #define int long long typedef pair<int, int> pint; typedef vector<int> vint; typedef vector<pint> vpint; #define pb push_back #define mp make_pair #define fi first #define se second #define all(v) (v).begin(), (v).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, f, n) for (int i = (f); i < (n); i++) #define each(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) template <class T, class U> inline void chmin(T &t, U f) { if (t > f) t = f; } template <class T, class U> inline void chmax(T &t, U f) { if (t < f) t = f; } const double EPS = 1e-7; int N; int par[100]; double x[100], y[100], z[100], r[100]; void init() { rep(i, N) par[i] = i; } int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } void unite(int x, int y) { par[find(y)] = find(x); } bool same(int x, int y) { return find(x) == find(y); } signed main() { while (cin >> N, N) { rep(i, N) cin >> x[i] >> y[i] >> z[i] >> r[i]; init(); vector<tuple<double, int, int>> vec; rep(i, N) rep(j, N) { double d = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])); if (r[i] + r[j] + EPS > d) unite(i, j); else vec.pb(make_tuple(d - r[i] - r[j], i, j)); } sort(all(vec)); double ans = 0; rep(i, vec.size()) { double d; int u, v; tie(d, u, v) = vec[i]; if (same(u, v)) continue; ans += d; unite(u, v); } printf("%.20f\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long typedef pair<int, int> pint; typedef vector<int> vint; typedef vector<pint> vpint; #define pb push_back #define mp make_pair #define fi first #define se second #define all(v) (v).begin(), (v).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, f, n) for (int i = (f); i < (n); i++) #define each(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) template <class T, class U> inline void chmin(T &t, U f) { if (t > f) t = f; } template <class T, class U> inline void chmax(T &t, U f) { if (t < f) t = f; } const double EPS = 1e-7; int N; int par[100]; double x[100], y[100], z[100], r[100]; void init() { rep(i, N) par[i] = i; } int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } void unite(int x, int y) { par[find(y)] = find(x); } bool same(int x, int y) { return find(x) == find(y); } signed main() { while (cin >> N, N) { rep(i, N) cin >> x[i] >> y[i] >> z[i] >> r[i]; init(); vector<tuple<double, int, int>> vec; rep(i, N) rep(j, N) { double d = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])); if (r[i] + r[j] + EPS > d) unite(i, j); else vec.pb(make_tuple(d - r[i] - r[j], i, j)); } sort(all(vec)); double ans = 0; rep(i, vec.size()) { double d; int u, v; tie(d, u, v) = vec[i]; if (same(u, v)) continue; ans += d; unite(u, v); } printf("%.3f\n", ans); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
556
#include <algorithm> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <string> #include <vector> using namespace std; const int MAX = 100; int par[MAX]; int rnk[MAX]; int nodeNum[MAX]; void init(int n) { for (int i = 0; i < n; i++) { par[i] = i; rnk[i] = 0; nodeNum[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rnk[x] < rnk[y]) { par[x] = y; } else { par[y] = x; if (rnk[x] == rnk[y]) rnk[x]++; } nodeNum[x] = nodeNum[y] = nodeNum[x] + nodeNum[y]; } bool same(int x, int y) { return find(x) == find(y); } typedef double W; struct edge { int u, v; W cost; }; bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; } vector<edge> es; W kruskal(int V, int E) { sort(es.begin(), es.end(), comp); init(V); W res = 0; for (int i = 0; i < E; i++) { edge e = es[i]; if (!same(e.u, e.v)) { unite(e.u, e.v); res += e.cost; } } return res; } double x[100], y[100], z[100], r[100]; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; while (cin >> N, N) { for (int i = 0; i < N; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } es.clear(); for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { double d = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])); d = max(0.0, d - r[i] - r[j]); es.push_back(edge{i, j, d}); } } double ans = kruskal(N, es.size()); cout << setprecision(12) << ans << endl; } }
#include <algorithm> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <string> #include <vector> using namespace std; const int MAX = 100; int par[MAX]; int rnk[MAX]; int nodeNum[MAX]; void init(int n) { for (int i = 0; i < n; i++) { par[i] = i; rnk[i] = 0; nodeNum[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rnk[x] < rnk[y]) { par[x] = y; } else { par[y] = x; if (rnk[x] == rnk[y]) rnk[x]++; } nodeNum[x] = nodeNum[y] = nodeNum[x] + nodeNum[y]; } bool same(int x, int y) { return find(x) == find(y); } typedef double W; struct edge { int u, v; W cost; }; bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; } vector<edge> es; W kruskal(int V, int E) { sort(es.begin(), es.end(), comp); init(V); W res = 0; for (int i = 0; i < E; i++) { edge e = es[i]; if (!same(e.u, e.v)) { unite(e.u, e.v); res += e.cost; } } return res; } double x[100], y[100], z[100], r[100]; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; while (cin >> N, N) { for (int i = 0; i < N; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } es.clear(); for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { double d = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])); d = max(0.0, d - r[i] - r[j]); es.push_back(edge{i, j, d}); } } double ans = kruskal(N, es.size()); cout << fixed << setprecision(3) << ans << endl; } }
[["+", 0, 16, 31, 16, 31, 16, 31, 16, 12, 22], ["+", 0, 1, 0, 16, 31, 16, 31, 16, 17, 151], ["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
663
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctype.h> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; int main() { int n; while (1) { cin >> n; if (n == 0) break; double x[101], y[101], z[101], r[101]; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } double kyori_pow2[101][101]; bool ischecked[101]; for (int i = 0; i < n; i++) { kyori_pow2[i][i] = 0.0; ischecked[i] = false; for (int j = i + 1; j < n; j++) { double k = sqrt(pow((x[i] - x[j]), 2) + pow((y[i] - y[j]), 2) + pow((z[i] - z[j]), 2)); kyori_pow2[i][j] = max(0.0, k - r[i] - r[j]); kyori_pow2[j][i] = max(0.0, k - r[i] - r[j]); } } double ans = 0.0; for (int i = 0; i < n; i++) { double mind = 10000000.0; int j = -1; for (int k = 0; k < n; k++) { if (ischecked[k]) continue; if (kyori_pow2[0][k] < mind) { mind = kyori_pow2[0][k]; j = k; } } if (j == -1) break; ans += mind; for (int k = 0; k < n; k++) { kyori_pow2[0][k] = min(kyori_pow2[0][k], kyori_pow2[j][k]); } ischecked[j] = true; } printf("%.4f\n", ans); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctype.h> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; int main() { int n; while (1) { cin >> n; if (n == 0) break; double x[101], y[101], z[101], r[101]; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } double kyori_pow2[101][101]; bool ischecked[101]; for (int i = 0; i < n; i++) { kyori_pow2[i][i] = 0.0; ischecked[i] = false; for (int j = i + 1; j < n; j++) { double k = sqrt(pow((x[i] - x[j]), 2) + pow((y[i] - y[j]), 2) + pow((z[i] - z[j]), 2)); kyori_pow2[i][j] = max(0.0, k - r[i] - r[j]); kyori_pow2[j][i] = max(0.0, k - r[i] - r[j]); } } double ans = 0.0; for (int i = 0; i < n; i++) { double mind = 10000000.0; int j = -1; for (int k = 0; k < n; k++) { if (ischecked[k]) continue; if (kyori_pow2[0][k] < mind) { mind = kyori_pow2[0][k]; j = k; } } if (j == -1) break; ans += mind; for (int k = 0; k < n; k++) { kyori_pow2[0][k] = min(kyori_pow2[0][k], kyori_pow2[j][k]); } ischecked[j] = true; } printf("%.3f\n", ans); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
450
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define LOG(...) printf(__VA_ARGS__) //#define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define CLR(a) memset((a), 0, sizeof(a)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; struct UnionFind { vector<int> v; UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; } int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); } void unite(int x, int y) { v[find(x)] = find(y); } }; struct pos3 { double x; double y; double z; double r; }; int main() { int n; while (cin >> n) { vector<pos3> cic(n); vector<pair<double, pii>> dis; REP(i, n) cin >> cic[i].x >> cic[i].y >> cic[i].z >> cic[i].r; REP(i, n - 1) FOR(j, i + 1, n) { double d = sqrt((cic[i].x - cic[j].x) * (cic[i].x - cic[j].x) + (cic[i].y - cic[j].y) * (cic[i].y - cic[j].y) + (cic[i].z - cic[j].z) * (cic[i].z - cic[j].z)) - cic[i].r - cic[j].r; dis.push_back({d, {i, j}}); } SORT(dis); UnionFind uf(n); double ans = 0.0; REP(i, dis.size()) { if (uf.find(dis[i].second.first) == uf.find(dis[i].second.second)) continue; uf.unite(dis[i].second.first, dis[i].second.second); ans += max(0.0, dis[i].first); int cnt = 0; REP(j, n) { if (uf.find(0) == uf.find(j)) cnt++; } if (cnt == n) break; } printf("%.3lf\n", ans); } }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define LOG(...) printf(__VA_ARGS__) //#define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define CLR(a) memset((a), 0, sizeof(a)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; struct UnionFind { vector<int> v; UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; } int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); } void unite(int x, int y) { v[find(x)] = find(y); } }; struct pos3 { double x; double y; double z; double r; }; int main() { int n; while (cin >> n, n) { vector<pos3> cic(n); vector<pair<double, pii>> dis; REP(i, n) cin >> cic[i].x >> cic[i].y >> cic[i].z >> cic[i].r; REP(i, n - 1) FOR(j, i + 1, n) { double d = sqrt((cic[i].x - cic[j].x) * (cic[i].x - cic[j].x) + (cic[i].y - cic[j].y) * (cic[i].y - cic[j].y) + (cic[i].z - cic[j].z) * (cic[i].z - cic[j].z)) - cic[i].r - cic[j].r; dis.push_back({d, {i, j}}); } SORT(dis); UnionFind uf(n); double ans = 0.0; REP(i, dis.size()) { if (uf.find(dis[i].second.first) == uf.find(dis[i].second.second)) continue; uf.unite(dis[i].second.first, dis[i].second.second); ans += max(0.0, dis[i].first); int cnt = 0; REP(j, n) { if (uf.find(0) == uf.find(j)) cnt++; } if (cnt == n) break; } printf("%.3lf\n", ans); } }
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
1
697
/* * * */ #include <bits/stdc++.h> using namespace std; #define LOG(...) fprintf(stderr, __VA_ARGS__) //#define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define SQ(n) (n) * (n) typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<char> vc; typedef vector<pii> vpi; typedef vector<pll> vpl; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vc> vvc; typedef vector<vll> vvll; double dist(double x1, double y1, double z1, double x2, double y2, double z2) { return sqrt(SQ(x1 - x2) + SQ(y1 - y2) + SQ(z1 - z2)); } struct Station { double x, y, z, r; }; int main() { int n; while (cin >> n) { vector<Station> stations; REP(i, n) { Station st; cin >> st.x >> st.y >> st.z >> st.r; stations.push_back(st); } double ans = 0; set<int> cell; multimap<double, int> corridor; corridor.insert({0, 0}); while (cell.size() < n) { double len = corridor.begin()->first; int c = corridor.begin()->second; corridor.erase(corridor.begin()); if (!cell.insert(c).second) continue; ans += len; for (int i = 0; i < n; i++) { if (cell.find(i) == cell.end()) { Station st = stations[i]; Station stc = stations[c]; corridor.insert( {max(0.0, dist(st.x, st.y, st.z, stc.x, stc.y, stc.z) - st.r - stc.r), i}); } } } printf("%.3f\n", ans); } }
/* * * */ #include <bits/stdc++.h> using namespace std; #define LOG(...) fprintf(stderr, __VA_ARGS__) //#define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define SQ(n) (n) * (n) typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<char> vc; typedef vector<pii> vpi; typedef vector<pll> vpl; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vc> vvc; typedef vector<vll> vvll; double dist(double x1, double y1, double z1, double x2, double y2, double z2) { return sqrt(SQ(x1 - x2) + SQ(y1 - y2) + SQ(z1 - z2)); } struct Station { double x, y, z, r; }; int main() { int n; while (cin >> n, n) { vector<Station> stations; REP(i, n) { Station st; cin >> st.x >> st.y >> st.z >> st.r; stations.push_back(st); } double ans = 0; set<int> cell; multimap<double, int> corridor; corridor.insert({0, 0}); while (cell.size() < n) { double len = corridor.begin()->first; int c = corridor.begin()->second; corridor.erase(corridor.begin()); if (!cell.insert(c).second) continue; ans += len; for (int i = 0; i < n; i++) { if (cell.find(i) == cell.end()) { Station st = stations[i]; Station stc = stations[c]; corridor.insert( {max(0.0, dist(st.x, st.y, st.z, stc.x, stc.y, stc.z) - st.r - stc.r), i}); } } } printf("%.3f\n", ans); } }
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
1
499
#include <bits/stdc++.h> using namespace std; typedef pair<double, int> P; struct point { double x, y, z, r; point() {} point(double x, double y, double z, double r) : x(x), y(y), z(z), r(r) {} }; bool used[111]; int n; point pos[111]; double dist(point p, point q) { double x = p.x - q.x, y = p.y - q.y, z = p.z - q.z; double d = sqrt(x * x + y * y + z * z); return max(d - p.r - q.r, 0.0); } double prim() { priority_queue<P, vector<P>, greater<P>> que; double total = 0.0; fill(used, used + 111, false); que.push(P(0.0, 0)); while (!que.empty()) { P p = que.top(); que.pop(); int now = p.second; double d = p.first; if (used[now]) continue; used[now] = true; total += d; for (int i = 0; i < n; i++) { if (i == now) continue; que.push(P(dist(pos[now], pos[i]), i)); } } return total; } int main() { while (cin >> n, n) { for (int i = 0; i < n; i++) { double x, y, z, r; cin >> x >> y >> z >> r; pos[i] = point(x, y, z, r); } printf("%.10f\n", prim()); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<double, int> P; struct point { double x, y, z, r; point() {} point(double x, double y, double z, double r) : x(x), y(y), z(z), r(r) {} }; bool used[111]; int n; point pos[111]; double dist(point p, point q) { double x = p.x - q.x, y = p.y - q.y, z = p.z - q.z; double d = sqrt(x * x + y * y + z * z); return max(d - p.r - q.r, 0.0); } double prim() { priority_queue<P, vector<P>, greater<P>> que; double total = 0.0; fill(used, used + 111, false); que.push(P(0.0, 0)); while (!que.empty()) { P p = que.top(); que.pop(); int now = p.second; double d = p.first; if (used[now]) continue; used[now] = true; total += d; for (int i = 0; i < n; i++) { if (i == now) continue; que.push(P(dist(pos[now], pos[i]), i)); } } return total; } int main() { while (cin >> n, n) { for (int i = 0; i < n; i++) { double x, y, z, r; cin >> x >> y >> z >> r; pos[i] = point(x, y, z, r); } printf("%.3f\n", prim()); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
403
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) typedef long long int ll; using namespace std; struct UnionFind { vector<int> par; vector<int> rank; UnionFind(int n) : par(n), rank(n, 1) { for (int i = 0; i < n; ++i) par[i] = i; } int find(int x) { if (x == par[x]) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); par[y] = x; rank[x] += rank[y]; } bool same(int x, int y) { return find(x) == find(y); } }; struct edge { int src, dst; double cost; edge(int s, int t, double c) : src(s), dst(t), cost(c) {} bool operator<(const edge &rhs) const { return cost < rhs.cost; } }; int main(void) { int n; while (cin >> n && n) { UnionFind uf(n); vector<double> x(n), y(n), z(n), r(n); vector<edge> edges; REP(i, n) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } REP(i, n) { REP(j, n) { if (i == j) continue; double c = sqrt(pow(x[i] - x[j], 2.0) + pow(y[i] - y[j], 2.0) + pow(z[i] - z[j], 2.0)) - r[i] - r[j]; edges.push_back(edge(i, j, max(0.0, c))); } } sort(edges.begin(), edges.end()); double ans = 0; for (auto e : edges) { if (!uf.same(e.src, e.dst)) { uf.unite(e.src, e.dst); ans += e.cost; } } printf("%.6f\n", ans); } }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) typedef long long int ll; using namespace std; struct UnionFind { vector<int> par; vector<int> rank; UnionFind(int n) : par(n), rank(n, 1) { for (int i = 0; i < n; ++i) par[i] = i; } int find(int x) { if (x == par[x]) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); par[y] = x; rank[x] += rank[y]; } bool same(int x, int y) { return find(x) == find(y); } }; struct edge { int src, dst; double cost; edge(int s, int t, double c) : src(s), dst(t), cost(c) {} bool operator<(const edge &rhs) const { return cost > rhs.cost; } }; int main(void) { int n; while (cin >> n && n) { UnionFind uf(n); vector<double> x(n), y(n), z(n), r(n); vector<edge> edges; REP(i, n) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } REP(i, n) { REP(j, n) { if (i == j) continue; double c = sqrt(pow(x[i] - x[j], 2.0) + pow(y[i] - y[j], 2.0) + pow(z[i] - z[j], 2.0)) - r[i] - r[j]; edges.push_back(edge(i, j, max(0.0, c))); } } sort(edges.rbegin(), edges.rend()); double ans = 0; for (auto e : edges) { if (!uf.same(e.src, e.dst)) { uf.unite(e.src, e.dst); ans += e.cost; } } printf("%.3f\n", ans); } }
[["-", 0, 14, 8, 9, 0, 37, 0, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 37, 0, 16, 17, 47], ["-", 0, 2, 3, 4, 0, 2, 63, 118, 119, 120], ["+", 0, 2, 3, 4, 0, 2, 63, 118, 119, 120], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
538
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define each(itr, c) \ for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr) #define all(x) (x).begin(), (x).end() #define pb push_back #define fi first #define se second typedef pair<int, int> pi; typedef pair<double, pi> P; struct UF { int n; //??£?????£????????????????????????,????????£???????????§??£?????????????????° vector<int> d; UF() {} UF(int N) : n(N), d(N, -1) {} int root(int v) { if (d[v] < 0) return v; return d[v] = root(d[v]); } bool unite(int X, int Y) { X = root(X); Y = root(Y); if (X == Y) return false; if (size(X) < size(Y)) swap(X, Y); d[X] += d[Y]; d[Y] = X; return true; } int size(int v) { return -d[root(v)]; } bool same(int X, int Y) { return root(X) == root(Y); } }; int n; double x[100], y[100], z[100], r[100]; inline double dist(int i, int j) { return sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])); } inline double make_edge(int i, int j) { double ret = dist(i, j) - r[i] - r[j]; return ret > 0 ? ret : 0; } int main() { while (cin >> n, n) { rep(i, n) scanf(" %lf %lf %lf %lf", &x[i], &y[i], &z[i], &r[i]); vector<P> e; rep(i, n) rep(j, i) e.pb(P(make_edge(i, j), pi(i, j))); sort(all(e)); double ans = 0; UF uf(n); rep(i, e.size()) { int a = e[i].se.fi, b = e[i].se.se; if (!uf.same(a, b)) { uf.unite(a, b); ans += e[i].fi; } if (uf.size(0) == n) break; } printf("%.10f\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define each(itr, c) \ for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr) #define all(x) (x).begin(), (x).end() #define pb push_back #define fi first #define se second typedef pair<int, int> pi; typedef pair<double, pi> P; struct UF { int n; //??£?????£????????????????????????,????????£???????????§??£?????????????????° vector<int> d; UF() {} UF(int N) : n(N), d(N, -1) {} int root(int v) { if (d[v] < 0) return v; return d[v] = root(d[v]); } bool unite(int X, int Y) { X = root(X); Y = root(Y); if (X == Y) return false; if (size(X) < size(Y)) swap(X, Y); d[X] += d[Y]; d[Y] = X; return true; } int size(int v) { return -d[root(v)]; } bool same(int X, int Y) { return root(X) == root(Y); } }; int n; double x[100], y[100], z[100], r[100]; inline double dist(int i, int j) { return sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])); } inline double make_edge(int i, int j) { double ret = dist(i, j) - r[i] - r[j]; return ret > 0 ? ret : 0; } int main() { while (cin >> n, n) { rep(i, n) scanf(" %lf %lf %lf %lf", &x[i], &y[i], &z[i], &r[i]); vector<P> e; rep(i, n) rep(j, i) e.pb(P(make_edge(i, j), pi(i, j))); sort(all(e)); double ans = 0; UF uf(n); rep(i, e.size()) { int a = e[i].se.fi, b = e[i].se.se; if (!uf.same(a, b)) { uf.unite(a, b); ans += e[i].fi; } if (uf.size(0) == n) break; } printf("%.3f\n", ans); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
608
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) for (int i = 0; i < (b); i++) #define all(a) (a).begin(), (a).end() #define show(x) cerr << #x << " = " << (x) << endl; using namespace std; struct Point { double x, y, z; double r; }; double dist(Point a, Point b) { auto pow = [](double a) { return a * a; }; return sqrt(pow(b.x - a.x) + pow(b.y - a.y) + pow(b.z - a.z)) - (a.r + b.r); } class Edge { public: int to; double cost; Edge(int to, double cost) : to(to), cost(cost) {} }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; double prim(const Graph &g, int s = 0) { int n = g.size(); double total = 0; vector<bool> visited(n); priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> q; // priority_queue<pair<double, int> > q; q.push(make_pair(0.0, s)); while (not q.empty()) { pair<double, int> u = q.top(); q.pop(); if (visited[u.second]) continue; total += u.first; visited[u.second] = true; for (auto it : g[u.second]) { if (not visited[it.to]) q.push(make_pair(it.cost, it.to)); } } return total; } int main() { int n; while (cin >> n, n) { Point p[105]; rep(i, n) cin >> p[i].x >> p[i].y >> p[i].z >> p[i].r; Graph g(n); rep(i, n) { rep(j, n) { double tmp = dist(p[i], p[j]); // cout << i << ' ' << j << endl; // show(tmp) g[i].emplace_back(Edge{j, tmp < 0 ? 0 : tmp}); } } cout << fixed << setprecision(8) << prim(g, 0) << endl; } }
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) for (int i = 0; i < (b); i++) #define all(a) (a).begin(), (a).end() #define show(x) cerr << #x << " = " << (x) << endl; using namespace std; struct Point { double x, y, z; double r; }; double dist(Point a, Point b) { auto pow = [](double a) { return a * a; }; return sqrt(pow(b.x - a.x) + pow(b.y - a.y) + pow(b.z - a.z)) - (a.r + b.r); } class Edge { public: int to; double cost; Edge(int to, double cost) : to(to), cost(cost) {} }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; double prim(const Graph &g, int s = 0) { int n = g.size(); double total = 0; vector<bool> visited(n); priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> q; // priority_queue<pair<double, int> > q; q.push(make_pair(0.0, s)); while (not q.empty()) { pair<double, int> u = q.top(); q.pop(); if (visited[u.second]) continue; total += u.first; visited[u.second] = true; for (auto it : g[u.second]) { if (not visited[it.to]) q.push(make_pair(it.cost, it.to)); } } return total; } int main() { int n; while (cin >> n, n) { Point p[105]; rep(i, n) cin >> p[i].x >> p[i].y >> p[i].z >> p[i].r; Graph g(n); rep(i, n) { rep(j, n) { double tmp = dist(p[i], p[j]); // cout << i << ' ' << j << endl; // show(tmp) g[i].emplace_back(Edge{j, tmp < 0 ? 0 : tmp}); } } cout << fixed << setprecision(3) << prim(g, 0) << endl; } }
[["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
496
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <vector> #define REP(i, a, n) for (int i = (a); i < (n); i++) using namespace std; struct edge { int p, q; double d; bool operator<(const edge &e) const { return d < e.d; } }; int N; double X[100], Y[100], Z[100], R[100]; double dist(int i, int j) { double dx = X[i] - X[j]; double dy = Y[i] - Y[j]; double dz = Z[i] - Z[j]; return max(sqrt(dx * dx + dy * dy + dz * dz) - R[i] - R[j], 0.0); } int find(int *a, int i) { return a[i] == i ? i : (a[i] = find(a, a[i])); } void unite(int *a, int i, int j) { if (find(a, i) != find(a, j)) a[find(a, i)] = find(a, j); } int main(void) { while (cin >> N, N) { REP(i, 0, N) cin >> X[i] >> Y[i] >> Z[i] >> R[i]; vector<edge> e; REP(i, 0, N) REP(j, i + 1, N) e.push_back((edge){i, j, dist(i, j)}); sort(e.begin(), e.end()); double ans = 0.0; int g[100]; REP(i, 0, N) g[i] = i; REP(i, 0, e.size()) if (find(g, e[i].p) != find(g, e[i].q)) { ans += e[i].d; unite(g, e[i].p, e[i].q); } printf("%.5lf\n", ans); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <vector> #define REP(i, a, n) for (int i = (a); i < (n); i++) using namespace std; struct edge { int p, q; double d; bool operator<(const edge &e) const { return d < e.d; } }; int N; double X[100], Y[100], Z[100], R[100]; double dist(int i, int j) { double dx = X[i] - X[j]; double dy = Y[i] - Y[j]; double dz = Z[i] - Z[j]; return max(sqrt(dx * dx + dy * dy + dz * dz) - R[i] - R[j], 0.0); } int find(int *a, int i) { return a[i] == i ? i : (a[i] = find(a, a[i])); } void unite(int *a, int i, int j) { if (find(a, i) != find(a, j)) a[find(a, i)] = find(a, j); } int main(void) { while (cin >> N, N) { REP(i, 0, N) cin >> X[i] >> Y[i] >> Z[i] >> R[i]; vector<edge> e; REP(i, 0, N) REP(j, i + 1, N) e.push_back((edge){i, j, dist(i, j)}); sort(e.begin(), e.end()); double ans = 0.0; int g[100]; REP(i, 0, N) g[i] = i; REP(i, 0, e.size()) if (find(g, e[i].p) != find(g, e[i].q)) { ans += e[i].d; unite(g, e[i].p, e[i].q); } printf("%.3lf\n", ans); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
466
#include <bits/stdc++.h> #define DEBUG(C) cerr << #C << " = " << C << endl using namespace std; const int MAX = 111; double x[MAX], y[MAX], z[MAX], r[MAX]; bool vis[MAX]; inline double dist(int p, int q) { return max<double>(0.0, sqrt((x[p] - x[q]) * (x[p] - x[q]) + (y[p] - y[q]) * (y[p] - y[q]) + (z[p] - z[q]) * (z[p] - z[q])) - r[p] - r[q]); } void solve(int n) { memset(vis, false, sizeof(vis)); for (int i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", x + i, y + i, z + i, r + i); } priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> pq; pq.push(make_pair(0, 0)); double ans = 0; while (!pq.empty()) { auto e = pq.top(); pq.pop(); if (vis[e.second]) { continue; } vis[e.second] = true; ans += e.first; for (int i = 0; i < n; i++) { if (i != e.second && !vis[i]) { pq.push(make_pair(dist(e.second, i), i)); } } } printf("%.10lf\n", ans); } int main() { while (true) { int n; scanf("%d", &n); if (n == 0) { break; } solve(n); } return 0; }
#include <bits/stdc++.h> #define DEBUG(C) cerr << #C << " = " << C << endl using namespace std; const int MAX = 111; double x[MAX], y[MAX], z[MAX], r[MAX]; bool vis[MAX]; inline double dist(int p, int q) { return max<double>(0.0, sqrt((x[p] - x[q]) * (x[p] - x[q]) + (y[p] - y[q]) * (y[p] - y[q]) + (z[p] - z[q]) * (z[p] - z[q])) - r[p] - r[q]); } void solve(int n) { memset(vis, false, sizeof(vis)); for (int i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", x + i, y + i, z + i, r + i); } priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> pq; pq.push(make_pair(0, 0)); double ans = 0; while (!pq.empty()) { auto e = pq.top(); pq.pop(); if (vis[e.second]) { continue; } vis[e.second] = true; ans += e.first; for (int i = 0; i < n; i++) { if (i != e.second && !vis[i]) { pq.push(make_pair(dist(e.second, i), i)); } } } printf("%.3lf\n", ans); } int main() { while (true) { int n; scanf("%d", &n); if (n == 0) { break; } solve(n); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
416
#include <algorithm> #include <complex> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, N) for (ll i = 0; i < N; ++i) #define FOR(i, a, b) for (ll i = a; i < b; ++i) #define ALL(a) (a).begin(), (a).end() #define pb push_back #define INF (long long)1000000000 #define MOD 1000000007 #define EPS (long double)1e-8 using namespace std; typedef long long ll; typedef pair<ll, ll> P; int dh[4] = {1, 0, -1, 0}; int dw[4] = {0, 1, 0, -1}; vector<vector<long double>> cost; // cost[u][v] ?????? e = (u, v) // ????????????(?????¨???????????´?????? INF) vector<long double> mincost; // ?????? X ???????????????????°?????????? vector<bool> used; // ?????? i ??? X ???????????????????????? ll V; // ????????° long double prim() { REP(i, V) { mincost[i] = INF; used[i] = false; } mincost[0] = 0; long double res = 0; while (true) { ll v = -1; REP(u, V) { if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u; } if (v == -1) break; used[v] = true; res += mincost[v]; REP(u, V) { mincost[u] = min(mincost[u], cost[v][u]); } } return res; } int main(void) { while (true) { cin >> V; if (V == 0) break; long double p[V][4]; cost.resize(0); cost.resize(V); REP(i, V) cost[i].resize(V); mincost.resize(0); mincost.resize(V); used.resize(0); used.resize(V); REP(i, V) cin >> p[i][0] >> p[i][1] >> p[i][2] >> p[i][3]; REP(i, V) { REP(j, V) { if (i == j) cost[i][j] = INF; else { long double dist = sqrt(pow(p[i][0] - p[j][0], 2) + pow(p[i][1] - p[j][1], 2) + pow(p[i][2] - p[j][2], 2)); if (p[i][3] + p[j][3] - dist > EPS) cost[i][j] = 0; else cost[i][j] = dist - (p[i][3] + p[j][3]); } } } cout << fixed << showpoint << setprecision(39) << prim() << endl; } }
#include <algorithm> #include <complex> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, N) for (ll i = 0; i < N; ++i) #define FOR(i, a, b) for (ll i = a; i < b; ++i) #define ALL(a) (a).begin(), (a).end() #define pb push_back #define INF (long long)1000000000 #define MOD 1000000007 #define EPS (long double)1e-8 using namespace std; typedef long long ll; typedef pair<ll, ll> P; int dh[4] = {1, 0, -1, 0}; int dw[4] = {0, 1, 0, -1}; vector<vector<long double>> cost; // cost[u][v] ?????? e = (u, v) // ????????????(?????¨???????????´?????? INF) vector<long double> mincost; // ?????? X ???????????????????°?????????? vector<bool> used; // ?????? i ??? X ???????????????????????? ll V; // ????????° long double prim() { REP(i, V) { mincost[i] = INF; used[i] = false; } mincost[0] = 0; long double res = 0; while (true) { ll v = -1; REP(u, V) { if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u; } if (v == -1) break; used[v] = true; res += mincost[v]; REP(u, V) { mincost[u] = min(mincost[u], cost[v][u]); } } return res; } int main(void) { while (true) { cin >> V; if (V == 0) break; long double p[V][4]; cost.resize(0); cost.resize(V); REP(i, V) cost[i].resize(V); mincost.resize(0); mincost.resize(V); used.resize(0); used.resize(V); REP(i, V) cin >> p[i][0] >> p[i][1] >> p[i][2] >> p[i][3]; REP(i, V) { REP(j, V) { if (i == j) cost[i][j] = INF; else { long double dist = sqrt(pow(p[i][0] - p[j][0], 2) + pow(p[i][1] - p[j][1], 2) + pow(p[i][2] - p[j][2], 2)); if (p[i][3] + p[j][3] - dist > EPS) cost[i][j] = 0; else cost[i][j] = dist - (p[i][3] + p[j][3]); } } } cout << fixed << showpoint << setprecision(3) << prim() << endl; } }
[["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
600
#include "bits/stdc++.h" #define REP(i, n) for (ll i = 0; i < n; ++i) #define RREP(i, n) for (ll i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (ll i = m; i < n; ++i) #define RFOR(i, m, n) for (ll i = n - 1; i >= m; --i) #define ALL(v) (v).begin(), (v).end() #define PB(a) push_back(a) #define UNIQUE(v) v.erase(unique(ALL(v)), v.end()); #define DUMP(v) \ REP(aa, (v).size()) { \ cout << v[a]; \ if (a != v.size() - 1) \ cout << " "; \ else \ cout << endl; \ } #define INF 1000000001ll #define MOD 1000000007ll #define EPS 1e-9 const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; ll max(ll a, int b) { return max(a, ll(b)); } ll max(int a, ll b) { return max(ll(a), b); } ll min(ll a, int b) { return min(a, ll(b)); } ll min(int a, ll b) { return min(ll(a), b); } ///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)/// struct UnionFind { vector<int> par; vector<int> ran; UnionFind(int n) { par.resize(n); ran.resize(n); REP(i, n) { par[i] = i; ran[i] = 0; } } int root(int a) { return par[a] == a ? a : par[a] = root(par[a]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (ran[x] < ran[y]) { par[x] = y; } else { par[y] = x; if (ran[x] == ran[y]) ran[x]++; } } }; struct E { int from, to; double cost; E(int a, int b, double c) { from = a; to = b; cost = c; } E(); bool operator<(E b) { return this->cost < b.cost; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; while (cin >> n, n) { vector<double> x(n), y(n), z(n), r(n); REP(i, n) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } vector<E> v; REP(i, n) { REP(j, n) { v.push_back(E(i, j, max(double(0), sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])) - r[i] - r[j]))); } } sort(ALL(v)); UnionFind uf(n); double ans = 0; REP(i, v.size()) { if (!uf.same(v[i].from, v[i].to)) { uf.unite(v[i].from, v[i].to); ans += v[i].cost; } } cout << fixed << setprecision(12) << ans << endl; } }
#include "bits/stdc++.h" #define REP(i, n) for (ll i = 0; i < n; ++i) #define RREP(i, n) for (ll i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (ll i = m; i < n; ++i) #define RFOR(i, m, n) for (ll i = n - 1; i >= m; --i) #define ALL(v) (v).begin(), (v).end() #define PB(a) push_back(a) #define UNIQUE(v) v.erase(unique(ALL(v)), v.end()); #define DUMP(v) \ REP(aa, (v).size()) { \ cout << v[a]; \ if (a != v.size() - 1) \ cout << " "; \ else \ cout << endl; \ } #define INF 1000000001ll #define MOD 1000000007ll #define EPS 1e-9 const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; ll max(ll a, int b) { return max(a, ll(b)); } ll max(int a, ll b) { return max(ll(a), b); } ll min(ll a, int b) { return min(a, ll(b)); } ll min(int a, ll b) { return min(ll(a), b); } ///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)/// struct UnionFind { vector<int> par; vector<int> ran; UnionFind(int n) { par.resize(n); ran.resize(n); REP(i, n) { par[i] = i; ran[i] = 0; } } int root(int a) { return par[a] == a ? a : par[a] = root(par[a]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (ran[x] < ran[y]) { par[x] = y; } else { par[y] = x; if (ran[x] == ran[y]) ran[x]++; } } }; struct E { int from, to; double cost; E(int a, int b, double c) { from = a; to = b; cost = c; } E(); bool operator<(E b) { return this->cost < b.cost; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; while (cin >> n, n) { vector<double> x(n), y(n), z(n), r(n); REP(i, n) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } vector<E> v; REP(i, n) { REP(j, n) { v.push_back(E(i, j, max(double(0), sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])) - r[i] - r[j]))); } } sort(ALL(v)); UnionFind uf(n); double ans = 0; REP(i, v.size()) { if (!uf.same(v[i].from, v[i].to)) { uf.unite(v[i].from, v[i].to); ans += v[i].cost; } } cout << fixed << setprecision(3) << ans << endl; } }
[["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
834
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; #define MAX 100 typedef pair<int, int> pii; typedef pair<double, pii> pp; struct union_find { int rnk[MAX], par[MAX]; union_find(int n) { for (int i = 0; i < n; i++) rnk[i] = 1, par[i] = i; } int find(int x) { if (x == par[x]) return x; else return par[x] = find(par[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (rnk[x] > rnk[y]) par[y] = x; else { par[x] = y; if (rnk[x] == rnk[y]) rnk[y]++; } return true; } }; inline double cor(vector<double> a, vector<double> b) { double ret = sqrt((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]) + (a[2] - b[2]) * (a[2] - b[2])); ret -= a[3] + b[3]; if (ret <= 0) return 0; else return ret; } int main(void) { int n; while (cin >> n && n) { vector<vector<double>> v(n, vector<double>(4)); for (int i = 0; i < n; i++) cin >> v[i][0] >> v[i][1] >> v[i][2] >> v[i][3]; vector<vector<double>> l(n, vector<double>(n)); priority_queue<pp, vector<pp>, greater<pp>> que; union_find uf(n); for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { pp t = make_pair(cor(v[i], v[j]), make_pair(i, j)); que.push(t); } } double ans = 0; while (!que.empty()) { pp t = que.top(); que.pop(); if (uf.unite(t.second.first, t.second.second)) ans += t.first; } printf("%.8lf\n", ans); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; #define MAX 100 typedef pair<int, int> pii; typedef pair<double, pii> pp; struct union_find { int rnk[MAX], par[MAX]; union_find(int n) { for (int i = 0; i < n; i++) rnk[i] = 1, par[i] = i; } int find(int x) { if (x == par[x]) return x; else return par[x] = find(par[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (rnk[x] > rnk[y]) par[y] = x; else { par[x] = y; if (rnk[x] == rnk[y]) rnk[y]++; } return true; } }; inline double cor(vector<double> a, vector<double> b) { double ret = sqrt((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]) + (a[2] - b[2]) * (a[2] - b[2])); ret -= a[3] + b[3]; if (ret <= 0) return 0; else return ret; } int main(void) { int n; while (cin >> n && n) { vector<vector<double>> v(n, vector<double>(4)); for (int i = 0; i < n; i++) cin >> v[i][0] >> v[i][1] >> v[i][2] >> v[i][3]; vector<vector<double>> l(n, vector<double>(n)); priority_queue<pp, vector<pp>, greater<pp>> que; union_find uf(n); for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { pp t = make_pair(cor(v[i], v[j]), make_pair(i, j)); que.push(t); } } double ans = 0; while (!que.empty()) { pp t = que.top(); que.pop(); if (uf.unite(t.second.first, t.second.second)) ans += t.first; } printf("%.3lf\n", ans); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
595
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <queue> #include <vector> using namespace std; const double eps = 1e-9; const double inf = 1e9; int main() { while (1) { int n; cin >> n; if (n == 0) break; vector<double> x(n), y(n), z(n), r(n); for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } vector<vector<double>> dist(n, vector<double>(n, inf)); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { double d = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])); if (d - r[i] - r[j] < eps) { dist[i][j] = dist[j][i] = 0.0; } else { dist[i][j] = dist[j][i] = d - r[i] - r[j]; } } } vector<int> used(n, false); double total = 0.0; priority_queue<pair<double, int>> pq; pq.push(make_pair(0.0, 0)); while (!pq.empty()) { double cost = -pq.top().first; int v = pq.top().second; pq.pop(); if (used[v]) continue; used[v] = true; total += cost; for (int i = 0; i < n; i++) { pq.push(make_pair(-dist[v][i], i)); } } cout << fixed; cout << setprecision(5); cout << total << endl; } return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <queue> #include <vector> using namespace std; const double eps = 1e-9; const double inf = 1e9; int main() { while (1) { int n; cin >> n; if (n == 0) break; vector<double> x(n), y(n), z(n), r(n); for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } vector<vector<double>> dist(n, vector<double>(n, inf)); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { double d = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])); if (d - r[i] - r[j] < eps) { dist[i][j] = dist[j][i] = 0.0; } else { dist[i][j] = dist[j][i] = d - r[i] - r[j]; } } } vector<int> used(n, false); double total = 0.0; priority_queue<pair<double, int>> pq; pq.push(make_pair(0.0, 0)); while (!pq.empty()) { double cost = -pq.top().first; int v = pq.top().second; pq.pop(); if (used[v]) continue; used[v] = true; total += cost; for (int i = 0; i < n; i++) { pq.push(make_pair(-dist[v][i], i)); } } cout << fixed; cout << setprecision(3); cout << total << endl; } return 0; }
[["-", 0, 1, 0, 16, 12, 2, 3, 4, 0, 13], ["+", 0, 1, 0, 16, 12, 2, 3, 4, 0, 13]]
1
472
#include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; double prim(int n, vector<vector<double>> &G) { double ans = 0; priority_queue<pair<double, int>> wait; bool connected[n]; fill(connected, connected + n, false); wait.push(make_pair(0.0, 0)); while (!wait.empty()) { double cost = -wait.top().first; int v = wait.top().second; wait.pop(); if (connected[v]) continue; ans += cost; connected[v] = true; for (int i = 0; i < n; ++i) { if (!connected[i]) { wait.push(make_pair(-G[v][i], i)); } } } return ans; } int main() { int n; while (cin >> n, n) { double x[n], y[n], z[n], r[n]; for (int i = 0; i < n; ++i) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } vector<vector<double>> dist(n, vector<double>(n, 0)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { dist[i][j] = max(0.0, sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])) - r[i] - r[j]); } } printf("%.12f\n", prim(n, dist)); } return 0; }
#include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; double prim(int n, vector<vector<double>> &G) { double ans = 0.0; priority_queue<pair<double, int>> wait; bool connected[n]; fill(connected, connected + n, false); wait.push(make_pair(0.0, 0)); while (!wait.empty()) { double cost = -wait.top().first; int v = wait.top().second; wait.pop(); if (connected[v]) continue; ans += cost; connected[v] = true; for (int i = 0; i < n; ++i) { if (!connected[i]) { wait.push(make_pair(-G[v][i], i)); } } } return ans; } int main() { int n; while (cin >> n, n) { double x[n], y[n], z[n], r[n]; for (int i = 0; i < n; ++i) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } vector<vector<double>> dist(n, vector<double>(n, 0)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { dist[i][j] = max(0.0, sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])) - r[i] - r[j]); } } printf("%.3f\n", prim(n, dist)); } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
433
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <string> #include <tuple> #include <vector> #define REP(i, a, b) for (int i = int(a); i < int(b); i++) #define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl using namespace std; typedef long long int lli; typedef tuple<double, int, int> T; class UnionFind { public: vector<int> par; int size; UnionFind(int n) { size = n; par.resize(n); REP(i, 0, n) par[i] = i; } int find(int n) { return (n == par[n] ? n : par[n] = find(par[n])); } bool unite(int a, int b) { a = find(a); b = find(b); if (a == b) return false; par[a] = b; size--; } bool same(int a, int b) { return find(a) == find(b); } }; int main() { int n; while (cin >> n, n) { vector<double> x(n), y(n), z(n), r(n); REP(i, 0, n) cin >> x[i] >> y[i] >> z[i] >> r[i]; UnionFind uf(n); vector<T> v; v.reserve(n * n); REP(i, 0, n) REP(j, i + 1, n) { double dis = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])); v.push_back(make_tuple(max(0.0, dis - r[i] - r[j]), i, j)); } sort(begin(v), end(v)); double ans = 0; REP(i, 0, v.size()) { T tp = v[i]; if (uf.unite(get<1>(tp), get<2>(tp))) { ans += get<0>(tp); } } cout << fixed << setprecision(3) << ans << endl; } return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <string> #include <tuple> #include <vector> #define REP(i, a, b) for (int i = int(a); i < int(b); i++) #define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl using namespace std; typedef long long int lli; typedef tuple<double, int, int> T; class UnionFind { public: vector<int> par; int size; UnionFind(int n) { size = n; par.resize(n); REP(i, 0, n) par[i] = i; } int find(int n) { return (n == par[n] ? n : par[n] = find(par[n])); } bool unite(int a, int b) { a = find(a); b = find(b); if (a == b) return false; par[a] = b; size--; return true; } bool same(int a, int b) { return find(a) == find(b); } }; int main() { int n; while (cin >> n, n) { vector<double> x(n), y(n), z(n), r(n); REP(i, 0, n) cin >> x[i] >> y[i] >> z[i] >> r[i]; UnionFind uf(n); vector<T> v; v.reserve(n * n); REP(i, 0, n) REP(j, i + 1, n) { double dis = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j])); v.push_back(make_tuple(max(0.0, dis - r[i] - r[j]), i, j)); } sort(begin(v), end(v)); double ans = 0; REP(i, 0, v.size()) { T tp = v[i]; if (uf.unite(get<1>(tp), get<2>(tp))) { ans += get<0>(tp); } } cout << fixed << setprecision(3) << ans << endl; } return 0; }
[["+", 8, 123, 0, 14, 8, 9, 0, 37, 0, 38], ["+", 8, 123, 0, 14, 8, 9, 0, 37, 0, 146], ["+", 8, 123, 0, 14, 8, 9, 0, 37, 0, 35]]
1
515
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdlib.h> #include <string.h> #include <string> #include <utility> #include <vector> using namespace std; #define ll long long #define ld long double #define EPS 0.0000001 #define INF 1e9 #define MOD 1000000007 #define rep(i, n) for (i = 0; i < (n); i++) #define loop(i, a, n) for (i = a; i < (n); i++) #define all(in) in.begin(), in.end() #define shosu(x) fixed << setprecision(x) typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; typedef vector<double> vd; #define max_n 100 #define max_e (max_n * max_n) double c[max_n][4]; double d(int a, int b) { double x = c[a][0] - c[b][0]; double y = c[a][1] - c[b][1]; double z = c[a][2] - c[b][2]; double ret = sqrt(x * x + y * y + z * z); ret = ret - c[a][3] - c[b][3]; return ret; } class UnionFind { private: vector<int> rank, p; void makeSet(int x) { p[x] = x; rank[x] = 0; } public: UnionFind(){}; UnionFind(int size) { rank.resize(size, 0); p.resize(size, 0); for (int i = 0; i < size; i++) { makeSet(i); } } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { link(find(x), find(y)); } int find(int x) { if (x != p[x]) p[x] = find(p[x]); return p[x]; } private: 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]++; } } }; struct edge { int u, v; double cost; }; bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; } edge es[max_e]; int v, e; double kruskal() { sort(es, es + e, comp); UnionFind uf = UnionFind(v); double ret = 0; int i; rep(i, e) { edge ee = es[i]; if (!uf.same(ee.u, ee.v)) { uf.unite(ee.u, ee.v); if (ee.cost > EPS) ret += ee.cost; } } return ret; } int main(void) { int i, j; int n; while (cin >> n, n) { v = n; e = 0; rep(i, n) rep(j, 4) cin >> c[i][j]; double ans = 0; rep(i, n) rep(j, i) { edge ee; ee.u = i; ee.v = j; ee.cost = d(i, j); es[e] = ee; e++; } cout << shosu(5) << kruskal() << endl; } }
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdlib.h> #include <string.h> #include <string> #include <utility> #include <vector> using namespace std; #define ll long long #define ld long double #define EPS 0.0000001 #define INF 1e9 #define MOD 1000000007 #define rep(i, n) for (i = 0; i < (n); i++) #define loop(i, a, n) for (i = a; i < (n); i++) #define all(in) in.begin(), in.end() #define shosu(x) fixed << setprecision(x) typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; typedef vector<double> vd; #define max_n 100 #define max_e (max_n * max_n) double c[max_n][4]; double d(int a, int b) { double x = c[a][0] - c[b][0]; double y = c[a][1] - c[b][1]; double z = c[a][2] - c[b][2]; double ret = sqrt(x * x + y * y + z * z); ret = ret - c[a][3] - c[b][3]; return ret; } class UnionFind { private: vector<int> rank, p; void makeSet(int x) { p[x] = x; rank[x] = 0; } public: UnionFind(){}; UnionFind(int size) { rank.resize(size, 0); p.resize(size, 0); for (int i = 0; i < size; i++) { makeSet(i); } } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { link(find(x), find(y)); } int find(int x) { if (x != p[x]) p[x] = find(p[x]); return p[x]; } private: 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]++; } } }; struct edge { int u, v; double cost; }; bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; } edge es[max_e]; int v, e; double kruskal() { sort(es, es + e, comp); UnionFind uf = UnionFind(v); double ret = 0; int i; rep(i, e) { edge ee = es[i]; if (!uf.same(ee.u, ee.v)) { uf.unite(ee.u, ee.v); if (ee.cost > EPS) ret += ee.cost; } } return ret; } int main(void) { int i, j; int n; while (cin >> n, n) { v = n; e = 0; rep(i, n) rep(j, 4) cin >> c[i][j]; double ans = 0; rep(i, n) rep(j, i) { edge ee; ee.u = i; ee.v = j; ee.cost = d(i, j); es[e] = ee; e++; } cout << shosu(3) << kruskal() << endl; } }
[["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
741
// clang-format off #include <bits/stdc++.h> using namespace std; #define int long long #define main signed main() #define loop(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) loop(i, 0, n) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define prec(n) fixed << setprecision(n) constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000; constexpr int MOD = 1000000007; constexpr double PI = 3.14159265358979; template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; } template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; } bool odd(const int &n) { return n & 1; } bool even(const int &n) { return ~n & 1; } template<typename T = int> T in() { T x; cin >> x; return x; } template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; } template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; } template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; } template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; } template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; } template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; } template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); } template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); } // clang-format on using Weight = double; struct Edge { int src, dst; Weight weight; Edge(const int &s = 0, const int &d = 0, const Weight &w = 0) : src(s), dst(d), weight(w) {} }; using Edges = vector<Edge>; using Array = vector<Weight>; using Matrix = vector<Array>; class Graph { vector<Edges> g; using iterator = vector<Edges>::iterator; using const_iterator = vector<Edges>::const_iterator; public: Graph(const int &size = 0) : g(size) {} size_t size() const { return g.size(); } const Edges &operator[](const int &i) const { return g[i]; } void addArc(const int &src, const int &dst, const Weight &w = 1) { g[src].emplace_back(src, dst, w); } void addEdge(const int &node1, const int &node2, const Weight &w = 1) { addArc(node1, node2, w); addArc(node2, node1, w); } iterator begin() { return g.begin(); } const_iterator begin() const { return g.begin(); } iterator end() { return g.end(); } const_iterator end() const { return g.end(); } }; class UnionFind { vector<int> tree, rank, setSize; public: UnionFind(const int &n) : tree(n), rank(n), setSize(n, 1) { iota(all(tree), 0); } int root(const int &x) { return tree[x] == x ? x : tree[x] = root(tree[x]); } bool same(const int &a, const int &b) { return root(a) == root(b); } bool unite(int a, int b) { a = root(a), b = root(b); if (a == b) return false; if (rank[a] < rank[b]) swap(a, b); tree[b] = a; setSize[a] += setSize[b]; if (rank[a] == rank[b]) rank[a]++; return true; } int setSizeOf(const int &x) { return setSize[root(x)]; } int size() { return tree.size(); } }; pair<Weight, Edges> kruskal(const Graph &g) { UnionFind uf(g.size()); Edges es; for (auto &adj : g) { for (auto &e : adj) es.emplace_back(e); } sort(es.begin(), es.end(), [](const Edge &e, const Edge &f) { return e.weight < f.weight; }); Weight total = 0; Edges tree; for (auto &e : es) { if (uf.unite(e.src, e.dst)) tree.push_back(e), total += e.weight; } return {total, tree}; } main { while (true) { int n = in(); if (n == 0) break; vector<double> x(n), y(n), z(n), r(n); rep(i, n) cin >> x[i] >> y[i] >> z[i] >> r[i]; Graph g(n); rep(i, n) loop(j, i + 1, n) { double dx = abs(x[i] - x[j]), dy = abs(y[i] - y[j]), dz = abs(z[i] - z[j]); double d = max(0.0, sqrt(dx * dx + dy * dy + dz * dz) - r[i] - r[j]); g.addEdge(i, j, d); } cout << prec(32) << kruskal(g).first << endl; } }
// clang-format off #include <bits/stdc++.h> using namespace std; #define int long long #define main signed main() #define loop(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) loop(i, 0, n) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define prec(n) fixed << setprecision(n) constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000; constexpr int MOD = 1000000007; constexpr double PI = 3.14159265358979; template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; } template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; } bool odd(const int &n) { return n & 1; } bool even(const int &n) { return ~n & 1; } template<typename T = int> T in() { T x; cin >> x; return x; } template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; } template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; } template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; } template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; } template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; } template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; } template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); } template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); } // clang-format on using Weight = double; struct Edge { int src, dst; Weight weight; Edge(const int &s = 0, const int &d = 0, const Weight &w = 0) : src(s), dst(d), weight(w) {} }; using Edges = vector<Edge>; using Array = vector<Weight>; using Matrix = vector<Array>; class Graph { vector<Edges> g; using iterator = vector<Edges>::iterator; using const_iterator = vector<Edges>::const_iterator; public: Graph(const int &size = 0) : g(size) {} size_t size() const { return g.size(); } const Edges &operator[](const int &i) const { return g[i]; } void addArc(const int &src, const int &dst, const Weight &w = 1) { g[src].emplace_back(src, dst, w); } void addEdge(const int &node1, const int &node2, const Weight &w = 1) { addArc(node1, node2, w); addArc(node2, node1, w); } iterator begin() { return g.begin(); } const_iterator begin() const { return g.begin(); } iterator end() { return g.end(); } const_iterator end() const { return g.end(); } }; class UnionFind { vector<int> tree, rank, setSize; public: UnionFind(const int &n) : tree(n), rank(n), setSize(n, 1) { iota(all(tree), 0); } int root(const int &x) { return tree[x] == x ? x : tree[x] = root(tree[x]); } bool same(const int &a, const int &b) { return root(a) == root(b); } bool unite(int a, int b) { a = root(a), b = root(b); if (a == b) return false; if (rank[a] < rank[b]) swap(a, b); tree[b] = a; setSize[a] += setSize[b]; if (rank[a] == rank[b]) rank[a]++; return true; } int setSizeOf(const int &x) { return setSize[root(x)]; } int size() { return tree.size(); } }; pair<Weight, Edges> kruskal(const Graph &g) { UnionFind uf(g.size()); Edges es; for (auto &adj : g) { for (auto &e : adj) es.emplace_back(e); } sort(es.begin(), es.end(), [](const Edge &e, const Edge &f) { return e.weight < f.weight; }); Weight total = 0; Edges tree; for (auto &e : es) { if (uf.unite(e.src, e.dst)) tree.push_back(e), total += e.weight; } return {total, tree}; } main { while (true) { int n = in(); if (n == 0) break; vector<double> x(n), y(n), z(n), r(n); rep(i, n) cin >> x[i] >> y[i] >> z[i] >> r[i]; Graph g(n); rep(i, n) loop(j, i + 1, n) { double dx = abs(x[i] - x[j]), dy = abs(y[i] - y[j]), dz = abs(z[i] - z[j]); double d = max(0.0, sqrt(dx * dx + dy * dy + dz * dz) - r[i] - r[j]); g.addEdge(i, j, d); } cout << prec(3) << kruskal(g).first << endl; } }
[["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
1,457
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdlib> #include <cstring> #include <deque> #include <exception> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<char> vc; typedef vector<string> vs; typedef vector<P> vp; #define rep(i, a, n) for (ll i = (a); i < (n); i++) #define per(i, a, n) for (ll i = (a); i > (n); i--) #define lep(i, a, n) for (ll i = (a); i <= (n); i++) #define pel(i, a, n) for (ll i = (a); i >= (n); i--) #define clr(a, b) memset((a), (b), sizeof(a)) #define pb push_back #define all(c) (c).begin(), (c).end() #define sz size() #define print(X) cout << (X) << "\n" static const int INF = 1e+9 + 7; ll n, m, l; string s, t; ll d[200010], dp[300][300]; char field[200][200]; #define MAX 10000 template <class TYPE> class Prim { private: struct Edge { int to; TYPE cost; }; static const int INF = 1e+9 + 7; vector<Edge> G[MAX]; TYPE mincost[MAX]; bool used[MAX]; int N; int m; public: // Prim(){ // cin >> N >> m; // for(int i = 0;i < m;i++){ // int p,q; // TYPE r; // cin >> p >> q >> r; // G[p].pb(Edge{q,r}); // G[q].pb(Edge{p,r}); // } // } Prim(int n, int l) { N = n; m = l; } void in(int p, int q, TYPE r) { // cin >> p >> q >> r; G[p].pb(Edge{q, r}); G[q].pb(Edge{p, r}); } TYPE prim() { TYPE res = 0; for (int i = 0; i < N; i++) { mincost[i] = INF; used[i] = false; } mincost[0] = (TYPE)0; while (1) { int v = -1; for (int i = 0; i < N; i++) { if (!used[i] && (v == -1 || mincost[i] < mincost[v])) v = i; } if (v == -1) break; used[v] = true; res += mincost[v]; for (int i = 0; i < G[v].size(); i++) { mincost[G[v][i].to] = min(mincost[G[v][i].to], G[v][i].cost); } } return res; } }; namespace Geometry { #define equals(a, b) (fabs((a) - (b)) < EPS) #define BOTTOM 0 #define LEFT 1 #define RIGHT 2 #define TOP 3 #define gprint(X) cout << fixed << setprecision(8) << (X) << endl #define gprints(X, Y) \ cout << fixed << setprecision(8) << (X) << " " << (Y) << endl const double EPS = 1e-10; static const int COUNTER_CLOCKWISE = 1; static const int CLOCKWISE = -1; static const int ONLINE_BACK = 2; static const int ONLINE_FRONT = -2; static const int ON_SEGMENT = 0; class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator+=(Point p) { return Point(x += p.x, y += p.y); } Point operator-=(Point p) { return Point(x -= p.x, y -= p.y); } Point operator*(double a) { return Point(a * x, a * y); } Point operator/(double a) { return Point(x / a, y / a); } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return equals(x, p.x) && equals(y, p.y); } double abs() { return sqrt(norm()); } double norm() { return x * x + y * y; } friend ostream &operator<<(ostream &os, Point &p) { return os << p.x << " " << p.y; } friend istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; } }; struct Segment { Point p1, p2; Segment(){}; Segment(Point p1, Point p2) : p1(p1), p2(p2){}; friend ostream &operator<<(ostream &os, Segment &seg) { return os << seg.p1 << " " << seg.p2; } friend istream &operator>>(istream &is, Segment &seg) { return is >> seg.p1 >> seg.p2; } }; class Circle { public: Point c; double r; Circle(Point c = Point(), double r = 0.0) : c(c), r(r) {} friend ostream &operator<<(ostream &os, Circle &c) { return os << c.c << " " << c.r; } friend istream &operator>>(istream &is, Circle &c) { return is >> c.c >> c.r; } }; typedef Point Vector; typedef Segment Line; typedef vector<Point> Polygon; double norm(Vector a) { return a.x * a.x + a.y * a.y; } double abs(Vector a) { return sqrt(norm(a)); } double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } bool Orthogonal(Vector a, Vector b) { return equals(dot(a, b), 0.0); } bool Orthogonal(Point a1, Point a2, Point b1, Point b2) { return Orthogonal(a1 - a2, b1 - b2); } bool Orthogonal(Segment s1, Segment s2) { return equals(dot(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } bool Parallel(Vector a, Vector b) { return equals(cross(a, b), 0.0); } bool Parallel(Point a1, Point a2, Point b1, Point b2) { return Parallel(a1 - a2, b1 - b2); } bool Parallel(Segment s1, Segment s2) { return equals(cross(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } bool less(const Point &a, const Point &b) { return (a.x != b.x) ? a.x < b.x : a.y < b.y; } bool greater(const Point &a, const Point b) { return !less(a, b); } Point project(Segment s, Point p) { Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / norm(base); return s.p1 + base * r; } Point reflect(Segment s, Point p) { return p + (project(s, p) - p) * 2.0; } int ccw(Point &p0, Point p1, Point p2) { p1 -= p0; p2 -= p0; if (cross(p1, p2) > EPS) return COUNTER_CLOCKWISE; if (cross(p1, p2) < -EPS) return CLOCKWISE; if (dot(p1, p2) < -EPS) return ONLINE_BACK; if (p1.norm() < p2.norm()) return ONLINE_FRONT; return ON_SEGMENT; } bool intersect(Point p1, Point p2, Point p3, Point p4) { return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0); } bool intersect(Segment s1, Segment s2) { return intersect(s1.p1, s1.p2, s2.p1, s2.p2); } double getDistance(Vector a, Vector b) { return abs(a - b); } double getDistanceLP(Line l, Point p) { return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1)); } double getDistanceSP(Segment s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return abs(p - s.p1); if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return abs(p - s.p2); return getDistanceLP(s, p); } double getDistance(Segment s1, Segment s2) { if (intersect(s1, s2)) return 0.0; return min(min(getDistanceSP(s1, s2.p1), getDistanceSP(s1, s2.p2)), min(getDistanceSP(s2, s1.p1), getDistanceSP(s2, s1.p2))); } Point getCrossPoint(Segment s1, Segment s2) { Vector base = s2.p2 - s2.p1; double d1 = abs(cross(base, s1.p1 - s2.p1)); double d2 = abs(cross(base, s1.p2 - s2.p1)); double t = d1 / (d1 + d2); return s1.p1 + (s1.p2 - s1.p1) * t; } pair<Point, Point> getCrossPoints(Circle c, Line l) { Vector pr = project(l, c.c); Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1); double base = sqrt(c.r * c.r - norm(pr - c.c)); return make_pair(pr + e * base, pr - e * base); } double arg(Vector p) { return atan2(p.y, p.x); } Vector polar(double a, double r) { return Point(cos(r) * a, sin(r) * a); } pair<Point, Point> getCrossPoints(Circle c1, Circle c2) { double d = abs(c1.c - c2.c); double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); double t = arg(c2.c - c1.c); return make_pair(c1.c + polar(c1.r, t + a), c1.c + polar(c1.r, t - a)); } int contains(Polygon g, Point p) { int n = g.size(); bool x = false; for (int i = 0; i < n; i++) { Point a = g[i] - p, b = g[(i + 1) % n] - p; if (abs(cross(a, b)) < EPS && dot(a, b) < EPS) return 1; if (a.y > b.y) swap(a, b); if (a.y < EPS && EPS < b.y && cross(a, b) > EPS) x = !x; } return (x ? 2 : 0); } //=====================================================// //==================== Andrew Scan ====================// //=====================================================// int Accw(Point p0, Point p1, Point p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > EPS) return 1; return -1; } Polygon andrewScan(Polygon s) { Polygon ans; Polygon u, l; if (s.size() < 3) return s; sort(s.begin(), s.end()); u.push_back(s[0]); u.push_back(s[1]); l.push_back(s[s.size() - 1]); l.push_back(s[s.size() - 2]); for (int i = 2; i < s.size(); i++) { for (int n = u.size(); n >= 2 && Accw(u[n - 2], u[n - 1], s[i]) != -1; n--) { u.pop_back(); } u.push_back(s[i]); } for (int i = s.size() - 3; i >= 0; i--) { for (int n = l.size(); n >= 2 && Accw(l[n - 2], l[n - 1], s[i]) != -1; n--) { l.pop_back(); } l.push_back(s[i]); } reverse(l.begin(), l.end()); for (int i = u.size() - 2; i >= 1; i--) { l.push_back(u[i]); } int f = 0; for (int i = 1; i < l.size(); i++) { if (l[f].y > l[i].y || (l[f].y == l[i].y && l[f].x > l[i].x)) { f = i; } } for (int i = 0; i < l.size(); i++) { ans.push_back( Point((int)l[(i + f) % l.size()].x, (int)l[(i + f) % l.size()].y)); } return ans; } //=====================================================// //======================== END ========================// //=====================================================// class EndPoint { public: Point p; int seg, st; EndPoint() {} EndPoint(Point p, int seg, int st) : p(p), seg(seg), st(st) {} bool operator<(const EndPoint &ep) const { if (p.y == ep.p.y) { return st < ep.st; } else { return p.y < ep.p.y; } } }; EndPoint EP[2 * 100000]; int manhattanIntersection(vector<Segment> S) { int n = S.size(); for (int i = 0, k = 0; i < n; i++) { if (S[i].p1.y == S[i].p2.y) { if (S[i].p1.x > S[i].p2.x) swap(S[i].p1, S[i].p2); } else if (S[i].p1.y > S[i].p2.y) { swap(S[i].p1, S[i].p2); } if (S[i].p1.y == S[i].p2.y) { EP[k++] = EndPoint(S[i].p1, i, LEFT); EP[k++] = EndPoint(S[i].p2, i, RIGHT); } else { EP[k++] = EndPoint(S[i].p1, i, BOTTOM); EP[k++] = EndPoint(S[i].p2, i, TOP); } } sort(EP, EP + (2 * n)); set<int> BT; BT.insert(1000000001); int cnt = 0; for (int i = 0; i < 2 * n; i++) { if (EP[i].st == TOP) { BT.erase(EP[i].p.x); } else if (EP[i].st == BOTTOM) { BT.insert(EP[i].p.x); } else if (EP[i].st == LEFT) { set<int>::iterator b = lower_bound(BT.begin(), BT.end(), S[EP[i].seg].p1.x); set<int>::iterator e = upper_bound(BT.begin(), BT.end(), S[EP[i].seg].p2.x); cnt += distance(b, e); } } return cnt; } }; // namespace Geometry using namespace Geometry; class Circle2 { public: double x, y, z; double r; Circle2(double x = 0.0, double y = 0.0, double z = 0.0, double r = 0.0) : x(z), y(y), z(z), r(r) {} friend istream &operator>>(istream &is, Circle2 &c) { return is >> c.x >> c.y >> c.z >> c.r; } }; int main() { while (cin >> n, n) { vector<Circle2> vec; Prim<double> ans(n, n - 1); rep(i, 0, n) { Circle2 a; cin >> a; vec.pb(a); } rep(i, 0, n) { rep(j, i + 1, n) { double distin = 0; double dist1 = sqrt((vec[i].x - vec[j].x) * (vec[i].x - vec[j].x) + (vec[i].y - vec[j].y) * (vec[i].y - vec[j].y) + (vec[i].z - vec[j].z) * (vec[i].z - vec[j].z)); double dist2 = vec[i].r + vec[j].r; if (dist1 > dist2) distin = dist1 - dist2; // printf("%f , %f : %f\n",dist1,dist2,distin); ans.in(i, j, distin); } } cout << fixed << setprecision(8) << ans.prim() << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdlib> #include <cstring> #include <deque> #include <exception> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<char> vc; typedef vector<string> vs; typedef vector<P> vp; #define rep(i, a, n) for (ll i = (a); i < (n); i++) #define per(i, a, n) for (ll i = (a); i > (n); i--) #define lep(i, a, n) for (ll i = (a); i <= (n); i++) #define pel(i, a, n) for (ll i = (a); i >= (n); i--) #define clr(a, b) memset((a), (b), sizeof(a)) #define pb push_back #define all(c) (c).begin(), (c).end() #define sz size() #define print(X) cout << (X) << "\n" static const int INF = 1e+9 + 7; ll n, m, l; string s, t; ll d[200010], dp[300][300]; char field[200][200]; #define MAX 10000 template <class TYPE> class Prim { private: struct Edge { int to; TYPE cost; }; static const int INF = 1e+9 + 7; vector<Edge> G[MAX]; TYPE mincost[MAX]; bool used[MAX]; int N; int m; public: // Prim(){ // cin >> N >> m; // for(int i = 0;i < m;i++){ // int p,q; // TYPE r; // cin >> p >> q >> r; // G[p].pb(Edge{q,r}); // G[q].pb(Edge{p,r}); // } // } Prim(int n, int l) { N = n; m = l; } void in(int p, int q, TYPE r) { // cin >> p >> q >> r; G[p].pb(Edge{q, r}); G[q].pb(Edge{p, r}); } TYPE prim() { TYPE res = 0; for (int i = 0; i < N; i++) { mincost[i] = INF; used[i] = false; } mincost[0] = (TYPE)0; while (1) { int v = -1; for (int i = 0; i < N; i++) { if (!used[i] && (v == -1 || mincost[i] < mincost[v])) v = i; } if (v == -1) break; used[v] = true; res += mincost[v]; for (int i = 0; i < G[v].size(); i++) { mincost[G[v][i].to] = min(mincost[G[v][i].to], G[v][i].cost); } } return res; } }; namespace Geometry { #define equals(a, b) (fabs((a) - (b)) < EPS) #define BOTTOM 0 #define LEFT 1 #define RIGHT 2 #define TOP 3 #define gprint(X) cout << fixed << setprecision(8) << (X) << endl #define gprints(X, Y) \ cout << fixed << setprecision(8) << (X) << " " << (Y) << endl const double EPS = 1e-10; static const int COUNTER_CLOCKWISE = 1; static const int CLOCKWISE = -1; static const int ONLINE_BACK = 2; static const int ONLINE_FRONT = -2; static const int ON_SEGMENT = 0; class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator+=(Point p) { return Point(x += p.x, y += p.y); } Point operator-=(Point p) { return Point(x -= p.x, y -= p.y); } Point operator*(double a) { return Point(a * x, a * y); } Point operator/(double a) { return Point(x / a, y / a); } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return equals(x, p.x) && equals(y, p.y); } double abs() { return sqrt(norm()); } double norm() { return x * x + y * y; } friend ostream &operator<<(ostream &os, Point &p) { return os << p.x << " " << p.y; } friend istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; } }; struct Segment { Point p1, p2; Segment(){}; Segment(Point p1, Point p2) : p1(p1), p2(p2){}; friend ostream &operator<<(ostream &os, Segment &seg) { return os << seg.p1 << " " << seg.p2; } friend istream &operator>>(istream &is, Segment &seg) { return is >> seg.p1 >> seg.p2; } }; class Circle { public: Point c; double r; Circle(Point c = Point(), double r = 0.0) : c(c), r(r) {} friend ostream &operator<<(ostream &os, Circle &c) { return os << c.c << " " << c.r; } friend istream &operator>>(istream &is, Circle &c) { return is >> c.c >> c.r; } }; typedef Point Vector; typedef Segment Line; typedef vector<Point> Polygon; double norm(Vector a) { return a.x * a.x + a.y * a.y; } double abs(Vector a) { return sqrt(norm(a)); } double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } bool Orthogonal(Vector a, Vector b) { return equals(dot(a, b), 0.0); } bool Orthogonal(Point a1, Point a2, Point b1, Point b2) { return Orthogonal(a1 - a2, b1 - b2); } bool Orthogonal(Segment s1, Segment s2) { return equals(dot(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } bool Parallel(Vector a, Vector b) { return equals(cross(a, b), 0.0); } bool Parallel(Point a1, Point a2, Point b1, Point b2) { return Parallel(a1 - a2, b1 - b2); } bool Parallel(Segment s1, Segment s2) { return equals(cross(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } bool less(const Point &a, const Point &b) { return (a.x != b.x) ? a.x < b.x : a.y < b.y; } bool greater(const Point &a, const Point b) { return !less(a, b); } Point project(Segment s, Point p) { Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / norm(base); return s.p1 + base * r; } Point reflect(Segment s, Point p) { return p + (project(s, p) - p) * 2.0; } int ccw(Point &p0, Point p1, Point p2) { p1 -= p0; p2 -= p0; if (cross(p1, p2) > EPS) return COUNTER_CLOCKWISE; if (cross(p1, p2) < -EPS) return CLOCKWISE; if (dot(p1, p2) < -EPS) return ONLINE_BACK; if (p1.norm() < p2.norm()) return ONLINE_FRONT; return ON_SEGMENT; } bool intersect(Point p1, Point p2, Point p3, Point p4) { return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0); } bool intersect(Segment s1, Segment s2) { return intersect(s1.p1, s1.p2, s2.p1, s2.p2); } double getDistance(Vector a, Vector b) { return abs(a - b); } double getDistanceLP(Line l, Point p) { return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1)); } double getDistanceSP(Segment s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return abs(p - s.p1); if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return abs(p - s.p2); return getDistanceLP(s, p); } double getDistance(Segment s1, Segment s2) { if (intersect(s1, s2)) return 0.0; return min(min(getDistanceSP(s1, s2.p1), getDistanceSP(s1, s2.p2)), min(getDistanceSP(s2, s1.p1), getDistanceSP(s2, s1.p2))); } Point getCrossPoint(Segment s1, Segment s2) { Vector base = s2.p2 - s2.p1; double d1 = abs(cross(base, s1.p1 - s2.p1)); double d2 = abs(cross(base, s1.p2 - s2.p1)); double t = d1 / (d1 + d2); return s1.p1 + (s1.p2 - s1.p1) * t; } pair<Point, Point> getCrossPoints(Circle c, Line l) { Vector pr = project(l, c.c); Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1); double base = sqrt(c.r * c.r - norm(pr - c.c)); return make_pair(pr + e * base, pr - e * base); } double arg(Vector p) { return atan2(p.y, p.x); } Vector polar(double a, double r) { return Point(cos(r) * a, sin(r) * a); } pair<Point, Point> getCrossPoints(Circle c1, Circle c2) { double d = abs(c1.c - c2.c); double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); double t = arg(c2.c - c1.c); return make_pair(c1.c + polar(c1.r, t + a), c1.c + polar(c1.r, t - a)); } int contains(Polygon g, Point p) { int n = g.size(); bool x = false; for (int i = 0; i < n; i++) { Point a = g[i] - p, b = g[(i + 1) % n] - p; if (abs(cross(a, b)) < EPS && dot(a, b) < EPS) return 1; if (a.y > b.y) swap(a, b); if (a.y < EPS && EPS < b.y && cross(a, b) > EPS) x = !x; } return (x ? 2 : 0); } //=====================================================// //==================== Andrew Scan ====================// //=====================================================// int Accw(Point p0, Point p1, Point p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > EPS) return 1; return -1; } Polygon andrewScan(Polygon s) { Polygon ans; Polygon u, l; if (s.size() < 3) return s; sort(s.begin(), s.end()); u.push_back(s[0]); u.push_back(s[1]); l.push_back(s[s.size() - 1]); l.push_back(s[s.size() - 2]); for (int i = 2; i < s.size(); i++) { for (int n = u.size(); n >= 2 && Accw(u[n - 2], u[n - 1], s[i]) != -1; n--) { u.pop_back(); } u.push_back(s[i]); } for (int i = s.size() - 3; i >= 0; i--) { for (int n = l.size(); n >= 2 && Accw(l[n - 2], l[n - 1], s[i]) != -1; n--) { l.pop_back(); } l.push_back(s[i]); } reverse(l.begin(), l.end()); for (int i = u.size() - 2; i >= 1; i--) { l.push_back(u[i]); } int f = 0; for (int i = 1; i < l.size(); i++) { if (l[f].y > l[i].y || (l[f].y == l[i].y && l[f].x > l[i].x)) { f = i; } } for (int i = 0; i < l.size(); i++) { ans.push_back( Point((int)l[(i + f) % l.size()].x, (int)l[(i + f) % l.size()].y)); } return ans; } //=====================================================// //======================== END ========================// //=====================================================// class EndPoint { public: Point p; int seg, st; EndPoint() {} EndPoint(Point p, int seg, int st) : p(p), seg(seg), st(st) {} bool operator<(const EndPoint &ep) const { if (p.y == ep.p.y) { return st < ep.st; } else { return p.y < ep.p.y; } } }; EndPoint EP[2 * 100000]; int manhattanIntersection(vector<Segment> S) { int n = S.size(); for (int i = 0, k = 0; i < n; i++) { if (S[i].p1.y == S[i].p2.y) { if (S[i].p1.x > S[i].p2.x) swap(S[i].p1, S[i].p2); } else if (S[i].p1.y > S[i].p2.y) { swap(S[i].p1, S[i].p2); } if (S[i].p1.y == S[i].p2.y) { EP[k++] = EndPoint(S[i].p1, i, LEFT); EP[k++] = EndPoint(S[i].p2, i, RIGHT); } else { EP[k++] = EndPoint(S[i].p1, i, BOTTOM); EP[k++] = EndPoint(S[i].p2, i, TOP); } } sort(EP, EP + (2 * n)); set<int> BT; BT.insert(1000000001); int cnt = 0; for (int i = 0; i < 2 * n; i++) { if (EP[i].st == TOP) { BT.erase(EP[i].p.x); } else if (EP[i].st == BOTTOM) { BT.insert(EP[i].p.x); } else if (EP[i].st == LEFT) { set<int>::iterator b = lower_bound(BT.begin(), BT.end(), S[EP[i].seg].p1.x); set<int>::iterator e = upper_bound(BT.begin(), BT.end(), S[EP[i].seg].p2.x); cnt += distance(b, e); } } return cnt; } }; // namespace Geometry using namespace Geometry; class Circle2 { public: double x, y, z; double r; Circle2(double x = 0.0, double y = 0.0, double z = 0.0, double r = 0.0) : x(z), y(y), z(z), r(r) {} friend istream &operator>>(istream &is, Circle2 &c) { return is >> c.x >> c.y >> c.z >> c.r; } }; int main() { while (cin >> n, n) { vector<Circle2> vec; Prim<double> ans(n, n * (n - 1)); rep(i, 0, n) { Circle2 a; cin >> a; vec.pb(a); } rep(i, 0, n) { rep(j, i + 1, n) { double distin = 0; double dist1 = sqrt((vec[i].x - vec[j].x) * (vec[i].x - vec[j].x) + (vec[i].y - vec[j].y) * (vec[i].y - vec[j].y) + (vec[i].z - vec[j].z) * (vec[i].z - vec[j].z)); double dist2 = vec[i].r + vec[j].r; if (dist1 > dist2) distin = dist1 - dist2; // printf("%f , %f : %f\n",dist1,dist2,distin); ans.in(i, j, distin); } } cout << fixed << setprecision(3) << ans.prim() << endl; } return 0; }
[["+", 0, 43, 49, 50, 51, 4, 0, 16, 17, 48], ["+", 49, 50, 51, 4, 0, 16, 12, 23, 0, 24], ["+", 51, 4, 0, 16, 12, 23, 0, 16, 31, 22], ["+", 49, 50, 51, 4, 0, 16, 12, 23, 0, 25], ["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
3,858
#include <algorithm> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; const ll MOD = 1000000007; const ll INF = (ll)1000000007 * 1000000007; const double EPS = 1e-9; typedef pair<int, int> P; typedef unsigned int ui; #define stop \ char nyaa; \ cin >> nyaa; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef long double ld; const ld eps = 1e-8; int par[100000]; int rank1[100000]; void init(int n) { for (int i = 0; i < n; i++) { par[i] = i; rank1[i] = 0; } } int find1(int x) { if (par[x] == x) { return x; } else { return par[x] = find1(par[x]); } } void unite1(int x, int y) { x = find1(x); y = find1(y); if (x == y) return; if (rank1[x] < rank1[y]) { par[x] = y; } else { par[y] = x; if (rank1[x] == rank1[y]) rank1[x]++; } } bool same(int x, int y) { return find1(x) == find1(y); } struct edge { int u, v; ld cost; }; bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; } vector<edge> es; int V; ld kruskal() { sort(es.begin(), es.end(), comp); init(V); ld res = 0; for (int i = 0; i < (int)es.size(); i++) { edge e = es[i]; if (!same(e.u, e.v)) { unite1(e.u, e.v); res += e.cost; } } return res; } struct point { ld x, y, z, r; }; ld dis(point a, point b) { ld res; res = sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2)); res -= a.r + b.r; if (res < eps) { res = 0; } return res; } int main() { while (cin >> V, V) { es.clear(); point p[101]; ld x, y, z, r; rep(i, V) { cin >> x >> y >> z >> r; p[i] = {x, y, z, r}; } rep(i, V) { Rep(j, i + 1, V) { es.push_back({i, j, dis(p[i], p[j])}); } } cout << fixed << setprecision(5) << kruskal() << endl; } return 0; }
#include <algorithm> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; const ll MOD = 1000000007; const ll INF = (ll)1000000007 * 1000000007; const double EPS = 1e-9; typedef pair<int, int> P; typedef unsigned int ui; #define stop \ char nyaa; \ cin >> nyaa; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef long double ld; const ld eps = 1e-8; int par[100000]; int rank1[100000]; void init(int n) { for (int i = 0; i < n; i++) { par[i] = i; rank1[i] = 0; } } int find1(int x) { if (par[x] == x) { return x; } else { return par[x] = find1(par[x]); } } void unite1(int x, int y) { x = find1(x); y = find1(y); if (x == y) return; if (rank1[x] < rank1[y]) { par[x] = y; } else { par[y] = x; if (rank1[x] == rank1[y]) rank1[x]++; } } bool same(int x, int y) { return find1(x) == find1(y); } struct edge { int u, v; ld cost; }; bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; } vector<edge> es; int V; ld kruskal() { sort(es.begin(), es.end(), comp); init(V); ld res = 0; for (int i = 0; i < (int)es.size(); i++) { edge e = es[i]; if (!same(e.u, e.v)) { unite1(e.u, e.v); res += e.cost; } } return res; } struct point { ld x, y, z, r; }; ld dis(point a, point b) { ld res; res = sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2)); res -= (a.r + b.r); if (res < 0) { res = 0; } return res; } int main() { while (cin >> V, V) { es.clear(); point p[101]; ld x, y, z, r; rep(i, V) { cin >> x >> y >> z >> r; p[i] = {x, y, z, r}; } rep(i, V) { Rep(j, i + 1, V) { es.push_back({i, j, dis(p[i], p[j])}); } } cout << fixed << setprecision(3) << kruskal() << endl; } return 0; }
[["+", 8, 9, 0, 1, 0, 11, 12, 23, 0, 24], ["+", 8, 9, 0, 1, 0, 11, 12, 23, 0, 25], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
710
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; static const double EPS = 1e-10; typedef long long ll; typedef pair<int, int> PI; typedef vector<int> vi; #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define mp(a, b) make_pair(a, b) #define pb(a) push_back(a) #define F first #define S second int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; vector<pair<int, double>> E[100]; int n; double prim() { bool used[n]; memset(used, 0, sizeof(used)); priority_queue<pair<double, int>> Q; Q.push(mp(0, 0)); double ret = 0; while (!Q.empty()) { double cc = -Q.top().F; int from = Q.top().S; Q.pop(); if (used[from]) continue; used[from] = true; ret += cc; rep(i, E[from].size()) { int to = E[from][i].F; double co = E[from][i].S; if (used[to]) continue; Q.push(mp(-co, to)); } } return ret; } main() { while (cin >> n, n) { double x[n], y[n], z[n], r[n]; rep(i, n) E[i].clear(); rep(i, n) cin >> x[i] >> y[i] >> z[i] >> r[i]; rep(i, n) { rep(j, i) { double tx = x[j] - x[i], ty = y[j] - y[i], tz = z[j] - z[i]; double len = sqrt(tx * tx + ty * ty + tz * tz); double cost = len - r[i] - r[j]; if (cost < 0) cost = 0; E[i].pb(mp(j, cost)); E[j].pb(mp(i, cost)); } } printf("%.5f\n", prim()); } }
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; static const double EPS = 1e-10; typedef long long ll; typedef pair<int, int> PI; typedef vector<int> vi; #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define mp(a, b) make_pair(a, b) #define pb(a) push_back(a) #define F first #define S second int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; vector<pair<int, double>> E[100]; int n; double prim() { bool used[n]; memset(used, 0, sizeof(used)); priority_queue<pair<double, int>> Q; Q.push(mp(0, 0)); double ret = 0; while (!Q.empty()) { double cc = -Q.top().F; int from = Q.top().S; Q.pop(); if (used[from]) continue; used[from] = true; ret += cc; rep(i, E[from].size()) { int to = E[from][i].F; double co = E[from][i].S; if (used[to]) continue; Q.push(mp(-co, to)); } } return ret; } main() { while (cin >> n, n) { double x[n], y[n], z[n], r[n]; rep(i, n) E[i].clear(); rep(i, n) cin >> x[i] >> y[i] >> z[i] >> r[i]; rep(i, n) { rep(j, i) { double tx = x[j] - x[i], ty = y[j] - y[i], tz = z[j] - z[i]; double len = sqrt(tx * tx + ty * ty + tz * tz); double cost = len - r[i] - r[j]; if (cost < 0) cost = 0; E[i].pb(mp(j, cost)); E[j].pb(mp(i, cost)); } } printf("%.3f\n", prim()); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
568
#include <algorithm> #include <cmath> #include <cstdio> #include <vector> using namespace std; const double INF = 10000.0; const int MAX_V = 1000; int n; struct cell { double x, y, z, r; }; double cost[MAX_V][MAX_V]; double mincost[MAX_V]; bool used[MAX_V]; void add_edge(int u, int v, double cost_) { cost[u][v] = cost[v][u] = cost_; } double prim() { for (int i = 0; i < n; i++) { mincost[i] = INF; used[i] = false; } mincost[0] = 0; double res = 0.0; while (true) { int v = -1; for (int u = 0; u < n; u++) { if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u; } if (v == -1) break; used[v] = true; res += mincost[v]; for (int u = 0; u < n; u++) { mincost[u] = min(mincost[u], cost[v][u]); } } return res; } double D(cell c1, cell c2) { double dx = c1.x - c2.x; double dy = c1.y - c2.y; double dz = c1.z - c2.z; double d = sqrt(dx * dx + dy * dy + dz * dz) - c1.r - c2.r; if (d < 0.0) d = 0.0; return d; } int main() { while (scanf("%d", &n), n) { vector<cell> v; for (int i = 0; i < n; i++) { double x, y, z, r; cell c; scanf("%lf %lf %lf %lf", &x, &y, &z, &r); c.x = x; c.y = y; c.z = z; c.r = r; v.push_back(c); } for (int i = 0; i < v.size(); i++) { for (int j = i + 1; j < v.size(); j++) { double d = D(v[i], v[j]); add_edge(i, j, d); } } double ans = prim(); printf("%.6f\n", ans); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <vector> using namespace std; const double INF = 10000.0; const int MAX_V = 1000; int n; struct cell { double x, y, z, r; }; double cost[MAX_V][MAX_V]; double mincost[MAX_V]; bool used[MAX_V]; void add_edge(int u, int v, double cost_) { cost[u][v] = cost[v][u] = cost_; } double prim() { for (int i = 0; i < n; i++) { mincost[i] = INF; used[i] = false; } mincost[0] = 0; double res = 0.0; while (true) { int v = -1; for (int u = 0; u < n; u++) { if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u; } if (v == -1) break; used[v] = true; res += mincost[v]; for (int u = 0; u < n; u++) { mincost[u] = min(mincost[u], cost[v][u]); } } return res; } double D(cell c1, cell c2) { double dx = c1.x - c2.x; double dy = c1.y - c2.y; double dz = c1.z - c2.z; double d = sqrt(dx * dx + dy * dy + dz * dz) - c1.r - c2.r; if (d < 0.0) d = 0.0; return d; } int main() { while (scanf("%d", &n), n) { vector<cell> v; for (int i = 0; i < n; i++) { double x, y, z, r; cell c; scanf("%lf %lf %lf %lf", &x, &y, &z, &r); c.x = x; c.y = y; c.z = z; c.r = r; v.push_back(c); } for (int i = 0; i < v.size(); i++) { for (int j = i + 1; j < v.size(); j++) { double d = D(v[i], v[j]); add_edge(i, j, d); } } double ans = prim(); printf("%.3f\n", ans); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
532
#include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) const double EPS = 1e-10; const double PI = acos(-1.0); typedef vector<double> vd; typedef vector<vd> vvd; struct sphere { double x, y, z, r; // sphere(double x,double y,double z,double r):x(x),y(y),z(z),r(r){} }; double dist(const sphere &left, const sphere &right) { double p = sqrt(pow(left.x - right.x, 2) + pow(left.y - right.y, 2) + pow(left.z - right.z, 2)); double d = p - left.r - right.r; if (d < EPS) { return 0.000; } else { return d; } } #define MAX INT_MAX / 10 int main() { int n; while (cin >> n, n) { vector<sphere> s(n); REP(i, n) { cin >> s[i].x >> s[i].y >> s[i].z >> s[i].r; } vvd d(n, vd(n, MAX)); REP(i, n) REP(j, n) { d[i][j] = dist(s[i], s[j]); } vi dones(n, 0); dones[0] = 1; double res = 0; while (find(ALL(dones), 0) != dones.end()) { double mindist = MAX; int minindex = -1; REP(i, n) { if (dones[i]) { REP(j, n) { if (!dones[j]) { if (mindist > d[i][j]) { mindist = d[i][j]; minindex = j; } } } } } assert(minindex != -1); res += mindist; dones[minindex] = 1; } printf("%.5f\n", res); } }
#include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) const double EPS = 1e-10; const double PI = acos(-1.0); typedef vector<double> vd; typedef vector<vd> vvd; struct sphere { double x, y, z, r; // sphere(double x,double y,double z,double r):x(x),y(y),z(z),r(r){} }; double dist(const sphere &left, const sphere &right) { double p = sqrt(pow(left.x - right.x, 2) + pow(left.y - right.y, 2) + pow(left.z - right.z, 2)); double d = p - left.r - right.r; if (d < EPS) { return 0.000; } else { return d; } } #define MAX INT_MAX / 10 int main() { int n; while (cin >> n, n) { vector<sphere> s(n); REP(i, n) { cin >> s[i].x >> s[i].y >> s[i].z >> s[i].r; } vvd d(n, vd(n, MAX)); REP(i, n) REP(j, n) { d[i][j] = dist(s[i], s[j]); } vi dones(n, 0); dones[0] = 1; double res = 0; while (find(ALL(dones), 0) != dones.end()) { double mindist = MAX; int minindex = -1; REP(i, n) { if (dones[i]) { REP(j, n) { if (!dones[j]) { if (mindist > d[i][j]) { mindist = d[i][j]; minindex = j; } } } } } assert(minindex != -1); res += mindist; dones[minindex] = 1; } printf("%.3f\n", res); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
575
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <sstream> #include <utility> #include <vector> using namespace std; typedef long long Long; #define whole(xs) xs.begin(), xs.end() struct UnionFind { int *parent; int N; UnionFind(int N) : N(N) { parent = new int[N]; fill(parent, parent + N, -1); } ~UnionFind() { delete[] parent; } int root(int x) { if (parent[x] == -1) return x; return parent[x] = root(parent[x]); } bool query(int x, int y) { return root(x) == root(y); } void merge(int x, int y) { x = root(x); y = root(y); if (x != y) { parent[x] = y; } } }; int N; #define L 100 double x[L], y[L], z[L], r[L]; bool input() { cin >> N; if (N == 0) return false; for (int i = 0; i < N; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } return true; } const double INF = 1e20; struct Kruskal { struct Edge { int from, to; double cost; Edge(int from, int to, double cost) : from(from), to(to), cost(cost) {} bool operator<(const Edge &e) const { return cost < e.cost; } }; vector<Edge> G; Kruskal() { G.clear(); } void addEdge(int from, int to, double cost) { G.push_back(Edge(from, to, cost)); } double solve() { UnionFind uf(N); sort(whole(G)); double Ret = 0; for (int i = 0; i < G.size(); i++) { if (!uf.query(G[i].from, G[i].to)) { uf.merge(G[i].from, G[i].to); Ret += G[i].cost; } } return Ret; } }; #define POW2(x) ((x) * (x)) double Dist(int a, int b) { return sqrt(POW2(x[a] - x[b]) + POW2(y[a] - y[b]) + POW2(z[a] - z[b])) - (r[a] + r[b]); } void solve() { Kruskal K; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { K.addEdge(i, j, max(0.0, Dist(i, j))); } } printf("%.8lf\n", K.solve()); } int main() { while (input()) solve(); return 0; }
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <sstream> #include <utility> #include <vector> using namespace std; typedef long long Long; #define whole(xs) xs.begin(), xs.end() struct UnionFind { int *parent; int N; UnionFind(int N) : N(N) { parent = new int[N]; fill(parent, parent + N, -1); } ~UnionFind() { delete[] parent; } int root(int x) { if (parent[x] == -1) return x; return parent[x] = root(parent[x]); } bool query(int x, int y) { return root(x) == root(y); } void merge(int x, int y) { x = root(x); y = root(y); if (x != y) { parent[x] = y; } } }; int N; #define L 100 double x[L], y[L], z[L], r[L]; bool input() { cin >> N; if (N == 0) return false; for (int i = 0; i < N; i++) { cin >> x[i] >> y[i] >> z[i] >> r[i]; } return true; } const double INF = 1e20; struct Kruskal { struct Edge { int from, to; double cost; Edge(int from, int to, double cost) : from(from), to(to), cost(cost) {} bool operator<(const Edge &e) const { return cost < e.cost; } }; vector<Edge> G; Kruskal() { G.clear(); } void addEdge(int from, int to, double cost) { G.push_back(Edge(from, to, cost)); } double solve() { UnionFind uf(N); sort(whole(G)); double Ret = 0; for (int i = 0; i < G.size(); i++) { if (!uf.query(G[i].from, G[i].to)) { uf.merge(G[i].from, G[i].to); Ret += G[i].cost; } } return Ret; } }; #define POW2(x) ((x) * (x)) double Dist(int a, int b) { return sqrt(POW2(x[a] - x[b]) + POW2(y[a] - y[b]) + POW2(z[a] - z[b])) - (r[a] + r[b]); } void solve() { Kruskal K; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { K.addEdge(i, j, max(0.0, Dist(i, j))); } } printf("%.3lf\n", K.solve()); } int main() { while (input()) solve(); return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
667
#define _USE_MATH_DEFINES #define INF 100000000 #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; static const float EPS = 1e-8; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; double path[100][100]; double mincost[100]; bool used[100]; class Data { public: double x; double y; double z; double r; Data(double _x, double _y, double _z, double _r) { x = _x; y = _y; z = _z; r = _r; } }; int main() { int n; while (~scanf("%d", &n)) { if (n == 0) break; vector<Data> nodes; fill((bool *)used, (bool *)used + 100, false); fill((double *)mincost, (double *)mincost + 100, numeric_limits<double>::max()); for (int i = 0; i < n; i++) { double x, y, z, r; scanf("%lf %lf %lf %lf", &x, &y, &z, &r); nodes.push_back(Data(x, y, z, r)); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { double diff = sqrt((nodes[i].x - nodes[j].x) * (nodes[i].x - nodes[j].x) + (nodes[i].y - nodes[j].y) * (nodes[i].y - nodes[j].y) + (nodes[i].z - nodes[j].z) * (nodes[i].z - nodes[j].z)); double cost = (diff - (nodes[i].r + nodes[j].r) >= 0 ? diff - (nodes[i].r + nodes[j].r) : 0); path[i][j] = path[j][i] = cost; } } mincost[0] = 0; double res = 0; while (1) { int v = -1; for (int u = 0; u < n; u++) { if (!used[u] && (v == -1 || mincost[u] < mincost[v])) { v = u; } } if (v == -1) break; used[v] = true; res += mincost[v]; for (int u = 0; u < n; u++) { mincost[u] = min(mincost[u], path[v][u]); } } printf("%lf\n", res); } }
#define _USE_MATH_DEFINES #define INF 100000000 #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; static const float EPS = 1e-8; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; double path[100][100]; double mincost[100]; bool used[100]; class Data { public: double x; double y; double z; double r; Data(double _x, double _y, double _z, double _r) { x = _x; y = _y; z = _z; r = _r; } }; int main() { int n; while (~scanf("%d", &n)) { if (n == 0) break; vector<Data> nodes; fill((bool *)used, (bool *)used + 100, false); fill((double *)mincost, (double *)mincost + 100, numeric_limits<double>::max()); for (int i = 0; i < n; i++) { double x, y, z, r; scanf("%lf %lf %lf %lf", &x, &y, &z, &r); nodes.push_back(Data(x, y, z, r)); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { double diff = sqrt((nodes[i].x - nodes[j].x) * (nodes[i].x - nodes[j].x) + (nodes[i].y - nodes[j].y) * (nodes[i].y - nodes[j].y) + (nodes[i].z - nodes[j].z) * (nodes[i].z - nodes[j].z)); double cost = (diff - (nodes[i].r + nodes[j].r) >= 0 ? diff - (nodes[i].r + nodes[j].r) : 0); path[i][j] = path[j][i] = cost; } } mincost[0] = 0; double res = 0; while (1) { int v = -1; for (int u = 0; u < n; u++) { if (!used[u] && (v == -1 || mincost[u] < mincost[v])) { v = u; } } if (v == -1) break; used[v] = true; res += mincost[v]; for (int u = 0; u < n; u++) { mincost[u] = min(mincost[u], path[v][u]); } } printf("%.3lf\n", res); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
655
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // common typedef int i32; typedef long long i64, ll; typedef long double ld; #define BR "\n" #define ALL(c) (c).begin(), (c).end() #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, l, r) for (int i = (int)l; i < (int)(r); ++i) #define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it) #define IN(l, v, r) ((l) <= (v) && (v) < (r)) // config //#define NDEBUG //#define INF 1<<30 //#define EPS 1e-8 // const ll MOD =100000007; // debug #ifdef NDEBUG #define DUMP(x) #define DUMPLN(x) #define DEBUG(x) #define DEBUGLN(x) #define LINE() #define LINELN() #define CHECK(exp, act) #define STOP(e) #else #define DUMP(x) cerr << #x << " = " << (x) #define DUMPLN(x) DUMP(x) << endl #define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__ #define DEBUGLN(x) DEBUG(x) << endl #define LINE() cerr << " (L" << __LINE__ << ")" #define LINELN() LINE() << endl #define CHECK(exp, act) \ if (exp != act) { \ DUMPLN(exp); \ DEBUGLN(act); \ } #define STOP(e) \ CHECK(e, true); \ if (!(e)) \ exit(1); #endif template <class T> inline string toString(const vector<T> &x) { stringstream ss; REP(i, x.size()) { if (i != 0) ss << " "; ss << x[i]; } return ss.str(); } template <class T> inline string toString(const vector<vector<T>> &map) { stringstream ss; REP(i, map.size()) { if (i != 0) ss << BR; ss << toString(map[i]); } return ss.str(); } template <class K, class V> string toString(map<K, V> &x) { string res; stringstream ss; for (auto &p : x) ss << p.first << ":" << p.second << " "; return ss.str(); } string BITtoString(int bit) { stringstream ss; while (bit != 0) { ss << (bit % 2); bit /= 2; } string res = ss.str(); reverse(ALL(res)); return res; } template <typename T, typename V> inline T pmod(T v, V MOD) { return (v % MOD + MOD) % MOD; } #define nextInt(n) scanf("%d", &n) #define defInt(n) \ int n; \ nextInt(n) #define nextLong(n) scanf("%lld", &n) #define defLong(n) \ ll n; \ nextLong(n) #define nextDouble(n) scanf("%lf", &n) struct UnionFind { vector<int> par; // 親 vector<int> rank; // 木の深さ vector<int> ss; // xの属する集合のサイズ:option int size; //集合の個数:option UnionFind(int n) { REP(i, n) par.push_back(i); rank = vector<int>(n); ss = vector<int>(n, 1); size = n; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; ss[y] += ss[x]; } else { par[y] = x; ss[x] += ss[y]; } if (rank[x] == rank[y]) rank[x]++; size--; } }; namespace SpanningTree { typedef double Cost; struct Edge { int from, to; Cost cost; Edge(int from, int to, Cost cost) : from(from), to(to), cost(cost){}; bool operator<(Edge r) const { return cost < r.cost; } bool operator>(Edge r) const { return cost > r.cost; } }; typedef vector<vector<Edge>> Graph; // O(E*log(V)) // verified by ACAC002 B // http://judge.u-aizu.ac.jp/onlinejudge/creview.jsp?rid=899132&cid=ACAC002 Cost kruskal(Graph g) { const int V = g.size(); UnionFind uf = UnionFind(V); priority_queue<Edge, vector<Edge>, greater<Edge>> que; REP(u, V) for (Edge e : g[u]) que.push(e); Cost res = 0; while (!que.empty()) { Edge e = que.top(); que.pop(); if (!uf.same(e.from, e.to)) { uf.unite(e.from, e.to); res += e.cost; } } return res; } } // namespace SpanningTree using namespace SpanningTree; class Main { public: void run() { // ifstream cin("in"); // ofstream cout( "out" ); while (true) { int N; nextInt(N); if (N == 0) return; Graph g(N); vector<double> xs(N), ys(N), zs(N), rs(N); REP(i, N) { nextDouble(xs[i]); nextDouble(ys[i]); nextDouble(zs[i]); nextDouble(rs[i]); } REP(i, N) REP(j, N) { double d = max(sqrt(pow(xs[i] - xs[j], 2) + pow(ys[i] - ys[j], 2) + pow(zs[i] - zs[j], 2)) - rs[i] - rs[j], 0.0); g[i].push_back(Edge(i, j, d)); } cout << SpanningTree::kruskal(g) << endl; } } }; int main() { cout << fixed << setprecision(15); ios::sync_with_stdio(false); Main().run(); return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // common typedef int i32; typedef long long i64, ll; typedef long double ld; #define BR "\n" #define ALL(c) (c).begin(), (c).end() #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, l, r) for (int i = (int)l; i < (int)(r); ++i) #define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it) #define IN(l, v, r) ((l) <= (v) && (v) < (r)) // config //#define NDEBUG //#define INF 1<<30 //#define EPS 1e-8 // const ll MOD =100000007; // debug #ifdef NDEBUG #define DUMP(x) #define DUMPLN(x) #define DEBUG(x) #define DEBUGLN(x) #define LINE() #define LINELN() #define CHECK(exp, act) #define STOP(e) #else #define DUMP(x) cerr << #x << " = " << (x) #define DUMPLN(x) DUMP(x) << endl #define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__ #define DEBUGLN(x) DEBUG(x) << endl #define LINE() cerr << " (L" << __LINE__ << ")" #define LINELN() LINE() << endl #define CHECK(exp, act) \ if (exp != act) { \ DUMPLN(exp); \ DEBUGLN(act); \ } #define STOP(e) \ CHECK(e, true); \ if (!(e)) \ exit(1); #endif template <class T> inline string toString(const vector<T> &x) { stringstream ss; REP(i, x.size()) { if (i != 0) ss << " "; ss << x[i]; } return ss.str(); } template <class T> inline string toString(const vector<vector<T>> &map) { stringstream ss; REP(i, map.size()) { if (i != 0) ss << BR; ss << toString(map[i]); } return ss.str(); } template <class K, class V> string toString(map<K, V> &x) { string res; stringstream ss; for (auto &p : x) ss << p.first << ":" << p.second << " "; return ss.str(); } string BITtoString(int bit) { stringstream ss; while (bit != 0) { ss << (bit % 2); bit /= 2; } string res = ss.str(); reverse(ALL(res)); return res; } template <typename T, typename V> inline T pmod(T v, V MOD) { return (v % MOD + MOD) % MOD; } #define nextInt(n) scanf("%d", &n) #define defInt(n) \ int n; \ nextInt(n) #define nextLong(n) scanf("%lld", &n) #define defLong(n) \ ll n; \ nextLong(n) #define nextDouble(n) scanf("%lf", &n) struct UnionFind { vector<int> par; // 親 vector<int> rank; // 木の深さ vector<int> ss; // xの属する集合のサイズ:option int size; //集合の個数:option UnionFind(int n) { REP(i, n) par.push_back(i); rank = vector<int>(n); ss = vector<int>(n, 1); size = n; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; ss[y] += ss[x]; } else { par[y] = x; ss[x] += ss[y]; } if (rank[x] == rank[y]) rank[x]++; size--; } }; namespace SpanningTree { typedef double Cost; struct Edge { int from, to; Cost cost; Edge(int from, int to, Cost cost) : from(from), to(to), cost(cost){}; bool operator<(Edge r) const { return cost < r.cost; } bool operator>(Edge r) const { return cost > r.cost; } }; typedef vector<vector<Edge>> Graph; // O(E*log(V)) // verified by ACAC002 B // http://judge.u-aizu.ac.jp/onlinejudge/creview.jsp?rid=899132&cid=ACAC002 Cost kruskal(Graph g) { const int V = g.size(); UnionFind uf = UnionFind(V); priority_queue<Edge, vector<Edge>, greater<Edge>> que; REP(u, V) for (Edge e : g[u]) que.push(e); Cost res = 0; while (!que.empty()) { Edge e = que.top(); que.pop(); if (!uf.same(e.from, e.to)) { uf.unite(e.from, e.to); res += e.cost; } } return res; } } // namespace SpanningTree using namespace SpanningTree; class Main { public: void run() { // ifstream cin("in"); // ofstream cout( "out" ); while (true) { int N; nextInt(N); if (N == 0) return; Graph g(N); vector<double> xs(N), ys(N), zs(N), rs(N); REP(i, N) { nextDouble(xs[i]); nextDouble(ys[i]); nextDouble(zs[i]); nextDouble(rs[i]); } REP(i, N) REP(j, N) { double d = max(sqrt(pow(xs[i] - xs[j], 2) + pow(ys[i] - ys[j], 2) + pow(zs[i] - zs[j], 2)) - rs[i] - rs[j], 0.0); g[i].push_back(Edge(i, j, d)); } cout << SpanningTree::kruskal(g) << endl; } } }; int main() { cout << fixed << setprecision(3); ios::sync_with_stdio(false); Main().run(); return 0; }
[["-", 0, 1, 0, 16, 12, 2, 3, 4, 0, 13], ["+", 0, 1, 0, 16, 12, 2, 3, 4, 0, 13]]
1
1,230
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <vector> #ifdef _MSC_VER #include <agents.h> #endif #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define rep(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define REV(v) v.rbegin(), v.rend() #define MEMSET(v, s) memset(v, s, sizeof(v)) #define X first #define Y second #define MP make_pair #define MT make_tuple using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> P; typedef unsigned int uint; struct UF { vector<int> par; int n; UF(int size) { n = size; par = vector<int>(n); rep(i, n) par[i] = i; } int find(int x) { if (x == par[x]) return x; return par[x] = find(par[x]); } void unite(int x, int y) { par[find(x)] = find(y); } bool same(int x, int y) { return find(x) == find(y); } }; int main() { auto sq = [](double x) { return x * x; }; cout.setf(ios::fixed); cout.precision(3); int n; while (cin >> n) { vector<double> x(n), y(n), z(n), r(n); rep(i, n) cin >> x[i] >> y[i] >> z[i] >> r[i]; vector<tuple<double, int, int>> edge; rep(i, n) rep(j, i) { double len = sqrt(sq(x[i] - x[j]) + sq(y[i] - y[j]) + sq(z[i] - z[j])); len -= r[i] + r[j]; len = max(len, 0.); edge.push_back(MT(len, i, j)); } sort(ALL(edge)); UF uf(n); double ans = 0; for (auto e : edge) { double cost; int s, t; tie(cost, s, t) = e; if (uf.same(s, t)) continue; uf.unite(s, t); ans += cost; } cout << ans << endl; } return 0; }
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <vector> #ifdef _MSC_VER #include <agents.h> #endif #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define rep(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define REV(v) v.rbegin(), v.rend() #define MEMSET(v, s) memset(v, s, sizeof(v)) #define X first #define Y second #define MP make_pair #define MT make_tuple using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> P; typedef unsigned int uint; struct UF { vector<int> par; int n; UF(int size) { n = size; par = vector<int>(n); rep(i, n) par[i] = i; } int find(int x) { if (x == par[x]) return x; return par[x] = find(par[x]); } void unite(int x, int y) { par[find(x)] = find(y); } bool same(int x, int y) { return find(x) == find(y); } }; int main() { auto sq = [](double x) { return x * x; }; cout.setf(ios::fixed); cout.precision(3); int n; while (cin >> n, n) { vector<double> x(n), y(n), z(n), r(n); rep(i, n) cin >> x[i] >> y[i] >> z[i] >> r[i]; vector<tuple<double, int, int>> edge; rep(i, n) rep(j, i) { double len = sqrt(sq(x[i] - x[j]) + sq(y[i] - y[j]) + sq(z[i] - z[j])); len -= r[i] + r[j]; len = max(len, 0.); edge.push_back(MT(len, i, j)); } sort(ALL(edge)); UF uf(n); double ans = 0; for (auto e : edge) { double cost; int s, t; tie(cost, s, t) = e; if (uf.same(s, t)) continue; uf.unite(s, t); ans += cost; } cout << ans << endl; } return 0; }
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
1
556
require 'prime' require 'set' require 'tsort' include Math def max(a,b); a > b ? a : b end def min(a,b); a < b ? a : b end def swap(a,b); a, b = b, a end def gif; gets.to_i end def gff; gets.to_f end def gsf; gets.chomp end def gi; gets.split.map(&:to_i) end def gf; gets.split.map(&:to_f) end def gs; gets.chomp.split.map(&:to_s) end def gc; gets.chomp.split('') end def pr(num); num.prime_division end def digit(num); num.to_s.length end def array(s,ini=nil); Array.new(s){ini} end def darray(s1,s2,ini=nil); Array.new(s1){Array.new(s2){ini}} end def rep(num); num.times{|i|yield(i)} end def repl(st,en,n=1); st.step(en,n){|i|yield(i)} end class UnionFindTree class ParArray < Hash def [] key self[key] = key if super(key).nil? super(key) end end class SizeArray < Hash def [] key self[key] = 1 if super(key).nil? super(key) end end def initialize() @par = ParArray.new @size = SizeArray.new end private def find(x) return x if x == @par[x] return @par[x] = find(@par[x]) end public def unite(x, y) x = find(x) y = find(y) return nil if x == y x, y = y, x if @size[x] < @size[y] @par[y] = x @size[x] += @size[y] end def same?(x, y) return find(x) == find(y) end def size(x) return @size[find(x)] end end def cost l1,l2 len = sqrt((l1[0]-l2[0])**2+(l1[1]-l2[1])**2+(l1[2]-l2[2])**2)-l1[3]-l2[3] if len <= 0 return 0 else return len end end ans = [] loop{ n = gif break if n == 0 l = [] c = [] un = UnionFindTree.new rep n do |i| ll = gf rep l.size do |j| c << [cost(l[j],ll),[i,j]] end l << ll end c.sort! sum = 0 p c c.each do |cc| unless un.same? cc[1][0],cc[1][1] sum += cc[0] un.unite cc[1][0],cc[1][1] end end ans << format("%.3f", sum) } puts ans
require 'prime' require 'set' require 'tsort' include Math def max(a,b); a > b ? a : b end def min(a,b); a < b ? a : b end def swap(a,b); a, b = b, a end def gif; gets.to_i end def gff; gets.to_f end def gsf; gets.chomp end def gi; gets.split.map(&:to_i) end def gf; gets.split.map(&:to_f) end def gs; gets.chomp.split.map(&:to_s) end def gc; gets.chomp.split('') end def pr(num); num.prime_division end def digit(num); num.to_s.length end def array(s,ini=nil); Array.new(s){ini} end def darray(s1,s2,ini=nil); Array.new(s1){Array.new(s2){ini}} end def rep(num); num.times{|i|yield(i)} end def repl(st,en,n=1); st.step(en,n){|i|yield(i)} end class UnionFindTree class ParArray < Hash def [] key self[key] = key if super(key).nil? super(key) end end class SizeArray < Hash def [] key self[key] = 1 if super(key).nil? super(key) end end def initialize() @par = ParArray.new @size = SizeArray.new end private def find(x) return x if x == @par[x] return @par[x] = find(@par[x]) end public def unite(x, y) x = find(x) y = find(y) return nil if x == y x, y = y, x if @size[x] < @size[y] @par[y] = x @size[x] += @size[y] end def same?(x, y) return find(x) == find(y) end def size(x) return @size[find(x)] end end def cost l1,l2 len = sqrt((l1[0]-l2[0])**2+(l1[1]-l2[1])**2+(l1[2]-l2[2])**2)-l1[3]-l2[3] if len <= 0 return 0 else return len end end ans = [] loop{ n = gif break if n == 0 l = [] c = [] un = UnionFindTree.new rep n do |i| ll = gf rep l.size do |j| c << [cost(l[j],ll),[i,j]] end l << ll end c.sort! sum = 0 c.each do |cc| unless un.same? cc[1][0],cc[1][1] sum += cc[0] un.unite cc[1][0],cc[1][1] end end ans << format("%.3f", sum) } puts ans
[["-", 0, 652, 196, 196, 8, 734, 0, 652, 735, 22], ["-", 196, 196, 8, 734, 0, 652, 3, 4, 0, 22]]
4
661
inf = 1 << 28 def calc(xyzr1,xyzr2): x1,y1,z1,r1 = xyzr1 x2,y2,z2,r2 = xyzr2 d = ((x1-x2)**2+(y1-y2)**2+(z1-z2)**2)**0.5 return max(0, d-r1-r2) while True: n = int(input()) if n == 0: break xyzr = [list(map(float,input().split())) for i in range(n)] cost = [[0]*n for i in range(n)] for i in range(n): for j in range(n): cost[i][j] = cost[j][i] = calc(xyzr[i],xyzr[j]) mincost = [inf]*n used = [False]*n mincost[0] = 0 ans = 0 while True: v = -1 for u in range(n): if not used[u] and (v == -1 or mincost[u] < mincost[v]): v = u if v == -1: break used[v] = True ans += mincost[v] for u in range(n): mincost[u] = min(mincost[u], cost[v][u]) print("%.6f"%ans)
inf = 1 << 28 def calc(xyzr1,xyzr2): x1,y1,z1,r1 = xyzr1 x2,y2,z2,r2 = xyzr2 d = ((x1-x2)**2+(y1-y2)**2+(z1-z2)**2)**0.5 return max(0, d-r1-r2) while True: n = int(input()) if n == 0: break xyzr = [list(map(float,input().split())) for i in range(n)] cost = [[0]*n for i in range(n)] for i in range(n): for j in range(n): cost[i][j] = cost[j][i] = calc(xyzr[i],xyzr[j]) mincost = [inf]*n used = [False]*n mincost[0] = 0 ans = 0 while True: v = -1 for u in range(n): if not used[u] and (v == -1 or mincost[u] < mincost[v]): v = u if v == -1: break used[v] = True ans += mincost[v] for u in range(n): mincost[u] = min(mincost[u], cost[v][u]) print("%.3f"%ans)
[["-", 0, 652, 3, 4, 0, 657, 31, 557, 0, 6], ["+", 0, 652, 3, 4, 0, 657, 31, 557, 0, 6]]
5
294
import java.util.Scanner; public class Main { static Scanner sc = new Scanner(System.in); static int W, H; static boolean[][] f; static int[][] size; static int[][] count; static int ans; public static void main(String[] args) { while (true) { W = sc.nextInt(); if (W == 0) break; H = sc.nextInt(); f = new boolean[H + 1][W + 1]; size = new int[H + 1][W + 1]; count = new int[H + 1][W + 1]; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { f[i][j] = sc.next().equals("1"); } } System.out.println(solve()); } } static int solve() { ans = 999; int rest = 0; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (f[i][j]) ++rest; OUT: for (int k = 1;; ++k) { for (int r = i; r < i + k; ++r) { for (int c = j; c < j + k; ++c) { if (!f[r][c]) { size[i][j] = k - 1; break OUT; } } } } } } if (rest == 0) return 0; rec(0, 0, 0, rest); return ans; } static void rec(int r, int c, int num, int rest) { if (rest == 0) { ans = num; return; } if (num + 1 >= ans) return; if (c == W) { rec(r + 1, 0, num, rest); return; } if (!f[r][c] || count[r][c] != 0) { rec(r, c + 1, num, rest); } // prune boolean filled = true; for (int i = 0; i < size[r][c]; ++i) { if (count[r + i][c] == 0) { filled = false; break; } } if (filled && size[r][c + 1] >= size[r][c]) return; filled = true; for (int i = 0; i < size[r][c]; ++i) { if (count[r][c + 1] == 0) { filled = false; break; } } if (filled && size[r + 1][c] >= size[r][c]) return; boolean use = false; for (int i = 0; i < size[r][c]; ++i) { for (int j = 0; j < size[r][c]; ++j) { if (count[r + i][c + j] == 0) { use = true; --rest; } count[r + i][c + j]++; } } if (use) { rec(r, c + 1, num + 1, rest); } for (int i = 0; i < size[r][c]; ++i) { for (int j = 0; j < size[r][c]; ++j) { count[r + i][c + j]--; } } } }
import java.util.Scanner; public class Main { static Scanner sc = new Scanner(System.in); static int W, H; static boolean[][] f; static int[][] size; static int[][] count; static int ans; public static void main(String[] args) { while (true) { W = sc.nextInt(); if (W == 0) break; H = sc.nextInt(); f = new boolean[H + 1][W + 1]; size = new int[H + 1][W + 1]; count = new int[H + 1][W + 1]; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { f[i][j] = sc.next().equals("1"); } } System.out.println(solve()); } } static int solve() { ans = 999; int rest = 0; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (f[i][j]) ++rest; OUT: for (int k = 1;; ++k) { for (int r = i; r < i + k; ++r) { for (int c = j; c < j + k; ++c) { if (!f[r][c]) { size[i][j] = k - 1; break OUT; } } } } } } if (rest == 0) return 0; rec(0, 0, 0, rest); return ans; } static void rec(int r, int c, int num, int rest) { if (rest == 0) { ans = num; return; } if (num + 1 >= ans) return; if (c == W) { rec(r + 1, 0, num, rest); return; } if (!f[r][c] || count[r][c] != 0) { rec(r, c + 1, num, rest); } // prune boolean filled = true; for (int i = 0; i < size[r][c]; ++i) { if (count[r + i][c] == 0) { filled = false; break; } } if (filled && size[r][c + 1] >= size[r][c]) return; filled = true; for (int i = 0; i < size[r][c]; ++i) { if (count[r][c + i] == 0) { filled = false; break; } } if (filled && size[r + 1][c] >= size[r][c]) return; boolean use = false; for (int i = 0; i < size[r][c]; ++i) { for (int j = 0; j < size[r][c]; ++j) { if (count[r + i][c + j] == 0) { use = true; --rest; } count[r + i][c + j]++; } } if (use) { rec(r, c + 1, num + 1, rest); } for (int i = 0; i < size[r][c]; ++i) { for (int j = 0; j < size[r][c]; ++j) { count[r + i][c + j]--; } } } }
[["-", 15, 15, 0, 16, 31, 504, 71, 16, 12, 499], ["+", 15, 15, 0, 16, 31, 504, 71, 16, 12, 22]]
3
784
#include <algorithm> #include <iostream> #include <map> #include <tuple> using namespace std; struct State { int a[10][10]; }; char c[10][10]; int p[10][10], H, W, mindep, cnts; int range(int sx, int sy, int gx, int gy) { int cnt = 0; for (int i = sx; i < gx; i++) { for (int j = sy; j < gy; j++) { if (c[i][j] == '0') cnt++; } } return cnt; } int range2(int sx, int sy, int gx, int gy, State G) { int cnt = 0; for (int i = sx; i < gx; i++) { if (G.a[i][sy] == 0) cnt++; } for (int i = sy; i < gy; i++) { if (G.a[sx][i] == 0) cnt++; } return cnt; } State nurie(State P, int cx, int cy) { for (int i = cx; i < cx + p[cx][cy]; i++) { for (int j = cy; j < cy + p[cx][cy]; j++) P.a[i][j] = 1; } return P; } long long hash_value(State I) { long long mod = 2555555555555555555LL, II = 0, J = 1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (I.a[i][j] == 1) II += J; J *= 2; if (II >= mod) II -= mod; if (J >= mod) J -= mod; } } return II; } map<tuple<int, int, int, long long>, int> M; void dfs(int cx, int cy, int depth, State S) { long long K = hash_value(S); if (M[make_tuple(cx, cy, depth, K)] == 1) return; M[make_tuple(cx, cy, depth, K)] = 1; cnts++; if (depth == mindep) return; if (cx == H) { mindep = min(mindep, depth); return; } int nx = cx, ny = cy + 1; if (ny == W) { ny = 0; nx++; } if (c[cx][cy] == '0') { dfs(nx, ny, depth, S); } else { if (S.a[cx][cy] == 0) { State V = nurie(S, cx, cy); dfs(nx, ny, depth + 1, V); } else if (range2(cx, cy, cx + p[cx][cy], cy + p[cx][cy], S) == 0) { dfs(nx, ny, depth, S); } else { dfs(nx, ny, depth, S); State V = nurie(S, cx, cy); dfs(nx, ny, depth + 1, V); } } } int main() { while (true) { cin >> W >> H; mindep = (1 << 30); if (H == 0 && W == 0) break; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) cin >> c[i][j]; } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 1; k <= min(H - i, W - j); k++) { if (range(i, j, i + k, j + k) >= 1) break; p[i][j] = k; } } } State S; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) S.a[i][j] = 0; } dfs(0, 0, 0, S); cout << mindep << endl; } // cout << "Totalcount : " << cnts << endl; return 0; }
#include <algorithm> #include <iostream> #include <map> #include <tuple> using namespace std; struct State { int a[10][10]; }; char c[10][10]; int p[10][10], H, W, mindep, cnts; int range(int sx, int sy, int gx, int gy) { int cnt = 0; for (int i = sx; i < gx; i++) { for (int j = sy; j < gy; j++) { if (c[i][j] == '0') cnt++; } } return cnt; } int range2(int sx, int sy, int gx, int gy, State G) { int cnt = 0; for (int i = sx; i < gx; i++) { if (G.a[i][sy] == 0) cnt++; } for (int i = sy; i < gy; i++) { if (G.a[sx][i] == 0) cnt++; } return cnt; } State nurie(State P, int cx, int cy) { for (int i = cx; i < cx + p[cx][cy]; i++) { for (int j = cy; j < cy + p[cx][cy]; j++) P.a[i][j] = 1; } return P; } long long hash_value(State I) { long long mod = 2555555555555555555LL, II = 0, J = 1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (I.a[i][j] == 1) II += J; J *= 2; if (II >= mod) II -= mod; if (J >= mod) J -= mod; } } return II; } map<tuple<int, int, int, long long>, int> M; void dfs(int cx, int cy, int depth, State S) { long long K = hash_value(S); if (M[make_tuple(cx, cy, depth, K)] == 1) return; M[make_tuple(cx, cy, depth, K)] = 1; cnts++; if (depth == mindep) return; if (cx == H) { mindep = min(mindep, depth); return; } int nx = cx, ny = cy + 1; if (ny == W) { ny = 0; nx++; } if (c[cx][cy] == '0') { dfs(nx, ny, depth, S); } else { if (S.a[cx][cy] == 0) { State V = nurie(S, cx, cy); dfs(nx, ny, depth + 1, V); } else if (range2(cx, cy, cx + p[cx][cy], cy + p[cx][cy], S) == 0) { dfs(nx, ny, depth, S); } else { dfs(nx, ny, depth, S); State V = nurie(S, cx, cy); dfs(nx, ny, depth + 1, V); } } } int main() { while (true) { cin >> W >> H; M.clear(); mindep = (1 << 30); if (H == 0 && W == 0) break; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) cin >> c[i][j]; } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 1; k <= min(H - i, W - j); k++) { if (range(i, j, i + k, j + k) >= 1) break; p[i][j] = k; } } } State S; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) S.a[i][j] = 0; } dfs(0, 0, 0, S); cout << mindep << endl; } // cout << "Totalcount : " << cnts << endl; return 0; }
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
920
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) using namespace std; int w, h, n, A[11][11], ans, cnt; bitset<100> B[11][11], p; void dfs(int d, bitset<100> s, int sco) { if (d == n) { ans = min(ans, sco); return; } cnt++; if (cnt >= 5000000) return; if (ans <= sco) return; if (!A[d / w][d % w]) dfs(d + 1, s, sco); else { int y = d / w; int x = d % w; if ((B[y][x] & s).any()) { p &= 0; p.set(y * 10 + x); //(y*100+x); if (!(s & p).any()) dfs(d + 1, s, sco); dfs(d + 1, (B[y][x] | s) ^ B[y][x], sco + 1); } else dfs(d + 1, s, sco); } } int main() { while (cin >> w >> h, w) { memset(A, 0, sizeof(A)); r(i, 11) r(j, 11) B[i][j] &= 0; n = h * w; bitset<100> b; r(i, h) r(j, w) cin >> A[i][j]; r(i, h) r(j, w) if (A[i][j]) { int sum = 1; for (;; sum++) { int f = 0; for (int y = i; y < i + sum; y++) { for (int x = j; x < j + sum; x++) { if (A[y][x] == 0) f++; } } if (f) { sum--; break; } } bitset<100> tmp; for (int y = i, k = 0; k < sum; k++, y++) { for (int x = j, l = 0; l < sum; l++, x++) { tmp.set(y * 10 + x); } } b.set(i * 10 + j); B[i][j] = tmp; } ans = 100; cnt = 0; dfs(0, b, 0); cout << ans << endl; } }
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) using namespace std; int w, h, n, A[11][11], ans, cnt; bitset<100> B[11][11], p; void dfs(int d, bitset<100> s, int sco) { if (d == n) { ans = min(ans, sco); return; } cnt++; if (cnt >= 19000000) return; if (ans <= sco) return; if (!A[d / w][d % w]) dfs(d + 1, s, sco); else { int y = d / w; int x = d % w; if ((B[y][x] & s).any()) { p &= 0; p.set(y * 10 + x); //(y*100+x); if (!(s & p).any()) dfs(d + 1, s, sco); dfs(d + 1, (B[y][x] | s) ^ B[y][x], sco + 1); } else dfs(d + 1, s, sco); } } int main() { while (cin >> w >> h, w) { memset(A, 0, sizeof(A)); r(i, 11) r(j, 11) B[i][j] &= 0; n = h * w; bitset<100> b; r(i, h) r(j, w) cin >> A[i][j]; r(i, h) r(j, w) if (A[i][j]) { int sum = 1; for (;; sum++) { int f = 0; for (int y = i; y < i + sum; y++) { for (int x = j; x < j + sum; x++) { if (A[y][x] == 0) f++; } } if (f) { sum--; break; } } bitset<100> tmp; for (int y = i, k = 0; k < sum; k++, y++) { for (int x = j, l = 0; l < sum; l++, x++) { tmp.set(y * 10 + x); } } b.set(i * 10 + j); B[i][j] = tmp; } ans = 100; cnt = 0; dfs(0, b, 0); cout << ans << endl; } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
551
#include <algorithm> #include <bitset> #include <cstdio> #include <cstdlib> #include <iostream> #include <iterator> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <string> #include <time.h> #include <vector> using namespace std; #define FOR(I, F, N) for (int I = F; I < (int)(N); I++) #define rep(i, n) FOR(i, 0, n) #define FIN(V) cout << V << endl #define pb push_back #define INF (1 << 28) template <typename T> void remove(vector<T> &vector, unsigned int index) { vector.erase(vector.begin() + index); } typedef pair<int, int> P; typedef long long ll; typedef priority_queue<int> pq; int StrToInt(string); string IntToStr(int); int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int main(void) { int n, r; while (cin >> n >> r, n | r) { int card[n]; card[0] = n; for (int i = 1; i < n; i++) card[i] = card[i - 1] - 1; rep(i, r) { int p, c; cin >> p >> c; int abv[p]; for (int j = 0; j < p - 1; j++) { abv[j] = card[j]; } for (int j = 0; j < c; j++) { card[j] = card[p - 1 + j]; } for (int j = 0; j < p; j++) { card[c + j] = abv[j]; } } cout << card[0] << endl; } return 0; } int StrToInt(string s) { stringstream ss; ss << s; int val; ss >> val; return val; } string IntToStr(int i) { stringstream ss; ss << i; return ss.str(); }
#include <algorithm> #include <bitset> #include <cstdio> #include <cstdlib> #include <iostream> #include <iterator> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <string> #include <time.h> #include <vector> using namespace std; #define FOR(I, F, N) for (int I = F; I < (int)(N); I++) #define rep(i, n) FOR(i, 0, n) #define FIN(V) cout << V << endl #define pb push_back #define INF (1 << 28) template <typename T> void remove(vector<T> &vector, unsigned int index) { vector.erase(vector.begin() + index); } typedef pair<int, int> P; typedef long long ll; typedef priority_queue<int> pq; int StrToInt(string); string IntToStr(int); int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int main(void) { int n, r; while (cin >> n >> r, n | r) { int card[n]; card[0] = n; for (int i = 1; i < n; i++) card[i] = card[i - 1] - 1; rep(i, r) { int p, c; cin >> p >> c; int abv[p]; for (int j = 0; j < p - 1; j++) { abv[j] = card[j]; } for (int j = 0; j < c; j++) { card[j] = card[p - 1 + j]; } for (int j = 0; j < p - 1; j++) { card[c + j] = abv[j]; } } cout << card[0] << endl; } return 0; } int StrToInt(string s) { stringstream ss; ss << s; int val; ss >> val; return val; } string IntToStr(int i) { stringstream ss; ss << i; return ss.str(); }
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
398
#include <algorithm> #include <iostream> using namespace std; int n, r, p, c; int h[51]; int main() { while (cin >> n >> r, n | r) { for (int i = 0; i < n; ++i) { h[i] = i; } for (int i = 0; i < r; ++i) { cin >> p >> c; rotate(h + 1, h + p, h + p + c); } cout << h[1] << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int n, r, p, c; int h[51]; int main() { while (cin >> n >> r, n | r) { for (int i = 1; i <= n; ++i) { h[i] = n - i + 1; } for (int i = 0; i < r; ++i) { cin >> p >> c; rotate(h + 1, h + p, h + p + c); } cout << h[1] << endl; } return 0; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 1, 0, 11, 12, 16, 31, 16, 31, 22], ["+", 0, 1, 0, 11, 12, 16, 31, 16, 17, 33], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
1
117