poj 1861 Network (kruskal)

Network

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 13633   Accepted: 5288   Special Judge

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 company, they can be connected to each other using cables. Since each worker of the company must have access to the whole network,
each hub must be accessible by cables from any other hub (with possibly some intermediate hubs).

Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the maximum length of a single cable is minimal. There is another problem — not each hub can be connected to any other one
because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections.

You are to help Andrew to find the way to connect hubs so that all above conditions are satisfied.

Input

The first line of the input contains two integer numbers: N - the number of hubs in the network (2 <= N <= 1000) and M - the number of possible hub connections (1 <= M <= 15000). All hubs are numbered from 1 to N. The following M lines contain information about
possible connections - the numbers of two hubs, which can be connected and the cable length required to connect them. Length is a positive integer number that does not exceed 106. There will be no more than one way to connect two hubs. A hub cannot
be connected to itself. There will always be at least one way to connect all hubs.

Output

Output first the maximum length of a single cable in your hub connection plan (the value you should minimize). Then output your plan: first output P - the number of cables used, then output P pairs of integer numbers - numbers of hubs connected by the corresponding
cable. Separate numbers by spaces and/or line breaks.

Sample Input

4 6
1 2 1
1 3 1
1 4 2
2 3 1
3 4 1
2 4 1

Sample Output

1
4
1 2
1 3
2 3
3 4

Source

Northeastern Europe 2001, Northern Subregion

样例没有错误,因为,只要连通图中最长边最小就行了, 至于边的个数则没有限制,但感觉这题还是很坑!

#include"stdio.h"
#include"string.h"
#include"queue"
#include"algorithm"
using namespace std;
#define N 1005
#define inf 0x7fffffff
int pre[N],ans[N];
struct node
{
    int u,v,d;
}e[N*15];
bool cmp(node a,node b)
{
    return a.d<b.d;
}
int find(int x)
{
    if(x!=pre[x])
        pre[x]=find(pre[x]);
    return pre[x];
}
void kruskal(int n,int m)
{
    int i,f1,f2,j,cnt=0;
    for(i=1;i<=n;i++)
        pre[i]=i;
    for(i=0;i<m;i++)
    {
        f1=find(e[i].u);
        f2=find(e[i].v);
        if(f1!=f2)
        {
            pre[f1]=f2;
            cnt++;
            if(cnt>=n-1)
                break;
        }
    }
    printf("%d\n",e[i].d);
    printf("%d\n",i+1);
    for(j=0;j<=i;j++)
        printf("%d %d\n",e[j].u,e[j].v);
}
int main()
{
    int n,m,i;
    while(~scanf("%d%d",&n,&m))
	{
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].d);
        }
        sort(e,e+m,cmp);
        kruskal(n,m);
	}
    return 0;
}

poj 1861 Network (kruskal)

时间: 2024-08-06 03:45:02

poj 1861 Network (kruskal)的相关文章

POJ 1861 Network (MST)

题意:求解最小生成树,以及最小瓶颈生成树上的瓶颈边. 思路:只是求最小生成树即可.瓶颈边就是生成树上权值最大的那条边. 1 //#include <bits/stdc++.h> 2 #include <cstdio> 3 #include <vector> 4 #include <iostream> 5 #include <algorithm> 6 #define INF 0x7f7f7f7f 7 #define pii pair<int,

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

poj 1459 Power Network (dinic)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23059   Accepted: 12072 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

POJ 3414 Pots(罐子)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

poj 3399 Product(模拟)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int cmp(int x,int y) { return x>y; } int main() { int a[110],a1[110],a2[110],ans[110]; int n,k,k1,k2,i,k3; while(~scanf("%d%d",&n,&k

poj 1861 Network

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13260   Accepted: 5119   Special Judge 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 c

POJ 1840 Eqs(暴力)

Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The coefficients are given integers from the interval [-50,50]. It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,

最小生成树之克鲁斯卡尔(Kruskal)算法

学习最小生成树算法之前我们先来了解下 下面这些概念: 树(Tree):如果一个无向连通图中不存在回路,则这种图称为树. 生成树 (Spanning Tree):无向连通图G的一个子图如果是一颗包含G的所有顶点的树,则该子图称为G的生成树. 生成树是连通图的极小连通子图.这里所谓极小是指:若在树中任意增加一条边,则将出现一条回路:若去掉一条边,将会使之变成非连通图. 最小生成树(Minimum Spanning Tree,MST):或者称为最小代价树Minimum-cost Spanning Tr

POJ 3034 Whac-a-Mole(DP)

题目链接 题意 : 在一个二维直角坐标系中,有n×n个洞,每个洞的坐标为(x,y), 0 ≤ x, y < n,给你一把锤子可以打到地鼠,最开始的时候,你可以把锤子放在任何地方,如果你上一秒在(x1,y1),那下一秒直线移动到的整数点(x2,y2)与这个点的距离小于等于d,并且当锤子移动(x2,y2)这个点时,所有在两点的直线上的整点数都可以打到.例如(0,0)移动到(0,3).如果(0,1),(0,2)有老鼠出现就会被打到.求能够打的最多老鼠. 思路 : Dp[i][j][k]代表点(i,j)