Cdoj 1146 A-Qiushi Big Brother and fast food chain the minimum spanning tree prim algorithm dense graph

Source: Internet
Author: User
Tags pow

Topic linksA-Big Brother Qiushi and fast food chainTime limit:3000MS Memory Limit:65535KB 64bit IO Format:%lld &%llu Submit Status Practice UESTC 1146 Appoint Description:System Crawler (2016-05-01)

Description

Into great events, inflexible has the super world, but also has the perseverance of the chi.

After Qiushi opened a fast-food restaurant, he soon earned a lot of money because of the influence of the people winning the halo. In order to continue to realize the lofty ideals in his heart, he intends to open branches all over the country to make money. Suppose there are nown A fast food restaurant (at least one of which is a flagship store) is located on the two-dimensional plane, the first I fast food restaurant coordinates (xi, yi). To facilitate traffic, he intends to build roads between some fast-food restaurants so that any fast-food restaurant can reach a flagship store by road.

But Qiushi Big Brother busy making money and festivals, there is no time to design the road, you can help Qiushi to figure out the minimum total need to build how long road?

Input

First line an integerN, indicating the number of fast food restaurants. (N≤6666)
NextNRows, two integers per lineXI,YI,ZI(−1000000≤XI,YI≤1000000)。 Indicates the location of the first-place restaurant (XI,YIzi=0 zi=1 means the store is a flagship store.

Guaranteed at least one flagship store

Output

The output must be at least the length of the road to be built, leaving two digits after the decimal point.

Sample Input

3
1-1 0
1 1 0
0 0 1

Sample Output

2.83

Hint

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <stack> #include <map> #include <algorithm > #include <set>using namespace std;typedef long long ll;typedef unsigned long long Ull; #define MM (A, B) memset (A, B , sizeof (a)); const double EPS = 1e-10;const ll inf = 1e15;const double Pi=acos ( -1); const int Max_v=6666;int X[max_v],y[max _v],flag[max_v];d ouble mincost[max_v];bool used[max_v];d ouble Dist (int i,int j) {if (Flag[i]&&flag[j]) return 0    ; else return sqrt (POW ((x[i]-x[j), 2) +pow ((Y[i]-y[j]), 2));}    The POW (, 2) is multiplied faster than the two, and two direct multiplication has timed out the int main () {int n;            while (~SCANF ("%d", &n)) {for (int i=1;i<=n;i++) {mincost[i]=inf;            Used[i]=false;        scanf ("%d%d%d", &x[i],&y[i],&flag[i]);        } double ans=0;        mincost[1]=0;        while (1) {int u=-1;   for (int v=1;v<=n;v++) if (!used[v]&& (u==-1| |           Mincost[v]<mincost[u]) u=v;//Find the nearest point if (u==-1) break from the current collection;           used[u]=true;//join current set ans+=mincost[u];        for (int i=1;i<=n;i++) if (!used[i])//optimization, find Mincost[i]=min (Dist (u,i), Mincost[i]) in the point where the collection has not yet been added;    } printf ("%.2f\n", ans); } return 0;}

Analysis: A very good problem;

1. The flagship store is connected to an edge with a weight of 0, the problem is converted to the minimum spanning tree;

2. Because it is dense map, can not open so large array, Kruskal can not use, only with prim algorithm;

3. This problem timeout card is very serious, to prevent time-out to see the code;

Cdoj 1146 A-Qiushi Big Brother and fast food chain the minimum spanning tree prim algorithm dense graph

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.