• POJ 2485 Highways(最小生成树Prim算法)


    Highways

    Time Limit: 1000MS

     

    Memory Limit: 65536K

    Total Submissions: 14534

     

    Accepted: 6776

    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.

    Source

    POJ Contest,Author:Mathematica@ZSU

     解题报告:这道题就是求最小生成树的同时,求出最小生成树中最长的一个边;

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int N = 1010;
    const int INF = 0x7fffffff;
    int map[N][N], dis[N], visit[N];
    int n, ans;
    void Prim()
    {
    int i, j, p, min;
    memset(visit, 0, sizeof(visit));
    memset(dis, 0, sizeof(dis));
    for(i = 1; i <= n; ++i)
    {
    dis[i] = map[1][i];
    }
    visit[1] = 1;
    for (i = 1; i <= n; ++i)
    {
    min = INF;
    for (j = 1; j <= n; ++j)
    {
    if (!visit[j] && min > dis[j])
    {
    min = dis[j];
    p = j;
    }
    }
    visit[p] = 1;
    if (ans < dis[p])//求最长的一条边
    {
    ans = dis[p];
    }
    for (j = 1; j <= n; ++j)
    {
    if (!visit[j] && dis[j] > map[p][j])
    {
    dis[j] = map[p][j];
    }
    }
    }
    }
    int main()
    {
    int i, t, j;
    scanf("%d", &t);
    while (t --)
    {
    scanf("%d", &n);
    memset(map, 0, sizeof(map));
    for (i = 1; i <= n; ++i)
    {
    for (j = 1; j <= n; ++j)
    {
    scanf("%d", &map[i][j]);
    }
    }
    ans = -1;//哎一开始的时候忘了赋初值,WA了一次
    Prim();
    printf("%d\n", ans);
    }
    }



  • 相关阅读:
    JavaScript深入浅出补充——(一)数据类型,表达式和运算符
    Linux简介,虚拟机安装,网络设置,桌面和vim安装
    JavaScript对象之document对象
    python之MySQL学习——数据查询
    python框架Scrapy中crawlSpider的使用
    在Scrapy中使用IP池或用户代理更新版(python3)
    封装IP池和用户代理相应的类(python3)
    在Scrapy中使用IP池或用户代理(python3)
    scrapy工程创建及pycharm运行
    python框架Scrapy报错TypeError: 'float' object is not iterable解决
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2388301.html
Copyright © 2020-2023  润新知