kruskal_poj 2485 Highways

原创 2012年03月31日 00:29:21

此题关键要理解输出的定义

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

 

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>

#define MAXEN 280000
#define MAXVN 550

using namespace std;

typedef struct{
    int u, v, value;
}Edge;

Edge eg[MAXEN];
int p[MAXVN], rank[MAXVN];
vector<Edge> usedEdge;

void MAKE_SET(int x){
    p[x] = x;
    rank[x] = 0;
}
int FIND_SET(int x){
    if(x != p[x])
        p[x] = FIND_SET(p[x]);
    return p[x];
}
void LINK(int x, int y){
    if(rank[x] > rank[y])  p[y] = x;
    else{
        p[x] = y;
        if(rank[x] == rank[y]) rank[y] += 1;
    }
}

void UNION_SET(int x, int y){
    LINK(FIND_SET(x), FIND_SET(y));
}
int cmp(const void *a, const void *b){
    return (*(Edge *)a).value - (*(Edge *)b).value;
}

bool cp(Edge a,Edge b){
    return a.value < b.value;
}
int kruskal(int en, int vn){
    usedEdge.clear();//printf("sfdds");
    for(int i = 1; i <= vn; i++) MAKE_SET(i);
    qsort(eg,en,sizeof(eg[0]), cmp);
    for(int i = 0; i < en; i++){
        if(FIND_SET(eg[i].u) != FIND_SET(eg[i].v)){
            UNION_SET(eg[i].u,eg[i].v);
            usedEdge.push_back(eg[i]);
        }
    }
    int sum = 0;
    for(int i = 0; i < usedEdge.size(); i++) sum+=usedEdge[i].value;
    sort(usedEdge.begin(), usedEdge.end(), cp);
    //printf("\n");
    //for(int i = 0; i < usedEdge.size(); i++) printf("%d ", usedEdge[i].value);
    return usedEdge[usedEdge.size() - 1].value;
}
int main(){
    int t, vn;
    while(scanf("%d", &t) != EOF){
        //getchar();
        while(t--){
            //getchar();
            scanf("%d", &vn);
            int a;
            int count = 0;
            for(int i = 1; i <= vn; i++){
                for(int j = 1; j <= vn; j++){
                    scanf("%d",&a);
                    if(i != j){
                        eg[count].u = i;
                        eg[count].v = j;
                        eg[count++].value = a;
                    }
                }
            }

            int len = vn?kruskal(count, vn):0;
            printf("%d\n", len);
        }
    }
    return 0;
}


 

POJ:2485 Highways(最小生成树kruskal算法)

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28445   Acce...
  • zugofn
  • zugofn
  • 2016年08月03日 21:37
  • 120

POJ2485 Highways(prim和Kruskal最小生成树两种解法)

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23459   Accepted: 10850...

poj2485 Highways 最小生成树,kruskal算法

题目链接:http://poj.org/problem?id=2485 思路:还是赤裸裸的最校舍

POJ 2485 highways解题报告(最小生成树)(应用prim和Kruskal两种方法)(c++)

让我先把原题贴上来: Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27912 ...

POJ 2485 Highways 最小生成树 (Kruskal)

Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public ...
  • AXuan_K
  • AXuan_K
  • 2014年07月22日 14:49
  • 1076

poj 2485Highways(最小生成树 Kruskal)

很明显的一道最小生成树的题…… #include #include #include using namespace std; struct Edge { int start;//边的起点 int...

【POJ】2485--Highways(最小生成树 --Kruskal)

B - Highways  POJ - 2485 The island nation of Flatopia is perfectly flat. Unfortunately, F...

POJ 2485 Highways 最小生成树 Kruskal && Prim

Highways 求最小生成树的最大边 The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia...

POJ 2485-Highways(最小生成树裸题-prim/kruskal)

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27781   Accepted: 12690...
  • MIKASA3
  • MIKASA3
  • 2016年06月01日 21:37
  • 785

文章标题 POJ 2485 : Highways(最小生成树--kruskal+并查集)

HighwaysDescription The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:kruskal_poj 2485 Highways
举报原因:
原因补充:

(最多只允许输入30个字)