Codeforces Round #597 (Div. 2)D(最小生成树)

Posted ldudxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #597 (Div. 2)D(最小生成树)相关的知识,希望对你有一定的参考价值。

/*每个点自己建立一座发电站相当于向超级源点连一条长度为c[i]的边,连电线即为(k[i]+k[j])*两点间曼哈顿距离,跑最小生成树(prim适用于稠密图,kruscal适用于稀疏图)*/

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int fa[2007];
int x[2007],y[2007];
int c[2007],k[2007];
long long m[2007][2007];
vector<pair<long long,pair<int,int> > >edge;
vector<int>ans_point;
vector<pair<int,int> >ans_edge;
int fi(int x){
return fa[x]==x?x:fa[x]=fi(fa[x]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=1;i<=n;++i)
cin>>x[i]>>y[i];
for(int i=1;i<=n;++i)
cin>>c[i];
for(int i=1;i<=n;++i)
cin>>k[i];
for(int i=1;i<=n;++i){
m[0][i]=c[i];
fa[i]=i;
edge.push_back({c[i],make_pair(0,i)});
}
for(int i=1;i<=n;++i){
for(int j=i+1;j<=n;++j){
long long tamp=1ll*(k[i]+k[j])*(abs(x[i]-x[j])+abs(y[i]-y[j]));
edge.push_back({tamp,make_pair(i,j)});
}
}
sort(edge.begin(),edge.end());
long long ans=0;
for(int i=0;i<edge.size();++i){
long long tamp=edge[i].first;
int x=edge[i].second.first;
int y=edge[i].second.second;
if(fi(x)==fi(y))
continue;
ans+=tamp;
if(!x)
ans_point.push_back(y);
else
ans_edge.push_back({x,y});
fa[fi(y)]=x;
}
cout<<ans<<" ";
cout<<ans_point.size()<<" ";
for(int i=0;i<ans_point.size();++i)
cout<<ans_point[i]<<" ";
cout<<" ";
cout<<ans_edge.size()<<" ";
for(int i=0;i<ans_edge.size();++i)
cout<<ans_edge[i].first<<" "<<ans_edge[i].second<<" ";
return 0;
}

以上是关于Codeforces Round #597 (Div. 2)D(最小生成树)的主要内容,如果未能解决你的问题,请参考以下文章

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

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

Codeforces Round #597 (Div. 2) A. Good ol' Numbers Coloring

Codeforces Round #597 (Div. 2) E. Hyakugoku and Ladders 概率dp

Codeforces Round #597 (Div. 2)D(最小生成树)

Codeforces Round #597 (Div. 2) C dp