Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- //#include <cstring>
- //#include <cmath>
- #include <algorithm>
- using namespace std;
- ifstream fin("date.in");
- //ofstream fout("date.out");
- const int mxn=1e3;
- const int nax=1e5;
- struct muchie{
- int x,y;
- int c;
- }v[nax];
- int L[nax], n, m;
- bool comparare(muchie a, muchie b){
- return a.c < b.c;
- }
- int main(){
- int i, j;
- int ax,ay;
- fin>>n>>m;
- for(i=1;i<=m;++i){
- fin>>v[i].x>>v[i].y>>v[i].c;
- }
- sort(v, v+m, comparare);
- for(i=1;i<=n;++i)
- L[i]=i;
- int ct=0, k=0;
- for(i=1;i<=n && k<n; ++i){
- if(L[v[i].x] != L[v[i].y]){
- ct+=v[i].c;
- ax=L[v[i].x]; ay=L[v[i].y];
- for(j=1;j<=n;++j)
- if(L[j]==ay)
- L[j]=ax;
- }
- }
- cout<<ct<<"\n";
- cout<<endl; return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement