poj 2485

题意://岛要修路,这个岛上有n个城市,要求修完路后,各城市之间可以相互到达,且修的总路程最短  求所有道路的最长的一段路程

思路:Kruskal 算法简单的应用


#include<iostream>
#include<cstring>
using namespace std;
int map[501][501];
int dist[501];
int s[501];
int n;
void Krudkal()
{
int i,j;
int max=0;
memset(s,0,sizeof(s));
for(i=1;i<=n;i++)
dist[i]=map[1][i];
s[1]=1;
for(i=1;i<n;i++)
{
int v=-1;
int min=999999999;
for(j=1;j<=n;j++)
if(dist[j]<min&&!s[j])
{
min=dist[j];
v=j;
}
if(v==-1) break;
if(min>max) max=min;
s[v]=1;
//sum+=dist[v];
dist[v]=0;
for(j=1;j<=n;j++)
{
if(dist[j]>map[v][j])
dist[j]=map[v][j];
}
}
cout<<max<<endl;
}
int min(int x,int y)
{
if(x<y) return x;
return y;
}
int main()
{
int t,i,j;
//cin>>t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
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=1;j<=n;j++)
{
int e=min(map[i][j],map[j][i]);
map[i][j]=e;
map[j][i]=e;
}
Krudkal();
}
return 0;
}

poj 2485,布布扣,bubuko.com

时间: 2024-08-07 21:18:51

poj 2485的相关文章

poj 2485 Highways

链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,只不过要求的不是最小价值,而是最小生成树中的最大权值,只需要加个判断 比较最小生成树每条边的大小就行 #include<cstdio> #include<algorithm> using namespace std; int f[510],n,m; struct stu { int a,b,c; }t[20100]; int cmp(struct

poj 2485 Highways (最小生成树)

链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要加个推断 比較最小生成树每条边的大小即可 kruskal算法 #include<cstdio> #include<algorithm> using namespace std; int f[510],n,m; struct stu { int a,b,c; }t[20100]; int

poj 2485 Highways(最小生成树)

题目链接:http://poj.org/problem?id=2485 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 plann

POJ 2485 Highways &amp;&amp; HDU1102(20/200)

题目链接:Highways 没看题,看了输入输出,就有种似曾相识的感觉,果然和HDU1102 题相似度99%,但是也遇到一坑 cin输入竟然TLE,cin的缓存不至于这么狠吧,题目很水,矩阵已经告诉你了,就敲个模板就是了,5分钟,1A 题意就是打印,最小生成树的最大边权,改了改输入,水过 这个题完了,我的个人POJ计划进度以完成 20/200,这其中主要是图论的题,等下周把POJ计划图论的题目打完,就回头打模拟!我就不信还能比图论难 #include <iostream> #include &

poj 2485(prim最小生成树)

Highways 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

POJ 2485(Kruskal算法)

第一道Kruskal算法题 #include <iostream>#include <algorithm>#include <cstdio>using namespace std;#define max 505int f[max],maxw;struct edge{    int st,en,w;}ed[max*max/2]; int  find(int k){    if(k!=f[k])f[k]=find(f[k]);    return f[k];}void co

【POJ 2485】 Highways

[POJ 2485] Highways 最小生成树模板 Prim #include using namespace std; int mp[501][501]; int dis[501]; bool vis[501]; int n; int Prim() { int i,j,w,p,mm = 0; memset(dis,-1,sizeof(dis)); memset(vis,0,sizeof(vis)); dis[1] = 0; for(i = 0; i < n; ++i) { w = INF;

POJ 2485 Highways (kruskal 最小生成树)

Highways POJ 2485so 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 ca

Prim算法求最大权,POJ(2485)

题目链接:http://poj.org/problem?id=2485 解题报告: #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; #define NUM 1000 const int maxint=10000000; int c[NUM][NUM]; ///邻接矩阵 int ans=maxint; int