Bessie is located in a network consisting of?NN?(2≤N≤1052≤N≤105) vertices labeled?1…N1…N?and?2N2N?portals labeled?1…2N1…2N. Each portal connects two distinct vertices?uu?and?vv?(u≠vu≠v). Multiple portals may connect the same pair of vertices.
Each vertex?vv?is adjacent to four distinct portals. The list of portals that?vv?is adjacent to is given by?pv=[pv,1,pv,2,pv,3,pv,4]pv=[pv,1,pv,2,pv,3,pv,4].
Your current location can be represented by an ordered pair?(current vertex,current portal)(current vertex,current portal); that is, a pair?(v,pv,i)(v,pv,i)?where?1≤v≤N1≤v≤N?and?1≤i≤41≤i≤4. You may use either of the following operations to change your current location:
There are?4N4N?distinct locations in total. Unfortunately, it might not be the case that every location is reachable from every other via a sequence of operations. Thus, for a cost of?cvcv?(1≤cv≤10001≤cv≤1000) moonies, you may permute the list of portals adjacent to?vv?in any order you choose. After this, the first two portals in the list are paired up, while the last two portals in the list are also paired up.
For example, if you permute the portals adjacent to?vv?in the order?[pv,3,pv,1,pv,2,pv,4][pv,3,pv,1,pv,2,pv,4], this means that if you are at vertex?vv,
Compute the minimum total amount of moonies required to modify the network in order to make it possible to reach every possible location from every other location. It is guaranteed that the test data is constructed in such a way that there exists at least one valid way of modifying the network.
The first line contains?NN.The next?NN?lines each describe a vertex. Line?v+1v+1?contains five space-separated integers?cv,pv,1,pv,2,pv,3,pv,4cv,pv,1,pv,2,pv,3,pv,4.
It is guaranteed that for each?vv?pv,1,pv,2,pv,3,pv,4pv,1,pv,2,pv,3,pv,4?are all distinct, and that every portal appears in the adjacency lists of exactly two vertices.
A single line containing the minimum total amount of moonies required to modify the network in order to make it possible to reach every possible location from every other location.
5 10 1 4 8 9 11 1 2 5 6 12 9 10 2 3 3 4 3 6 7 15 10 8 7 5
13
It suffices to permute the adjacency lists of vertices?11?and?44. This requires a total of?c1+c4=13c1+c4=13?moonies. We can let?p1=[1,9,4,8]p1=[1,9,4,8]?and?p4=[7,4,6,3]p4=[7,4,6,3].
Problem credits: Benjamin Qi
[/hide]
(Analysis by Benjamin Qi)
Consider a new undirected multigraph?GG?with?2N2N?vertices such that
Every vertex in?GG?has degree exactly two, so?GG?is a union of disjoint cycles. The goal is to join all vertices in?GG?into a single cycle.
Suppose that portals?pv,0pv,0?and?pv,1pv,1?are not contained within the same cycle as?pv,2pv,2?and?pv,3pv,3?in?GG. Then if we permute the portals adjacent to vertex?vv?so that the adjacency list is now?pv,0,pv,2,pv,1,pv,3pv,0,pv,2,pv,1,pv,3, this will combine all of?pv,0,pv,1,pv,2,pv,0,pv,1,pv,2,?and?pv,3pv,3?into a single cycle. In other words, every vertex has the potential to unite two cycles.
If we replace all occurrences of "cycle" above with "connected component," then it's clear that we're looking for a?minimum spanning tree.
Specifically, the answer is the cost of the minimum spanning tree of?G′G′, where?G′G′?has the same vertex set as?GG?and the following edges and costs:
The minimum spanning tree can be found using Kruskal's algorithm.
Danny Mittal's code:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.StringTokenizer; public class Portals { static int[] union; static int find(int u) { if (union[union[u]] != union[u]) { union[u] = find(union[u]); } return union[u]; } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); union = new int[(2 * n) + 1]; for (int p = 1; p <= 2 * n; p++) { union[p] = p; } List<Edge> edges = new ArrayList<>(); for (int a = 1; a <= n; a++) { StringTokenizer tokenizer = new StringTokenizer(in.readLine()); int cost = Integer.parseInt(tokenizer.nextToken()); int[] portals = new int[4]; for (int j = 0; j < 4; j++) { portals[j] = Integer.parseInt(tokenizer.nextToken()); } edges.add(new Edge(portals[0], portals[1], 0)); edges.add(new Edge(portals[2], portals[3], 0)); edges.add(new Edge(portals[3], portals[0], cost)); } edges.sort(Comparator.comparingInt(edge -> edge.cost)); int answer = 0; for (Edge edge : edges) { int u = find(edge.a); int v = find(edge.b); if (u != v) { answer += edge.cost; union[u] = v; } } System.out.println(answer); } static class Edge { final int a; final int b; final int cost; Edge(int a, int b, int cost) { this.a = a; this.b = b; this.cost = cost; } } }
[/hide]
? 2025. All Rights Reserved. 沪ICP备2023009024号-1