D. Shichikuji and Power Grid(Codeforces Round #597 (Div. 2)题解)

题目链接:D. Shichikuji and Power Grid
思路:这是一个完全图,如果没有发电站的限制,只靠电线就能通电,那么很明显是一道\(MST\)题目,只需要用Kruscal算法求一遍\(MST\)就可以,不过该题目表明必须间接或直接与发电站相连,不妨就设0点为一个超级发电站,初始时除了图上边之外,还另外的将每个点都连到0点一条边,边权是\(C[i]\),那么这样就可以满足题目的要求。建立超级原点是许多题目能用到的。
\(Code:\)


/* -*- encoding: utf-8 -*-
‘‘‘
@File    :   ac3728.cpp
@Time    :   2021/07/03 10:21:35
@Author  :   puddle_jumper
@Version :   1.0
@Contact :   1194446133@qq.com
‘‘‘

# here put the import lib*/
#include<set>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#define ch() getchar()
#define pc(x) putchar(x)
#include<stack>
#include<unordered_map>
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define PI acos(-1)
using namespace std;
template<typename T>void read(T&x){
	static char c;
	static int f;
	for(c=ch(),f=1; c<‘0‘||c>‘9‘; c=ch())if(c==‘-‘)f=-f;
	for(x=0; c>=‘0‘&&c<=‘9‘; c=ch())x=x*10+(c&15);
	x*=f;
}
template<typename T>void write(T x){
	static char q[65];
	int cnt=0;
	if(x<0)pc(‘-‘),x=-x;
	q[++cnt]=x%10,x/=10;
	while(x)
		q[++cnt]=x%10,x/=10;
	while(cnt)pc(q[cnt--]+‘0‘);
}
const int N = 4e6+10;
int n,m;
pair<ll,pair<int,int> >S[N];

ll c[2010],k[2010];
pair<int,int>a[2010];

ll dis(int x,int y){
	return abs(a[x].first-a[y].first) + abs(a[x].second-a[y].second);
}
int fa[2010];
int found(int x){
	if(fa[x] == x)return x;
	return fa[x] = found(fa[x]);

}
int id[2010];
ll mi[N];
pair<int,int>s[2010];
int t[2010];
void solve(){
	read(n);
	rep(i,1,n)fa[i] = i,id[i] = i;
	int tot = 0;
	rep(i,1,n){
		read(a[i].first);read(a[i].second);
	}
	rep(i,1,n)read(c[i]),mi[i] = c[i];
	rep(i,1,n)read(k[i]);
	rep(i,1,n){
		rep(j,1,i-1){
			S[++tot] = {(k[i] + k[j]) * dis(i,j),{i,j}};
		}
	}
	rep(i,1,n){
        S[++tot] = {c[i],{0,i}};
	}
	sort(S+1,S+1+tot);
	ll ans = 0ll;
	int tt = 0,cnt = 0 ;
	rep(i,1,tot){
		int q = found(S[i].second.first),p = found(S[i].second.second);
		ll val = S[i].first;
		if(q == p)continue;
		//val大于
		if(!S[i].second.first or !S[i].second.second){
            t[++cnt] = max(S[i].second.first,S[i].second.second);
            ans += c[max(S[i].second.first,S[i].second.second)];
		} else {
            s[++tt] = {S[i].second.first,S[i].second.second};
            ans += S[i].first;
		}
		fa[q] = p;
	}
	//rep(i,1,n)printf("%d ",id[i]);pc(‘\n‘);
	//write(mi[found(1)]);pc(‘\n‘);
	write(ans);pc(‘\n‘);
	printf("%d\n",cnt);
	rep(i,1,cnt){
		printf("%d%c",t[i]," \n"[i==cnt]);
	}//pc(‘\n‘);
	printf("%d\n",tt);
	rep(i,1,tt){
		printf("%d %d\n",s[i].first,s[i].second);
	}
}

signed main(){solve();return 0;}


D. Shichikuji and Power Grid(Codeforces Round #597 (Div. 2)题解)

上一篇:基于实际项目的SQL学习总结


下一篇:oracle linux了解基本命令行