poj 2485 Highways

题目链接:http://poj.org/problem?id=2485 点击打开链接

题目思路:prim 算法(转载)

最小生成树prim算法实现

今天从志权师兄那里学会了最小生成树。所谓生成树,就是n个点之间连成n-1条边的图形。而最小生成树,就是权值(两点间直线的值)之和的最小值。

  

         首先,要用二维数组记录点和权值。如上图所示无向图:

int map[7][7];
       map[1][2]=map[2][1]=4;
       map[1][3]=map[3][1]=2;
       ......

      然后再求最小生成树。具体方法是:

1.先选取一个点作起始点,然后选择它邻近的权值最小的点(如果有多个与其相连的相同最小权值的点,随便选取一个)。如1作为起点。

visited[1]=1;

pos=1;

//用low[]数组不断刷新最小权值,low[i](0<i<=点数)的值为:i点到邻近点(未被标记)的最小距离。

low[1]=0;  //起始点i到邻近点的最小距离为0

low[2]=map[pos][2]=4;

low[3]=map[pos][3]=2;

low[4]==map[pos][4]=3;

low[5]=map[pos][5]=MaxInt;  //无法直达

low[6]=map[pos][6]=MaxInt;

 

  2.再在伸延的点找与它邻近的两者权值最小的点。

//low[]以3作当前位置进行更新

visited[3]=1;

pos=3;

low[1]=0;   //已标记,不更新

low[2]=map[1][2]=4;  //比5小,不更新

low[3]=2;  //已标记,不更新

low[4]=map[1][4]=3;   //比1大,更新后为:low[4]=map[3][4]=1;

low[5]=map[1][5]=MaxInt;//无法直达,不更新

low[6]=map[1][6]=MaxInt;//比2大,更新后为:low[6]=map[3][6]=2;

 

    3.如此类推...

 
 
     当所有点都连同后,结果最生成树如上图所示。

     所有权值相加就是最小生成树,其值为2+1+2+4+3=12。



Highways
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24687 Accepted: 11390

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.


#include <stdio.h> //定义输入/输出函数
#include <limits.h> //定义各种数据类型最值常量
#include <math.h> //定义数学函数
#include <stdlib.h> //定义杂项函数及内存分配函数
#include <string.h> //字符串处理
#include <algorithm>//算法
#include <queue>//队列
#include <stack>//栈
#include <vector>//动态数组
using namespace std;
int T, N, pos, mins;
int map[505][505];
bool vis[505];
int low[505];
int cmp(int a, int b) {return a > b;}
int main()
{
    while (scanf("%d", &T) != EOF){
 		while (T--){
        memset(map, 0, sizeof(map));
        memset(low, 0, sizeof(low));
        memset(vis, 0, sizeof(vis));
        scanf("%d", &N);
        printf("N = %d\n", N);
        for (int i = 1; i <= N; i++)
            for (int j = 1; j <= N; j++)
                scanf("%d", &map[i][j]);
        for (int i = 1; i <= N; i++)
            low[i] = map[1][i];
        low[1] = 0;
        vis[1] = pos = 1;
        for (int i = 1; i < N; i++){
            mins = 65550;
            for (int j = 1; j <= N; j++)
                if (vis[j] == 0 && mins > low[j]){
                    mins = low[j];
                    pos = j;
                }
            //在d[N]中找到最小值的位置
            vis[pos] = 1;
            for (int j = 1; j <= N; j++){
                if(low[j] > map[pos][j] && vis[j] == 0)
                    low[j] = map[pos][j];
            }
            //用最小值的位置更新Low数组
        }
        //for (int i = 0; i <= N; i++) printf(" %d", low[i]);
        //printf("\n");
        sort(low, low + N + 1, cmp);
        for (int i = 0; i <= N; i++) printf(" %d", low[i]);
        printf("\n");
        printf("%d\n", low[0]);
    	}
    }
}
/*
2
 
3
0 990 692
990 0 179
692 179 0
 
6
0 4 2 3 100 100
4 0 5 4 3 100
2 5 0 1 100 2
3 4 1 0 6 2
100 3 100 6 0 4
100 100 2 2 4 0
*/




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值