CODE FESTIVAL 2016 Final

Submission #6352887

Source codeソースコード

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::min; using std::max;
using std::swap; using std::sort;
typedef long long ll;

template<typename T>
void read(T &x) {
	int flag = 1; x = 0; char ch = getchar();
	while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
	while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
}

const int _ = 4e5 + 10;
int n, q, d[_], cnt, fa[_];
struct Edge { int u, v, w; } e[_];
inline bool operator < (const Edge &a, const Edge &b) { return a.w < b.w; }
inline void ChkMin(int &a, int b) { if(a > b) a = b; }
inline void addEdge(int u, int v, int w) { e[++cnt] = (Edge){u, v, w}; }
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
bool unionn(int x, int y) {
	int fx = find(x), fy = find(y);
	if(fx == fy) return 0;
	fa[fx] = fy; return 1;
}

int main () {
#ifdef OFFLINE_JUDGE
	freopen("233.in", "r", stdin);
	freopen("233.out", "w", stdout);
#endif
	read(n), read(q);
	memset(d, 127, sizeof d);
	for(int i = 1, a, b, c; i <= q; ++i) {
		read(a), read(b), read(c), addEdge(a, b, c);
		ChkMin(d[a], c + 1), ChkMin(d[b], c + 2);
	}
	for(int i = 0; i < n; ++i) ChkMin(d[i], d[(i - 1 + n) % n] + 2);
	for(int i = 0; i < n; ++i) ChkMin(d[i], d[(i - 1 + n) % n] + 2);
	for(int i = 0; i <= n; ++i) addEdge(i, (i + 1) % n, d[i]);
	sort(e + 1, e + cnt + 1);
	for(int i = 1; i < n; ++i) fa[i] = i;
	int blo = n; ll ans = 0;
	for(int i = 1; i <= cnt && blo > 1; ++i)
		if(unionn(e[i].u, e[i].v)) ans += e[i].w, --blo;
	printf("%lld\n", ans);
	return 0;
} 

Submission

Task問題 G - Zigzag MST
User nameユーザ名 vjudge2
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 1300
Source lengthソースコード長 1578 Byte
File nameファイル名
Exec time実行時間 74 ms
Memory usageメモリ使用量 10240 KB

Test case

Set

Set name Score得点 / Max score Cases
sample - sample-01.txt,sample-02.txt,sample-03.txt
all 1300 / 1300 sample-01.txt,sample-02.txt,sample-03.txt,01-01.txt,01-02.txt,01-03.txt,01-04.txt,01-05.txt,01-06.txt,01-07.txt,01-08.txt,01-09.txt,01-10.txt,01-11.txt,01-12.txt,01-13.txt,01-14.txt,01-15.txt,01-16.txt,01-17.txt,01-18.txt,01-19.txt,01-20.txt,01-21.txt,01-22.txt,01-23.txt,01-24.txt,01-25.txt,01-26.txt,01-27.txt,01-28.txt,01-29.txt,01-30.txt,sample-01.txt,sample-02.txt,sample-03.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
01-01.txt AC 2 ms 3840 KB
01-02.txt AC 38 ms 7808 KB
01-03.txt AC 72 ms 8192 KB
01-04.txt AC 21 ms 10240 KB
01-05.txt AC 13 ms 8960 KB
01-06.txt AC 17 ms 9344 KB
01-07.txt AC 16 ms 8192 KB
01-08.txt AC 16 ms 8064 KB
01-09.txt AC 19 ms 7936 KB
01-10.txt AC 44 ms 7936 KB
01-11.txt AC 61 ms 7808 KB
01-12.txt AC 73 ms 7808 KB
01-13.txt AC 73 ms 7936 KB
01-14.txt AC 71 ms 7808 KB
01-15.txt AC 74 ms 7808 KB
01-16.txt AC 72 ms 7808 KB
01-17.txt AC 71 ms 7936 KB
01-18.txt AC 56 ms 7808 KB
01-19.txt AC 15 ms 8192 KB
01-20.txt AC 18 ms 7808 KB
01-21.txt AC 34 ms 7808 KB
01-22.txt AC 59 ms 7808 KB
01-23.txt AC 59 ms 7808 KB
01-24.txt AC 14 ms 7808 KB
01-25.txt AC 73 ms 7808 KB
01-26.txt AC 19 ms 7808 KB
01-27.txt AC 22 ms 7808 KB
01-28.txt AC 48 ms 7808 KB
01-29.txt AC 60 ms 7808 KB
01-30.txt AC 72 ms 7808 KB
sample-01.txt AC 2 ms 3840 KB
sample-02.txt AC 2 ms 3712 KB
sample-03.txt AC 2 ms 3712 KB