标签:find clu cross style com planning sla nat 生成树
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They‘re planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.
Input
Output
Sample Input
1
3
0 990 692
990 0 179
692 179 0
Sample Output
692
题目意思:和畅通工程差不多,只是要输出最长的那条路:
解题思路:最小生成树
1 #include <iostream>
2 #include <queue>
3 #include <string.h>
4 #include <stdio.h>
5 #include <math.h>
6 using namespace std;
7
8 const int MAX = 500 + 100;
9 int visit[MAX];
10 int n;
11
12 struct S
13 {
14 int a,b;
15 int len;
16 };
17
18 struct cmp
19 {
20 bool operator() ( S a,S b )
21 {
22 return a.len>b.len;
23 }
24 };
25
26 int Find (int x)
27 {
28 if(x == visit[x])
29 return x;
30 else
31 return visit[x] = Find(visit[x]);
32 }
33
34 int mix(int x,int y)
35 {
36 int Tx = Find(x);
37 int Ty = Find(y);
38 if(Tx != Ty)
39 {
40 visit[Tx] = Ty;
41 return 1;
42 }
43 return 0;
44
45 }
46
47 int main()
48 {
49 int N;
50 cin>>N;
51
52 while(N--)
53 {
54 cin>>n;
55 int Map[MAX][MAX];
56 for(int i = 1;i <= n;i++)
57 {
58 visit[i] = i;
59 for(int j = 1;j <= n;j++)
60 cin>>Map[i][j];
61 }
62
63 S temp;
64 priority_queue<S,vector<S>,cmp>P;
65 for(int i = 1;i < n;i++)
66 for(int j = i+1;j <=n;j++)
67 {
68 temp.a = i;temp.b = j;
69 temp.len = Map[i][j];
70 P.push(temp);
71 }
72
73 int Max = 0;
74 while(!P.empty())
75 {
76 temp = P.top();
77 P.pop();
78
79 if( mix(temp.a,temp.b)==1)
80 {
81 Max = Max > temp.len?Max:temp.len;
82 }
83 }
84
85 cout<<Max<<endl;
86 }
87
88 return 0;
89 }
标签:find clu cross style com planning sla nat 生成树
原文地址:http://www.cnblogs.com/a2985812043/p/7294578.html