hdu4081 (minimum spanning tree deformation, prim)

Source: Internet
Author: User

Qin Shi Huang ' s National Road System Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6108 Accepted Submission (s): 2131


Problem Description During The Warring states Period of ancient China (476 BC to 221 BC), there were seven kingdoms in Chin A----They were Qi, Chu, Yan, Han, Zhao, Wei and Qin. Ying Zheng was the king of the Kingdom Qin. Through 9 years of wars, he finally conquered all six other kingdoms and became the first emperor of a unified in 22 1 BC. That is Qin dynasty----the first imperial dynasty of China (not to being confused with the Qing dynasty, the Last dynasty O F China). So Ying Zheng named himself "Qin Shi Huang" because "Shi Huang" means "the first emperor" in Chinese.

Qin Shi Huang undertook gigantic projects, including the first version of the great Wall of China, the now famous City-siz Ed Mausoleum guarded by a life-sized Terracotta Army, and a massive national road system. There is a stories about the road system:
There were n cities in China and Qin Shi Huang wanted them all being connected by n-1 roads, in order so he could go to Eve Ry City from the capital city Xianyang.
Although Qin Shi Huang is a tyrant, he wanted the total length of all roads to is minimum,so that the road system could not Cost too many people ' s life. A daoshi (some kind of monk) named Xu Fu told Qin Shi Huang that he could build a road by magic and that Magic Road would Cost no money and no labor. But Xu Fu could only build one magic road for Qin Shi Huang. So Qin Shi Huang had to decide where to build the Magic Road. Qin Shi Huang wanted the total length of all none magic roads to being as small as possible, but Xu Fu wanted the Magic Road To benefit as many people as possible----so Qin Shi Huang decided the value of A/b (the ratio of A to B) must is th e maximum, which A is the total population of the The both of the cites connected by the Magic Road, and B are the total length of none Magic Roads.
Would Qin Shi Huang?
A city can is considered as a point, and a road can is considered as a line segment connecting the points.
Input The first line contains a integer t meaning that there is t test Cases (T <= 10).
For each test case:
The first line was an integer n meaning this there is n cities (2 < n <= 1000).
then n lines follow. Each line contains three integers x, y and p (0 <= X, y <=, 0 < P < 100000). (X, Y) is the coordinate of a city and P is the population of the.
It is guaranteed, that, each of the city have a distinct location.
Print a line indicating the above mentioned maximum ratio A/b, Output for each test case. The result should is rounded to 2 digits after decimal point.
Sample Input

2 4 1 1 20 1 2 30 200 2 80 200 1 100 3 1 1 20 1 2 30 2 2 40
Sample Output
65.00 70.00

Test instructions: There are n points, now requires a spanning tree, each point has the right value, now can let the spanning tree in the cost of an edge of 0, but to make this side of the two points of the weight as large as possible, so that A/b result is the largest, a represents the edge of the connection of the two points of the sum of weights, b represents the sum of the weights of all edges of the spanning tree minus the edge of a.

