Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid 最小生成树
D. Shichikuji and Power Grid</centerD.>
Shichikuji is the new resident deity of the South Black Snail Temple. Her first job is as follows:
There are 𝑛 new cities located in Prefecture X. Cities are numbered from 1 to 𝑛. City 𝑖 is located 𝑥𝑖 km North of the shrine and 𝑦𝑖 km East of the shrine. It is possible that (𝑥𝑖,𝑦𝑖)=(𝑥𝑗,𝑦𝑗) even when 𝑖≠𝑗.
Shichikuji must provide electricity to each city either by building a power station in that city, or by making a connection between that city and another one that already has electricity. So the City has electricity if it has a power station in it or it is connected to a City which has electricity by a direct connection or via a chain of connections.
Building a power station in City 𝑖 will cost 𝑐𝑖 yen;
Making a connection between City 𝑖 and City 𝑗 will cost 𝑘𝑖+𝑘𝑗 yen per km of wire used for the connection. However, wires can only go the cardinal directions (North, South, East, West). Wires can cross each other. Each wire must have both of its endpoints in some cities. If City 𝑖 and City 𝑗 are connected by a wire, the wire will go through any shortest path from City 𝑖 to City 𝑗. Thus, the length of the wire if City 𝑖 and City 𝑗 are connected is |𝑥𝑖−𝑥𝑗|+|𝑦𝑖−𝑦𝑗| km.
Shichikuji wants to do this job spending as little money as possible, since according to her, there isn't really anything else in the world other than money. However, she died when she was only in fifth grade so she is not smart enough for this. And thus, the new resident deity asks for your help.
And so, you have to provide Shichikuji with the following information: minimum amount of yen needed to provide electricity to all cities, the cities in which power stations will be built, and the connections to be made.
If there are multiple ways to choose the cities and the connections to obtain the construction of minimum price, then print any of them.
Input
First line of input contains a single integer 𝑛 (1≤𝑛≤2000) — the number of cities.
Then, 𝑛 lines follow. The 𝑖-th line contains two space-separated integers 𝑥𝑖 (1≤𝑥𝑖≤106) and 𝑦𝑖 (1≤𝑦𝑖≤106) — the coordinates of the 𝑖-th city.
The next line contains 𝑛 space-separated integers 𝑐1,𝑐2,…,𝑐𝑛 (1≤𝑐𝑖≤109) — the cost of building a power station in the 𝑖-th city.
The last line contains 𝑛 space-separated integers 𝑘1,𝑘2,…,𝑘𝑛 (1≤𝑘𝑖≤109).
Output
In the first line print a single integer, denoting the minimum amount of yen needed.
Then, print an integer 𝑣 — the number of power stations to be built.
Next, print 𝑣 space-separated integers, denoting the indices of cities in which a power station will be built. Each number should be from 1 to 𝑛 and all numbers should be pairwise distinct. You can print the numbers in arbitrary order.
After that, print an integer 𝑒 — the number of connections to be made.
Finally, print 𝑒 pairs of integers 𝑎 and 𝑏 (1≤𝑎,𝑏≤𝑛, 𝑎≠𝑏), denoting that a connection between City 𝑎 and City 𝑏 will be made. Each unordered pair of cities should be included at most once (for each (𝑎,𝑏) there should be no more (𝑎,𝑏) or (𝑏,𝑎) pairs). You can print the pairs in arbitrary order.
If there are multiple ways to choose the cities and the connections to obtain the construction of minimum price, then print any of them.
Examples
input
3
2 3
1 1
3 2
3 2 3
3 2 3
output
8
3
1 2 3
0
input
3
2 1
1 2
3 3
23 2 23
3 2 3
output
27
1
2
2
1 2
2 3
Note
For the answers given in the samples, refer to the following diagrams (cities with power stations are colored green, other cities are colored blue, and wires are colored red):
For the first example, the cost of building power stations in all cities is 3+2+3=8. It can be shown that no configuration costs less than 8 yen.
For the second example, the cost of building a power station in City 2 is 2. The cost of connecting City 1 and City 2 is 2⋅(3+2)=10. The cost of connecting City 2 and City 3 is 3⋅(2+3)=15. Thus the total cost is 2+10+15=27. It can be shown that no configuration costs less than 27 yen.
题意
在一个二维平面上面,有n个城市,现在每个城市都没有电。
你可以选择一些城市建发电站,代价是c[i];你也可以给每个城市拉电线,给城市(i,j)之间拉电线的代价是(abs(x[i]-x[j])+abs(y[i]-y[j]))*(k[i]+k[j])。
现在问你最少花费多少代价,能够使得全部城市都有电,输出方案。
题解
我们定义一个0号点,我们假设0号点一开始就有点,然后0号点建每个点i的电线的代价是c[i]。
那么这个题就所有的都是建边了,相当于给你一个完全图,让你选择代价最少的边使得变成一个连通图。是不是就是一个最小生成树的问题?
代码
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 2005;
int fa[maxn],n,ax[maxn],ay[maxn];
int c[maxn],k[maxn];
long long m[maxn][maxn];
int fi(int x){
return fa[x]==x?x:fa[x]=fi(fa[x]);
}
vector<pair<long long,pair<int,int> > >Edge;
vector<int> ans_point;
vector<pair<int,int> >ans_edge;
long long ans;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&ax[i],&ay[i]);
}
for(int i=1;i<=n;i++){
scanf("%d",&c[i]);
}
for(int i=1;i<=n;i++){
scanf("%d",&k[i]);
}
for(int i=1;i<=n;i++){
m[0][i]=c[i];
fa[i]=i;
Edge.push_back(make_pair(c[i],make_pair(0,i)));
}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
long long cost = 1ll*(k[i]+k[j])*(abs(ax[i]-ax[j])+abs(ay[i]-ay[j]));
Edge.push_back(make_pair(cost,make_pair(i,j)));
}
}
sort(Edge.begin(),Edge.end());
for(int i=0;i<Edge.size();i++){
long long cost = Edge[i].first;
int x = Edge[i].second.first;
int y = Edge[i].second.second;
if(fi(x)==fi(y))continue;
ans+=cost;
if(Edge[i].second.first==0){
ans_point.push_back(Edge[i].second.second);
} else {
ans_edge.push_back(make_pair(Edge[i].second.first,Edge[i].second.second));
}
fa[fi(y)]=x;
}
cout<<ans<<endl;
cout<<ans_point.size()<<endl;
for(int i=0;i<ans_point.size();i++)
cout<<ans_point[i]<<" ";
cout<<endl;
cout<<ans_edge.size()<<endl;
for(int i=0;i<ans_edge.size();i++)
cout<<ans_edge[i].first<<" "<<ans_edge[i].second<<endl;
}