标签:mon hide can play 通过 color 压缩 none count
#include<cstdio> #include<cstring> #include<cmath> #include<map> #include<queue> #include<iostream> #include<algorithm> using namespace std; const int maxn=102; int n,m; int pre[maxn]; struct rode { int x; int y; int z; } p[1000]; int compare(rode node1,rode node2) //排序 ,通过值 { if(node1.z < node2.z) return 1; return 0; } void hb()//初始化 { for(int i=1; i<=n; i++) pre[i]=i; } int Find(int x)//路径压缩 { while(x!=pre[x]) x=pre[x]; return x; } void uoin(int x,int y)//合并 { int fx=Find(x); int fy=Find(y); if(fx!=fy) pre[fx]=fy; } int main() { while(scanf("%d%d",&n,&m)&&n) { int i; for(i=0; i<n; i++) scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z); sort(p,p+n,compare); hb(); int minz=0,count=0; for (i = 0; i < n; i++) { if (Find(p[i].x) != Find(p[i].y)) { uoin(p[i].x,p[i].y); minz+= p[i].z; count++; if(count == m-1) break; } } if(count < m-1) printf("?\n"); else printf("%d\n", minz); } }
加油加油
标签:mon hide can play 通过 color 压缩 none count
原文地址:https://www.cnblogs.com/She-Chuan/p/9744324.html