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:ios

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 𝑖≠𝑗.less

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.ide

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.ui

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.this

If there are multiple ways to choose the cities and the connections to obtain the construction of minimum price, then print any of them.spa

Input

First line of input contains a single integer 𝑛 (1≤𝑛≤2000) — the number of cities.code

Then, 𝑛 lines follow. The 𝑖-th line contains two space-separated integers 𝑥𝑖 (1≤𝑥𝑖≤106) and 𝑦𝑖 (1≤𝑦𝑖≤106) — the coordinates of the 𝑖-th city.orm

The next line contains 𝑛 space-separated integers 𝑐1,𝑐2,…,𝑐𝑛 (1≤𝑐𝑖≤109) — the cost of building a power station in the 𝑖-th city.ip

The last line contains 𝑛 space-separated integers 𝑘1,𝑘2,…,𝑘𝑛 (1≤𝑘𝑖≤109).ci

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;
}
相關文章
相關標籤/搜索