[Codeforces 1245D] Shichikuji and Power Grid (最小生成树)

题面

有n个城市,坐标为\((x_i,y_i)\),还有两个系数\(c_i,k_i\).在每个城市建立发电站需要费用\(c_i\).如果不建立发电站,要让城市通电,就需要与有发电站的城市连通。i与j之间连一条无向的边的费用是\((k_i+k_j)\)*两个城市之间的曼哈顿距离.求让每个城市都通电的最小费用,并输出任意一个方案。

分析

把选每个点的代价转成虚拟原点到这个点的边,这个套路很常见,但在最小生成树题里还是第一次见到。

城市之间两两连边,边权按题目里提到的计算。然后建立一个虚拟源点,向每个城市\(i\)连边,边权为\(c_i\).对整个图跑一个最小生成树即可.

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define maxn 2000
using namespace std;
typedef long long ll;
int n;
struct node{
int x;
int y;
int c;
int k;
}a[maxn+5];
inline ll dist(ll x1,ll y1,ll x2,ll y2){
return abs(x1-x2)+abs(y1-y2);
} struct edge{
int from;
int to;
ll len;
int next;
}E[maxn*maxn+5];
int head[maxn+5];
int sz=0;
void add_edge(int u,int v,ll w){
sz++;
E[sz].from=u;
E[sz].to=v;
E[sz].len=w;
E[sz].next=head[u];
head[u]=sz;
}
bool cmp(edge p,edge q){
return p.len<q.len;
} int fa[maxn+5];
int find(int x){
if(fa[x]==x) return x;
else return fa[x]=find(fa[x]);
} vector< pair<int,int> >res;
ll kruskal(){
ll ans=0;
for(int i=1;i<=n+1;i++) fa[i]=i;
sort(E+1,E+1+sz,cmp);
for(int i=1;i<=sz;i++){
int u=E[i].from,v=E[i].to;
int fu=find(u),fv=find(v);
if(fu!=fv){
ans+=E[i].len;
fa[fu]=fv;
res.push_back(make_pair(E[i].from,E[i].to));
}
}
return ans;
} vector<int>poi;//poi~
vector< pair<int,int> >edg;
int main(){
int st;
scanf("%d",&n);
st=n+1;
for(int i=1;i<=n;i++) scanf("%d %d",&a[i].x,&a[i].y);
for(int i=1;i<=n;i++){
scanf("%d",&a[i].c);
add_edge(st,i,a[i].c);
}
for(int i=1;i<=n;i++) scanf("%d",&a[i].k);
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
add_edge(i,j,(a[i].k+a[j].k)*dist(a[i].x,a[i].y,a[j].x,a[j].y));
}
}
printf("%I64d\n",kruskal());
for(auto ed : res){
if(ed.first==st) poi.push_back(ed.second);
else edg.push_back(ed);
}
printf("%d\n",poi.size());
for(int x : poi) printf("%d ",x);
printf("\n");
printf("%d\n",edg.size());
for(auto x : edg) printf("%d %d\n",x.first,x.second);
}

[Codeforces 1245D] Shichikuji and Power Grid (最小生成树)的更多相关文章

  1. CodeForces 1245D Shichikuji and Power Grid

    cf题面 解题思路 比赛过程中想了一个贪心--把所有城市按照自建代价排序,排在第一的城市肯定自建,之后依次判断排在后面的城市要自建还是要连接前面的.这么做WA13了(第一次忘开long longWA4 ...

  2. 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 Blac ...

  3. Shichikuji and Power Grid

    D. Shichikuji and Power Grid 参考:Codeforces Round #597 (Div. 2) 思路:一个很裸的最小生成树.把建立基站看成是,城市与源点(虚构的)建边.由 ...

  4. CF1245D: Shichikuji and Power Grid

    CF1245D: Shichikuji and Power Grid 题意描述: 给定\(n\)个点\((n\leq2000)\),在第\(i\)个点上建立一个基站需要\(c_i\)的代价,连接两个点 ...

  5. Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid 题解 最小生成树

    题目链接:https://codeforces.com/contest/1245/problem/D 题目大意: 平面上有n座城市,第i座城市的坐标是 \(x[i], y[i]\) , 你现在要给n城 ...

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

    链接: https://codeforces.com/contest/1245/problem/D 题意: Shichikuji is the new resident deity of the So ...

  7. Codeforces 1245 D. Shichikuji and Power Grid

    传送门 经典的最小生成树模型 建一个点 $0$ ,向所有其他点 $x$ 连一条边权为 $c[x]$ 的边,其他任意两点之间连边,边权为 $(k_i+k_j)(\left | x_i-x_j\right ...

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

    #include<bits/stdc++.h> using namespace std ; int n; struct City { int id; long long x,y; //坐标 ...

  9. [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理)

    [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理) 题面 一个\(n \times n\)的格子,每个格子里可以填\([1,k]\)内的整数. ...

随机推荐

  1. jquery的checked

    目前使用的jQuery版本为 v1.11.2 jquery判断checked的三种方法: .attr('checked'):   //看版本1.6+返回:"checked"或&qu ...

  2. Ueditor 从word中复制内容带多张图片

    粘贴文本 注意,以下配置暂时对 IE 无效.IE 暂时使用系统自带的粘贴功能,没有样式过滤! 关闭粘贴样式的过滤 当从其他网页复制文本内容粘贴到编辑器中,编辑器会默认过滤掉复制文本中自带的样式,目的是 ...

  3. Apicloud_(模板)登陆注册功能模板

    项目已托管到Github上 传送门 不需要使用任何图片资源,需要用到SHA1.js库文件, Apicloud_(接口验证)用户注册头部信息X-APICloud-AppKey生成 传送门 项目全代码放到 ...

  4. 数据重塑图解—Pivot, Pivot-Table, Stack and Unstack

    Pivot pivot函数用于创建一个新的派生表,该函数有三个参数:index, columns和values.你需要在原始表中指定这三个参数所对定的列名,接下来pivot函数会创建一个新的表格,其中 ...

  5. 使用宝塔面板 配置nginx 访问ftp服务器下面的图片

    如果 你在服务器上 运行war项目 可以在tomcat 配置访问的: tomcat 也贴出来吧! 一.tomca配置访问,需要更改配置文件server.xml ,如果找不到,自己好好找一下  一般在 ...

  6. 20175215 2018-2019-2 第五周java课程学习总结

    第六章学习内容 1.接口 使用interface来定义一个接口. 接口体中包含常量的声明(没有变量)和抽象方法两部分.接口体中只有抽象方法,没有普通的方法,而且接口体中所有的常量的访问权限一定都是pu ...

  7. 如何将浏览器上的JS文件屏蔽

    在学习自定验证时,需要在页面上进行调试运行结果,这时的JS会影响运行结果的显示,所以我们会将JS代码屏蔽掉,那么我们该如何做呢? 第一步: 在浏览器地址栏输入about:config  点击“我保证会 ...

  8. CentOS 6.5 安装OSA监控精灵监控主机

    OSA监控是一个开源的图形化免费好用的监控,安装之前首先要配置好PHP环境, yum install httpd mysql mysql-server php-mysql php* -y 编辑http ...

  9. 整理ing

    RT 要学习的 专克bzoj权限题 钟神p系列

  10. DeepWalk 安装指南

    DeepWalk 安装指南 创建 conda 虚拟环境 conda create -n deepwalk pip python=3.5 conda activate deepwalk 安装 deepw ...