atcoder#ABC245G. [ABC245G] Foreign Friends
[ABC245G] Foreign Friends
Score : points
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
- $1 \leq B_1
- $1\leq U_i
- 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.
4 4 2 2
1 1 2 2
2 3
1 2 15
2 3 30
3 4 40
1 4 10
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 .
3 1 3 1
1 2 3
1
1 2 1000000000
-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.