POJ1861&ZOJ1542--Network【最小生成树】

链接:http://poj.org/problem?id=1861

最小生成树裸题,输出生成树的最长边、节点个数、节点坐标。另外OJ上样例输出时错的,4个点的最小生成树怎么可能4条边。。

主要是熟悉手写kruskal

#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 20010
#define eps 1e-7
#define INF 0x7FFFFFFF
#define seed 131
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

struct node{
    int u,v,dis;
}edge[MAXN];
int father[1010],point[1010][2];
int n,m,ans,sum;
bool cmp(node x,node y){
    return x.dis<y.dis;
}
int find(int x){
    int t = x;
    while(t!=father[t]){
        t = father[t];
    }
    int k = x;
    while(k!=t){
        int temp = father[k];
        father[k] = t;
        k = temp;
    }
    return t;
}
void kruskal(){
    int i,j=0;
    for(i=1;i<=n;i++)   father[i] = i;
    for(i=0;i<m;i++){
        int a = find(edge[i].u);
        int b = find(edge[i].v);
        if(a!=b){
            father[a] = b;
            point[j][0] = edge[i].u;
            point[j][1] = edge[i].v;
            sum+=edge[i].dis;
            ans = edge[i].dis;
            j++;
            if(j>=n-1)  break;
        }
    }
}
int main(){
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF){
        sum = 0;
        for(i=0;i<m;i++){
            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].dis);
        }
        sort(edge,edge+m,cmp);
        kruskal();
        printf("%d\n%d\n",ans,n-1);
        for(i=0;i<n-1;i++){
            printf("%d %d\n",point[i][0],point[i][1]);
        }
    }
    return 0;
}

POJ1861&ZOJ1542--Network【最小生成树】

时间: 2024-10-19 05:00:13

POJ1861&ZOJ1542--Network【最小生成树】的相关文章

POJ 2349 Arctic Network 最小生成树题解

本题也是使用Prime和Kruskal都可以的最小生成树的题解. 本题一点新意就是:需要除去最大的S-1个距离,因为可以使用卫星覆盖这些距离. 技巧:建图建有向图,速度快点,不用计算两边. 这里使用Prime,因为是稠密图. #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <al

poj2349Arctic Network(最小生成树kruscal+第k长的边)

题目链接: 啊哈哈,点我点我 题意: 北极的某区域共有n座村庄( 1 ≤ n ≤ 500 ),每座村庄的坐标用一对整数(x, y)表示,其中 0 ≤ x, y ≤ 10000.为了加强联系,决定在村庄之间建立通讯网络.通讯工具可以是无线电收发机,也可以是卫星设备.所有的村庄都可以拥有一部无线电收发机, 且所有的无线电收发机型号相同.但卫星设备数量有限,只能给一部分村庄配备卫星设备.  不同型号的无线电收发机有一个不同的参数d,两座村庄之间的距离如果不超过d就可以用该型号的无线电收发机直接通讯,d

QS Network(最小生成树)

题意:若两个QS之间要想连网,除了它们间网线的费用外,两者都要买适配器, 求使所有的QS都能连网的最小费用. 分析:这个除了边的权值外,顶点也有权值,因此要想求最小价值,必须算边及顶点的权值和. 解决方法:用prim算法,在构造邻接矩阵时,在i到j的权值的基础上再加上i点的权值和j点的权值即可. 附上AC代码: #include <stdio.h> #include <string.h> #include <stdlib.h> #define infinity 1000

ZOJ1586——QS Network(最小生成树)

QS Network DescriptionIn the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunicate with each other via networks. If two QS want to get connected, they need to buy two network adapters (one for each QS) and a segm

POJ-1861,Network,最小生成树水题,,注意题面输出有问题,不必理会~~

Network Time Limit: 1000MS   Memory Limit: 30000K          Special Judge http://poj.org/problem?id=1861 Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the compa

POJ 2349 Arctic Network (最小生成树第K大(小)边)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13108   Accepted: 4256 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

CSU 1633: Landline Telephone Network (最小生成树)

1633: Landline Telephone Network Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 63  Solved: 8 [Submit][Status][Web Board] Description The mayor of RMRCity wants to create a secure landline telephone network for emergency use in case of serious disas

POJ_2349_Arctic Network(最小生成树)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11782   Accepted: 3867 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

poj2349 Arctic Network - 最小生成树

2017-08-04 16:19:13 writer:pprp 题意如下: Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every o