# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
145548 | surface03 | 비용 (KOI11_cost) | C++14 | 57 ms | 4088 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
const LL MOD=(int)1e9;
const int LM=(int)1e5+1;
int N,M,G[LM],cnt[LM];
LL sum,ans;
struct data{
int s,e,w;
bool operator<(const data&r)const{
return w>r.w;
}
}A[LM];
void input(){
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++){
G[i]=i;
cnt[i]=1;
}
for(int i=0;i<M;i++){
scanf("%d%d%d",&A[i].s,&A[i].e,&A[i].w);
sum+=A[i].w;
}
sort(A,A+M);
}
int Find(int n){
if(G[n]==n)return n;
return G[n]=Find(G[n]);
}
void Union(int s,int e){
s=Find(s),e=Find(e);
if(s==e)return;
ans=(ans+sum*cnt[s]*cnt[e])%MOD;
G[s]=e;
cnt[e]+=cnt[s];
}
int main(){
input();
for(int i=0;i<M;i++){
Union(A[i].s,A[i].e);
sum-=A[i].w;
}
printf("%lld\n",ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |