NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

// Minimum Spanning Tree Algorithm (MST), Kruskal

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

#define MAXN 500
#define pb push_back

using namespace std;

typedef pair<int,int> ii;

struct edge{

int a,b,len;

friend bool operator < ( const edge &first , const edge &second ){
return first.len < second.len;
}
};

int N,M;
int rep[MAXN]; // rep[i] i numalari nodeun o an bulundugu unionuin temsilcisi

vector<edge> E;

int find( int nd ){ // nd numarali nodeun bulundugu uniounun temsilcisini bulan fonksiyon

if( nd == rep[nd] ) // Temsilciyi bulduk
return nd;

return rep[nd] = find(rep[nd]); // Bu node temsilci degilse bir sonraki adima geciyoruz, daha sonraki sorgularda isimizi kolaylastirmasi icin de rep[nd] yerine gelen sonucu yaziyoruz
}

int main(){

scanf("%d%d",&N,&M); // N tane node, M tane edge var

for( int a,b,c,i=0 ; i<M ; i++ ){

scanf("%d%d%d",&a,&b,&c);

edge k;
k.a = a;
k.b = b;
k.len = c;

E.pb(k); // Bu kez graphi liste ya a matris seklinde tutmuyoruz. Sadece edgelerin listesini tutuyoruz, Kruskal algorithmasi icin edgelerin kucukten buyuge siralanmis hali yeterli
}

for( int i=1 ; i<=N ; i++ ) // MST'yi olusturmadan once her node kendi basina bir union
rep[i] = i;

sort(E.begin(),E.end()); // Edgeleri kucukten buyuge sıralıyoruz

int i=0;
int cnt = 0;
int sum = 0;

while( cnt<N-1 ){ // N-1 tane edgei agaca ekledikten sonra baska edge eklemeye calismanin bir anlami yok

int a = E[i].a; // Siradaki edgein bagladigi nodelar a ve b
int b = E[i].b;
int len = E[i].len; // Edgein agirligi(uzunluk, maliyet, deger vs. problem neyse artik)

if( find(a) != find(b) ){ // a ve b nodelari ayni unionda degillerse bu edgei agaca ekleyebiliriz, uzunluk kontrolu yapmiyoruz cunku daha sonra ele alacagimiz edgelrin hic biri bu edgeden daha kucuk degil
rep[find(a)] = find(b); // Iki uniondan birinin temsilcisini digerine baglamak yeterli
cnt++; // Bir edge ekledigimiz icin cnt++
sum += len; // Toplam maliyetimiz edgedin agirligi kadar artti
}

i++; // Sonraki edge e gectik
}

printf("%dn",sum);
return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.