【prim】poj 2485

深度探讨信息技术领域:从基础到前沿
本文深入解析了信息技术领域的多个方面,包括前端开发、后端开发、移动开发等细分技术领域,同时覆盖了大数据开发、开发工具、嵌入式硬件、音视频基础、AI音视频处理等前沿话题,旨在为读者提供全面的技术洞察。
Highways
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20214 Accepted: 9377

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 of input is an integer T, which tells how many test cases followed. 
The first line of each case 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. There is an empty line after each test case.

Output

For each test case, 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.

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

Hint

Huge input,scanf is recommended.

第一次用prim解题,于是乎搜了一道prim水题
题目大意:
政府在n个城市间建造公路,每两个城市都有一个距离,求能连接所有城市的最短消耗建造资源的路线里,最长的两个城市间的距离是多少。
方法就是用最小生成树 prim   列出一个数组储存起始点到各点的距离,搜出距离最短的城市,标记,然后更新数组,更新方法是遍历数组取min(原值,新城市到改城市的距离)。以此类推直到经过所有城市。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <queue>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
int t,n;
const int maxn=500+2;
int dis[maxn][maxn]; //储存距离
bool vis[maxn];
int lowcost[maxn]; //prim数组
int prim()
{
	memset(vis,false,sizeof(vis));
	int minn=INF; //最小生成树
	int maxx=-INF; //最大的那条路
	vis[1]=true; //标记1
	for(int i=2;i<=n;i++)
		lowcost[i]=dis[1][i];//初始化prim队列
	for(int i=2;i<=n;i++)
	{
		minn=INF;
		int k;
		for(int j=2;j<=n;j++) //搜出队列中最短的路
			if(!vis[j]&&minn>lowcost[j])
                minn=lowcost[j],k=j;
        vis[k]=true; //标记该点
        for(int j=2;j<=n;j++) //更新队列
            if(!vis[j]&&dis[k][j]<lowcost[j])
                lowcost[j]=dis[k][j];
        maxx=max(maxx,minn); //取最大路
	}
    return maxx;
}
void solve() //呵呵- -
{
	int sum=prim();
	cout<<sum<<endl;
}
int main()
{
	cin>>t;
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				scanf("%d",&dis[i][j]);//你敢用cin吗- -
		solve();
	}
	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值