[sicily online]1090. Highways

來源:互聯網
上載者:User
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}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.