Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<fstream>
- using namespace std;
- fstream f("a.in") ;
- ofstream g("a.out") ;
- int n,mc;
- struct muchie{int x,y,c;} ;
- muchie m[101] ;
- void sortare(muchie m[],int mc)
- {
- muchie aux;
- for(int i=1;i<=mc-1;i++)
- {
- for(int j=i+1;j<=mc;j++)
- {
- if(m[i].c>m[j].c)
- {
- aux=m[i] ;
- m[i]=m[j] ;
- m[j]=aux ;
- }
- }
- }
- }
- int prim(int n, muchie m[],int mc)
- {
- int ct=0,k=0,viz[101],i,t[101];
- for(i=1;i<=n;i++)
- viz[i]=0;
- viz[m[1].x]=1;
- while(k<n-1)
- {
- for(int i=1;i<=mc;i++)
- {
- if(viz[m[i].x]==1&&viz[m[i].y]==0||viz[m[i].x]==0&&viz[m[i].y]==1)
- {
- if(viz[m[i].x]==0)
- {
- viz[m[i].x]=1 ;
- t[m[i].x]=m[i].y ;
- }
- else
- {
- viz[m[i].y]=1 ;
- t[m[i].y]=m[i].x ;
- }
- ct=ct+m[i].c ;
- i=1;
- k++ ;
- }
- }
- }
- }
- int main()
- { int t[101];
- f>>n>>mc ;
- for(int i=1;i<=mc;i++)
- {
- f>>m[i].x>>m[i].y>>m[i].c ;
- }
- sortare(m,mc) ;
- g<<prim(n,m,mc);
- g<<endl ;
- for(int i=1;i<=n;i++)
- {
- g<<t[i]<<" ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement