浙江财经大学
信息管理与工程学院

HDU 4463 Outlets(Prime最小生成树)

本文由 Ocrosoft 于 2016-07-29 17:20:18 发表

Outlets

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3220    Accepted Submission(s): 1495

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.
 

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
	
4 2 3 0 0 1 0 0 -1 1 -1 0
 

Sample Output
	
3.41
 

Solution

题意:n家店要用最短的道路相连(坐标),但是有一个要求,就是第二行给出的两家店之间必须直接相连。

因为要求两家店直连,那么把两家店的距离设置为0,这样Prime的时候就会选择这条路,但是花费没有记录,在输出答案的时候加上就行。

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ms(a) memset(a,0,sizeof(a))
#define INF INT_MAX
#define MAXN 10000+10
using namespace std;
int n,m;
double mp[MAXN][MAXN],lowcast[MAXN];
bool vis[MAXN];
struct Point
{
    double x,y;
    double dis(Point p)
    {
        return sqrt(abs(x-p.x)*abs(x-p.x)+abs(y-p.y)*abs(y-p.y));
    }
};
double prime(int u0)
{
    double ans=0;
    int pos=u0;//pos记录为起点
    ms(vis),vis[u0]=1;//清空vis数组,标记起点
    for(int i=1; i<=n; i++)//初始化lowcast数组
        if(i!=pos)lowcast[i]=mp[pos][i];
    for(int i=1; i<n; i++)
    {
        double minn=INF;
        for(int j=1; j<=n; j++) //找到没访问过的而且最近的邻近点
            if(!vis[j]&&minn>lowcast[j])minn=lowcast[j],pos=j;
        if(minn==INF)return 0;
        ans+=minn;
        vis[pos]=1;//标记这个邻近点
        for(int j=1; j<=n; j++) //更新lowcast数组
            if(!vis[j]&&lowcast[j]>mp[pos][j])lowcast[j]=mp[pos][j];
    }
    return ans;
}
int main()
{
    while(cin>>n&&n)
    {
        int t1,t2;
        cin>>t1>>t2;//这两家店要直连,mp记录为0就一定会连接,输出答案的时候要加上两家店的距离
        vector<Point> v;
        Point t;
        v.push_back(t);
        for(int i=1; i<=n; i++)
        {
            cin>>t.x>>t.y;
            v.push_back(t);
        }
        for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            mp[i][j]=(i==j?0:v[i].dis(v[j]));
        double ans=mp[t1][t2];
        mp[t1][t2]=mp[t2][t1]=0;
        ans+=prime(1);
        printf("%.2lf\n",ans);
    }
    return 0;
}

 

欢迎转载,请保留出处与链接。Ocrosoft » HDU 4463 Outlets(Prime最小生成树)

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址