[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\).对整个图跑一个最小生成树即可.

代码

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<vector>
  6. #define maxn 2000
  7. using namespace std;
  8. typedef long long ll;
  9. int n;
  10. struct node{
  11. int x;
  12. int y;
  13. int c;
  14. int k;
  15. }a[maxn+5];
  16. inline ll dist(ll x1,ll y1,ll x2,ll y2){
  17. return abs(x1-x2)+abs(y1-y2);
  18. }
  19. struct edge{
  20. int from;
  21. int to;
  22. ll len;
  23. int next;
  24. }E[maxn*maxn+5];
  25. int head[maxn+5];
  26. int sz=0;
  27. void add_edge(int u,int v,ll w){
  28. sz++;
  29. E[sz].from=u;
  30. E[sz].to=v;
  31. E[sz].len=w;
  32. E[sz].next=head[u];
  33. head[u]=sz;
  34. }
  35. bool cmp(edge p,edge q){
  36. return p.len<q.len;
  37. }
  38. int fa[maxn+5];
  39. int find(int x){
  40. if(fa[x]==x) return x;
  41. else return fa[x]=find(fa[x]);
  42. }
  43. vector< pair<int,int> >res;
  44. ll kruskal(){
  45. ll ans=0;
  46. for(int i=1;i<=n+1;i++) fa[i]=i;
  47. sort(E+1,E+1+sz,cmp);
  48. for(int i=1;i<=sz;i++){
  49. int u=E[i].from,v=E[i].to;
  50. int fu=find(u),fv=find(v);
  51. if(fu!=fv){
  52. ans+=E[i].len;
  53. fa[fu]=fv;
  54. res.push_back(make_pair(E[i].from,E[i].to));
  55. }
  56. }
  57. return ans;
  58. }
  59. vector<int>poi;//poi~
  60. vector< pair<int,int> >edg;
  61. int main(){
  62. int st;
  63. scanf("%d",&n);
  64. st=n+1;
  65. for(int i=1;i<=n;i++) scanf("%d %d",&a[i].x,&a[i].y);
  66. for(int i=1;i<=n;i++){
  67. scanf("%d",&a[i].c);
  68. add_edge(st,i,a[i].c);
  69. }
  70. for(int i=1;i<=n;i++) scanf("%d",&a[i].k);
  71. for(int i=1;i<=n;i++){
  72. for(int j=i+1;j<=n;j++){
  73. add_edge(i,j,(a[i].k+a[j].k)*dist(a[i].x,a[i].y,a[j].x,a[j].y));
  74. }
  75. }
  76. printf("%I64d\n",kruskal());
  77. for(auto ed : res){
  78. if(ed.first==st) poi.push_back(ed.second);
  79. else edg.push_back(ed);
  80. }
  81. printf("%d\n",poi.size());
  82. for(int x : poi) printf("%d ",x);
  83. printf("\n");
  84. printf("%d\n",edg.size());
  85. for(auto x : edg) printf("%d %d\n",x.first,x.second);
  86. }

[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. Nowcoder farm ( 树状数组、二维前缀和、二维偏序 )

    题目链接 分析 : 最简单的想法当然就是去模拟 直接对每个施肥料的操作进行模拟.然后计算贡献 但是这显然会超时.这题需要换一个思维 对于一个土地(也就是二维平面上的一个点)的种类是 T' 如果它被操作 ...

  2. HyperSQL 链接参数中文件的路径

    如果我们在系统中配置下面的连接参数: spring.datasource.url=jdbc:hsqldb:file:~/db/cwiki-us-jpetstore 我们怎么知道 hsqldb 数据库的 ...

  3. Haystack全文检索框架

    一.什么是Haystack Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高 ),该框架支持Solr,Elasticsearch,Whoosh ...

  4. Python3 获取一大段文本之间两个关键字之间的内容

    用re或者string.find.以下是re代码 123456789101112131415import re#文本所在TXT文件file = '123.txt' #关键字1,2(修改引号间的内容)w ...

  5. qt 程序发布

    Qt 程序发布步骤: 注意5.5 以后不再支持 WinXP 1) dll 抽取工具 windeployqt.exe 2) Windows脚本 实例: D:\Qt\Qt5.5.0\5.5\mingw49 ...

  6. 9.Python关键字(保留字)一览表

    保留字是 Python 语言中一些已经被赋予特定意义的单词,这就要求开发者在开发程序时,不能用这些保留字作为标识符给变量.函数.类.模板以及其他对象命名. Python 包含的保留字可以执行如下命令进 ...

  7. vue router 报错:NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated"}

    https://blog.csdn.net/weixin_43202608/article/details/98884620

  8. MySQL 获取格林时间

    SELECT *FROM 表面WHERE DATE_SUB( NOW( ), INTERVAL 3 MINUTE ) <= CONVERT_TZ( 时间字段, @@SESSION.time_zo ...

  9. Error:Connection activation failed: No suitable device found for this connection

    原文链接: https://blog.csdn.net/baiboya/article/details/80452822 ens33这个网卡一直无法激活,在网上找了半天,找到这个博主的文章,才解决,虽 ...

  10. java高级面试题汇总(复习)从最基础的往上复习,每天定期更新。

    每天搬一点砖,总有一天成为大牛! 看问题的时候请不要立马去翻答案,多想想. 看完答案可以问问为什么,尝试拓展!一起加油吧! 每个答案后面都有一个小彩蛋(一个以上的拓展问题),钻研让你先人一步. jav ...