1 solutions
-
0
C++ :
#include<bits/stdc++.h> using namespace std; //存储一条路连接的两端的编号 以及 修好的时间 struct node{ int x,y,t; }a[100100]; int f[1100];//存储村庄之间是否有路的关系描述 //查:查询元素的根 int find(int x){ return f[x]==x?x:f[x]=find(f[x]); } //排序辅助函数:按时间升序 bool cmp(node n1,node n2){ return n1.t < n2.t; } int main() { int n,m,i; cin>>n>>m; for(int i = 1;i <= m;i++){ cin>>a[i].x>>a[i].y>>a[i].t; } //所有数据,按时间升序 sort(a+1,a+1+m,cmp); //初始化 for(int i = 1;i <= n;i++){ f[i] = i; } //合并(修路) int fx,fy,c = 0; for(int i = 1;i <= m;i++){ fx = find(a[i].x); fy = find(a[i].y); //如果两者之间没有路 if(fx != fy){ f[fx] = fy; c++; } if(c == n - 1){ cout<<a[i].t; return 0; } } cout<<-1; return 0; }
- 1
Information
- ID
- 9969
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By