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. PowerShell 反弹渗透技巧

    Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能,并且与现有的WSH保持向后兼容,因此它的脚本程序不仅能访 ...

  2. 怎样禁用浏览器的Cookie功能

    使用: window.navigator.cookieEnabled; window.navigator.cookieEnabled = true; 这样设置以后, 浏览器就不会接受和保存服务器传过来 ...

  3. hdu 1671 复习字典树

    #include<cstdio> #include<iostream> #include<string> #include<cstdlib> #defi ...

  4. springboot中的参数传递

    1.前端传递到后端 1-1.js function add(){ var obj = {}; obj.parame_empname = $("#EMPNAME").val(); i ...

  5. docker启动mysql 自定义配置文件

    命令行如下: docker run --name mysql56 -p : -v /home/mysql56/data:/var/lib/mysql -v /home/mysql56/conf:/et ...

  6. git基本操作及实用工具

    //git1.安装客户端git Git-2.9.3-rebase-i-64-bit.exe2.安装完成后打开git bashgit config --global user.name "li ...

  7. loj 2292「THUSC 2016」成绩单

    loj 看着就很区间dp,所以考虑求\(f_{i,j}\)表示区间\([i,j]\)的答案.注意到贡献答案的方式是每次选一个连续段,拿走后剩下的段拼起来继续段,所以转移就考虑从最后一次选的方法转移过来 ...

  8. Shiro——入门Demo

    Shiro——入门Demo 环境-  引入相关maven依赖, shiro-core,commons-logging 配置shiro配置文件:ini后缀 主方法测试: import org.apach ...

  9. 基于AccessToken方式实现API设计

    一.举例说明: 需求: A.B机构需要调用X服务器的接口,那么X服务器就需要提供开放的外网访问接口. 分析: 1.开放平台提供者X,为每一个合作机构提供对应的appid.app_secret. 2.a ...

  10. Graphics与Canvas

    Graphics: 1. java.awt.Graphics;2.android.graphics Canvas:1.java.awt.Canvas;2.android.graphics.Canvas ...