Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
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.
Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town
that is located at the end of both highways.
The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.
Input
The first line is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village
i and village j.
Output
You should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.
This problem contains multiple test cases!
The first line of a multiple input is an integer T, then a blank line followed by T input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of T output blocks. There is a blank line between output blocks.
Sample Input
130 990 692990 0 179692 179 0
Sample Output
692
題目分析;
剛開始錯誤的分析:
//把所有資料從大到小排序
//然後把最大的去掉,其所在的行和列各減1
//直到是該行或者該列最後一個元素(除了對角線) 我覺得這個就是結果
以上這種分析,沒有考慮到連通性的問題,比如四個點,最後可能得到 兩兩相連,而不是全連通的
正確做法:最小產生樹,prim演算法
#include<iostream>#include<stdio.h>#include<cmath>#include<iomanip>#include<list>#include <map>#include <vector>#include <string>#include <algorithm>#include <sstream>#include <stack>#include<queue>#include<string.h>using namespace std;int data[501][501];bool flag[501];int main(){int geshu;cin>>geshu;for(int xx=0;xx<geshu;xx++){memset(data,0,sizeof(data));memset(flag,0,sizeof(flag));int n;cin>>n;int m=n*n;for(int i=0;i<n;i++){for(int j=0;j<n;j++)cin>>data[i][j];}vector<int> muster;flag[0]=true;muster.push_back(0);int result=0;while(muster.size()<n){int index=0,max=99999;for(vector<int>::size_type i=0;i<muster.size();i++){for(int j=0;j<n;j++){if(data[muster[i]][j]!=0&&flag[j]==false&&data[muster[i]][j]<max){max=data[muster[i]][j];index=j;}}//end for j}//end for iflag[index]=true;muster.push_back(index);if(result<max)result=max;}//end whilecout<<result<<endl;if(xx!=geshu-1)cout<<endl;}//end for xx}