Submission #3446755


Source Code Expand

#include <bits/stdc++.h>

#define Set(a, b) memset(a, b, sizeof (a))
#define For(i, j, k) for (int i = j; i <= k; ++ i)
#define Forr(i, j, k) for (int i = j; i >= k; -- i)

using namespace std;

typedef long long ll;

inline int read() {
	int x = 0, p = 1; char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') p = -1;
	for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	return x * p;
}

template <typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

inline void File() {
	freopen("g.in", "r", stdin);
	freopen("g.out", "w", stdout);
}

const int N = 2e5 + 10, inf = 0x3f3f3f3f;

int n, m, fa[N], cnt, val[N];

struct edge {
	int u, v, w;
	bool operator < (const edge &rhs) const {
		return w < rhs.w;
	}
} E[N << 1];

int find(int x) {
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}

int main() {

	Set(val, inf);

	n = read(), m = read();
	For(i, 1, m) {
		int u = read(), v = read(), w = read();
		chkmin(val[u], w + 1), chkmin(val[v], w + 2);
		E[++ cnt] = (edge) {u, v, w};
	}

	For(i, 1, n << 1) chkmin(val[i % n], val[(i - 1) % n] + 2);

	For(i, 0, n - 1) E[++ cnt] = (edge) {i, i == n - 1 ? 0 : i + 1, val[i]}, fa[i] = i;

	sort(E + 1, E + 1 + cnt);

	int c = 0; ll ans = 0;
	For(i, 1, cnt) {
		int u = find(E[i].u), v = find(E[i].v);
		if (u ^ v) 
			++ c, ans += E[i].w; fa[u] = v;
		if (c == n - 1) break;
	}
	
	cout << ans << endl;

	return 0;
}

Submission Info

Submission Time
Task G - Zigzag MST
User LSTete
Language C++14 (GCC 5.4.1)
Score 1300
Code Size 1486 Byte
Status AC
Exec Time 73 ms
Memory 8832 KB

Judge Result

Set Name sample all
Score / Max Score 0 / 0 1300 / 1300
Status
AC × 3
AC × 36
Set Name Test Cases
sample sample-01.txt, sample-02.txt, sample-03.txt
all 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
Case Name Status Exec Time Memory
01-01.txt AC 2 ms 2304 KB
01-02.txt AC 38 ms 6400 KB
01-03.txt AC 73 ms 6784 KB
01-04.txt AC 21 ms 8832 KB
01-05.txt AC 14 ms 7552 KB
01-06.txt AC 17 ms 8064 KB
01-07.txt AC 16 ms 6912 KB
01-08.txt AC 16 ms 6656 KB
01-09.txt AC 20 ms 6528 KB
01-10.txt AC 44 ms 6528 KB
01-11.txt AC 62 ms 6400 KB
01-12.txt AC 72 ms 6528 KB
01-13.txt AC 72 ms 6528 KB
01-14.txt AC 72 ms 6528 KB
01-15.txt AC 73 ms 6528 KB
01-16.txt AC 71 ms 6528 KB
01-17.txt AC 72 ms 6528 KB
01-18.txt AC 53 ms 6528 KB
01-19.txt AC 15 ms 6784 KB
01-20.txt AC 19 ms 6528 KB
01-21.txt AC 34 ms 6528 KB
01-22.txt AC 59 ms 6528 KB
01-23.txt AC 60 ms 6528 KB
01-24.txt AC 13 ms 6528 KB
01-25.txt AC 73 ms 6528 KB
01-26.txt AC 20 ms 6528 KB
01-27.txt AC 22 ms 6528 KB
01-28.txt AC 50 ms 6528 KB
01-29.txt AC 60 ms 6400 KB
01-30.txt AC 71 ms 6528 KB
sample-01.txt AC 2 ms 2304 KB
sample-02.txt AC 1 ms 2304 KB
sample-03.txt AC 1 ms 2304 KB