Submission #4044424
Source Code Expand
#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b) for(int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clar(a, b) memset((a), (b), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
typedef long long LL;
typedef long double LD;
int read() {
char ch = getchar();
int x = 0, flag = 1;
for (;!isdigit(ch); ch = getchar()) if (ch == '-') flag *= -1;
for (;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
return x * flag;
}
void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) write(x / 10);
putchar(x % 10 + 48);
}
const int Maxn = 200009;
int fa[Maxn];
int find(int u) {
return fa[u] != u ? fa[u] = find(fa[u]) : u;
}
struct edge {
int u, v;
LL w;
int operator < (const edge b) const {
return w < b.w;
}
};
vector <edge> s;
int n, q;
LL val[Maxn];
void init() {
n = read(), q = read();
rep (i, 1, n) fa[i] = i;
rep (i, 0, n) val[i] = INT_MAX - n;
rep (i, 1, q) {
int u = read(), v = read(), w = read();
s.push_back((edge){u % n, v % n, w});
val[(u + 1) % n] = min(val[(u + 1) % n], w + 1ll);
val[(v + 1) % n] = min(val[(v + 1) % n], w + 2ll);
}
}
void solve() {
rep (i, 1, n - 1) val[i] = min(val[i - 1] + 2, val[i]);
val[0] = min(val[n - 1] + 2, val[0]);
rep (i, 1, n - 1) val[i] = min(val[i - 1] + 2, val[i]);
rep (i, 1, n - 1) s.push_back((edge){i - 1, i, val[i]});
s.push_back((edge){n - 1, 0, val[0]});
LL ans = 0;
sort(s.begin(), s.end());
rep (i, 0, s.size() - 1) {
int x = find(s[i].u), y = find(s[i].v);
if (x != y) {
ans += s[i].w;
fa[x] = y;
}
}
cout << ans << endl;
}
int main() {
/// freopen("G.in", "r", stdin);
/// freopen("G.out", "w", stdout);
init();
solve();
#ifdef Qrsikno
debug("\nRunning time: %.3lf(s)\n", clock() * 1.0 / CLOCKS_PER_SEC);
#endif
return 0;
}
Submission Info
Submission Time |
|
Task |
G - Zigzag MST |
User |
qrsikno |
Language |
C++ (GCC 5.4.1) |
Score |
1300 |
Code Size |
1981 Byte |
Status |
AC |
Exec Time |
82 ms |
Memory |
13672 KB |
Judge Result
Set Name |
sample |
all |
Score / Max Score |
0 / 0 |
1300 / 1300 |
Status |
|
|
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 |
1 ms |
256 KB |
01-02.txt |
AC |
44 ms |
6256 KB |
01-03.txt |
AC |
80 ms |
12780 KB |
01-04.txt |
AC |
23 ms |
8172 KB |
01-05.txt |
AC |
15 ms |
8688 KB |
01-06.txt |
AC |
19 ms |
8304 KB |
01-07.txt |
AC |
18 ms |
7920 KB |
01-08.txt |
AC |
18 ms |
7792 KB |
01-09.txt |
AC |
23 ms |
8688 KB |
01-10.txt |
AC |
52 ms |
11372 KB |
01-11.txt |
AC |
68 ms |
10092 KB |
01-12.txt |
AC |
80 ms |
10988 KB |
01-13.txt |
AC |
81 ms |
12780 KB |
01-14.txt |
AC |
81 ms |
12780 KB |
01-15.txt |
AC |
82 ms |
10988 KB |
01-16.txt |
AC |
81 ms |
12908 KB |
01-17.txt |
AC |
82 ms |
12780 KB |
01-18.txt |
AC |
59 ms |
13032 KB |
01-19.txt |
AC |
17 ms |
8432 KB |
01-20.txt |
AC |
22 ms |
7792 KB |
01-21.txt |
AC |
37 ms |
8304 KB |
01-22.txt |
AC |
66 ms |
12908 KB |
01-23.txt |
AC |
66 ms |
12652 KB |
01-24.txt |
AC |
16 ms |
9196 KB |
01-25.txt |
AC |
78 ms |
13672 KB |
01-26.txt |
AC |
22 ms |
8684 KB |
01-27.txt |
AC |
25 ms |
7532 KB |
01-28.txt |
AC |
55 ms |
11756 KB |
01-29.txt |
AC |
65 ms |
10476 KB |
01-30.txt |
AC |
78 ms |
11884 KB |
sample-01.txt |
AC |
1 ms |
256 KB |
sample-02.txt |
AC |
1 ms |
256 KB |
sample-03.txt |
AC |
1 ms |
256 KB |