UVA 1151 Buy or Build

Source: Internet
Author: User
Tags ini

Original question:
World Wide Networks (WWN) are a leading company, operates large telecommunication Networks. WWN would like-to-setup a new network in Borduria, a nice country the recently managed to get rid of its military dictato R Kurvi-tasch and which is today seeking for investments of international companies (for a complete description of Borduria, Has a look to the following Tintin albums "King Ottokar ' s
Sceptre "," The Calculus Affair "and" Tintin and the Picaros "). You is requested to the help WWN todecide how to setup it network for a minimal total cost.
There is several local companies running small networks (called Subnetworks in the following) that partially cover the n Largest cities of Borduria. WWN would like to setup a network, connects all n cities. To achieve the it can either build edges between cities from scratch or it can buy one or several subnetworks from local Companies. You is requested to the help WWN to decide how to setup it network for a minimal total cost.
All n Cities is located by their two-dimensional Cartesian coordinates.
There is Q existing subnetworks. If Q≥1 then each subnetwork C (1≤C≤Q) are defined by a set of interconnected cities (the exact shape of a subnetwork Is isn't relevant to our problem).
a subnetwork C can be bought-A total cost W C and it cannot is split (i.e., the network cannot be fractioned).
to connect cities that was not connected through the Subnetworks bought, WWN have to build an edge whose cost is Exac tly the square of the Euclidean distance between the cities. You are decide which existing networks you buy and which edges, you are minimal. Note that the number of existing networks are always very small (typically smaller than 8).
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This was followed by a blank line, and there was also a blank line between the consecutive inputs.
Each test case was described by one input file, contains all the relevant data. The first line contains the number N of cities in the country (1≤n≤1000) followed by the number Q of existing Subnetwo Rks (0≤q≤8). Cities is identified by a unique integer value ranging from 1 to N. The
First line was followed by Q lines (one per subnetwork), all of them following the same pattern:the first integer is the n Umber of cities in the subnetwork. The second integer is the "the cost of the subnetwork" (not greater than 2x10 6). The remaining integers on the line (as many as the number of cities in the subnetwork) is the identifiers of the cities I n the subnetwork. The last part of the file contains n lines
That's provide the coordinates of the cities (city 1 on the first line, City 2 on the second one, etc). Each of Made's 2 integer values (ranging from 0 to +) corresponding to the integer coordinates of the city.
Output
For each test case, your program have to write the optimal total cost to interconnect all cities. The outputs of the consecutive cases would be separated to a blank line. A Cities Instance consider a Cities Instance of the problem with 4 Subnetworks (the 4 first graphs in Figure 1). As mentioned earlier the exact shape of a subnetwork is not relevant still, to keep figures easy to read and we have assumed A arbitrary tree like structure for each subnetworks. The bottom network in Figure 1 corresponds to the solution in which the first and the third networks has been bought. Thin edges
correspond to edges build from scratch while thick edges is those from one of the initial networks.

Sample Input
1
7 3
2 4 1 2
3 3 3) 6 7
3 9 2) 4 5
0 2
4 0
2 0
4 2
1 3
0 5
4 4
Sample Output
17

English:

Give you a pile of points on the two-dimensional plane, the distance from the point of time is the square of Euclidean distance. Now let you implement a minimal spanning tree to connect these points to the minimum cost, the cost value is two points distance. However, before connecting these points, you can spend money to buy some networks that are already connected. Now ask you how to combine buy and build at the same time, how to make the smallest spanning tree value least.

#include <bits/stdc++.h> using namespace std;
typedef long Long LL;
const int maxn=1010;
struct point {int x, y;};
    struct buy {int cost;
Vector<int> Road;

};
        struct Edge {edge (int xx,int yy,int dd) {x=xx;
        Y=yy;
    D=DD;
    } int x, y;
int D;


};
int n,q,t;
int FATHER[MAXN];
int RANK[MAXN];
Buy bs[10];
Point PS[MAXN];

