import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; public final class fio { private static final long MULTIPLIER = 262_144L; public static InputStream INPUT_STREAM = System.in; public static PrintStream PRINT_STREAM = System.out; private static int[] weight; private static Map<Integer, Set<Integer>> vialContent; private static Map<Long, Integer> reactionSmart; private static int[][] steps; private static long result = 0; private static final class Pair { Integer src; Integer dest; public Pair(Integer src, Integer dest) { this.src = src; this.dest = dest; } } private static long computeKey(long c1, long c2) { if (c1 < c2) { return MULTIPLIER * c1 + c2; } else { return MULTIPLIER * c2 + c1; } } public static void main(String[] args) throws NumberFormatException, IOException { result = 0; BufferedReader br = new BufferedReader(new InputStreamReader(INPUT_STREAM)); StringTokenizer st = new StringTokenizer(br.readLine()); int vialNumber = Integer.parseInt(st.nextToken()); int stepNumber = Integer.parseInt(st.nextToken()); int reactionNumber = Integer.parseInt(st.nextToken()); weight = new int[vialNumber + 1]; vialContent = new HashMap<Integer, Set<Integer>>(); st = new StringTokenizer(br.readLine()); for (int t = 1; t < weight.length; t++) { weight[t] = Integer.parseInt(st.nextToken()); Set<Integer> s = new HashSet<Integer>(); s.add(t); vialContent.put(t, s); } steps = new int[stepNumber][2]; for (int t = 0; t < stepNumber; t++) { st = new StringTokenizer(br.readLine()); steps[t][0] = Integer.parseInt(st.nextToken()); steps[t][1] = Integer.parseInt(st.nextToken()); } reactionSmart = new HashMap<Long, Integer>(); for (int t = 1; t < reactionNumber + 1; t++) { st = new StringTokenizer(br.readLine()); long s1 = Long.parseLong(st.nextToken()); long s2 = Long.parseLong(st.nextToken()); reactionSmart.put(computeKey(s1, s2), t); } solve(); result = result * 2L; PRINT_STREAM.println(result); } private static void solve() { for (int i = 0; i < steps.length; i++) { Map<Integer, Pair> toExecute = new TreeMap<Integer, Pair>(); Set<Integer> srcContent = vialContent.get(steps[i][0]); Set<Integer> destContent = vialContent.get(steps[i][1]); for (Integer c1 : srcContent) { for (Integer c2 : destContent) { long key = computeKey(c1, c2); if (reactionSmart.containsKey(key)) { toExecute.put(reactionSmart.get(key), new Pair(c1, c2)); } } } if (!toExecute.isEmpty()) { for (Integer id : toExecute.keySet()) { Pair pairFire = toExecute.get(id); int destroy = Math.min(weight[pairFire.src], weight[pairFire.dest]); result += destroy; weight[pairFire.src] -= destroy; weight[pairFire.dest] -= destroy; } } destContent.addAll(srcContent); if (!toExecute.isEmpty()) { for (Iterator<Integer> it = destContent.iterator(); it.hasNext();) { if (weight[it.next()] == 0) { it.remove(); } } } } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; public final class fio { private static final long MULTIPLIER = 262_144L; public static InputStream INPUT_STREAM = System.in; public static PrintStream PRINT_STREAM = System.out; private static int[] weight; private static Map<Integer, Set<Integer>> vialContent; private static Map<Long, Integer> reactionSmart; private static int[][] steps; private static long result = 0; private static final class Pair { Integer src; Integer dest; public Pair(Integer src, Integer dest) { this.src = src; this.dest = dest; } } private static long computeKey(long c1, long c2) { if (c1 < c2) { return MULTIPLIER * c1 + c2; } else { return MULTIPLIER * c2 + c1; } } public static void main(String[] args) throws NumberFormatException, IOException { result = 0; BufferedReader br = new BufferedReader(new InputStreamReader(INPUT_STREAM)); StringTokenizer st = new StringTokenizer(br.readLine()); int vialNumber = Integer.parseInt(st.nextToken()); int stepNumber = Integer.parseInt(st.nextToken()); int reactionNumber = Integer.parseInt(st.nextToken()); weight = new int[vialNumber + 1]; vialContent = new HashMap<Integer, Set<Integer>>(); st = new StringTokenizer(br.readLine()); for (int t = 1; t < weight.length; t++) { weight[t] = Integer.parseInt(st.nextToken()); Set<Integer> s = new HashSet<Integer>(); s.add(t); vialContent.put(t, s); } steps = new int[stepNumber][2]; for (int t = 0; t < stepNumber; t++) { st = new StringTokenizer(br.readLine()); steps[t][0] = Integer.parseInt(st.nextToken()); steps[t][1] = Integer.parseInt(st.nextToken()); } reactionSmart = new HashMap<Long, Integer>(); for (int t = 1; t < reactionNumber + 1; t++) { st = new StringTokenizer(br.readLine()); long s1 = Long.parseLong(st.nextToken()); long s2 = Long.parseLong(st.nextToken()); reactionSmart.put(computeKey(s1, s2), t); } solve(); result = result * 2L; PRINT_STREAM.println(result); } private static void solve() { for (int i = 0; i < steps.length; i++) { Map<Integer, Pair> toExecute = new TreeMap<Integer, Pair>(); Set<Integer> srcContent = vialContent.get(steps[i][0]); Set<Integer> destContent = vialContent.get(steps[i][1]); for (Integer c1 : srcContent) { for (Integer c2 : destContent) { long key = computeKey(c1, c2); if (reactionSmart.containsKey(key)) { toExecute.put(reactionSmart.get(key), new Pair(c1, c2)); } } } if (!toExecute.isEmpty()) { for (Integer id : toExecute.keySet()) { Pair pairFire = toExecute.get(id); int destroy = Math.min(weight[pairFire.src], weight[pairFire.dest]); result += destroy; weight[pairFire.src] -= destroy; weight[pairFire.dest] -= destroy; } } destContent.addAll(srcContent); if (!toExecute.isEmpty()) { for (Iterator<Integer> it = destContent.iterator(); it.hasNext();) { if (weight[it.next()] == 0) { it.remove(); } } } } } } |