#abc245g. G - Foreign Friends
G - Foreign Friends
Score : points
问题陈述
设有 名人物和 个国家,分别标记为人物 , 人物 , , 人物 和国家 , 国家 , , 国家 。 每个人物恰好属于一个国家:人物 属于国家 。此外,在这 名人物中有 名受欢迎的人物:人物 , 人物 , , 人物 是受欢迎的。最初,这 名人物中任意两人之间都不是朋友关系。
对于 对人物,神明 Takahashi 可以通过支付一定的代价使他们成为朋友:对于每个 ,他可以支付 的代价,使得人物 和人物 成为朋友。
现在,对于每 ,解决以下问题。
Takahashi 是否能够以最小的总代价使得人物 间接与一个来自不同国家的受欢迎人物建立朋友关系?如果可以做到,请找出所需的最小总成本。这里,当存在非负整数 和人物序列 且满足 、 以及人物 和人物 在 范围内都是朋友时,称人物 是人物 的间接朋友。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There are people and nations, labeled as Person , Person , , Person and Nation , Nation , , Nation , respectively.
Each person belongs to exactly one nation: Person belongs to Nation . Additionally, there are popular people among them: Person , Person , , Person are popular. Initially, no two of the people are friends.
For pairs of people, Takahashi, a God, can make them friends by paying a certain cost: for each , he can pay the cost of to make Person and Person friends.
Now, for each , solve the following problem.
Can Takahashi make Person an indirect friend of a popular person belonging to a nation different from that of Person ? If he can do so, find the minimum total cost needed. Here, Person is said to be an indirect friend of Person when there exists a non-negative integer and a sequence of people such that , , and Person and Person are friends for each .
Constraints
- if .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Let defined as follows: is if it is impossible to make Person an indirect friend of a popular person belonging to a nation different from that of Person ; otherwise, is the minimum total cost needed to do so. Print 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 , , , belong to Nation , , , , respectively, and there are two popular people: Person and . Here,
- For Person , the only popular person belonging to a different nation is Person . To make them indirect friends with the minimum cost, we should pay the cost of to make Person and friends and pay to make Person and friends, for a total of .
- For Person , the only popular person belonging to a different nation is Person . The minimum cost is achieved by making Person and friends by paying .
- For Person , the only popular person belonging to a different nation is Person . The minimum cost is achieved by making Person and friends by paying .
- For Person , the only popular person belonging to a different nation is Person . To make them indirect friends with the minimum cost, we should pay the cost of to make Person and friends and pay to make Person and friends, for a total of .
Sample Input 2
3 1 3 1
1 2 3
1
1 2 1000000000
Sample Output 2
-1 1000000000 -1
Note that, for Person , Person 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.
update @ 2024/3/10 10:32:32