#include<bits/stdc++.h>
using namespace std;
int ace[222222],n;
priority_queue<pair<int,pair<int,int> > > q;
int ancestor(int x){
return x-ace[x]?ace[x]=ancestor(ace[x]):x;
}
int main(){
int m;
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
ace[i] = i;
while(m--){
int A,B,C;
scanf("%d%d%d",&A,&B,&C);
q.push(make_pair(-C,make_pair(A,B)));
q.push(make_pair(-(C+1),make_pair(B,(A+1)%n)));
}
long long int res = 0;
while(!q.empty()){
pair<int,pair<int,int> > pr = q.top();
q.pop();
int C = -pr.first, A = pr.second.first, B = pr.second.second;
if(ancestor(A) == ancestor(B))
continue;
res += C;
q.push(make_pair(-(C+2),make_pair((A+1)%n,(B+1)%n)));
ace[ancestor(A)] = ancestor(B);
}
cout << res << endl;
return 0;
}
./Main.cpp: In function ‘int main()’:
./Main.cpp:10:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
^
./Main.cpp:15:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&A,&B,&C);
^