Poj 2485 Highways

Posted vetsama

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Poj 2485 Highways相关的知识,希望对你有一定的参考价值。

 

因为这个图比较稠密,所以用prim算法

每次选到最小边的时候判断,保存最小边里面最大的边

注意要用scanf不然会超时

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;


int main()
{
    int n;
    cin >> n;
    while (n--) {
        int m;
        scanf("%d", &m);
        int map[501][501] = { 0 };
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= m; j++) {
                scanf("%d", &map[i][j]);
            }
        }
        int max = 0;
        int pos[501] = { 0 };
        int book[501] = { 0 };
        for (int i = 1; i <= m; i++) {
            pos[i] = map[1][i];
        }
        book[1] = 1;

        for(int k=1;k<m;k++){
            int min = 99999999;
            int t = 0;
            for (int i = 1; i <= m; i++) {
                if (pos[i] < min && book[i] == 0) {
                    min = pos[i];
                    t = i;
                }
            }
            if (t == 0) {
                break;
            }
            book[t] = 1;
            if (min > max) {
                max = min;
            }
            for (int i = 1; i <= m; i++) {
                if (map[t][i] < pos[i] && book[i] == 0) {
                    pos[i] = map[t][i];
                }
            }
        }

        cout << max << endl;

    }



    return 0;
}

 

以上是关于Poj 2485 Highways的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2485 Highways (最小生成树)

Highways POJ 2485Prim

poj 2485 Highways

POJ 2485 Highways (prim)

POJ2485 Highways MST

POJ 2485 Highways 最小生成树 (Kruskal)