[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; 删除完 ...
随机推荐
- mall :sa-token项目源码解析
目录 一.mall开源项目 1.1 来源 1.2 项目转移 1.3 项目克隆 二.Sa-Toekn框架 2.1 Sa-Token 简介 2.2 分布式后端项目的使用流程 2.3 分布式后端项目的使用场 ...
- Flutter系列文章-Flutter 插件开发
在本篇文章中,我们将学习如何开发 Flutter 插件,实现 Flutter 与原生平台的交互.我们将详细介绍插件的开发过程,包括如何创建插件项目.实现方法通信.处理异步任务等.最后,我们还将演示如何 ...
- LeetCode46全排列(回溯入门)
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 题目描述 难度:中等 给定一个不含重复数字的数组 nu ...
- 如何做一个完美的api接口?
如何做一个api接口?:我们知道API其实就是应用程序编程接口,可以把它理解为是一种通道,用来和不同软件系统间进行通信,本质上它是预先定义的函数:-api,接口 1 我们知道API其实就是应用程序编程 ...
- VR国标
<软件基本要求与测试方法>
- React仿大众点评外卖app
主要使用技术: react react-router4 redux: action.reducer.store管理数据 fetch: 进行数据交互 prismjs : 页面嵌入代码,高亮显示插件 bu ...
- 斜率优化DP 学习笔记
斜率优化 DP 适用情况 适用于求解最优解(最大.最小)问题. 上凸壳与下凸壳 求解步骤 对于任意状态转义方程,设 \(A_i\),\(B_i\),使状态转移方程转化为 \(f_i = \min(f_ ...
- 5.0 CRC32校验技术概述
CRC校验技术是用于检测数据传输或存储过程中是否出现了错误的一种方法,校验算法可以通过计算应用与数据的循环冗余校验(CRC)检验值来检测任何数据损坏.通过运用本校验技术我们可以实现对特定内存区域以及磁 ...
- 🖖少年,该升级 Vue3 了!
你好,我是 Kagol. 前言 根据 Vue 官网文档的说明,Vue2 的终止支持时间是 2023 年 12 月 31 日,这意味着从明年开始: Vue2 将不再更新和升级新版本,不再增加新特性,不再 ...
- 在 Net7.0环境下通过反射创建泛型实例和调用泛型方法
一.介绍 最近没事干,就用闲暇时间写点东西,也记录一下温习历程.老人说的好,好记性,不如烂笔头.时间一长,当时记忆的再清楚,都会变得模糊,索性就写博客记录下来,如果下次需要,直接打开博客就找到了,不用 ...