[Codeforces 1245D] Shichikuji and Power Grid (最小生成树)
[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 (最小生成树)的更多相关文章
- CodeForces 1245D Shichikuji and Power Grid
cf题面 解题思路 比赛过程中想了一个贪心--把所有城市按照自建代价排序,排在第一的城市肯定自建,之后依次判断排在后面的城市要自建还是要连接前面的.这么做WA13了(第一次忘开long longWA4 ...
- 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 ...
- Shichikuji and Power Grid
D. Shichikuji and Power Grid 参考:Codeforces Round #597 (Div. 2) 思路:一个很裸的最小生成树.把建立基站看成是,城市与源点(虚构的)建边.由 ...
- CF1245D: Shichikuji and Power Grid
CF1245D: Shichikuji and Power Grid 题意描述: 给定\(n\)个点\((n\leq2000)\),在第\(i\)个点上建立一个基站需要\(c_i\)的代价,连接两个点 ...
- Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid 题解 最小生成树
题目链接:https://codeforces.com/contest/1245/problem/D 题目大意: 平面上有n座城市,第i座城市的坐标是 \(x[i], y[i]\) , 你现在要给n城 ...
- 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 ...
- Codeforces 1245 D. Shichikuji and Power Grid
传送门 经典的最小生成树模型 建一个点 $0$ ,向所有其他点 $x$ 连一条边权为 $c[x]$ 的边,其他任意两点之间连边,边权为 $(k_i+k_j)(\left | x_i-x_j\right ...
- 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; //坐标 ...
- [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理)
[Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理) 题面 一个\(n \times n\)的格子,每个格子里可以填\([1,k]\)内的整数. ...
随机推荐
- unittest详解(三) 测试套件(TestSuite)
在前面一章中示例了如何编写一个简单的测试,但有两个问题: 我们知道测试用例的执行顺序是根据测试用例名称顺序执行的,在不改变用例名称的情况下,我们怎么来控制用例执行的顺序呢? 一个测试文件,我们直接执行 ...
- CodeForces–471D--MUH and Cube Walls(KMP)
Time limit 2000 ms Memory limit 262144 kB Polar bears Menshykov and Uslada from the zoo of ...
- sass用法总结(持续更新中)
官网:https://www.sass.hk/ 1,嵌套规则 1.1普通嵌套:Sass 允许将一套 CSS 样式嵌套进另一套样式中,内层的样式将它外层的选择器作为父选择器 1.2父选择器 &: ...
- 通过Flink实现个推海量消息数据的实时统计
背景 消息报表主要用于统计消息任务的下发情况.比如,单条推送消息下发APP用户总量有多少,成功推送到手机的数量有多少,又有多少APP用户点击了弹窗通知并打开APP等.通过消息报表,我们可以很直观地看到 ...
- 前端开发——让算法"动"起来
正文 当然在我们不清楚具体操作细节前我们可以先假设一下,我们能够用什么来实现.按照以前看过的排序动画我将其分为 1.Js操作Dom,再搭配简单的css 2.Canvas动画 之后在查资料的时候发现还有 ...
- 微信小程序 — 速学速查笔记
1. 配置 配置全解析 project.config.json ( 项目配置文件 ) { // 文件描述 "description": "项目配置文件", // ...
- nginx+uWSGI+django+virtualenv+supervisor发布web服务器流程
导论 WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器如何与Web应用程序通信,以及Web应用程序如何链接在一起以处理一个请求,(接收请求,处理请求,响应请求)基于wsgi运行的框架有 ...
- 关于varchar的总结
摘自:https://www.jianshu.com/p/c3e188440c67 大家都知道用 varchar 比用 char 类型更省空间(不过性能略有下降,char查询更快),相对于定长的 ch ...
- debug1: Could not open authorized keys
ssh登录的时候一直日志一直出现debug1: Could not open authorized keys登录不上,检查文件夹权限都正常用这条命令解决了 restorecon -FRvv /home ...
- kolla-ansible-----常用命令
常用命令 kolla-ansible prechecks -i multinode #部署前环境检测 kolla-genpwd #生成/etc/kolla/password.yml密码配置文件 kol ...