hdu——4463Outlets
来源:互联网 发布:爱奇艺网络大电影 编辑:程序博客网 时间:2024/06/07 02:32
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.
42 30 01 00 -1 1 -10
3.41
最小生成树稍微变形一下,但还是题水题。
把那条必选的边的权值先改成0就行了。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
using namespace std;
struct node
{
int from,to;
double weight;
}edge[10010];
struct point
{
int x,y;
}list[100];
double dis(int x1,int y1,int x2,int y2)
{
return sqrt((double)((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
}
int cmp(node a,node b)
{
return a.weight < b.weight;
}
int father[100];
void unit(int n)
{
for(int i=0;i<=n;i++)
father[i]=i;
}
int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
void merge(int a,int b)
{
int aa=find(a);
int bb=find(b);
if(aa!=bb)
father[aa]=bb;
}
double kruskal(int n,int v,int p,int q)//t是必选的那条边 ,n是边的个数
{
unit(v);//并查集初始化,v是点的个数
sort(edge,edge+n,cmp);
int m=0;
double sum=dis(list[p].x,list[p].y,list[q].x,list[q].y);
for(int i=0;i<n;i++)
{
int a=find(edge[i].from);
int b=find(edge[i].to);
if(a!=b)
{
merge(a,b);
sum+=edge[i].weight;
m++;
}
if(m==v-1)
break;
}
return sum;
}
int main()
{
int n;
while(~scanf("%d",&n) && n)
{
int p,q,cnt=0;
scanf("%d%d",&p,&q);
for(int i=1;i<=n;i++)
scanf("%d%d",&list[i].x,&list[i].y);
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
{
edge[cnt].from=i;
edge[cnt].to=j;
edge[cnt].weight=dis(list[i].x,list[i].y,list[j].x,list[j].y);
if((i==p && j==q) || (i==q && j==p))
edge[cnt].weight=0;
cnt++;
}
printf("%.2f\n",kruskal(cnt,n,p,q));
}
return 0;
}
- hdu——4463Outlets
- HDU-4463-Outlets
- hdu 4463 Outlets
- HDU-4463-Outlets
- hdu 4463 Outlets(prim)
- hdu 4463 Outlets
- HDU 4463 Outlets
- hdu 4463 Outlets
- HDU 4463 Outlets
- HDU 4463 Outlets【Prim】
- HDU 4463 Outlets
- HDU 4463 Outlets
- HDU-4463-Outlets
- hdu 4463 Outlets
- HDU 4463 Outlets (枚举)
- HDU 4663 —— Outlets(最小生成树)
- HDU-4463-Outlets -最小生成树
- hdu 4463 Outlets (次小生成树)
- POJ2947——Widget Factory
- POJ3185——The Water Bowls
- 如何观看Britishusa德国国外电视在互联网上
- POJ1753——Flip Game
- 高斯消元法小结
- hdu——4463Outlets
- ZOJ——3204 Connect them
- POJ3026——Borg Maze
- N!Again hdu 2674 c++
- 今年,遇到就业最难季,应该好好规划自己了
- NOJ——[1264] Flandre\'s First Escape
- POJ1679——The Unique MST
- 利用Dom读取和修改XML文件,并处理获取路径中空格(%20)问题
- NOJ1538——[1538] K-Submartix