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的更多相关文章

  1. 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 ...

  2. Shichikuji and Power Grid

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

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

    [Codeforces 1245D] Shichikuji and Power Grid (最小生成树) 题面 有n个城市,坐标为\((x_i,y_i)\),还有两个系数\(c_i,k_i\).在每个 ...

  4. 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 ...

  5. Codeforces 1245 D. Shichikuji and Power Grid

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

  6. CodeForces 1245D Shichikuji and Power Grid

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

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

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

  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 Round #597 (Div. 2)

    A - Good ol' Numbers Coloring 题意:有无穷个格子,给定 \(a,b\) ,按以下规则染色: \(0\) 号格子白色:当 \(i\) 为正整数, \(i\) 号格子当 \( ...

随机推荐

  1. .Net 如何使用Nlog

    NLog是一个简单灵活的.NET日志记录类库,NLog的API非常类似于log4net,且配置方式非常简单.通过使用NLog,我们可以在任何一种.NET语言中输出带有上下文的调试信息,根据项目需求配置 ...

  2. 『Tree nesting 树形状压dp 最小表示法』

    Tree nesting (CF762F) Description 有两个树 S.T,问 S 中有多少个互不相同的连通子图与 T 同构.由于答案 可能会很大,请输出答案模 1000000007 后的值 ...

  3. SQL分类之DML:增删改表中的数据

    DML:增删改表中的数据 1.添加数据: 语法: insert into 表名(列名1,列名2,...列名n) values(值1,值2,...值n): 注意: 1.列名和值要一一对应. 2.如果表名 ...

  4. Linux中历史命令

    历史命令,即之前登录session会话中的在命令行键入的命令. 在Linux中可以有两种方式查询历史命令 history命令 当前用户目录中的隐藏文件.bash_history 一.history 作 ...

  5. LeetCode-SQL(一)

    1.组合两个表 表1: Person +-------------+---------+| 列名         | 类型     |+-------------+---------+| Person ...

  6. C#中字符串转换为计算公式

    添加COM引用: private void button_Click(object sender, EventArgs e) { MSScriptControl.ScriptControl sc = ...

  7. docker搭建elasticsearch、kibana,并集成至spring boot

    步骤如下: 一.基于docker搭建elasticsearch环境 1.拉取镜像 docker pull elasticsearch5.6.8 2.制作elasticsearch的配置文件 maste ...

  8. echarts 3D地球实现自动旋转

    素材已上传至https://gitee.com/i1520/echarts3DEarth.git     https://github.com/i1520/echarts3DEarth 1.引入js文 ...

  9. JavaScript 之 offset 、client、scroll

    下面这三组是关于元素大小.位置相关的属性 一.offset 偏移量 1.offsetParent 该属性获取距离当前元素最近的定位父元素,如果没有定位父元素此时是 body 元素 2.offsetLe ...

  10. android中实现service动态更新UI界面

    案例:通过service向远程服务器发送请求,根据服务器返回的结果动态更新主程序UI界面,主程序可实时关闭或重启服务. 注册BroadcastReceiver 在主程序activity中注册一个Bro ...