Idea: traverse all the edges, but the traversal is to build a minimum spanning tree based on the first, that is, to build a minimum spanning tree, if the edge of two points in the smallest spanning tree directly ans=max (ans, (a[i].z+a[j].z)/(Sum-tu[i][j]); If we are not on the minimum spanning tree, then suppose we have this edge, then we must have an edge on the smallest spanning tree that has been built, and we take the optimal solution to remove the maximum weight edge (excluding the added edge) after connecting to the Benchen, then Ans=max (ans, (a[i].z+a[j].z)/ (Sum-len[i][j]), the Len[i][j] array is the maximum weighted edge of the ring that joins I and J to make the smallest spanning tree ring, except for IJ.


Note: This problem is particularly helpless is just beginning to use the Kruskal algorithm, Baidu a bit Kruskal and prim time difference, dense prim than kcuskal faster than a bit, sparse diagram Kruskal faster. The results are just beginning or dead tle, the heart is unwilling, if someone has the optimization method hope to inform Bo Master.


Here is the Tle code (KRUSKAL):

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <
algorithm> #include <math.h> #include <queue> using namespace std;
typedef long Long LL;
const int n=1010;
Double Tu[n][n],len[n][n];
BOOL Vis[n][n];
Vector <int>g[N];
int n,f[n]; struct data {int x,y,p;}
A[n];
    struct HH {int a a B;
Double W;
}P[N*N/2]; inline double dis (int i,int j) {return sqrt ((a[i].x-a[j].x) * (a[i].x-a[j].x) + (A[I].Y-A[J].Y) * (A[I].Y-A[J].Y));} bool
    CMP (hh a,hh b) {return a.w<b.w;} int fa (int x) {if (x!=f[x]) return F[x]=fa (F[x]);
return f[x];
    } int main () {int t;
    scanf ("%d", &t);
         while (t--) {memset (tu,0,sizeof (TU));
        memset (len,0,sizeof (len));
        memset (vis,0,sizeof (VIS));
        scanf ("%d", &n);
        for (int i=0;i<n;i++) scanf ("%d%d%d", &AMP;A[I].X,&AMP;A[I].Y,&AMP;A[I].P), f[i]=i;
        int bj=0;
     for (int i=0;i<n;i++)       for (int j=i+1;j<n;j++) Tu[i][j]=tu[j][i]=dis (I,J), p[bj].a=i,p[bj].b=j,p[bj++].w=tu[i][j];
        int k=0;
        Sort (p,p+bj,cmp);
        Double sum=0;
        for (int i=0;i<n;i++) g[i].push_back (i);
            for (int i=0;i<bj;i++) {if (k==n-1) break;
            int X1=fa (P[I].A), X2=FA (P[I].B);
                if (x1!=x2) {vis[p[i].a][p[i].b]=vis[p[i].b][p[i].a]=1;
                SUM+=TU[P[I].A][P[I].B];
                int l=g[x1].size (), ll=g[x2].size (); for (int j=0;j<l;j++) for (int k=0;k<ll;k++) len[g[x1][j]][g[x2][k]]=len[g [X2] [K]]
                [G[X1][J]]=TU[P[I].A][P[I].B];
                f[x1]=x2;
                int l1=g[x1].size ();
                for (int j=0;j<l1;j++) G[x2].push_back (G[x1][j]);
            G[x1].clear ();
        }} double Ans=-1; for (int i=0;i<n;i++) for (intj=i+1;j<n;j++) if (Vis[i][j]) Ans=max (ans, (double) (A[I].P+A[J].P)/(Sum-tu[i][j]));
       Else Ans=max (ans, (double) (A[I].P+A[J].P)/(Sum-len[i][j]));
    printf ("%.2f\n", ans);
} return 0; }


AC Code (PRIM):

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <
algorithm> #include <math.h> #include <queue> using namespace std;
typedef long Long LL;
const int n=1010;
const int maxn=1010;

const double INF=1E14; struct Note {int x, y, Z;}

A[MAXN];
Double TU[MAXN][MAXN],DIS[MAXN];

int pre[maxn],n;
int FLAG[MAXN][MAXN],VIS[MAXN];

Double LEN[MAXN][MAXN];
    Double prim (int u) {double sum=0;
    memset (flag,0,sizeof (flag));
    memset (vis,0,sizeof (VIS));
    memset (len,0,sizeof (len));
        for (int i=1; i<=n; i++) {dis[i]=tu[u][i];
    Pre[i]=u;
    } vis[u]=1;
        for (int i=1; i<n; i++) {double minn=inf;
        int v=-1;
                for (int j=1; j<=n; J + +) {if (!vis[j]&&dis[j]<minn) {v=j;
            MINN=DIS[J];
            }} if (V!=-1) {sum+=dis[v]; FLAG[V][PRE[V]]=flag[pre[v]][v]=1;
            Vis[v]=1; for (int k=1; k<=n; k++) {if (vis[k]&&k!=v) {Le
                N[v][k]=len[k][v]=max (Len[k][pre[v]],dis[v]);
                    } if (!vis[k]&&tu[v][k]<dis[k]) {dis[k]=tu[v][k];
                Pre[k]=v;
}}}} return sum;

} double lenth (int i,int j) {return sqrt ((a[i].x-a[j].x) * (a[i].x-a[j].x) + (A[I].Y-A[J].Y) * (A[I].Y-A[J].Y));}
    int main () {int T;
    scanf ("%d", &t);
        while (t--) {scanf ("%d", &n);
        for (int i=1; i<=n; i++) scanf ("%d%d%d", &a[i].x,&a[i].y,&a[i].z);
        for (int i=1, i<= n; i++) for (int j=i+1; j<=n; j + +) Tu[i][j]=tu[j][i]=lenth (I,J);
        Double Sum=prim (1);
        Double ans=-1; for (int i=1, i<=n; i++) for (int j=i+1; j<=n;
                J + +) if (Flag[i][j]) Ans=max (ans, (a[i].z+a[j].z)/(Sum-tu[i][j]);

        Else Ans=max (ans, (a[i].z+a[j].z)/(Sum-len[i][j]);
    printf ("%.2lf\n", ans);
} return 0; }

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.