CF1245D: Shichikuji and Power Grid
CF1245D: Shichikuji and Power Grid
题意描述:
- 给定\(n\)个点\((n\leq2000)\),在第\(i\)个点上建立一个基站需要\(c_i\)的代价,连接两个点需要\((|x_i-x_j|+|y_i-y_j|)*(k_i+k_j)\)的代价。对于一个点要么建立基站,要么连接建立基站的点。问最小代价是多少,同时输出建立基站的点和线路。
思路:
- 建立一个超级根,对于每个城市建立发电站,连接一条权值为\(c_i\)的边,再把每个城市之间连接电线的花费算出来,跑\(kruskal\)。
- 在跑的过程中记录哪些城市建立了发电站,哪些城市建立了电线。
- \(hint:\)注意开\(long\ long\)
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2000 + 10;
int n;
ll ans, x[maxn], y[maxn], k[maxn], c[maxn];
struct Node{
int x, y;
ll z;
}p[maxn*maxn];
int tot;
void add_edge(int x, int y, ll z){
p[++tot].x = x, p[tot].y = y, p[tot].z = z;
}
int fa[maxn];
bool cmp(Node a, Node b){
return a.z < b.z;
}
int get_fa(int x)
{
if(x == fa[x]) return x;
return fa[x] = get_fa(fa[x]);
}
int ans1[maxn], cnt1;
int ax[maxn], ay[maxn], cnt2;
void kruskal()
{
for(int i = 0; i <= n; i++) fa[i] = i;
sort(p+1, p+1+tot, cmp);
int cnt = 0;
for(int i = 1; i <= tot; i++)
{
int x = p[i].x, y = p[i].y;
ll z = p[i].z;
int fx = get_fa(x), fy = get_fa(y);
if(fx != fy)
{
if(x == 0 || y == 0) ans1[++cnt1] = x + y;
else ax[++cnt2] = x, ay[cnt2] = y;
ans += z; fa[fx] = fy; cnt++;
}
if(cnt == n) break;
}
cout << ans << endl;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%lld%lld", &x[i], &y[i]);
for(int i = 1; i <= n; i++) scanf("%lld", &c[i]);
for(int i = 1; i <= n; i++) scanf("%lld", &k[i]);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
if(i != j)
{
ll w = (k[i] + k[j]) * (abs(x[i]-x[j]) + abs(y[i]-y[j]));
add_edge(i, j, w);
}
}
for(int i = 1; i <= n; i++)
add_edge(0, i, c[i]), add_edge(i, 0, c[i]);
kruskal();
cout << cnt1 << endl;
for(int i = 1; i <= cnt1; i++)
printf("%d ", ans1[i]); puts("");
cout << cnt2 << endl;
for(int i = 1; i <= cnt2; i++)
printf("%d %d\n", ax[i], ay[i]);
return 0;
}
CF1245D: Shichikuji and Power Grid的更多相关文章
- 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) 思路:一个很裸的最小生成树.把建立基站看成是,城市与源点(虚构的)建边.由 ...
- [Codeforces 1245D] Shichikuji and Power Grid (最小生成树)
[Codeforces 1245D] Shichikuji and Power Grid (最小生成树) 题面 有n个城市,坐标为\((x_i,y_i)\),还有两个系数\(c_i,k_i\).在每个 ...
- 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 1245D Shichikuji and Power Grid
cf题面 解题思路 比赛过程中想了一个贪心--把所有城市按照自建代价排序,排在第一的城市肯定自建,之后依次判断排在后面的城市要自建还是要连接前面的.这么做WA13了(第一次忘开long longWA4 ...
- 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 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 Round #597 (Div. 2)
A - Good ol' Numbers Coloring 题意:有无穷个格子,给定 \(a,b\) ,按以下规则染色: \(0\) 号格子白色:当 \(i\) 为正整数, \(i\) 号格子当 \( ...
随机推荐
- NOI2019网络同步赛游记
我发的邮件**f没收到,后来去专门询问才整到一个名额(估计是嫌我太菜,参加了也是垫底) day -1 上午写了到类似随机游走的高斯消元期望dp,然后颓颓颓 下午打洛咕月赛.T1一直50pts,后来才知 ...
- java中反射知识点总结
1 package Demo; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.Field; 5 impo ...
- Django中一些常用的文档段落
1. Settings¶ STATIC_URL¶ MEDIA_ROOT¶ MEDIA_URL¶ AUTH_USER_MODEL¶ USE_I18N¶ USE_L10N¶ USE_TZ¶ (三者默 ...
- java之maven之初识maven
1.maven是一个项目管理工具. 包括项目创建.资源管理.项目运行.项目发布等功能. 2.为什么使用 maven? a. jar 依赖管理(升级.降级)等 b. 项目之间依赖管理 c. 资源文件管理 ...
- 自定义WPF Popup控件
解决问题 1.WPF Popup 不随着 Window 一起移动的问题 2.WPF Popup 总是显示在最前面 引用命名空间 xmlns:ctrl="clr-namespace:Micro ...
- English--动词语态
English|动词语态 动词的语态在理解长难句中很重要.了解被动语态与主动语态,掌握语态的变化方式. 前言 目前所有的文章思想格式都是:知识+情感. 知识:对于所有的知识点的描述.力求不含任何的自我 ...
- MES被重新定义?做到这几点才算智能制造
在工业4.0背景下,推动智能制造,构建智能工厂.智能生产.智能物流和智能服务体系,我们需要对MES系统重新进行定义,主要从以下几个方面进行说明: MES深度融入企业运营环节 智能工厂中的机器将全部由软 ...
- python实现广度优先搜索
from collections import deque #解决从你的人际关系网中找到芒果销售商的问题#使用字典表示映射关系graph = {} graph["you"] = [ ...
- yaml文件配置logger
yaml 今天用yaml文件写了一下logging的配置,文件如下: version: 1 disable_existing_loggers: False formatters: simple: fo ...
- SCP免密传输和SSH登录流程详解
SCP免密传输和SSH登录协议详解 在linux下开发时,经常需要登录到其他的设备上,例如虚拟机内ubuntu.树莓派等等,经常涉及到传输文件的操作,传输文件有很多中方法,如物理磁盘拷贝,基于网络的s ...