当前位置:网站首页>Minimum spanning tree -- unblocked project hdu1863
Minimum spanning tree -- unblocked project hdu1863
2022-04-23 05:08:00 【FOWng_ lp】
Sample Input
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
Sample Output
3
?
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
typedef long long LL;
LL ans,t;
int fa[105];
struct N{
int a,b,c;
};
bool cmp(N a, N b){
return a.c < b.c;
}
int find(int x){
if(x!=fa[x]) return find(fa[x]);
else return x;
}
void combine(int a,int b){
if(find(a)!=find(b))fa[find(a)] = find(b);
}
int main(){
int n,m;
N A[105];
while(cin >> n >> m){
if(!n)break;
ans=0,t=1;
for(int i = 1;i <= m;i++)fa[i]=i;
for(int i = 1;i <= n;i++){
cin >> A[i].a >> A[i].b >>A[i].c;
}
sort(A+1,A+1+n,cmp);
for(int i = 1;i <= n;i++){
if(find(A[i].a)!=find(A[i].b)){
combine(A[i].a,A[i].b);
ans+=A[i].c;
}
}
for(int i = 2;i <= m;i++){
if(find(i)!=find(i-1)){
t=0;
break;
}
}
if(t)cout <<ans <<endl;
else cout << "?" <<endl;
}
return 0;
}
版权声明
本文为[FOWng_ lp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220549345393.html
边栏推荐
- Graduation project
- How does PostgreSQL parse URLs
- [database] MySQL multi table query (I)
- Discussion on flow restriction
- Using MySQL with Oracle
- Progress of innovation training (III)
- Installing kuberneters using kubedm
- 什么是指令周期,机器周期,和时钟周期?
- The applet calls the function of scanning QR code and jumps to the path specified by QR code
- Summary of R & D technology
猜你喜欢
#define 定义常量和宏,指针和结构体
Sword finger offer: the path with a certain value in the binary tree (backtracking)
The applet calls the function of scanning QR code and jumps to the path specified by QR code
[WinUI3]编写一个仿Explorer文件管理器
A trinomial expression that causes a null pointer
Unique primary key ID of tidb sub table -- solution to failure of sequence and Gorm to obtain primary key
Use the built-in function of win to transfer files between two computers in the same LAN (the speed is the same as that between local disks)
[2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
Deep learning notes - fine tuning
深度学习笔记 —— 数据增广
随机推荐
HRegionServer的详解
vscode ipynb文件没有代码高亮和代码补全解决方法
One month countdown, pgconf What are the highlights of the latest outlook of asia2021 Asian Conference?
Sword finger offer: the path with a certain value in the binary tree (backtracking)
Transaction isolation level of MySQL transactions
Innovation training (V) mid term inspection
[database] MySQL basic operation (basic operation ~)
C. Tree Infection(模拟+贪心)
[WinUI3]编写一个仿Explorer文件管理器
Making message board with PHP + MySQL
[WinUI3]編寫一個仿Explorer文件管理器
Knowledge points sorting: ES6
The vscode ipynb file does not have code highlighting and code completion solutions
Deep learning notes - fine tuning
Luogu p2731 horse riding fence repair
At pgconf Asia Chinese technology forum, listen to Tencent cloud experts' in-depth understanding of database technology
[database] MySQL single table query
Perfect test of coil in wireless charging system with LCR meter
Differences between redis and MySQL
TypeError: ‘Collection‘ object is not callable. If you meant to call the ......