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\)只在最高位计算一次, ...
随机推荐
- MTK Recovery 模式横屏修改(适用于6.0 + 8.1)
修改前 修改后 6.0 Recovery 模式横屏修改方法 修改相关文件 bootable\recovery\minui\Android.mk bootable\recovery\minui\mt_g ...
- (嵌入式)工程模板的创建和MDK下配置开发STM32F103ZE
⭐ 我的网站: www.mengyingjie.com ⭐ 遇到此类问题,但看了文章还是未解决, 评论或加 QQ:781378815
- 3、netty第二个例子,使用netty建立客户端,与服务端通讯
第一个例子中,建立了http的服务器端,可以直接使用curl命令,或者浏览器直接访问. 在第二个例子中,建立一个netty的客户端来主动发送请求,模拟浏览器发送请求. 这里先启动服务端,再启动客户端, ...
- 提速企业应用开发,Ntaub 3极速开发平台发布
企业管理系统搞了这么多年,还是以表单.数据增删改这些功能为主.唯一的变化就是“变化”,企业的业务模式和流程越来越多样化,开发需求变更越来越频繁,开发周期却要求越来越短. 无代码.低代码开发平台开始流行 ...
- react官方脚手架添加less配置
装两个包 npm install --save less less-loader 在node-modules/react-scripts/config/webpack.config.js中 在大概58 ...
- Vue项目中使用jquery插件
1.引入jquery,并且在vue.config.js里配置 config.plugin('provide') .use(webpack.ProvidePlugin, [{ $: 'jquery', ...
- Unable to open debugger port: java.net.SocketException
网上都说是tomcat端口被占用,其实不是,这是因为文件权限不够,脚本不能执行,debug当然不能接受网络连接的数据 可以在Event Log里看到 所以只需要更改文件的级别就可以了(可读可写可执行) ...
- 用dotnet core搭建web服务器(三)ORM访问数据库
访问传统sql数据库,大家以前都是用sql语句去查询.这些年流行orm方法 ORM是对象关系映射的简拼,就是用一个对象(class)去表示数据的一行,用对象的成员去表述数据的列 dotnet 官方很早 ...
- Docker-Nginx,发布前端服务
1.安装环境: yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 yum-config-manager \ --add-r ...
- Redis实战(一)Redis简介及环境安装(Windows)
提到Redis,大家肯定都听过,并且应该都在项目中或多或少的使用过,也许你觉得Redis用起来挺简单的呀,但如果有人问你下面的几个问题(比如同事或者面试官),你能回答的上来吗? 什么是Redis? R ...