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; visView Code [p]=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 ; }
相关文章推荐
- C#获取程序的当前路径,启动和关闭一个Process
- eclipse-android-activity_main/fragment_main文件处理
- jquery在线预览PDF文件,打开PDF文件
- 每天一道算法题(14)——N个降序数组,找到最大的K个数
- 每天一道算法题(14)——N个降序数组,找到最大的K个数
- 读书笔记——读《构建之法:现代软件工程》第13~17章
- ThinkPHP里用U方法调用js文件实例
- php实现mysql数据库分表分段备份
- 配置tomcat服务
- SVN 中的 trunk branches tag 详解
- 【转载】2015 Objective-C 三大新特性 | 干货
- Ubuntu下apt-get安装软件时缺少依赖包解决方法
- iOS the file "xxx.app" couldn't be opened because you don't have permission to view it.
- JAVA对象去重 和数组对象排序
- LinkedIn面试官的经验,给程序猿面试提些建议
- 【转载】2015 Objective-C 三大新特性 | 干货
- IOS开发限制UITextField只可以输入数字
- SharePoint 2013 开发——Provider-hosted APP准备工作
- Waves:类Material Design 的圆形波浪(涟漪)点击特效插件
- FOOTER固定在页面的顶部