http://acm.hdu.edu.cn/showproblem.php?pid=1875
继续MST,一开始没太注意double,醉了
畅通工程再续
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20770 Accepted Submission(s): 6558
Problem Description
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。
Input
输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
Output
每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.
Sample Input
2 2 10 10 20 20 3 1 1 2 2 1000 1000
Sample Output
1414.2 oh!
Author
8600
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int INF=1<<30;
struct node{
double x,y;
};
struct edge{
int v;
double w;
edge(int vv,double dd):v(vv),w(dd){}
edge(){}
friend bool operator < (const edge &a,const edge &b)
{
return a.w>b.w;
}
};
node cor[110];
int c;
vector<vector<edge> >G;
double get_d(node p1,node p2){
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
bool used[110];
double dis[110];
void prim(int s){
priority_queue<edge>q;
memset(used,0,sizeof(used));
for(int i=0;i<110;i++)
dis[i]=1.0+INF;
dis[s]=0.;
q.push(edge(s,0));
double ans=0;
int cnt=0;
while(!q.empty()){
edge p=q.top();
q.pop();
int u=p.v;
if(used[u])continue;
ans+=p.w;
cnt++;
used[u]=true;
if(cnt==c){printf("%.1lf\n",ans*100);return;}
for(int i=0;i<G[u].size();i++){
int v=G[u][i].v;
double w=G[u][i].w;
if(used[v]||dis[v]<=w)continue;
dis[v]=w;
q.push(edge(v,w));
}
}
printf("oh!\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&c);
G.clear();
G.resize(c+10);
for(int i=0;i<c;i++)
scanf("%lf%lf",&cor[i].x,&cor[i].y);
for(int i=0;i<c;i++){
node p1=cor[i];
for(int j=i+1;j<c;j++){
node p2=cor[j];
double d=get_d(p2,p1);
if(d<10.0||d>1000.0)continue;
G[i].push_back(edge(j,d));
G[j].push_back(edge(i,d));
}
}
prim(0);
}
return 0;
}