POJ2349 prim+heap

将MST的图分成S个联通分支,即是求第K大的生成树的边,这明显是稠密图,所以用prim。
Arctic Network
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3833 Accepted: 1389

Description

The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel.
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.

Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.

Input

The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000).

Output

For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.

Sample Input

1
2 4
0 100
0 300
0 600
150 750

Sample Output

212.13

#include <iostream> #include <cstdio> #include <vector> #include <cstring> #include <queue> #include <cmath> #include <algorithm> using namespace std; const int MAXN=510; struct cpoint { double x; double y; }point[MAXN]; const int INF=100000000; struct node { int to; double w; node(int _to=0,double _w=INF) { to=_to; w=_w; } }edge[MAXN]; bool operator<(const node &e1,const node &e2) { return e1.w>e2.w; } int main() { int t; scanf("%d",&t); int p; int i,j; int sub; int s; int visit[MAXN]; node dist[MAXN];//the minimum distance of one vertex to the tree double ans[MAXN]; while(t--) { priority_queue<node> qu; vector<node> cmap[MAXN]; scanf("%d %d",&s,&p); for(i=1;i<=p;i++) { scanf("%lf %lf",&point[i].x,&point[i].y); } memset(visit,0,sizeof(visit)); for(i=0;i<=p;i++) { dist[i].to=i; dist[i].w=INF; } node xmap; for(i=1;i<=p;i++) { sub=0; for(j=1;j<=p;j++) { xmap.to=j; xmap.w=sqrt((point[i].x-point[j].x)*(point[i].x-point[j].x)+(point[i].y-point[j].y)*(point[i].y-point[j].y)); cmap[i].push_back(xmap); } } qu.push(node(1,0)); int sum=p; node current; int AnsIndex=0; while(sum--) { do { current=qu.top(); qu.pop(); }while(visit[current.to]==true&&!qu.empty()); if(visit[current.to]==false) { visit[current.to]=true; ans[AnsIndex++]=current.w; for(i=0;i<cmap[current.to].size();i++)//for each v belong to Adj[current.to] { if(visit[cmap[current.to][i].to]==false) { if(cmap[current.to][i].w<dist[cmap[current.to][i].to].w) { dist[cmap[current.to][i].to].w=cmap[current.to][i].w; qu.push(dist[cmap[current.to][i].to]); } } } } } sort(&ans[1],&ans[p]); if(p<=s) { printf("0.00/n"); } else { printf("%.2f/n",ans[p-s]); } } return 0; }

你可能感兴趣的:(struct,input,each,output,pair,distance)