poj 1861 network

来源:互联网 发布:数据库查询数据再处理 编辑:程序博客网 时间:2024/04/26 14:03

睡了一觉……怀疑被雨淋得要发烧了。真不爽。

Network
Time Limit: 1000MS Memory Limit: 30000KTotal 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 61 2 11 3 11 4 22 3 13 4 12 4 1

Sample Output

141 21 32 33 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 20002int n,m;int root[MAX];int num[MAX];int cnt;//rocord the No. of used edgeint 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都还不会呐……忧愁……前路漫漫


原创粉丝点击