HDU 4463 kruskal 水题
来源:互联网 发布:毛衣编制软件 编辑:程序博客网 时间:2024/05/17 23:00
OutletsTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3165 Accepted Submission(s): 1471Problem DescriptionIn China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on the United Airline flight, they give you Haagendazs ice cream for free, but in China, you will pay $10 to buy just a little cup.So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can't match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores.InputThere are several test cases. For each test case: The first line is an integer N( 3 <= N <= 50) , meaning there are N stores in the outlets. These N stores are numbered from 1 to N. The second line contains two integers p and q, indicating that the No. p store is a Nike store and the No. q store is an Apple store. Then N lines follow. The i-th line describes the position of the i-th store. The store position is represented by two integers x,y( -100<= x,y <= 100) , meaning that the coordinate of the store is (x,y). These N stores are all located at different place. The input ends by N = 0.OutputFor each test case, print the minimum total road length. The result should be rounded to 2 digits after decimal point.Sample Input42 30 01 00 -1 1 -10Sample Output3.41Source2012 Asia Hangzhou Regional ContestRecommendWe have carefully selected several similar problems for you: 5733 5732 5731 5730 5729
感触良多,本来这是一道kruskal水题,但是WA*n ,交叉测试后才发现是强制转换浮点哪里出来问题,可能是qsort出错,可能是其他的错误,AC和WA都放在下面,等qsort再练熟一点看看是怎么回事
//AC:#include <cstdio>#include <iostream>#include <cstdlib>#include <cmath>using namespace std;#define MAXN 100#define MAXN_E 500000 //边可能很多struct P{ int X, Y;}Point[MAXN * 2];struct A_Edge_B{ int A, B; //A->B int length; //Cost}Edge[MAXN_E];int k;int Par[MAXN], Rank[MAXN];void Init(int n){ for (int i = 1;i <= n;i++) { Par[i] = i; Rank[i] = 0; }}int Find(int x){ if (Par[x] == x) return x; else return Par[x] = Find(Par[x]);}void Unite(int x, int y){ x = Find(x);y = Find(y); if (x == y) return; if (Rank[x]<Rank[y]) Par[x] = y; else Par[y] = x; if (Rank[x] == Rank[y]) Rank[x]++;}int cmp(const void *a, const void *b){ A_Edge_B *c, *d; c = (A_Edge_B *)a; d = (A_Edge_B *)b; return c->length - d->length;}double Cal_Len(int a,int b)//a->b{ int dX = Point[a].X - Point[b].X; int dY = Point[a].Y - Point[b].Y; return dX * dX + dY * dY;}double Handle(int n){ int Nike, Apple; double ans; Init(n); scanf("%d%d", &Nike, &Apple); for (int i = 1;i<=n;i++) scanf("%d%d", &Point[i].X, &Point[i].Y); k = 0; for (int i = 1;i<=n;i++) for (int j = 1;j<=i;j++) { k++; Edge[k].A = i; Edge[k].B = j; Edge[k].length = Cal_Len(i,j); } qsort(Edge+1, k, sizeof(Edge[0]),cmp); ans = sqrt((double)Cal_Len(Nike,Apple)); Unite(Nike, Apple); for (int i = 1;i<=k;i++) { int Pa = Find(Edge[i].A); int Pb = Find(Edge[i].B); if (Pa == Pb) continue; Unite(Pa, Pb); ans += sqrt((double)Edge[i].length); } return ans;}int main(){ // freopen("F:\\test.txt", "r", stdin); int n; while (~scanf("%d", &n) && n) printf("%.2lf\n", Handle(n)); return 0;}
//WA#include <cstdio>#include <iostream>#include <cstdlib>#include <cmath>using namespace std;#define MAXN 100#define MAXN_E 500000 //边可能很多int Par[MAXN], Rank[MAXN];struct P{ int X, Y;}Point[MAXN * 2];struct A_Edge_B{ int A, B; //A->B double length; //Cost}Edge[MAXN_E];int k;void Init(int n){ for (int i = 1;i <= n;i++) { Par[i] = i; Rank[i] = 0; }}int Find(int x){ if (Par[x] == x) return x; else return Par[x] = Find(Par[x]);}void Unite(int x, int y){ x = Find(x);y = Find(y); if (x == y) return; if (Rank[x]<Rank[y]) Par[x] = y; else Par[y] = x; if (Rank[x] == Rank[y]) Rank[x]++;}int cmp(const void *a, const void *b){ A_Edge_B *c, *d; c = (A_Edge_B *)a; d = (A_Edge_B *)b; return c->length - d->length >=0? 1:0;}double Cal_Len(int a,int b)//a->b{ int dX = Point[a].X - Point[b].X; int dY = Point[a].Y - Point[b].Y; return sqrt(double(dX * dX + dY * dY));}double Handle(int n){ int Nike, Apple; double ans; Init(n); scanf("%d%d", &Nike, &Apple); for (int i = 1;i<=n;i++) scanf("%d%d", &Point[i].X, &Point[i].Y); k = 0; for (int i = 1;i<=n;i++) for (int j = 1;j<=i;j++) { k++; Edge[k].A = i; Edge[k].B = j; Edge[k].length = Cal_Len(i,j); } qsort(Edge+1, k, sizeof(Edge[0]),cmp); ans = Cal_Len(Nike,Apple); Unite(Nike, Apple); for (int i = 1;i<=k;i++) { int Pa = Find(Edge[i].A); int Pb = Find(Edge[i].B); if (Pa == Pb) continue; Unite(Pa, Pb); ans += Edge[i].length; } return ans;}int main(){ //freopen("F:\\test.txt", "r", stdin); int n; while (~scanf("%d", &n) && n) printf("%.2lf\n", Handle(n)); return 0;}
0 0
- HDU 4463 kruskal 水题
- HDU 4463 Outlets (Kruskal 最小生成树)
- hdu 1102 kruskal
- hdu 1233 kruskal
- hdu 4313(kruskal思想)
- HDU 3371 Kruskal
- HDU 1102 Kruskal
- hdu 1162(Kruskal)
- hdu 1863 kruskal
- HDU 1863-kruskal
- HDU 1875-kruskal
- hdu 1863(prim&kruskal)
- HDU 3367 Pseudoforest(Kruskal)
- HDU 1233 Kruskal算法
- hdu 1863 Kruskal起步
- hdu 1233 【Kruskal】
- hdu 4463 Outlets(最小生成树,kruskal,前向星)
- Ice_cream’s world III+最短路kruskal+水题+HDU
- ScrollerView与NavigationController的冲突解析
- SharedPreferences的使用
- 嵌入式环境的搭建(用于Arm开发板)
- 解决远程连接kafka集群无响应
- share wechat
- HDU 4463 kruskal 水题
- 自定义View实现空调遥控器切换度数
- 欢迎使用CSDN-markdown编辑器
- iOS开发UI篇-自定义CALayer
- 简单实现断点下载
- POJ 3984 迷宫问题
- HDU 5646 DZY Loves Partition
- 求助,svn资源库的项目是javweb项目,检出后却是javase项目
- Linux makefile 教程,简单粗暴,2小时学习makefile