poj 1861 network
2011-11-21 22:21
337 查看
睡了一觉……怀疑被雨淋得要发烧了。真不爽。
Network
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
Sample Output
这道题告诉我们,读题是很重要的,读题也不是那么重要的,数据是会坑人的,special judge简直就是坑爹啊。
poj最有爱的一点就是它的discuss。当我觉得纠结的时候看看discuss,大家和我一起纠结甚至一起抱怨骂娘,顿时就觉得生活如此美丽。。。咳。。跑题了。这次的题中有一点很重要:the maximum length of a single cable is minimal。最长的边要最短。没有其他的了。即不要求总路程最短。所以,sample有环也没问题。但问题是,主流的做法kruskal还是用的最短路求啊喂,所以看着sample真是让人纠结啊纠结。
所以如果在茫茫description中看到了那句话并且深刻理解了它的含义的话,当然没有问题,如果没有,就只能像我一样,连sample都没有好好领悟,就开始做,然后误打误撞的居然想对了……不过,真要比赛的话不会出这种坑人的东西的吧?要是要求在最长边最短的情况下总路程最短或最长……就不会惹来这么多麻烦了。主要这题还是sample略微妙。是为了让我们看不出来是用最短路借咩?
其实我连prim都还不会呐……忧愁……前路漫漫
Network
Time Limit: 1000MS | Memory Limit: 30000K | |||
Total Submissions: 9453 | Accepted: 3510 | 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 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都还不会呐……忧愁……前路漫漫
相关文章推荐
- POJ 1861-Network(最小生成树-Kruskal)
- POJ 1861 Network (MST kruskal 加权合并 (瓶颈生成树))
- poj 1861 Network
- poj 1861 Network 解题报告
- ZOJ 1542 poj 1861 Network(并查集+最小树)
- Poj 1861 Network
- poj1861 Network(kruskal求最小生成树)
- POJ 1861 Network(KUS算法)
- POJ 1861 Network (并查集的应用,kruskal)
- POJ 1861 ——Network——————【最小瓶颈生成树】
- poj&nbsp;1861&nbsp;network&nbsp;(kruskal)
- POJ:1861 Network
- ZOJ 1542 poj 1861 Network(并查集+最小树)
- POJ 1861 Network
- POJ 1861 Network (Kruskal求MST模板题)
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
- pojn 1861 Network(Kruskal)
- poj 1861 Network(最小生成树)
- (kruscal12.3.1)POJ 1861 Network(求最小生成树的最大边&&并且输出各边的信息)
- POJ 1861 Network (Kruskal算法)