睡了一覺……懷疑被雨淋得要發燒了。真不爽。
Time Limit: 1000MS | Memory Limit: 30000K | |||
Total Submissions: 9453 | Accepted: 3510 | Special Judge |
Description
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
Output
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都還不會吶……憂愁……前路漫漫