#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;  // 改数组大小!!!
}

猜你喜欢

转载自blog.csdn.net/weixin_43851525/article/details/103014005
今日推荐