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, 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 file 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
10^6. 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.

Process to the end of file.

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

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

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

#define maxn 1010
#define maxm 15002
int n, m, maxlen, cnt;
int parent[maxn], ans[maxn];
struct edge
{
    int u, v, w;
}EG[maxm];

bool cmp(edge a, edge b)
{
    return a.w < b.w;
}
int Find(int x)
{
    if(parent[x] == -1) return x;
    return Find(parent[x]);
}
void Kruskal()
{
    memset(parent, -1, sizeof(parent));
    sort(EG, EG+m, cmp);
    for(int i = 0; i < m; i++)
    {
        int t1 = Find(EG[i].u), t2 = Find(EG[i].v);
        if(t1 != t2)
        {
            if(maxlen < EG[i].w)
                maxlen = EG[i].w;
            ans[cnt++] = i;
            parent[t1] = t2;
        }
    }
}
int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        for(int i = 0; i < m; i++)
            scanf("%d%d%d", &EG[i].u, &EG[i].v, &EG[i].w);
        maxlen = cnt = 0;
        Kruskal();
        printf("%d\n", maxlen);
        printf("%d\n", cnt);
        for(int i = 0; i < cnt; i++)
            printf("%d %d\n", EG[ans[i]].u, EG[ans[i]].v);
    }
    return 0;
}
时间: 2025-01-02 14:25:36

ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法的相关文章

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

题意   求n个点m条边的图的连通子图中最长边的最小值 实际上就是求最小生成树中的最长边  因为最小生成树的最长边肯定是所有生成树中最长边最小的  那么就也变成了最小生成树了  不要被样例坑到了  样例并不是最佳方案  只是最长边与最小生成树的最长边相等  题目是特判  直接用最小生成树做就行 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N =

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 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 c

POJ 2774 后缀数组:求最长公共子串

思路:其实很简单,就是两个字符串连接起来,中间用个特殊字符隔开,然后用后缀数组求最长公共前缀,然后不同在两个串中,并且最长的就是最长公共子串了. 注意的是:用第一个字符串来判断是不是在同一个字符中,刚开始用了第二个字符的长度来判断WA了2发才发现. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<

POJ 1743 后缀数组:求最长不重叠子串

数据:这题弄了好久,WA了数十发,现在还有个例子没过,可却A了,POJ 的数组也太弱了. 10 1 1 1 1 1 1 1 1 1 1 这组数据如果没有那个n-1<10判断的话,输入的竟然是5,我靠-- 思路:这个题目关键的地方有两个:第一,重复的子串一定可以看作是某两个后缀的公共前缀,第二,把题目转化成去判定对于任意的一个长度k,是否存在长度至少为k的不重叠的重复的子串. 转化成判定问题之后,就可以二分去解答了.在验证判定是否正确时,我们可以把相邻的所有不小于k的height[]看成一组,然后

最小生成树之 prim算法和kruskal算法(以 hdu 1863为例)

最小生成树的性质 MST性质:设G = (V,E)是连通带权图,U是V的真子集.如果(u,v)∈E,且u∈U,v∈V-U,且在所有这样的边中, (u,v)的权c[u][v]最小,那么一定存在G的一棵最小生成树,(u,v)为其中一条边. 构造最小生成树,要解决以下两个问题: (1).尽可能选取权值小的边,但不能构成回路(也就是环). (2).选取n-1条恰当的边以连接网的n个顶点. Prim算法的思想: 设G = (V,E)是连通带权图,V = {1,2,-,n}.先任选一点(一般选第一个点),首

POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14021   Accepted: 5484   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 1861 Network 解题报告

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16171   Accepted: 6417   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 1861 ——Network——————【最小瓶颈生成树】

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15268   Accepted: 5987   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