Outlets
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1053 Accepted Submission(s): 475
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.
4 2 3 0 0 1 0 0 -1 1 -1 0
3.41
#include <iostream>
#include<cmath>
#define inf 0xffff
using namespace std;
struct s
{
int x,y;
};
double map[100][100];
int mark[100];
int n,p,q;
double prim()
{
double sum=0;
double lowcost[100];
memset(lowcost,0,sizeof(lowcost));
memset(mark,0,sizeof(mark));
for(int i=0;i<n;i++)
{
if(map[p-1][i]>map[q-1][i])
lowcost[i]=map[q-1][i];
else
lowcost[i]=map[p-1][i];
}
mark[p-1]=1;
mark[q-1]=1;
sum+=map[p-1][q-1];
for(i=0;i<n-2;i++)
{
double Min=inf;
int pos;
for(int j=0;j<n;j++)
{
if(Min>lowcost[j]&&mark[j]==0)
{
Min=lowcost[j];
pos=j;
}
}
sum+=Min;
mark[pos]=1;
for(j=0;j<n;j++)
{
if(lowcost[j]>map[pos][j]&&mark[j]==0)
lowcost[j]=map[pos][j];
}
}
return sum;
}
int main()
{
while(cin>>n)
{
if(n==0) break;
else
{
memset(map,0,sizeof(map));
cin>>p>>q;
s orz[101];
for(int i=0;i<n;i++)
{
int a,b;
cin>>a>>b;
orz[i].x=a;orz[i].y=b;
}
for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
map[i][j]=map[j][i]=sqrt(1.0*(orz[i].x-orz[j].x)*(orz[i].x-orz[j].x)+1.0*(orz[i].y-orz[j].y)*(orz[i].y-orz[j].y));
}
}
cout<<fixed;
cout.precision(2);
cout<<prim()<<endl;
}
}
return 0;
}
2万+




被折叠的 条评论
为什么被折叠?



