POJ

来源:互联网 发布:linux top 某个进程 编辑:程序博客网 时间:2024/05/19 19:58
Buy or Build
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 1608 Accepted: 614

Description

World Wide Networks (WWN) is a leading company that operates large telecommunication networks. WWN would like to setup a new network in Borduria, a nice country that recently managed to get rid of its military dictator Kurvi-Tasch and which is now seeking for investments of international companies (for a complete description of Borduria, have a look to the following Tintin albums ``King Ottokar's Sceptre", ``The Calculus Affair" and ``Tintin and the Picaros"). You are requested to help WWN todecide how to setup its network for a minimal total cost. 
Problem 
There are 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 that connects all n cities. To achieve this, it can either build edges between cities from scratch or it can buy one or several subnetworks from local companies. You are requested to help WWN to decide how to setup its network for a minimal total cost. 
  • All n cities are located by their two-dimensional Cartesian coordinates. 
  • There are q existing subnetworks. If q>=1 then each subnetwork c ( 1<=c<=q ) is defined by a set of interconnected cities (the exact shape of a subnetwork is not relevant to our problem). 
  • A subnetwork c can be bought for a total cost wc and it cannot be split (i.e., the network cannot be fractioned). 
  • To connect two cities that are not connected through the subnetworks bought, WWN has to build an edge whose cost is exactly the square of the Euclidean distance between the cities.

You have to decide which existing networks you buy and which edges you setup so that the total cost is minimal. Note that the number of existing networks is always very small (typically smaller than 8). 
A 115 Cities Instance 
Consider a 115 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, we have assumed an 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 have been bought. Thin edges correspond to edges build from scratch while thick edges are 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 subnetworks ( 0<=q<=8 ). Cities are identified by a unique integer value ranging from 1 to n . The first line is followed by q lines (one per subnetwork), all of them following the same pattern: The first integer is the 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) are the identifiers of the cities in the subnetwork. The last part of the file contains n lines that provide the coordinates of the cities (city 1 on the first line, city 2 on the second one, etc). Each line is made of 2 integer values (ranging from 0 to 3000) corresponding to the integer coordinates of the city.

Output

Your program has to write the optimal total cost to 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 in Figure 2. An optimal solution is described in Figure 3 (thick edges come from an existing network while thin edges have been setup from scratch). 


Figure 3: An optimal solution of the 7 City instance in which which the first and second existing networkshave been bought while two extra edges (1, 5) and (2, 4) 



比起正常的求最小生成树,多了 最多8个套餐 可以选择,如果一一列举所有情况,显然一共生成256棵最小生成树,显然不现实。(POJ枚举可以过)
先生成一个最小生成树,并记录所选取的边,就像是在进行Kruskal,显然在记录后不再需要后面的边。然后进行套餐模拟。
排列的模拟可以通过 模拟二进制 进行。。
做题的时候忘了这回事,拿递归函数写了,无伤大雅。。
本题也是UVA1151,UVA的数据一向比较强。(UVA就别想枚举了!!!)

#include<iostream>#include<cstdio>#include<algorithm>#include<queue>#include<cmath>#include<vector>using namespace std;#define min(a,b) ((a)>(b)?(b):(a))int UFS[1008],n,m,FLAG,MIN;int UFS_find(int x){    if(x!=UFS[x])        UFS[x]=UFS_find(UFS[x]);    return UFS[x];}void UFS_insert(int x,int y){    x=UFS_find(x);    y=UFS_find(y);    UFS[max(x,y)]=min(x,y);}struct AP//line 最小生成树的线段{    int x,y,value;    bool operator < (const AP &a)const    {        return value>a.value;    }} line[1008];struct AK//TC  套餐情况{    int n,in[1008],value;} TC[10];AP func1(int x,int y,int z){    AP t;    t.x=x;    t.y=y;    t.value=z;    return t;}void func2(int str,bool com[]){    int ans=0;    for(int i=1; i<=n; i++)        UFS[i]=i;    for(int i=1; i<=m; i++)        if(com[i])        {            ans+=TC[i].value;            for(int j=2; j<=TC[i].n; j++)                UFS_insert(TC[i].in[j],TC[i].in[1]);        }    for(int i=1; i<FLAG; i++)        if(UFS_find(line[i].x)!=UFS_find(line[i].y))        {            int sum=0;            for(int j=1; j<=n; j++)                if(UFS[j]==j)                    sum++;            if(sum==1)                break;            UFS_insert(line[i].x,line[i].y);            ans+=line[i].value;        }    MIN=min(MIN,ans);    if(str==m+1)        return ;    for(int i=str; i<=m; i++)        if(!com[i])        {            com[i]=true;            func2(i+1,com);            com[i]=false;        }}int main(){    int N;    cin>>N;    while(N--)    {        cin>>n>>m;        for(int i=1; i<=m; i++)        {            cin>>TC[i].n>>TC[i].value;            for(int j=1; j<=TC[i].n; j++)                cin>>TC[i].in[j];        }        int in[1008][3],T=n-1,sum=0;        priority_queue<AP>que1;        for(int i=1; i<=n; i++)        {            UFS[i]=i;            cin>>in[i][0]>>in[i][1];            for(int j=1; j<i; j++)                que1.push(func1(i,j,(in[j][0]-in[i][0])*(in[j][0]-in[i][0])+(in[j][1]-in[i][1])*(in[j][1]-in[i][1])));        }        FLAG=1;        while(que1.size())        {            AP temp=que1.top();            que1.pop();            if(UFS_find(temp.x)!=UFS_find(temp.y))            {                UFS_insert(temp.x,temp.y);                sum++;                line[FLAG++]=temp;                if(T==sum)                    break;            }        }        MIN=1000000000;        bool com[10]= {};        func2(1,com);        cout<<MIN<<endl;        if(N)            cout<<endl;    }    return 0;}


原创粉丝点击