[ABC245G] Foreign Friends
Problem Statement
There are $N$ people and $K$ nations, labeled as Person $1$, Person $2$, $\ldots$, Person $N$ and Nation $1$, Nation $2$, $\ldots$, Nation $K$, respectively.
Each person belongs to exactly one nation: Person $i$ belongs to Nation $A_i$.
Additionally, there are $L$ popular people among them: Person $B_1$, Person $B_2$, $\ldots$, Person $B_L$ are popular.
Initially, no two of the $N$ people are friends.
For $M$ pairs of people, Takahashi, a God, can make them friends by paying a certain cost: for each $1\leq i\leq M$, he can pay the cost of $C_i$ to make Person $U_i$ and Person $V_i$ friends.
Now, for each $1\leq i\leq N$, solve the following problem.
Can Takahashi make Person $i$ an indirect friend of a popular person belonging to a nation different from that of Person $i$?
If he can do so, find the minimum total cost needed.
Here, Person $s$ is said to be an indirect friend of Person $t$ when there exists a non-negative integer $n$ and a sequence of people $(u_0, u_1, \ldots, u_n)$
such that $u_0=s$, $u_n=t$, and Person $u_i$ and Person $u_{i+1}$ are friends for each $0\leq i < n$.
Constraints
- $2 \leq N \leq 10^5$
- $1 \leq M \leq 10^5$
- $1 \leq K \leq 10^5$
- $1 \leq L \leq N$
- $1 \leq A_i \leq K$
- $1 \leq B_1<B_2<\cdots<B_L\leq N$
- $1\leq C_i\leq 10^9$
- $1\leq U_i<V_i\leq N$
- $(U_i, V_i)\neq (U_j,V_j)$ if $i \neq j$.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
$N$ $M$ $K$ $L$
$A_1$ $A_2$ $\cdots$ $A_N$
$B_1$ $B_2$ $\cdots$ $B_L$
$U_1$ $V_1$ $C_1$
$U_2$ $V_2$ $C_2$
$\vdots$
$U_M$ $V_M$ $C_M$
Output
Let $X_i$ defined as follows: $X_i$ is $-1$ if it is impossible to make Person $i$ an indirect friend of a popular person belonging to a nation different from that of Person $i$; otherwise, $X_i$ is the minimum total cost needed to do so.
Print $X_1, X_2, \ldots, X_N$ in one line, with spaces in between.
Sample Input 1
4 4 2 2
1 1 2 2
2 3
1 2 15
2 3 30
3 4 40
1 4 10
Sample Output 1
45 30 30 25
Person $1$, $2$, $3$, $4$ belong to Nation $1$, $1$, $2$, $2$, respectively, and there are two popular people: Person $2$ and $3$. Here,
- For Person $1$, the only popular person belonging to a different nation is Person $3$. To make them indirect friends with the minimum cost, we should pay the cost of $15$ to make Person $1$ and $2$ friends and pay $30$ to make Person $2$ and $3$ friends, for a total of $15+30=45$.
- For Person $2$, the only popular person belonging to a different nation is Person $3$. The minimum cost is achieved by making Person $2$ and $3$ friends by paying $30$.
- For Person $3$, the only popular person belonging to a different nation is Person $2$. The minimum cost is achieved by making Person $2$ and $3$ friends by paying $30$.
- For Person $4$, the only popular person belonging to a different nation is Person $2$. To make them indirect friends with the minimum cost, we should pay the cost of $15$ to make Person $1$ and $2$ friends and pay $10$ to make Person $1$ and $4$ friends, for a total of $15+10=25$.
Sample Input 2
3 1 3 1
1 2 3
1
1 2 1000000000
Sample Output 2
-1 1000000000 -1
Note that, for Person $1$, Person $1$ itself is indeed an indirect friend, but it does not belong to a different nation, so there is no popular person belonging to a different nation.
暴力怎么做?枚举每一种国籍 \(i\),考虑这个国籍的明星带给其他人的影响。建立一个虚拟节点,向每一个这个国籍的明星连一条0边。然后以 \(0\) 为起点跑一次 dijkstra,得到每一个点的最短路,而对于 \(a_i\ne a_j\) 的人,用当前跑出来的最短路更新其答案。复杂度 \(O(mk\log m)\)
为什么我们要枚举国籍?其实就只是为了保证 \(a_i\ne a_j\),这个作用有点废,考虑二进制优化,什么时候 \(a_i\ne a_j\) 呢?一定存在某一个二进制位不同。枚举每个二进制位,把虚拟节点向所有 \(a_i\) 的这个二进制位为 1 的节点连边,跑完之后,所有这一个二进制位为 0 的节点更新答案。复杂度将为 \(O(m \log^2m)\)
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
struct edge{
int v,nxt,w;
}e[N*3];
int hd[N],a[N],f[N],idx,u,v,w;
long long dis[N],ans[N];
void add_edge(int u,int v,int w)
{
e[++idx]=(edge){v,hd[u],w};
hd[u]=idx;
}
struct node{
int v;
long long w;
bool operator<(const node&n)const{
return w>n.w;
}
};
int n,m,k,l;
priority_queue<node>q;
inline void dijkstra()
{
memset(dis,0x7f,sizeof(dis));
q.push((node){0,0});
dis[0]=0;
while(!q.empty())
{
v=q.top().v;
if(dis[v]!=q.top().w)
{
q.pop();
continue;
}
q.pop();
for(int i=hd[v];i;i=e[i].nxt)
{
if(dis[v]+e[i].w<dis[e[i].v])
{
dis[e[i].v]=dis[v]+e[i].w;
q.push((node){e[i].v,dis[e[i].v]});
}
}
}
}
inline void calc(int i,int t)
{
for(int j=1;j<=n;j++)
if(((a[j]>>i&1)==t)&&f[j])
add_edge(0,j,0);
dijkstra();
hd[0]=0,idx=m<<1;
for(int j=1;j<=n;j++)
if(t!=(a[j]>>i&1))
ans[j]=min(ans[j],dis[j]);
}
int main()
{
memset(ans,0x7f,sizeof(ans));
scanf("%d%d%d%d",&n,&m,&k,&l);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
for(int i=1;i<=l;i++)
scanf("%d",&u),f[u]=1;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
add_edge(v,u,w);
}
for(int i=0;i<17;i++)
{
calc(i,0);
calc(i,1);
}
for(int i=1;i<=n;i++)
printf("%lld ",ans[i]>1e18? -1:ans[i]);
return 0;
}
[ABC245G] Foreign Friends的更多相关文章
- Constraint6:更新外键约束(Foreign Key Constraint)的引用列
在SQL Server中,表之间存在引用关系,引用关系通过创建外键约束(Foreign Key Constraint)实现.如果一个Table中的column被其他Table引用,那么该表是参考表,或 ...
- MySQL主从复制中断,报“Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217” 错误
前几天,发现从库挂了,具体报错信息如下: 分析思路 1. 因为我采用的是选择性复制,只针对以下几个库进行复制: card,upay,deal,monitor,collect.所以,不太可能出现对于sa ...
- ORA-02266: unique/primary keys in table referenced by enabled foreign keys
在数据库里面使用TRUNCATE命令截断一个表的数据时,遇到如下错误 SQL >TRUNCATE TABLE ESCMOWNER.SUBX_ITEM ORA-02266: unique/prim ...
- SQL Server 2008 R2——TRUNCATE TABLE 无法截断表 该表正由 FOREIGN KEY 约束引用
=================================版权声明================================= 版权声明:原创文章 禁止转载 请通过右侧公告中的“联系邮 ...
- 【MySQL】Create table 以及 foreign key 删表顺序考究。
1.以下是直接从数据库导出的建表语句. 1 -- ---------------------------- 2 -- Table structure for files 3 -- ---------- ...
- MYSQL外键(Foreign Key)的使用
在MySQL 3.23.44版本后,InnoDB引擎类型的表支持了外键约束.外键的使用条件:1.两个表必须是InnoDB表,MyISAM表暂时不支持外键(据说以后的版本有可能支持,但至少目前不支持): ...
- SQL PRIMARY KEY 约束\SQL FOREIGN KEY 约束\SQL CHECK 约束
SQL PRIMARY KEY 约束 PRIMARY KEY 约束唯一标识数据库表中的每条记录. 主键必须包含唯一的值. 主键列不能包含 NULL 值. 每个表都应该有一个主键,并且每个表只能有一个主 ...
- Netsuite > Foreign Currency Revaluation 外币评估
MENU: Transactions > Financial > Revalue Open Currency Balances 使用频率: - 每个月月底,结账前, 手工操作. - 或者在 ...
- 无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用。
在删除northwindcs表时,发生报错,消息 3726,级别 16,状态 1,第 2 行,无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用.此时判断是因为有其他表的外键 ...
- 解决Cannot delete or update a parent row: a foreign key constraint fails (`current_source_products`.`product_storage`, CONSTRAINT `product_storage_ibfk_3` FOREIGN KEY (`InOperatorID`)
出现这个异常的原因是因为设置了外键,造成无法更新或删除数据. 1.通过设置FOREIGN_KEY_CHECKS变量来避免这种情况 删除前设置 SET FOREIGN_KEY_CHECKS=0; 删除完 ...
随机推荐
- java与es8实战之二:实战前的准备工作
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本篇是<java与es8实战>系 ...
- PHP写一个 Api接口需要注意哪些?考虑哪些?
随着互联网的飞速发展,前后端分离的开发模式越来越流行.编写一个稳定.可靠和易于使用的 API 接口是现代互联网应用程序的关键.本文将介绍在使用 thinkphp6 框架开发 API 接口时需要注意的要 ...
- ThreadLocal:线程中的全局变量
最近接了一个新需求,业务场景上需要在原有基础上新增2个字段,接口新增参数意味着很多类和方法的逻辑都需要改变,需要先判断是否属于该业务场景,再做对应的逻辑.原本的打算是在入口处新增变量,在操作数据的时候 ...
- 面霸的自我修养:volatile专题
王有志,一个分享硬核Java技术的互金摸鱼侠 加入Java人的提桶跑路群:共同富裕的Java人 今天是<面霸的自我修养>第4篇文章,我们一起来看看面试中会问到哪些关于volatile的问题 ...
- ipa客户端安装
ipa客户端安装 安装操作官网 非交互式安装IPa客户端 kinit admin ipa host-find ipa host-add ipa host-add --help ipa host-add ...
- LeetCode买卖股票之一:基本套路(122)
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 关于<LeetCode买卖股票>系列 在L ...
- day02 数据类型转换 运算符 方法
数据类型转换 自动类型转换 强制类型转换 1. 自动类型转换:就是范围小的向范围大的转换 将取值范围小刀的类型自动提升为取值范围大的类型. 转换规则 byte.short.char int--- ...
- 用go封装一下二级认证功能
用go封装一下二级认证 本篇为用go设计开发一个自己的轻量级登录库/框架吧 - 秋玻 - 博客园 (cnblogs.com)的二级认证业务篇,会讲讲二级认证业务的实现,给库/框架增加新的功能. 源码: ...
- 轻松掌握组件启动之MongoDB(番外篇):高可用复制集架构环境搭建-mtools
引言 在前两章节中,我们详细讲解了如何手动配置启动MongoDB.然而,现在有许多不同的工具可以帮助我们更方便地启动和创建MongoDB数据库.因此,今天我将介绍一个名为mtools的开源项目,它可以 ...
- 前端CSS五中元素定位类型
元素想通过底部.顶部.左侧.右侧属性定位是必须先设定position的属性值 posistion属性的五个值:static.relative.fixed.absoulte.sticky static定 ...