您的位置:首页 > 其它

hdu 4463 有一条边必须加上 (2012杭州区域赛K题)

2015-06-18 10:38 411 查看

耐克店 和 苹果店必须相连


Sample Input
4
2 3
0 0
1 0
0 -1
1 -1
0

Sample Output
3.41

 

# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ;

const int INF=0x3f3f3f3f;
const int MAXN=110;
bool vis[MAXN];
double lowc[MAXN];
int n ;
double cost[MAXN][MAXN] ;

struct poin
{
int x ;
int y ;
}p[MAXN];

double Prim()//点是0~n-1
{
double ans=0;
memset(vis,false,sizeof(vis));
vis[0]=true;
for(int i=1;i<n;i++)lowc[i]=cost[0][i];
for(int i=1;i<n;i++)
{
double minc=INF;
int p=-1;
for(int j=0;j<n;j++)
if(!vis[j]&&minc>lowc[j])
{
minc=lowc[j];
p=j;
}
if(minc==INF)return -1;//原图不连通
ans+=minc;
vis

=true; for(int j=0;j<n;j++) if(!vis[j]&&lowc[j]>cost[p][j]) lowc[j]=cost[p][j]; } return ans; } int main() { // freopen("in.txt","r",stdin) ; while(scanf("%d" , &n) != EOF) { if (n == 0) break ; int i , j ; int pp , qq ; scanf("%d %d" , &pp , &qq) ; for (i = 0 ; i < n ; i++) scanf("%d %d" , &p[i].x , &p[i].y) ; for (i = 0 ; i < n ; i++) for (j = i+1 ; j < n ; j++) { double t = sqrt((double)(p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y)) ; cost[i][j] = t ; cost[j][i] = t ; } double k = cost[pp-1][qq-1] ; cost[pp-1][qq-1] = 0 ; cost[qq-1][pp-1] = 0 ; k += Prim() ; printf("%.2lf\n" , k) ; } return 0 ; }

View Code [p] 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: