POJ 2485(Kruskal算法)

第一道Kruskal算法题

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
#define max 505
int f[max],maxw;
struct edge{
    int st,en,w;
}ed[max*max/2];

int  find(int k){
    if(k!=f[k])f[k]=find(f[k]);
    return f[k];
}
void combine(int a,int b,int w){
    if(a==b)return;
    if(a>b){f[a]=b;}
    if(b>a){f[b]=a;}
    maxw=w;
}
bool cmp(edge a,edge b){
    return a.w<b.w;
}
void Kruskal(int l){
    for(int i=0;i<l;i++){
        combine(find(ed[i].st),find(ed[i].en),ed[i].w);
    }
}
int main(){
    int t,k,n,b;
    //freopen("test.txt","r",stdin);
    cin>>t;
    while(t--){
        b=0;
        cin>>n;
        for(int i=0;i<n;i++){
            f[i]=i;
            for(int j=0;j<n;j++){
                cin>>k;
                if(j>i){ed[b].st=i;ed[b].en=j;ed[b++].w=k;}
            }
        }
        sort(ed,ed+b,cmp);
        Kruskal(b);
        cout<<maxw<<endl;
    }
    return 0;
}

POJ 2485(Kruskal算法)

时间: 2024-10-13 02:44:37

POJ 2485(Kruskal算法)的相关文章

POJ 1797 kruskal 算法

题目链接:http://poj.org/problem?id=1797 开始题意理解错.不说题意了. 并不想做这个题,主要是想测试kruskal 模板和花式并查集的正确性. 已AC: /* 最小生成树 kruskal算法 过程:每次选取没有参与构造最小生成树并且加入之后不会构成回路的边中权值最小的一条 作为最小生成树的一条新边.直至选择了V-1条边. */ #include <stdio.h> #include <string.h> #include <iostream>

Highways - poj 2485 (Prim 算法)

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24383   Accepted: 11243 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian g

POJ 2485 Highways【最小生成树最大边,Prime算法】

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28592   Accepted: 13037 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Fl

ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

题目链接:ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds      Memory Limit: 65536 KB You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

POJ 2421 Constructing Roads 修建道路 最小生成树 Kruskal算法

题目链接:POJ 2421 Constructing Roads 修建道路 Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19698   Accepted: 8221 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that e

ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

题目连接:ZOJ 1542 POJ 1861 Network 网络 Network Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, t

HDU 1301 &amp;POJ 1215 Jungle Roads【最小生成树,Prime算法+Kruskal算法】

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6737    Accepted Submission(s): 4893 Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst o

poj——2031 最小生成树(MST) Kruskal算法

poj——2031 最小生成树(MST)  Kruskal算法 Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4985   Accepted: 2503 Description You are a member of the space station engineering team, and are assigned a task in the constructio