HDOJ 4463 Outlets 最小生成树
来源:互联网 发布:java电商项目简历 编辑:程序博客网 时间:2024/05/16 08:13
Prim....似乎没有考虑多点共线也能A.....
Outlets
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2565 Accepted Submission(s): 1182
Problem Description
In 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.
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.
Input
There 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.
Output
For each test case, print the minimum total road length. The result should be rounded to 2 digits after decimal point.
Sample Input
42 30 01 00 -1 1 -10
Sample Output
3.41
Source
2012 Asia Hangzhou Regional Contest
/* ***********************************************Author :CKbossCreated Time :2015年08月17日 星期一 21时13分29秒File Name :HDOJ4463.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;const double inf=1e30;const int maxn=100;int n;double g[maxn][maxn];double dist[maxn];bool vis[maxn];double prim(){for(int i=0;i<100;i++) { dist[i]=inf; vis[i]=false; }dist[0]=0;double sum=0;for(int i=0;i<n;i++){int mark=-1;double mindist=inf;for(int j=0;j<n;j++){if(!vis[j]&&dist[j]<mindist){mark=j; mindist=dist[j];}}if(mark==-1) return inf;sum+=mindist; vis[mark]=true;for(int j=0;j<n;j++){if(!vis[j]&&g[mark][j]<dist[j]){dist[j]=g[mark][j];}}}return sum;}int P,Q,N;struct Point{double x,y;void toString(){printf("(%lf,%lf)\n",x,y);}}pt[maxn],sP,sQ;double Dist(Point a,Point b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);while(scanf("%d",&N)!=EOF&&N){scanf("%d%d",&P,&Q);P--; Q--; n=0;for(int i=0,x,y;i<N;i++){scanf("%d%d",&x,&y);if(i==P) sP.x=x,sP.y=y;else if(i==Q) sQ.x=x,sQ.y=y;else pt[n].x=x,pt[n].y=y,n++;}n++; for(int i=0;i<n-1;i++){g[i][n-1]=g[n-1][i]=min(Dist(pt[i],sP),Dist(pt[i],sQ));}for(int i=0;i<n-1;i++){for(int j=0;j<n-1;j++){g[i][j]=g[i][j]=Dist(pt[i],pt[j]);}}double ans=prim()+Dist(sP,sQ);printf("%.2lf\n",ans);} return 0;}
0 0
- HDOJ 4463 Outlets 最小生成树
- HDOJ 4463 Outlets(最小生成树--prime)
- Outlets 最小生成树
- HDU-4463-Outlets -最小生成树
- HDU 4463 Outlets 最小生成树
- HDU 4463 Outlets (Kruskal 最小生成树)
- hdu 4463 Outlets (最小生成树)
- hdu 4463 Outlets(最小生成树)
- hdu 4463 Outlets(最小生成树)
- HDU 4463Outlets 最小生成树Kr~
- hdu 4463 Outlets 最小生成树
- HDU 4463 Outlets(最小生成树)
- hdu 4463 Outlets【最小生成树】水题
- Outlets(最小生成树)
- hdu4463 Outlets 最小生成树
- hdu 4463 Outlets(图论:最小生成树)
- hdu 4463 Outlets(最小生成树,kruskal,前向星)
- HDU 4663 —— Outlets(最小生成树)
- leetcode-110:判断平衡二叉树 Java
- CSS选择器
- 【CF148D】【D. Bag of mice】【概率dp】
- centOS 6.3 上配置SVN服务器
- Web Storage实现简单的留言本
- HDOJ 4463 Outlets 最小生成树
- 公式画图
- Struts2中的ModelDriven机制及其运用
- linux下Mysql乱码问题的处理
- php使用curl post 乱码 淘宝手机 curl 注意事项 cookie无法传递
- Mark Knowledge of Java Thread (2): Notify and Wait
- NS3网络仿真(12): ICMPv4协议
- 一道关于try,finally执行顺序的面试题
- oracle下载地址。