zoukankan      html  css  js  c++  java
  • 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);
    }
    }



  • 相关阅读:
    电子商务运营模式
    PHP 常用资源
    Ajax 常用资源
    java 常用资源
    Net 常用资源
    Web App 响应式页面制作 笔记整理
    移动端click事件延迟300ms该如何解决
    阻止冒泡事件
    关于jquery stopPropagation()阻止冒泡事件
    集算器如何处理类文本数据计算
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2388301.html
Copyright © 2011-2022 走看看