POJ 1861 Network【最小生成树】

原创 2016年06月01日 12:25:34

Network

Time Limit: 1000MS

 

Memory Limit: 30000K

Total Submissions: 15829

 

Accepted: 6255

 

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

 

题目大意:给出n个节点,给出m条边,输出最小生成树中的最大边权值,然后输出一个可行生成树的解。


思路:直接克鲁斯卡尔算法水之。。。。


AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int f[100000];
int ans[100000][2];
struct zuobiao
{
    int x,y,w;
}a[121212];
int find(int x)
{
    return f[x] == x ? x : (f[x] = find(f[x]));
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
int cmp(zuobiao a,zuobiao b)
{
    return a.w<b.w;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)f[i]=i;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
    }
    sort(a,a+m,cmp);
    int output;
    int k=0;
    for(int i=0;i<m;i++)
    {
        if(find(a[i].x)!=find(a[i].y))
        {
            ans[k][0]=a[i].x;
            ans[k][1]=a[i].y;
            merge(a[i].x,a[i].y);
            k++;
            if(k==n-1)output=a[i].w;
        }
    }
    printf("%d\n%d\n",output,k);
    for(int i=0;i<k;i++)
    {
        printf("%d %d\n",ans[i][0],ans[i][1]);
    }
}





版权声明:0.0小白有写的不好不对的地方请指出~

相关文章推荐

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

求最小生成树中的最长边的值,然后输出所选择的边。Kruskal算法。

POJ1861 Network(最小生成树)

题意: 有n个顶点,m条边,每条边双向的,并且有一定的长度,现在要求使每个顶点连通起来,并且使总长度最短输出最大边,和边的总数,还有是这些边的情况输出 要点: 这破题,special jud...

POJ 1861-Network(最小生成树-Kruskal)

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16043   Accepted: 6360   ...
  • MIKASA3
  • MIKASA3
  • 2016年07月30日 18:50
  • 169

POJ 1861:Network(最小生成树&&kruskal)

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13266   Accepted: 5123   ...

POJ 1861 Network(隐含最小生成树 打印方案)

题意   求n个点m条边的图的连通子图中最长边的最小值 实际上就是求最小生成树中的最长边  因为最小生成树的最长边肯定是所有生成树中最长边最小的  那么就也变成了最小生成树了  不要被样例坑到了  ...
  • acvay
  • acvay
  • 2014年10月22日 11:16
  • 768

POJ 1861 Network Krusakl模板题 最小生成树

!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!...

poj1861 Network(kruskal求最小生成树)

Network Time Limit: 1000MS   Memory Limit: 30000K           Descrip...

network 最小生成树 Kruskal 算法 poj 1861 zoj 1542

#include #include #include using namespace std; const int MAXN=1001; const int MAXM=15001; str...

POJ 1861 & ZOJ 1542 Network(最小生成树之Krusal)

POJ 1861 & ZOJ 1542 Network(最小生成树之Krusal)

POJ 1861 Network(最小瓶颈生成树)

POJ 1861 Network(最小瓶颈生成树) http://poj.org/problem?id=1861 题意:        给你一个N个点和M条边的图,现在要你从这M条边中选一些边的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1861 Network【最小生成树】
举报原因:
原因补充:

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