Submission #6352838


Source Code Expand

Copy
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define clear(x, y) memset(x, y, sizeof(x))
#define int long long

inline int read()
{
    int data = 0, w = 1; char ch = getchar();
    while(ch != '-' && (!isdigit(ch))) ch = getchar();
    if(ch == '-') w = -1, ch = getchar();
    while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
    return data * w;
}

const int maxn(2e5 + 10);
struct edge { int x, y, w; } e[maxn << 2];
inline int cmp(const edge &lhs, const edge &rhs) { return lhs.w < rhs.w; }
int dis[maxn], n, Q, e_num, fa[maxn], ans;
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
template<typename T> inline void chkmin(T &x, const T &y) { if(y < x) x = y; }
inline void add_edge(int x, int y, int w) { e[++e_num] = (edge) {x, y, w}; }

signed main()
{
    n = read(), Q = read(); memset(dis, 0x3f, sizeof dis);
    for(RG int i = 1, a, b, c; i <= Q; i++)
        a = read(), b = read(), c = read(),
        add_edge(a, b, c), chkmin(dis[a], c + 1), chkmin(dis[b], c + 2);
    for(RG int i = 0; i < n; i++) chkmin(dis[i], dis[(i - 1 + n) % n] + 2);
    for(RG int i = 0; i < n; i++) chkmin(dis[i], dis[(i - 1 + n) % n] + 2);
    for(RG int i = 0; i < n; i++) add_edge(i, (i + 1) % n, dis[i]), fa[i] = i;
    std::sort(e + 1, e + e_num + 1, cmp);
    for(RG int i = 1; i <= e_num; i++)
    {
        if(find(e[i].x) == find(e[i].y)) continue;
        fa[find(e[i].x)] = find(e[i].y); ans += e[i].w;
    }
    printf("%lld\n", ans);
    return 0;
}
//rtggsgs

Submission Info

Submission Time
Task G - Zigzag MST
User vjudge4
Language C++14 (GCC 5.4.1)
Score 1300
Code Size 1620 Byte
Status
Exec Time 109 ms
Memory 17664 KB

Judge Result

Set Name Score / Max Score Test Cases
sample 0 / 0 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
Case Name Status Exec Time Memory
01-01.txt 1 ms 2688 KB
01-02.txt 47 ms 6784 KB
01-03.txt 96 ms 14720 KB
01-04.txt 38 ms 10624 KB
01-05.txt 26 ms 9472 KB
01-06.txt 32 ms 9856 KB
01-07.txt 27 ms 8704 KB
01-08.txt 27 ms 8448 KB
01-09.txt 32 ms 8320 KB
01-10.txt 61 ms 10368 KB
01-11.txt 77 ms 9600 KB
01-12.txt 94 ms 14464 KB
01-13.txt 95 ms 14464 KB
01-14.txt 94 ms 14464 KB
01-15.txt 97 ms 14464 KB
01-16.txt 95 ms 14464 KB
01-17.txt 95 ms 14464 KB
01-18.txt 74 ms 17664 KB
01-19.txt 27 ms 8704 KB
01-20.txt 29 ms 8320 KB
01-21.txt 46 ms 10368 KB
01-22.txt 75 ms 14464 KB
01-23.txt 76 ms 14464 KB
01-24.txt 27 ms 11520 KB
01-25.txt 109 ms 17664 KB
01-26.txt 38 ms 11008 KB
01-27.txt 35 ms 9856 KB
01-28.txt 66 ms 13952 KB
01-29.txt 74 ms 12672 KB
01-30.txt 90 ms 16000 KB
sample-01.txt 1 ms 2688 KB
sample-02.txt 1 ms 2688 KB
sample-03.txt 1 ms 2688 KB