Vector<edge> E,use; int Distance (const point &p1,const point &p2) {return ((p1.x-p2.x) * (p1.x-p2.x) + (P1.Y-P2.Y) * (P1.Y-P2.Y));} VO
        ID INI () {for (int i=0;i<=n;i++) {father[i]=i;
    rank[i]=0;
    }} int Find (int x) {if (father[x]==x) return x;
Return Father[x]=find (Father[x]);

} int cmp (const edge &e1,const edge &e2) {return e1.d<e2.d;}
    BOOL Merge (int x,int y) {int u=find (x);
    int V=find (y);
        if (u!=v) {if (rank[u]<rank[v]) father[u]=v;
           else {father[v]=u; if (Rank[u]==rank[v]) rank[u]++;
    } return true;
} return false;
    } int Kruskal (int cnt,vector<edge> &used) {if (cnt==1) return 0;
    int ans=0;
    Used.clear ();
            for (int i=0;i<e.size (); i++) {if (Merge (E[I].X,E[I].Y)) {ans+=e[i].d;
            Used.push_back (E[i]);
        if (--cnt==1) break;
}} return ans;
    } int main () {Ios::sync_with_stdio (false);
    cin>>t;
    int b,c,x;
        while (t--) {cin>>n>>q;
        INI ();
        E.clear ();
        Use.clear ();

        for (int i=0;i<q;i++) bs[i].road.clear ();
            for (int i=0;i<q;i++) {cin>>b>>c; Bs[i].
            Cost=c;
                for (int j=0;j<b;j++) {cin>>x;
            Bs[i].road.push_back (x-1); }} for (int i=0;i<n;i++) CIN>>ps[i].x>>ps[i].y; for (int i=0;i<n;i++) {for (int j=i+1;j<n;j++) {double D=distance (PS
                [I],ps[j]);
            E.push_back (Edge (i,j,d));
        }} INI ();

        Sort (E.begin (), E.end (), CMP);

        int Ans=kruskal (n,use);
            for (int mask=0;mask< (1&LT;&LT;Q); mask++) {ini ();
            int cnt=n,c=0; for (int i=0;i<q;i++) {if (Mask & (1<<i)) {c+ =bs[i].
                    cost; for (int j=1;j<bs[i].road.size (); j + +) {if (Merge (bs[i].road[j],bs[i].road.fr
                    Ont ()))--cnt;
            }}} vector<edge> dummy;
        Ans=min (Ans,c+kruskal (cnt,dummy));
        } cout<<ans<<endl;
   if (t) cout<<endl; } return 0; }

Answer:
Purple book is an example, the violent enumeration package plus the smallest spanning tree can be too.

But this book gives an optimization method that first runs the minimal spanning tree without any plan, preserving the edges in the spanning tree. The plan is then enumerated using bitwise operations, using the minimum spanning tree edge before using the Bien Hoa in the plan to make the minimum spanning tree algorithm.
Continually enumerate the packages and update the minimum values.

Why do you do this?

The word in the book means that, using the principle of the Kruskal algorithm, if a new edge joins the minimum spanning tree, that edge must be passed and checked to determine whether the two endpoints of this side belong to the same Unicom component, belonging to the same Unicom component will not enter the minimum spanning tree. So after each purchase of the package, the equivalent of the value of the side of the package into 0, at this time to use the Kruskal algorithm to find the smallest side, the weights of 0 of the side will definitely be ranked first, and was added to the smallest spanning tree. Secondly, how to ensure the use of the given edge of the package, coupled with the simple use of the minimum spanning tree edge can be found to meet the use of the minimum generation of the plan tree. The reason is simple, because if you do not use the package, the calculated edge is already small to large sort of, adding a few weights to 0 of the edge, the result will certainly get the best result of a use plan. So the conclusion is established.

Use the merge by rank in code, not fast.-_-| | |

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.