最小生成树算法——Kruskal算法

#include<stdio.h>
#include<algorithm>
#include<windows.h>
using namespace std;
struct edge{
   int begin;
   int end;
   int weight;
};
bool cmp(struct edge a,struct edge b){
   return a.weight<b.weight;
}
];
];
int find(int index){
    ){
        index=father[index];
    }
    return index;
}
void minTree(int num){
   ;i<num;i++){
       int m=find(edges[i].begin);
       int n=find(edges[i].end);
       if(m!=n){
          father[m]=n;
          printf("(%d %d)\n",edges[i].begin,edges[i].end);
       }
   }
}
int main(){
   int n;
   scanf("%d",&n);
   ;i<n;i++){
      scanf("%d%d%d",&edges[i].begin,&edges[i].end,&edges[i].weight);
   }
   printf("\n");
   sort(edges,edges+n,cmp);
   minTree(n);

}
上一篇:c#的数据类型、运算符


下一篇:PrintWriter 和 BufferedWriter 写入文件.