#codeforces div2 597 D.Shichikuji and Power Grid (建立源點 + MST)

D. Shichikuji and Power Grid

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Shichikuji is the new resident deity of the South Black Snail Temple. Her first job is as follows:

There are nn new cities located in Prefecture X. Cities are numbered from 11 to nn. City ii is located xixi km North of the shrine and yiyi km East of the shrine. It is possible that (xi,yi)=(xj,yj)(xi,yi)=(xj,yj) even when i≠ji≠j.

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 ii will cost cici yen;
  • Making a connection between City ii and City jj will cost ki+kjki+kj 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 ii and City jj are connected by a wire, the wire will go through any shortest path from City ii to City jj. Thus, the length of the wire if City ii and City jj are connected is |xi−xj|+|yi−yj||xi−xj|+|yi−yj| 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 nn (1≤n≤20001≤n≤2000) — the number of cities.

Then, nn lines follow. The ii-th line contains two space-separated integers xixi (1≤xi≤1061≤xi≤106) and yiyi (1≤yi≤1061≤yi≤106) — the coordinates of the ii-th city.

The next line contains nn space-separated integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤1091≤ci≤109) — the cost of building a power station in the ii-th city.

The last line contains nn space-separated integers k1,k2,…,knk1,k2,…,kn (1≤ki≤1091≤ki≤109).

Output

In the first line print a single integer, denoting the minimum amount of yen needed.

Then, print an integer vv — the number of power stations to be built.

Next, print vv space-separated integers, denoting the indices of cities in which a power station will be built. Each number should be from 11 to nn and all numbers should be pairwise distinct. You can print the numbers in arbitrary order.

After that, print an integer ee — the number of connections to be made.

Finally, print ee pairs of integers aa and bb (1≤a,b≤n1≤a,b≤n, a≠ba≠b), denoting that a connection between City aa and City bb will be made. Each unordered pair of cities should be included at most once (for each (a,b)(a,b) there should be no more (a,b)(a,b) or (b,a)(b,a) 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

Copy

3
2 3
1 1
3 2
3 2 3
3 2 3

output

Copy

8
3
1 2 3 
0

input

Copy

3
2 1
1 2
3 3
23 2 23
3 2 3

output

Copy

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=83+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)=102⋅(3+2)=10. The cost of connecting City 2 and City 3 is 3⋅(2+3)=153⋅(2+3)=15. Thus the total cost is 2+10+15=272+10+15=27. It can be shown that no configuration costs less than 27 yen.

題目大意 :

有N個城市需要通電,每個城市要麼自己發電, 花費c【i】, 要麼和別的發電城市連接, 連接的花費爲(k【i】 + k【j】) × 兩個城市的距離(| Xi - Xj | + |Yi - Yj |), 現在讓你輸出最小花費, 哪些城市建了發電站,哪些城市連了一條邊

思路 :

最開始寫的時候認爲是貪心, 遍歷兩個城市, 若建邊的花費大於建發電站的花費, 那麼不連邊, 最後遍歷每個連通塊, 找到每個連通塊的最小發電花費, 但是這樣寫是不正確的, 當你遍歷兩個連通塊的時候, 他們的發電站的花費是連通塊當中最小的花費, 而合併後二者也要更新。 再看題面,特別像MST, 所以正確做法是這樣 : 建立一個源點, 到每個點的邊權是該點的發電花費, 再將所有的點之間建一條邊, 整張圖的MST就是答案, 因爲肯定需要有城市發電的, 這樣就保證了所有城市都是有電的

Accepted code 

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e3 + 100;
const int MAXM = 4e6 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

struct node
{
	int x, y;
}t[MAXN];
struct Edge
{
	int u, v; ll w;
	bool operator < (const Edge &oth) const
	{
		return w < oth.w;
	}
}s[MAXM];
struct Answer
{
	int u, v;
}a[MAXN];
int p[MAXN], pre[MAXN], n, tot, X;
ll c[MAXN], k[MAXN], ans, C;
int find_(int x) {
	while (x != p[x]) x = p[x] = p[p[x]];
	return x;
}
void unite(int x, int y) {
	x = find_(x);
	y = find_(y);
	if (x != y) p[x] = y;
}
ll dis(int i, int j) {
	return (abs(t[i].x - t[j].x) + abs(t[i].y - t[j].y)) * (k[i] + k[j]);
}

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) sc("%d %d", &t[i].x, &t[i].y), p[i] = i;
	for (int i = 1; i <= n; i++) sc("%lld", &c[i]), s[++tot] = { 0, i, c[i] };  // 源點
	for (int i = 1; i <= n; i++) sc("%lld", &k[i]);
	for (int i = 1; i < n; i++) {
		for (int j = i + 1; j <= n; j++) {
			ll dist = dis(i, j);
			s[++tot] = { i, j, dist };   // 兩兩建邊
		}
	}
	int K = 0; sort(s + 1, s + tot + 1);   // MST
	for (int i = 1; i <= tot; i++) {
		int ui = s[i].u, vi = s[i].v;
		if (K == n) break;
		if (find_(ui) != find_(vi)) {
			unite(ui, vi);
			ans += s[i].w;
			K++;
			if (ui == 0) pre[++X] = vi;
			else if (vi == 0) pre[++X] = ui;
			else a[++C] = { ui, vi };
		}
	}
	printf("%lld\n%d\n", ans, X); 
	for (int i = 1; i <= X; i++) printf("%d ", pre[i]); printf("\n");
	printf("%d\n", C);
	for (int i = 1; i <= C; i++) printf("%d %d\n", a[i].u, a[i].v);
	return 0;  // 改數組大小!!!
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章