Highways
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 18 Accepted Submission(s) : 6
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.
1 3 0 990 692 990 0 179 692 179 0
692
#include <stdio.h>
int n,m[505][505],dis[505];
void Prim()
{
int vis[505] = {0};
vis[0] = true;
int k=0,minm = 70000;
for(int i=1; i<n; i++)
{
dis[i] = m[0][i];
if(dis[i] < minm) {minm = dis[i]; k = i;}
}
for(int j=1; j<n; j++)
{
minm = 70000;
vis[k] = 1;
for(int i=0; i<n; i++)
{
if(m[k][i]<dis[i] && !vis[i]) {dis[i] = m[k][i];}
}
for(int i=0; i<n; i++)
{
if(!vis[i] && dis[i]<minm)
{
minm = dis[i];
k = i;
}
}
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
scanf("%d", &m[i][j]);
}
int maxm = 0;
Prim();
for(int i=0; i<n; i++)
{
if(dis[i]>maxm) maxm = dis[i];
}
printf("%d\n", maxm);
}
return 0;
}
7862



被折叠的 条评论
为什么被折叠?



