CODE FESTIVAL 2016 Final

Submission #6352834

Source codeソースコード

//It is made by M_sea
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define re register
typedef long long ll;
using namespace std;

inline void chkmin(int& x,int y) { if (y<x) x=y; }
inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
    return X*w;
}

const int N=400000+10;

int n,m,Q;
int f[N],fa[N];

inline int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); }
inline void merge(int x,int y) {
    x=find(x),y=find(y);
    if (x!=y) fa[x]=y;
}

struct Edge { int u,v,w; } e[N];
bool operator <(Edge a,Edge b) { return a.w<b.w; }

int main() {
    n=read(),Q=read();
    memset(f,0x3f,sizeof(f));
    for (re int i=1;i<=Q;++i) {
        int a=read()+1,b=read()+1,c=read();
        chkmin(f[a%n+1],c+1),chkmin(f[b%n+1],c+2);
        e[++m]=(Edge){a,b,c};
    }
    int pos=1;
    for (re int i=2;i<=n;++i) if (f[i]<f[pos]) pos=i;
    for (re int i=pos%n+1,j=pos;i!=pos;j=i,i=i%n+1) chkmin(f[i],f[j]+2);
    for (re int i=1;i<n;++i) e[++m]=(Edge){i,i+1,f[i+1]};
    e[++m]=(Edge){n,1,f[1]};
    sort(e+1,e+m+1);
    for (re int i=1;i<=n;++i) fa[i]=i;
    int tot=0; ll ans=0;
    for (re int i=1;i<=m;++i) {
        int u=e[i].u,v=e[i].v,w=e[i].w;
        if (find(u)==find(v)) continue;
        ++tot,ans+=w,merge(u,v);
        if (tot==n-1) break;
    }
    printf("%lld\n",ans);
    return 0;
}
//qwqwq

Submission

Task問題 G - Zigzag MST
User nameユーザ名 vjudge2
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 1300
Source lengthソースコード長 1495 Byte
File nameファイル名
Exec time実行時間 76 ms
Memory usageメモリ使用量 9344 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 7936 KB
01-03.txt AC 74 ms 8064 KB
01-04.txt AC 21 ms 9344 KB
01-05.txt AC 14 ms 8576 KB
01-06.txt AC 17 ms 8832 KB
01-07.txt AC 16 ms 8192 KB
01-08.txt AC 16 ms 8064 KB
01-09.txt AC 20 ms 7936 KB
01-10.txt AC 45 ms 7936 KB
01-11.txt AC 64 ms 7936 KB
01-12.txt AC 74 ms 7936 KB
01-13.txt AC 74 ms 7936 KB
01-14.txt AC 74 ms 7936 KB
01-15.txt AC 76 ms 7936 KB
01-16.txt AC 74 ms 7936 KB
01-17.txt AC 74 ms 7936 KB
01-18.txt AC 54 ms 7936 KB
01-19.txt AC 16 ms 8192 KB
01-20.txt AC 19 ms 7936 KB
01-21.txt AC 34 ms 7936 KB
01-22.txt AC 61 ms 7936 KB
01-23.txt AC 61 ms 7936 KB
01-24.txt AC 14 ms 7936 KB
01-25.txt AC 75 ms 7936 KB
01-26.txt AC 20 ms 7936 KB
01-27.txt AC 22 ms 7936 KB
01-28.txt AC 50 ms 7936 KB
01-29.txt AC 61 ms 7936 KB
01-30.txt AC 71 ms 7936 KB
sample-01.txt AC 2 ms 3840 KB
sample-02.txt AC 2 ms 3840 KB
sample-03.txt AC 2 ms 3840 KB