poj 1861 network

睡了一覺……懷疑被雨淋得要發燒了。真不爽。

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

這道題告訴我們,讀題是很重要的,讀題也不是那麼重要的,數據是會坑人的,special judge簡直就是坑爹啊。

poj最有愛的一點就是它的discuss。當我覺得糾結的時候看看discuss,大家和我一起糾結甚至一起抱怨罵娘,頓時就覺得生活如此美麗。。。咳。。跑題了。這次的題中有一點很重要:the maximum length of a single cable is minimal。最長的邊要最短。沒有其他的了。即不要求總路程最短。所以,sample有環也沒問題。但問題是,主流的做法kruskal還是用的最短路求啊喂,所以看着sample真是讓人糾結啊糾結。

所以如果在茫茫description中看到了那句話並且深刻理解了它的含義的話,當然沒有問題,如果沒有,就只能像我一樣,連sample都沒有好好領悟,就開始做,然後誤打誤撞的居然想對了……不過,真要比賽的話不會出這種坑人的東西的吧?要是要求在最長邊最短的情況下總路程最短或最長……就不會惹來這麼多麻煩了。主要這題還是sample略微妙。是爲了讓我們看不出來是用最短路借咩?

#include <iostream>
#include <algorithm>
using namespace std;

#define MAX 20002

int n,m;
int root[MAX];
int num[MAX];
int cnt;//rocord the No. of used edge
int plan[MAX];

struct edge
{
       int a,b,w;
       bool operator < (const edge &x)const
       {
            return w<x.w;
       }
} edge[MAX];



int find(int x)
{
    if(root[x]!=x)
    root[x]=find(root[x]);
    return root[x];
}

void merge(int a,int b)
{
     if(num[a]<=num[b])
 {
  root[a]=b;
  num[b] += num[a];
 }
 else
 {
  root[b]=a;
  num[a] += num[b];
 }
}

int kruskal()
{
    int i,fa,fb;
    cnt=0;
    sort(edge,edge+m);
    for(i=1;i<=n;i++)
    {
        root[i]=i;
        num[i]=1;
    }
    for(i=0;i<m;i++)
    {
        fa=find(edge[i].a);
        fb=find(edge[i].b);
        if(fa!=fb)
        {
            plan[cnt++]=i;//record the edge
            merge(fa,fb);
            if(cnt==n-1)
                return edge[i].w;
            //since the edge is sorted by w.now is the longest w in all edges
        }
    }
}

int main()
{
    
    cin>>n>>m;
    int i;
    for(i=0;i<m;i++)
    {
        cin>>edge[i].a>>edge[i].b>>edge[i].w;
    }
    cout<<kruskal()<<endl;//the longest edge used
    cout<<cnt<<endl;//
    
    for(i=0;i<cnt;i++)
        cout<<edge[plan[i]].a<<" "<<edge[plan[i]].b<<endl;
      //  system("pause");
        return 0;
               
}

其實我連prim都還不會吶……憂愁……前路漫漫


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章