Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct muchie{int x, y, c;};
- muchie u[100]; int L[100];
- ifstream f("graf.in");
- int n, m;
- void citire()
- {
- f>>n>>m;
- for(int i=1;i<=m;i++)
- {
- f>>u[i].x>>u[i].y>>u[i].c;
- }
- for(int i=1;i<=n;i++)
- {
- L[i]=i;
- }
- }
- void Kruskal()
- {
- for(int i=1;i<=m;i++)
- {
- for(int j=i+1;j<=m;j++)
- {
- if(u[i].c>u[j].c)
- swap(u[i], u[j]);
- }
- }
- int k=1, i=1, ct=0;
- while(k<=n-1)
- {
- int xx=u[i].x;
- int yy=u[i].y;
- if(L[xx]!=L[yy])
- {
- cout<<xx<<" "<<yy<<" "<<u[i].c<<endl;
- ct=ct+u[i].c;
- for(int j=1;j<=n;j++)
- {
- if(L[j]==yy)
- {
- L[j]=xx;
- }
- }
- k++;
- }
- i++;
- }
- cout<<ct;
- }
- int main()
- {
- citire();
- Kruskal();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement