Buy or Build
| Time Limit: 2000MS |
|
Memory Limit: 65536K |
| Total Submissions: 1348 |
|
Accepted: 533 |
Description
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, There is 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.
problem
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 sub Network is not relevant to our problem).
- A subnetwork C can be bought-a total cost WC 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 a edge whose cost is Exactl Y 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).
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.
Input
The first line contains the number N of cities in the country (1<=n<=1000) followed by the number Q of existing Su Bnetworks (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 T He number of cities in the subnetwork. The second integer is the "the cost of the subnetwork" (not greater than 2 x 106). 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 this 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
Your program have to write the optimal total cost of interconnect all cities.
Sample Input
7 32 4 1 23 3 3 6 73 9 2 4 50 24 02 04 21 30 54 4
Sample Output
17
Hint
Sample explanation:the above instance is shown on Figure 2. An optimal solution was described in Figure 3 (thick edges come from a existing network while thin edges has been setup F Rom scratch).
Figure 3:an Optimal solution of the 7 city instance in which which the first and second existing networkshave been bought While extra edges (1, 5) and (2, 4)
Source
Southwestern Europe 2005
Test instructions: N cities, tell the coordinates of each city, and Q a unicom block, now to connect the N cities, you can buy Unicom blocks (each with a certain fee), or create a new edge (the cost of the distance between points squared), ask the minimum cost is how much.
Idea: Q is small, binary enumeration of which blocks to choose, each time Kruskal, to find the minimum value.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MOD 1000000009#define INF 0x3f3f3f3f#define Pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ( "%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, & c) #define PF Printf#define DBG pf ("hi\n") typedef long long ll;using namespace Std;const int maxn = 1005;c onst int MAXN = 500500;struct node{int x, y;} node[maxn];struct edge{int U,v,len; BOOL operator< (const Edge &a) Const {return len<a.len; }}edge[maxn];int father[maxn],cost[10];int n,q,num;vector<int>g[10];void Init () {for (int i=0;i<=n;i++) Father[i]=i;} void Addedge (int u,int v) {edge[num].u=u; Edge[num].v=v; edge[num++].len= (node[u].x-node[v].x) * (node[u].x-node[v].x) + (NODE[U].Y-NODE[V].Y) * (NODE[U].Y-NODE[V].Y);} int find_father (int x) {if (x!=father[x]) Father[x]=find_father (father[x]); return father[x];} BOOL Union (int a,int b) {int fa=find_father (a); int Fb=find_father (b); if (FA==FB) return false; FATHER[FA]=FB; return true;} int Kruskal () {int ans=0; int cnt=0; for (int i=0;i<num;i++) {if (Union (EDGE[I].U,EDGE[I].V)) {Ans+=edge[i].len; cnt++; } if (cnt==n-1) break; } return ans; void Solve () {init (); int Ans=kruskal (); for (int i=0;i< (1<<Q), i++) {inIt (); int all=0; for (int j=0;j<q;j++) {if (!) ( (I>>J) &1) continue; ALL+=COST[J]; for (int k=1;k<g[j].size (); k++) Union (g[j][k],g[j][0]); } ans=min (Ans,all+kruskal ()); } PF ("%d\n", ans);} int main () {int i,j,t,number,x;//sf (t),//while (t--) {SFF (n,q); num=0; for (i=0;i<q;i++) {g[i].clear (); SFF (Number,cost[i]); for (j=0;j<number;j++) {SF (x); G[i].push_back (x); }} for (i=1;i<=n;i++) SFF (NODE[I].X,NODE[I].Y); for (i=1;i<=n;i++) for (j=i+1;j<=n;j++) Addedge (I,J); Sort (edge,edge+num); Solve ();//if (T) puts (""); } return 0;} /*17 32 4 1 23 3 3 6 73 9 2 4 50 24 02 04 21 30 54 4*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Buy or Build (POJ 2784 minimum spanning tree)