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的更多相关文章

  1. Constraint6:更新外键约束(Foreign Key Constraint)的引用列

    在SQL Server中,表之间存在引用关系,引用关系通过创建外键约束(Foreign Key Constraint)实现.如果一个Table中的column被其他Table引用,那么该表是参考表,或 ...

  2. 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 ...

  3. ORA-02266: unique/primary keys in table referenced by enabled foreign keys

    在数据库里面使用TRUNCATE命令截断一个表的数据时,遇到如下错误 SQL >TRUNCATE TABLE ESCMOWNER.SUBX_ITEM ORA-02266: unique/prim ...

  4. SQL Server 2008 R2——TRUNCATE TABLE 无法截断表 该表正由 FOREIGN KEY 约束引用

    =================================版权声明================================= 版权声明:原创文章 禁止转载  请通过右侧公告中的“联系邮 ...

  5. 【MySQL】Create table 以及 foreign key 删表顺序考究。

    1.以下是直接从数据库导出的建表语句. 1 -- ---------------------------- 2 -- Table structure for files 3 -- ---------- ...

  6. MYSQL外键(Foreign Key)的使用

    在MySQL 3.23.44版本后,InnoDB引擎类型的表支持了外键约束.外键的使用条件:1.两个表必须是InnoDB表,MyISAM表暂时不支持外键(据说以后的版本有可能支持,但至少目前不支持): ...

  7. SQL PRIMARY KEY 约束\SQL FOREIGN KEY 约束\SQL CHECK 约束

    SQL PRIMARY KEY 约束 PRIMARY KEY 约束唯一标识数据库表中的每条记录. 主键必须包含唯一的值. 主键列不能包含 NULL 值. 每个表都应该有一个主键,并且每个表只能有一个主 ...

  8. Netsuite > Foreign Currency Revaluation 外币评估

    MENU: Transactions > Financial > Revalue Open Currency Balances 使用频率: - 每个月月底,结账前, 手工操作. - 或者在 ...

  9. 无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用。

    在删除northwindcs表时,发生报错,消息 3726,级别 16,状态 1,第 2 行,无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用.此时判断是因为有其他表的外键 ...

  10. 解决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; 删除完 ...

随机推荐

  1. 深度学习(十二)——神经网络:搭建小实战和Sequential的使用

    一.torch.nn.Sequential代码栗子 官方文档:Sequential - PyTorch 2.0 documentation # Using Sequential to create a ...

  2. 《Web安全基础》02. 信息收集

    @ 目录 1:CDN 绕过 1.1:判断是否有 CDN 服务 1.2:常见绕过方法 1.3:相关资源 2:网站架构 3:WAF 4:APP 及其他资产 5:资产监控 本系列侧重方法论,各工具只是实现目 ...

  3. Redis的五大数据类型的数据结构

    概述   Redis底层有六种数据类型包括:简单动态字符串.双向链表.压缩列表.哈希表.跳表和整数数组.这六种数据结构五大数据类型关系如下: String:简单动态字符串 List:双向链表.压缩列表 ...

  4. 树状数组复习 leetcode 307

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  5. 《Python魔法大冒险》 001 序章:少年小鱼的不平凡一天

    在一个普通的城市里,生活着一个名叫小鱼的少年.他是一名初中生,但在班级里,他的学习成绩总是垫底.同学们经常取笑他,有时甚至戏称他为"倒数王". 放学后,小鱼一个人走在回家的路上,他 ...

  6. [python] 在ubuntu中, 如何运行指定位置的py程序

    首先打开终端 Ctrl + Alt + T 运行py程序 进入py文件所在目录 (例如: cd 桌面) 运行py程序: python a.py

  7. 文心一言 VS 讯飞星火 VS chatgpt (92)-- 算法导论8.3 5题

    五.用go语言,在本节给出的第一个卡片排序算法中,为排序 d 位十进制数,在最坏情况下需要多少轮排序?在最坏情况下,操作员需要记录多少堆卡片? 文心一言: 你提到的第一个卡片排序算法可能是指的基数排序 ...

  8. mpi转以太网连接200plc以太网监控同时与步科触摸屏通信

    西门子PLC200 226PLC转以太网通过PPI-ETH-XD1.0集中采集不占用编程口同时与步科触摸屏通信 现有设备及联网要求客户车间内有6台纺机设备,控制系统采用西门子PLC,型号为CPU226 ...

  9. 记录一次gcc的编译

    在deepin23上编译gcc13.2 deepin20素以软件版本过老被人诟病,换成最新的deepin23之后情况大有好转,但是gcc版本为11.2,鄙人对此仍有意见,所以特意研究了如何编译一份较新 ...

  10. 【PHP正则表达式】

    [PHP正则表达式] 最近写题总是遇到php正则表达式的匹配函数,于是进行一个总结. 1.什么是正则表达式 是php在进行搜索时用于匹配的模式字符串.一般用于php对特定字符序列的替换和搜索. 2.正 ...