poj2485 kruskal与prim

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.

图论kruskal,一般结合并查集进行贪心思想.

先进行排序sort

pre[i]=i头结点赋值

a,b,=find()找到头节点

(a!=b){进行操作,pre[b]=a//合并}

结束.

图论Prim

邻接矩阵,vis=0;prim(1)

low=map[1][i];

vis[1]=1;

for 1~n-1

  min=inf

   for j-n

   if !vis[j] &&lowcost<min   min=lowcost ,k=j

     vis[k]=1

   for j-n

   if !vis[j]&&lowcost>map[k][j]

        lowcost=map[k][j]

一般无源最短路可以选用prim,和kruskal,怎么选用就要看数据大小了,这里N最大为500,开个500*500的数组时候最好用prim做。这里是练练kruskal。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 510
int n,q,pre[N];
struct point
{
    int x,y,len;
}p[N*N];
/*int cmp(const void * x,const void * y)
{
    return ((point*)x)->len>((point*)y)->len?1:-1;
}*/
int cmp(point x,point y)
{
    return x.len<y.len;
}
int find(int x)
{
    while(x!=pre[x])
        x=pre[x];
    return x;
}
void kruskal()
{
    int i,mix,a,b;
    mix=0;
    sort(p,p+q,cmp);
    //qsort(p,q,sizeof(point),cmp);
    for(i=1;i<=n;i++)
        pre[i]=i;
    for(i=0;i<q;i++)
    {
        a=find(p[i].x);
        b=find(p[i].y);
        if(a!=b)
        {
            if(p[i].len>mix)
                mix=p[i].len;
            pre[b]=a;
        }
    }
    cout<<mix<<endl;
}
void input()
{
    scanf("%d",&n);
    q=0;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
        {
                scanf("%d",&p[q].len);
                p[q].x=i;p[q].y=j;
                q++;
        }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        input();
        kruskal();
    }
    return 0;
}

prim代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define N 510
#define inf 0x3f3f3f3f
int map[N][N];
int lowcost[N];
int vis[N];
int n;
void prim(int u)
{
    int i,j,k,start,min,max;
    memset(vis,0,sizeof(vis));
    for(i=1;i<=n;i++)
        if(i!=u)
            lowcost[i]=map[1][i];
    vis[1]=true;
    k=0;
    min=0;max=0;
    for(i=1;i<n;i++)        //n-1条边
    {
        min=inf;
        for(j=1;j<=n;j++)
        if(lowcost[j]<min&&!vis[j])
        {
            min=lowcost[j];
            k=j;
        }
        if(min>max)
        max=min;
        vis[k]=true;
        for(j=1;j<=n;j++)
        if(lowcost[j]>map[k][j]&&!vis[j])
        {
            lowcost[j]=map[k][j];
        }
    }
    cout<<max<<endl;
}
void input()
{
    scanf("%d",&n);
    memset(map,inf,sizeof(map));
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    {
        scanf("%d",&map[i][j]);
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        input();
        prim(1);
    }
    return 0;
}



 

原文地址:https://www.cnblogs.com/amourjun/p/5134229.html