Submission #2865281


Source Code Expand

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<bitset>
#include<deque>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define ri register int
#define il inline
#define fi first
#define se second
#define mp make_pair
#define pi pair<int,int>
#define mem0(x) memset((x),0,sizeof (x))
#define mem1(x) memset((x),0x3f,sizeof (x))
#define pb push_back
#define gc getchar
template<class T>void in(T &x)
{
    x = 0; bool f = 0; char c = gc();
    while (c < '0' || c > '9') {if (c == '-') f = 1; c = gc();}
    while ('0' <= c && c <= '9') {x = (x << 3) + (x << 1) + (c ^ 48); c = gc();}
    if (f) x = -x;
}
#undef gc
#define N 200010
#define M N<<3
#define int ll
struct Edge {
    int u, v, w, nx;
    Edge() {}
    Edge(int uu, int vv, int ww, int nxt) {
        u = uu, v = vv, w = ww, nx = nxt;
    }
    friend bool operator<(const Edge& a, const Edge& b) {
        return a.w < b.w;
    }
} edge[M];
int cnt, head[M];
il void add(int u, int v, int w) {
    edge[++cnt] = Edge(u, v, w, head[u]);
    head[u] = cnt;
}
int f[N];
int find(int x) {
    return x == f[x] ? x : f[x] = find(f[x]);
}
bool cmpy(int x, int y) {
    int fx = find(x), fy = find(y);
    if (fx == fy) return 0;
    if (fy > fx) f[fy] = fx;
    else f[fx] = fy;
    return 1;
}
int n, q;
int d[N];
signed main() {
    in(n), in(q);
    mem1(d);
    for (ri i = 1, a, b, c; i <= q; ++i) {
        in(a), in(b), in(c);
        add(a, b, c);
        d[a] = min(d[a], c + 1);
        d[b] = min(d[b], c + 2);
    }
    for (ri i = 0; i < n; ++i) {
        d[i] = min(d[i], d[(i - 1 + n) % n] + 2);
    }
    for (ri i = 0; i < n; ++i) {
        d[i] = min(d[i], d[(i - 1 + n) % n] + 2);
    }
    for (ri i = 0; i <= n; ++i) {
        add(i, (i + 1) % n, d[i]);
    }
    sort(edge + 1, edge + 1 + cnt);
    for (ri i = 1; i < n; ++i) f[i] = i;
    int lt = n, ans = 0;
    for (ri i = 1; i <= cnt && lt > 1; ++i) {
        if (cmpy(edge[i].u, edge[i].v)) {
            ans += edge[i].w;
            --lt;
        }
    }
    printf("%lld", ans);
    return 0;
}

Submission Info

Submission Time
Task G - Zigzag MST
User emm207m
Language C++14 (GCC 5.4.1)
Score 1300
Code Size 2323 Byte
Status AC
Exec Time 89 ms
Memory 20736 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 3 ms 5888 KB
01-02.txt AC 41 ms 12032 KB
01-03.txt AC 79 ms 20736 KB
01-04.txt AC 28 ms 14592 KB
01-05.txt AC 20 ms 14592 KB
01-06.txt AC 24 ms 14592 KB
01-07.txt AC 23 ms 14592 KB
01-08.txt AC 22 ms 14592 KB
01-09.txt AC 24 ms 14592 KB
01-10.txt AC 50 ms 18688 KB
01-11.txt AC 65 ms 16640 KB
01-12.txt AC 80 ms 20736 KB
01-13.txt AC 79 ms 20736 KB
01-14.txt AC 77 ms 20736 KB
01-15.txt AC 78 ms 20736 KB
01-16.txt AC 77 ms 20736 KB
01-17.txt AC 77 ms 20736 KB
01-18.txt AC 70 ms 20736 KB
01-19.txt AC 22 ms 14592 KB
01-20.txt AC 21 ms 14592 KB
01-21.txt AC 33 ms 16640 KB
01-22.txt AC 60 ms 20736 KB
01-23.txt AC 61 ms 20736 KB
01-24.txt AC 22 ms 14592 KB
01-25.txt AC 89 ms 20736 KB
01-26.txt AC 28 ms 14592 KB
01-27.txt AC 27 ms 14592 KB
01-28.txt AC 51 ms 18688 KB
01-29.txt AC 65 ms 16640 KB
01-30.txt AC 76 ms 20736 KB
sample-01.txt AC 3 ms 5888 KB
sample-02.txt AC 2 ms 5888 KB
sample-03.txt AC 3 ms 5888 KB