题意:
Example
Input
4 5 1 1 2 3 1 3 4 1 4 5 2 3 8 3 4 2
Output
4
Note
我的题解:
考察最小生成树相关算法。
由于root节点直接收数据,其余节点可以传输数据给任意其他节点,但不能传输给多个。所有节点可以接收多个不同节点的数据,我们可以将问题转化成以root为生成树的根节点,求一颗最大边权最小的生成树!
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 typedef struct node{ 6 int u,v,w; 7 bool operator <( node &edg) 8 { 9 return w<edg.w; 10 }; 11 }Edge; 12 Edge e[100002]; 13 int n,m,rt; 14 int f[50002]; 15 void init(int n){ 16 for(int i=1;i<=n;i++) 17 f[i]=i; 18 } 19 int find(int x){ 20 return f[x]==x?x:f[x]=find(f[x]) ; 21 } 22 bool unite(int x,int y){ 23 x=find(x);y=find(y); 24 if(x==y) return false; 25 f[x]=y; 26 return true; 27 } 28 29 int kruskal(){ 30 sort(e+1,e+1+m); 31 int cnt = 0, ans = 0; 32 for(int i=1;i<=m;i++){ 33 if(unite(e[i].u,e[i].v)) 34 { 35 ans = max(ans, e[i].w); 36 if(++cnt == n-1) break; 37 } 38 } 39 return cnt==n-1?ans:-1; 40 } 41 int main(){ 42 cin>>n>>m>>rt; 43 init(n); 44 for(int i=1;i<=m;i++) 45 cin>>e[i].u>>e[i].v>>e[i].w; 46 47 cout<<kruskal()<<endl; 48 return 0; 49 }