Question C: Good king time limit: 1 Sec memory limit: MB
Submitted by: Resolve: 48
Submitted State [Discussion Version] Title Description
A long time ago, there is a poor country, the country has a good people of the king, but fortunes difficult, the country has always had to face the severe challenges of this scourge, another month of heavy rain, caused the flood, the flood broke the road, the water faded after a lot of villages became islands, good King am father, So he wanted to take some of the money out of his modest taxes to fix the roads for the villages, but with limited power to repair all the roads, the king decided to make sure that the village could be reached between 22. Now the king wants to know how long it will take to build the longest road.
Input
The input contains multiple sets of test data, each set of test data first entered an n, indicating that there are N villages 3 <= N <= 500, numbered 1-n, and then the bottom of a n*n matrix, the value of the first row J column, the distance of the village indicating the label I to the number J is this value, Unit 1~65536
Output
Output the longest minimum length of the road The king is going to build.
Sample input
30 990 692990 0 179692 179 0
Sample output
692
Simple minimum spanning tree to find the longest edge in the smallest spanning tree: Use the Kurskal algorithm to write one more step for the longest edge when adding edges , and then the prime algorithm is not written here.
Kurskal algorithm:
#include <stdio.h> #include <string.h> #include <algorithm> #define MAX 1100#define maxn (x, y) (x>y? X:Y) #define INF 0x3f3f3fusing namespace Std;int n,k,maxx;int set[max],map[max][max];struct node{int b;int e;int m;} S[max];bool CMP (node A,node b) {return A.M<B.M;} int find (int fa) {if (Fa==set[fa]) return Fa;return set[fa]=find (Set[fa]);} void Mix (int x,int y) {int fx=find (x); int fy=find (y); if (Fx!=fy) set[fx]=fy;} void init () {int i,j;for (i=1;i<=n;i++) set[i]=i;} void Getmap () {int I,j;memset (map,0,sizeof (map)), for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {scanf ("%d", &map[i] [j]);}} for (i=1;i<=n;i++) for (j=i+1;j<=n;j++) {if (map[i][j]>0) {s[k].b=i;s[k].e=j;s[k++].m=map[i][j];}}} void Solve () {int i,j;sort (S,S+K,CMP), for (i=0;i<k;i++) {if (Find (s[i].b)!=find (S[I].E)) {Mix (S[I].B,S[I].E); maxx= MAXN (MAXX,S[I].M);}} printf ("%d\n", Maxx);} int main () {int i,j;while (scanf ("%d", &n)!=eof) {k=0;maxx=0;init (); Getmap (); Solve ();} return 0;}
Hpuoj problem C: Good king "minimum spanning tree Kurskal"