CODE FESTIVAL 2016 Grand Final

Submission #4127488

Source codeソースコード

#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
#include <cmath>
using namespace std;

class UnionFind {
public:
	vector<int>par;

	void init(int size_) {
		par.resize(size_, -1);
	}
	int root(int pos) {
		if (par[pos] == -1) return pos;
		par[pos] = root(par[pos]);
		return par[pos];
	}
	void unite(int u, int v) {
		u = root(u); v = root(v);
		if (u != v) par[u] = v;
	}
	bool same(int u, int v) {
		if (root(u) == root(v)) return true;
		return false;
	}
};

long long N, x[19], y[19], a[19]; long double r[1 << 17], dp[1 << 17];

int main() {
	cin >> N;
	for (int i = 0; i < N; i++) cin >> x[i] >> y[i] >> a[i];

	for (int i = 0; i < (1 << N); i++) {
		int bit[15], cnt = 0; for (int j = 0; j < N; j++) { bit[j] = (i / (1 << j)) % 2; cnt += bit[j]; }

		vector<tuple<long double, int, int>>vec;
		for (int j = 0; j < N; j++) {
			for (int k = j + 1; k < N; k++) {
				if (bit[j] == 1 && bit[k] == 1) {
					long double leng = sqrtl(1.0L * (x[j] - x[k]) * (x[j] - x[k]) + 1.0L * (y[j] - y[k]) * (y[j] - y[k]));
					vec.push_back(make_tuple(leng, j, k));
				}
			}
		}
		sort(vec.begin(), vec.end());

		UnionFind UF; UF.init(N); long double sum = 0;
		for (int j = 0; j < vec.size(); j++) {
			int to1 = get<1>(vec[j]), to2 = get<2>(vec[j]);
			if (UF.same(to1, to2) == false) {
				sum += get<0>(vec[j]); UF.unite(to1, to2);
			}
		}

		long double A = 0; for (int j = 0; j < N; j++) A += 1.0L * bit[j] * a[j];
		r[i] = 1.0L * (A - sum) / cnt;
	}

	dp[0] = 1e9;
	for (int i = 0; i < (1 << N); i++) {
		vector<int>vec; for (int j = 0; j < N; j++) { if ((i / (1 << j)) % 2 == 0) vec.push_back(j); }
		
		for (int j = 0; j < (1 << vec.size()); j++) {
			int S = 0; for (int k = 0; k < vec.size(); k++) { if ((j&(1 << k)) != 0) S += (1 << vec[k]); }
			dp[i + S] = max(dp[i + S], min(dp[i], r[S]));
		}
	}
	printf("%.12Lf\n", dp[(1 << N) - 1]);
	return 0;
}

Submission

Task問題 E - Water Distribution
User nameユーザ名 E869120
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 1000
Source lengthソースコード長 1960 Byte
File nameファイル名
Exec time実行時間 763 ms
Memory usageメモリ使用量 2944 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - example0.txt,example1.txt
All 1000 / 1000 000.txt,001.txt,002.txt,003.txt,004.txt,005.txt,006.txt,007.txt,008.txt,009.txt,010.txt,011.txt,012.txt,013.txt,014.txt,015.txt,016.txt,017.txt,018.txt,019.txt,020.txt,021.txt,022.txt,023.txt,024.txt,025.txt,026.txt,027.txt,028.txt,029.txt,030.txt,031.txt,032.txt,033.txt,034.txt,035.txt,036.txt,037.txt,038.txt,039.txt,040.txt,041.txt,042.txt,043.txt,example0.txt,example1.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
000.txt AC 755 ms 2816 KB
001.txt AC 757 ms 2816 KB
002.txt AC 755 ms 2816 KB
003.txt AC 750 ms 2816 KB
004.txt AC 756 ms 2816 KB
005.txt AC 750 ms 2816 KB
006.txt AC 761 ms 2816 KB
007.txt AC 754 ms 2816 KB
008.txt AC 758 ms 2816 KB
009.txt AC 755 ms 2816 KB
010.txt AC 755 ms 2816 KB
011.txt AC 756 ms 2816 KB
012.txt AC 763 ms 2816 KB
013.txt AC 752 ms 2816 KB
014.txt AC 760 ms 2816 KB
015.txt AC 761 ms 2816 KB
016.txt AC 751 ms 2816 KB
017.txt AC 761 ms 2816 KB
018.txt AC 753 ms 2816 KB
019.txt AC 756 ms 2816 KB
020.txt AC 746 ms 2816 KB
021.txt AC 748 ms 2816 KB
022.txt AC 749 ms 2816 KB
023.txt AC 735 ms 2816 KB
024.txt AC 752 ms 2816 KB
025.txt AC 733 ms 2816 KB
026.txt AC 737 ms 2816 KB
027.txt AC 733 ms 2816 KB
028.txt AC 731 ms 2816 KB
029.txt AC 729 ms 2816 KB
030.txt AC 728 ms 2816 KB
031.txt AC 735 ms 2816 KB
032.txt AC 727 ms 2816 KB
033.txt AC 728 ms 2944 KB
034.txt AC 724 ms 2816 KB
035.txt AC 726 ms 2816 KB
036.txt AC 727 ms 2816 KB
037.txt AC 725 ms 2816 KB
038.txt AC 727 ms 2816 KB
039.txt AC 726 ms 2816 KB
040.txt AC 2 ms 2304 KB
041.txt AC 2 ms 2304 KB
042.txt AC 2 ms 2304 KB
043.txt AC 2 ms 2304 KB
example0.txt AC 2 ms 2304 KB
example1.txt AC 747 ms 2816 KB