D. Shichikuji and Power Grid

参考:Codeforces Round #597 (Div. 2)

思路:一个很裸的最小生成树。把建立基站看成是,城市与源点(虚构的)建边。由此建立最小生成树,即可得出答案。

代码:

  1. // Created by CAD on 2019/11/2.
  2. #include <bits/stdc++.h>
  3. #define ll long long
  4. #define pii pair<int,int>
  5. using namespace std;
  6. const int maxn=2020*2000;
  7. struct edge{
  8. ll u,v;
  9. ll w;
  10. bool operator<(edge &e)
  11. {
  12. return w<e.w;
  13. }
  14. }e[maxn];
  15. ll fa[2005],x[2005],y[2005],c[2005],k[2005];
  16. int n;
  17. vector<int> out1;
  18. vector<pii> out2;
  19. int find(int i)
  20. {
  21. return i==fa[i]?i:fa[i]=find(fa[i]);
  22. }
  23. int cnt=0;
  24. int cnt1=0;
  25. ll kruskal()
  26. {
  27. ll ans=0;
  28. for(int i=0;i<=n;++i)
  29. fa[i]=i;
  30. for(int i=1;i<=cnt;++i)
  31. {
  32. int u=find(e[i].u),v=find(e[i].v);
  33. if(u!=v)
  34. {
  35. ans+=e[i].w,fa[v]=u;
  36. if(!e[i].u) out1.push_back(e[i].v);
  37. else out2.push_back({e[i].u,e[i].v});
  38. }
  39. }
  40. return ans;
  41. }
  42. int main()
  43. {
  44. // FOPEN;
  45. ios::sync_with_stdio(false);
  46. cin.tie(0);
  47. cin>>n;
  48. for(int i=1;i<=n;++i)
  49. cin>>x[i]>>y[i];
  50. for(int i=1;i<=n;++i)
  51. cin>>c[i];
  52. for(int i=1;i<=n;++i)
  53. cin>>k[i];
  54. for(int i=1;i<=n;++i)
  55. e[++cnt]=edge{0,i,c[i]};
  56. for(int i=1;i<=n;++i)
  57. for(int j=i+1;j<=n;++j)
  58. e[++cnt]=edge{i,j,1ll*(abs(x[i]-x[j])+abs(y[i]-y[j]))*(k[i]+k[j])};
  59. sort(e+1,e+cnt+1);
  60. cout<<kruskal()<<endl;
  61. cnt1=out1.size();
  62. cout<<cnt1<<endl;
  63. for(int i=0;i<cnt1-1;++i)
  64. cout<<out1[i]<<" ";
  65. if(cnt1)
  66. cout<<out1[cnt1-1]<<endl;
  67. cout<<out2.size()<<endl;
  68. for(auto i:out2)
  69. cout<<i.first<<" "<<i.second<<endl;
  70. return 0;
  71. }

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. CF1245D: Shichikuji and Power Grid

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

  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. mysql数据库的锁表与解决办法(原博客url:http://www.cnblogs.com/wanghuaijun/p/5949934.html)

    MySQL锁概述 相对其他数据库而言,MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎采用的是表级锁(table-level loc ...

  2. linux下mysql数据导入到redis

    自Redis 2.6以上版本起,Redis支持快速大批量导入数据,即Pipe传输.通过将要导入的命令转换为Resp格式,然后通过MySQL的concat()来整理出最终导入的命令集合,以达到快速导入的 ...

  3. Unity 更改鼠标指针

    1. 把鼠标指针图标导入到Unity中,把它的Texture Type改为Cursor : 2. 打开PlayerSettings面板,把鼠标指针图片拖到Default Cursor中: 3. 在场景 ...

  4. 基于PROMISE解决回调地狱问题

    回调地狱问题: 在使用JavaScript时,为了实现某些逻辑经常会写出层层嵌套的回调函数,如果嵌套过多,会极大影响代码可读性和逻辑,这种情况也被成为回调地狱.比如说你要把一个函数 A 作为回调函数, ...

  5. struts访问jsp api内置对象的集中方式

    1 default-action-ref元素改元素用来配置默认的action,如果struts找不到对应的action,就会调用这个默认的action 2 dmi处理方式是通过请求action中的一个 ...

  6. 关于写SQL语句的技巧

    一.SQL总结写法 SQL的写法无非就是几种,关联查询,子查询,分组函数,各种函数的使用 1.首先根据要做的需求,先分析一下,需要用到哪些查询,例如要用到关联查询,就先把用到的表列出来,比如a,b,c ...

  7. C语言memset函数详解

    C语言memset函数详解 memset() 的作用:在一段内存块中填充某个给定的值,通常用于数组初始化与数组清零. 它是直接操作内存空间,mem即“内存”(memory)的意思.该函数的原型为: # ...

  8. 手写走通HTTP server 第二版本

    HTTP server 2.0 1 接收客户请求 2 解析客户端请求 3 组合数据,形成HTTP response 4 将数据发送给客户端 升级 : 1 多线程接收客户端请求 2 基本的请求解析,根据 ...

  9. redis系列二: linux下安装redis

    下面介绍在Linux环境下,Redis的安装与配置 一. 安装 1.首先上官网下载Redis 压缩包,地址:http://redis.io/download 下载稳定版3.0即可. 2.通过远程管理工 ...

  10. centos能进入命令行界面,进不了图形界面

    在开机引导界面按“e”, 找到linux16开头的一行,定位到ro然后修改ro为rw,并添加:init=/sysroot/bin/sh 使用ctrl x进入安全模式. 使用命令:chroot /sys ...