cf 【并查集】
http://codeforces.com/contest/1245/problem/D
题意就是:你需要让所有城市都有电,你看也在该城市建电站使他有电,同时你可以链接他与其他城市,使之有电
解决:
我们可以吧每个城市自己建电站以及自己与其他城市的费用用结构体存起来,排个序,再用并查集连起来。
#include<bits/stdc++.h>
#define numm ch-48
#define pd putchar(' ')
#define pn putchar('\n')
#define pb push_back
#define debug(args...) cout<<#args<<"->"<<args<<endl
#define bug cout<<"************"
using namespace std;
template <typename T>
void read(T &res) {
bool flag=false;char ch;
while(!isdigit(ch=getchar())) (ch=='-')&&(flag=true);
for(res=numm;isdigit(ch=getchar());res=(res<<1)+(res<<3)+numm);
flag&&(res=-res);
}
template <typename T>
void write(T x) {
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
typedef long long ll;
typedef long double ld;
const int maxn=2000+10;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const double alpha=0.7;
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
struct node {
int u,v;
ll w;
node(){}
node(int u,int v,ll w):u(u),v(v),w(w){}
bool operator<(const node&a) {
return w<a.w;
}
}e[maxn*maxn+maxn];
struct a {
ll x,y;
int pos;
}a[maxn];
ll c[maxn],k[maxn];
int f[maxn];
vector<int >vec1;
vector<pii >vec2;
int getf(int v) {
return f[v]==v?v:f[v]=getf(f[v]);
}
int main()
{
int n;
read(n);
for(int i=1;i<=n;i++)
f[i]=i;
for(int i=1;i<=n;i++) {
read(a[i].x),read(a[i].y);
// a[i].pos=i;
}
for(int i=1;i<=n;i++)
read(c[i]);
for(int i=1;i<=n;i++)
read(k[i]);
int cnt=0;
for(int i=1;i<=n;i++){
e[++cnt]=node(0,i,c[i]);
for(int j=i+1;j<=n;j++)
e[++cnt]=node(i,j,(k[i]+k[j])*(abs(a[i].x-a[j].x)+abs(a[i].y-a[j].y)));
}
sort(e+1,e+1+cnt);
int num=0;
ll sum=0;
for(int i=1;i<=cnt;i++){
int u=e[i].u,v=e[i].v;
int a=getf(u);
int b=getf(v);
if(a!=b){//父亲不同才需要计算,例如你三个点肯定三条边构架出个三角形,但实际上只需要其中两条边就行了,第三条肯定是会被认出有共同父亲的
f[b]=a;
if(u==0)
vec1.pb(v);
else {
vec2.pb(mp(u,v));
}
num++;//无论你是自己建基站还是连接,因为连接就是默认前面那个已经有电,都只加一个
sum+=e[i].w;
if(num==n) break;//保证了要n个城市都亮
}
}
write(sum);pn;
write(vec1.size());pn;
if(vec1.size()) {
for(int i=0;i<vec1.size();i++)
write(vec1[i]),pd;
pn;
}
write(vec2.size());pn;
if(vec2.size()) {
for(int i=0;i<vec2.size();i++)
write(vec2[i].fi),pd,write(vec2[i].se),pn;;
pn;
}
return 0;
}
cf 【并查集】的更多相关文章
- CF 115 A 【求树最大深度/DFS/并查集】
CF A. Party time limit per test3 seconds memory limit per test256 megabytes inputstandard input outp ...
- CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...
- [CF#250 Div.2 D]The Child and Zoo(并查集)
题目:http://codeforces.com/problemset/problem/437/D 题意:有n个点,m条边的无向图,保证所有点都能互通,n,m<=10^5 每个点都有权值,每条边 ...
- CF 500 B. New Year Permutation 并查集
User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permut ...
- C. Glass Carving (CF Round #296 (Div. 2) STL--set的运用 && 并查集方法)
C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- CF # 296 C Glass Carving (并查集 或者 multiset)
C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- cf 之lis+贪心+思维+并查集
https://codeforces.com/contest/1257/problem/E 题意:有三个集合集合里面的数字可以随意变换位置,不同集合的数字,如从第一个A集合取一个数字到B集合那操作数+ ...
- 79: cf 444E 并查集+思维
$des$ 题面 $sol$ 把边从小到大排序,枚举每条边作为答案,然后把两个点合并,判断每条边是否可以作为答案时,$cnt_i$ 表示节点 $i$ 已经合并的 $x$ 之和$size_i$ 表示已经 ...
- CF 452E. Three strings(后缀数组+并查集)
传送门 解题思路 感觉这种题都是套路之类的??首先把三个串并成一个,中间插入一些奇怪的字符,然后跑遍\(SA\).考虑按照\(height\)分组计算,就是每个\(height\)只在最高位计算一次, ...
随机推荐
- Centos7部署mysql
安装mysql yum install mysql mysql-server 会出现以下错误: [root@yl-web yl]# yum install mysql-server Loaded pl ...
- 微服务-Springboot+Redis缓存管理接口代码实现
废话少说,上代码,结合代码讲解: 一.创建maven工程:导入依赖: <packaging>war</packaging><!--修改jdk的版本--><pr ...
- 列表list常用的方法
列表 name = ['Lisa', 'Mike', 'Jone', 'Lucy'] name.sort() # 排序,根据对应的ASCII码值,可以排各种类型的字符 name.append('Ms ...
- MySQL常用DDL、DML、DCL语言整理
DDL ----Data Definition Language 数据库定义语言 如 create procedure之类 创建数据库 CREATE DATABASE [IF NOT EXISTS] ...
- Python通过pymysql连接数据库并进行查询和更新SQL方法封装
1.通过pymysql连接数据库并进行数据库操作2.查询数据3.更新修改SQL方法封装 import pymysql.cursors import json class OperationMysql: ...
- 27.t分布随机近邻嵌入t-SNE
t分布随机近邻嵌入(t-distributed Stohastic Neighbor Embedding) 基本思路:为高维特征空间在二维平面(或三维超平面,不过基本上总是使用二维空间)上寻找一个投影 ...
- 22(8).模型融合---RegionBoost
在adaboost当中,样本的权重alpha是固定的,蓝色五角星所在的圈中3个○分错了,红色五角星所在的圈中4个×和1个○都分对了,很容易让人想到,这个模型,对于红色位置的判断更加可信. 动态权重,每 ...
- 在vue-cli3中使用axios获取本地json
在vue-cli3中,公共文件夹由static变成了public 先把要访问的json放到public文件夹下 使用axios的get方法获取,不能用post,不然会报404 axios..json` ...
- 你的首个golang语言详细入门教程 | your first golang tutorial
本文首发于个人博客https://kezunlin.me/post/a0fb7f06/,欢迎阅读最新内容! your first golang tutorial go tutorial version ...
- Linux中长时间运行程序的方法
一.场景: 如果临时有一个命令需要长时间运行,比如 python hello.py ,什么方法能最简便的保证它在后台稳定运行呢?解决方法: 当用户注销(logout)或者网络断开时,终端会收 ...