Description
Download as PDF
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 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 line is followed by a blank line, and there is also a blank line between two consecutive inputs.
Each test case is described by one input file that 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 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
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
For each input file, your program has to write the optimal total cost to interconnect all cities.
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
一些点,点之间的距离平方就是造路的价格,另外还有套餐,套餐中的点是联通的,有固定的花费,套餐可以买多个,问价格最少的生成树。
一开始想到了枚举,但是不知道怎么表示状态,之后学习了状态压缩。
for (i = 0; i < 1 << q; i++)
i就是方案的编号
for (j = 0; j < q; j++)
{
if (i >> j & 1)
{
j是套餐的编号,第j个套餐是否选用。
如果直接枚举会炸,可以不用套餐跑一次,把用到的边存起来,之后枚举套餐的时候,再跑的时候,取边只从这个集合中取,就一定可以组成树。
还有……注意向量清空……
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
using namespace std;
typedef long long ll;
const int maxn = 1000005;
int p[1005], mid[10], len, x[1005], y[1005], m, n;
vector<int>cpoint[10];
vector<int>real;
struct Node{
int u, v;
int w;
}node[1000000];
bool cmp(Node a,Node b)
{
return a.w < b.w;
}
int find(int x)
{
return p[x] == x ? x : p[x] = find(p[x]);
}
int KrusKal()
{
int ans = 0;
for (int i = 1; i <= n; i++)
p[i] = i;
for (int i = 1; i <= len; i++)
{
int x = find(node[i].u);
int y = find(node[i].v);
if (x != y)
{
ans += node[i].w;
p[x] = y;
real.push_back(i);
}
}
return ans;
}
int exKrusKal()
{
int ans = 0;
for (int i = 0; i < real.size(); i++)
{
int x = find(node[real[i]].u);
int y = find(node[real[i]].v);
if (x != y)
{
ans += node[real[i]].w;
p[x] = y;
}
}
return ans;
}
int main()
{
int i, j, t, q, kase = 1;
int ans;
cin >> t;
while (t--)
{
cin >> n >> q;
for (i = 1; i <= q; i++)
{
cpoint[i - 1].clear();
int tempx, temp1;
cin >> tempx >> mid[i - 1];
for (j = 1; j <= tempx; j++)
{
cin >> temp1;
cpoint[i - 1].push_back(temp1);
}
}
real.clear();
for (i = 1; i <= n; i++)
cin >> x[i] >> y[i];
int cnt = 1;
for (i = 1; i <= n - 1; i++)
{
for (j = i + 1; j <= n; j++)
{
node[cnt].u = i; node[cnt].v = j;
node[cnt].w = (x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]);
cnt++;
}
}
len = n*(n - 1) / 2;
sort(node+1, node + 1 + len, cmp);
ans = KrusKal();
for (i = 0; i < 1 << q; i++)
{
for (j = 1; j <= n; j++)p[j] = j;
int ans1 = 0;
for (j = 0; j < q; j++)
{
if (i >> j & 1)
{
ans1 += mid[j];
for (int k = 1; k < cpoint[j].size(); k++)
{
int x = find(cpoint[j][0]);
int y = find(cpoint[j][k]);
if (x != y)
{
p[y] = x;
}
}
}
}
ans = min(ans, ans1 + exKrusKal());
}
if (kase != 1)cout << endl;
kase++;
cout << ans << endl;
}
return 0;
}