ZOJ 1542 POJ 1861 Network 網路 最小產生樹,求最長邊,Kruskal演算法,zojkruskal

來源:互聯網
上載者:User

ZOJ 1542 POJ 1861 Network 網路 最小產生樹,求最長邊,Kruskal演算法,zojkruskal

題目串連:ZOJ 1542 POJ 1861 Network 網路

NetworkTime 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 15002int 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;}






poj1861,我的理解:最大邊最小的最小產生樹,用kruskal,最後還要輸出這個樹,對樹的輸出沒限制,對

這道題求的是所有的邊的長度的和最小的產生樹,不是最大邊最小的產生樹。。看一看它給的例子就能明白了。
 
最小產生樹 利用Kruskal演算法圖G的一棵最小產生樹T,用c語言

#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Description: 並查集儲存結構
// Tags: 值為-1則表示為根節點
struct DisjointSet
{
int *arr;// 值為父節點下標
int number;// arr元素個數
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Description: 初始化並查集結構
// Input: number - 元素的個......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.