cf题面

解题思路

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

赛后看看题解,又参考了之前同样WA13的 Artoriax的代码,大概发现了这种做法的漏洞。假设自建代价是\(c_1<c_2<c_3\),可以构造连边的代价,使得在花费最小的连接方式中,连边应该是1—3—2,我之前那样的做法,1号城市自建以后,判断2号城市要自建还是要连1号城市,再判断3号城市要自建还是要连1号城市或者2号城市。

具体的hack数据如下——

3
1 1
2 2
2 1
1 5 100
1 2 1

说简单点大概就是,1、2、3自建代价是1、5、100,1到2连边代价是5,1到3的连边代价是2,2到3的连边代价是3。最小代价答案是6,我那种方法跑出来是8。

我后来AC的思路大概是:首先假设每个点都自建,那么每个点的代价就是自建代价。然后按照代价排序,用代价最小的点去更新后面那些点,如果能更新用电代价,就把那些点连接到当前点。然后进入下一轮循环,排除上一次代价最小的点,把剩下的点再次按照代价排序,然后用这些点中代价最小的去更新其他的,以此类推。

官方题解还提供了一种更一般的想法:这题其实就是求一个最小生成树,图是这么建的——首先所有点之间连边,边权就是连接代价,然后加一个0号点,所有点向0号点连边,边权是自建代价。这么一想,我AC的思路就是毫无堆优化的、还不如线性直接找最小值的、很蠢的Prim了。

源代码

#include<cstdio>
#include<algorithm>
int n;
struct City{
int id;
long long x,y;
long long cc,kk;
bool self;
int fa;
bool operator < (const City & a)const{
return cc<a.cc;
}
}c[2005];
int main()
{
// freopen("test.in","r",stdin);
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
c[i].id=i;
c[i].self=1;
scanf("%lld%lld",&c[i].x,&c[i].y);
}
for(int i=1;i<=n;i++) scanf("%lld",&c[i].cc);
for(int i=1;i<=n;i++) scanf("%lld",&c[i].kk);
long long ans=0,selfnum=0;
for(int i=1;i<=n;i++)
{
std::sort(c+i,c+1+n);//大概就是要随时排序,每次找到最小的
ans+=c[i].cc;
if(c[i].self) selfnum++;
for(int j=i+1;j<=n;j++)
{
long long cost=(c[i].kk+c[j].kk)*(std::abs(c[i].x-c[j].x)+std::abs(c[i].y-c[j].y));
if(cost<c[j].cc)
{
c[j].cc=cost;
c[j].self=0;//放弃自建
c[j].fa=c[i].id;
}
}
}
printf("%lld\n%lld\n",ans,selfnum);
for(int i=1;i<=n;i++)
if(c[i].self) printf("%d ",c[i].id);
printf("\n%lld\n",n-selfnum);
for(int i=1;i<=n;i++)
if(!c[i].self) printf("%d %d\n",c[i].id,c[i].fa);
return 0;
}

CodeForces 1245D Shichikuji and Power Grid的更多相关文章

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

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

  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 题意: Shichikuji is the new resident deity of the So ...

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

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

  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. python接口自动化-重定向(Location)

    一.重定向 1.重定向(redirect)就是通过各种方法将各种网络请求重新定个方向转到其它位置,从地址A跳转到地址B了. 2.重定向状态码: --301 redirect: 301 代表永久性转移( ...

  2. 微信小程序打开地图选择位置

    wx.getLocation({ type: 'wgs84', success(res) { const latitude = res.latitude const longitude = res.l ...

  3. 卸载mysql后再安装提示The service already exists!问题解决方法

    卸载mysql后再安装输入mysqld --install 回车后提示The service already exists! 原因:卸载的时候没有卸载干净 方法: 一.重新以管理员身份打开cmd 二. ...

  4. 小白学习django第六站-http相关

    请求与相应 HttpRequest对象API def home(request): print('path:', request.path) print('mothod:', request.meth ...

  5. pc端和移动端的“窗口”(viewport)故事(part1)

    A tale of two viewports - part one 在以下的系列文章中,我将为大家解释浏览器中的视窗和一些重要的元素的尺寸是如何起作用的,如:大家最熟悉的html元素以及window ...

  6. volatile关键字和transient关键字

    Java 语言包含两种内在的同步机制:同步块(或方法)和 volatile 变量 1.volatile 关键字-----针对变量的可变性 专业解释:volatile关键字是个类型修饰符用它声明的类型变 ...

  7. 命名空间System.IO

    基本介绍:System.IO 命名空间提供读写文件和数据流的类型.基本文件和目录支持的类型. 原文:http://blog.sina.com.cn/s/blog_48a45b950100erhz.ht ...

  8. 使用HBuilder创建图表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 使用 flask-restful 编写 自己的 ai web service

    本项目在 win 平台采用 pycharm 编写, 技能与环境要求: python 基础, web 基础知识, python.exe = 3.6+ 算法>第四版,操作系统推荐<现代操作系统 ...

  10. Firefox 的User Agent 将移除 CPU 架构信息

    Mozilla 计划从 Firefox 的 User Agent(用户代理)和几个支持的 API 中移除 CPU 架构信息,以减少 Firefox 用户的“数字指纹”.Web 浏览器会自动向用户在应用 ...