2016 NEERC, Moscow Subregional Contest K. Knights of the Old Republic



题意:有一张图,第i个点被占领需要ai个兵,而每个兵传送至该点需要bi的费用。占领第i条边需要其两端点的兵数之和大等于ci。对于已占领的点或边可以免费通行。因此兵达到一个点的手段有传送和沿路走。图上的兵可以在已占领的点、边随意调度。

求占领所有点的最小花费。

思路:将边按ci进行升序排列,对于每条边两端点所在的连通块进行合并,合并细节见代码。这里有一点值得思考:当你想打通当前这条边时,由于排了序,你总可以默认之前那些ci小的边已打通(因为兵可以调度)。因此,我们需要用并查集维护连通块,以及每个连通块中最小的传送单价,最大的单点兵需求量,来更新花费。

#include<iostream>
#include<cstdio>
#include<algorithm>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,pair<int,int> > P;
const int maxn=3e5+10;
P edge[maxn];
int fa[maxn];
ll a[maxn],b[maxn],cost[maxn];
void init(int n)
{
for (int i=1;i<=n;++i)
{
fa[i]=i;
cost[i]=a[i]*b[i];
}
}
int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void unio(int x,int y,ll c)
{
int fx=find(x),fy=find(y);
if (fx==fy)
return;
fa[fx]=fy;
a[fy]=max(c,max(a[fy],a[fx]));
b[fy]=min(b[fy],b[fx]);
cost[fy]=min(cost[fy]+cost[fx],a[fy]*b[fy]);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i)
scanf("%lld%lld",&a[i],&b[i]);
for (int i=0;i<m;++i)
scanf("%d%d%lld",&edge[i].se.fi,&edge[i].se.se,&edge[i].fi);
sort(edge,edge+m);
init(n);
for (int i=0;i<m;++i)
{
int u=edge[i].se.fi,v=edge[i].se.se,c=edge[i].fi;
unio(u,v,c);
}
ll ans=0;
for (int i=1;i<=n;++i)
if (fa[i]==i)
ans+=cost[i];
printf("%lld",ans);
return 0;
}

2016 NEERC, Moscow Subregional Contest K. Knights of the Old Republic(Kruskal思想)的更多相关文章

  1. 2016-2017 ACM-ICPC, NEERC, Moscow Subregional Contest

    A. Altitude 从小到大加入每个数,用set查找前驱和后继即可. 时间复杂度$O(n\log n)$. #include <bits/stdc++.h> using namespa ...

  2. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem K. KMC Attacks 交互题 暴力

    Problem K. KMC Attacks 题目连接: http://codeforces.com/gym/100714 Description Warrant VI is a remote pla ...

  3. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem D. Distance 迪杰斯特拉

    Problem D. Distance 题目连接: http://codeforces.com/gym/100714 Description In a large city a cellular ne ...

  4. 2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest E. Equal Digits

    E. Equal Digits time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  5. 2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest A. Advanced 2048

    A. Advanced 2048 time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. [Gym]2008-2009 ACM-ICPC, NEERC, Moscow Subregional Contest

    比赛链接:http://codeforces.com/gym/100861 A模拟,注意两个特殊的缩写. #include <bits/stdc++.h> using namespace ...

  7. 2017-2018 ACM-ICPC, NEERC, Moscow Subregional Contest

    A. Advertising Strategy 最优策略一定是第一天用$y$元,最后一天再用$x-y$元补满. 枚举所有可能的$y$,然后模拟即可,天数为$O(\log n)$级别. 时间复杂度$O( ...

  8. 2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest J - Jealousy

    题意:有n张照片,每张照片上有一些妹子,要按照片顺序给妹纸安排男朋友,如果妹纸i安排的男朋友之前有女朋友,那么费用+wi,求总费用最小,和输出路径 题解:费用流,先把照片天数建点i连i+1,流量k(最 ...

  9. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem F. Finance 模拟题

    Problem F. Finance 题目连接: http://codeforces.com/gym/100714 Description The Big Boss Company (BBC) pri ...

随机推荐

  1. Redis 的订阅发布(PUB/SUB)示例

    ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1"); ISubscriber sub ...

  2. 【原创】大叔经验分享(79)mysql内存设置

    mysql内存设置,首先要知道当前的设置 MySQL [(none)]> show variables like '%buffer%'; +--------------------------- ...

  3. CNN 笔记

    1. 卷积后的图像的大小为    (w+2p-f)*3 / s    W为图像的宽,p为padding的大小, f为卷积核大小, 3 为图像的通道数, s为步长 2. 卷积层和池化层的区别? 卷积层是 ...

  4. TypeScript入门二:基本数据类型

    浅析基本数据类型 TypeScript类型解析 一.浅析基本数据类型 首先有一个问题TypeScript是一门编译型语言?还是解释性语言?显然已经不能被这两个分类来区分,TypeScript的并不是为 ...

  5. 日志(logging)与正则(re)模块

    logging模块 #日志:日常的流水 =>日志文件,将程序运行过程中的状态或数据进行记录,一般都是记录到日志文件中 #1.logging模块一共分为五个打印级别 debug.info.warn ...

  6. 安卓开发之sql语句增删改查

    package com.lidaochen.phonecall; import android.content.Context; import android.database.sqlite.SQLi ...

  7. Yii 2.0 GII 访问404错误

    网上大部分都是普通的开启和配置资料 按照网上资料配置 访问localhost/index/php?r=gii 总是提示404错误 解决方法如下: Yii基础版中的 web.php 代码如下 if (Y ...

  8. mORMot学习笔记 (一)

    官方网站:https://synopse.info/fossil/wiki/Synopse+OpenSource 下载地址:https://synopse.info/fossil/wiki?name= ...

  9. 6.Tray Monitor服务(监控服务)

    1.   Tray Monitor服务(监控服务) 该服务需要运行在gui环境下,用于查看baclua client.存储等状态.下面以windows下安装为例. 1.1.   Tray Monito ...

  10. elasticsearch联想加搜索实例

    //搜索框具体的ajax如下: <form class="form-wrapper cf"> <img src="__PUBLIC__/Home/img ...