import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class fio { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer; long resHalf = 0; Map<Integer, List<Integer>> pairsMap = new HashMap<Integer, List<Integer>>(); //numer_substancji, index_reakcji Map<Integer, Map<Integer, Integer>> vialsMap = new HashMap<Integer, Map<Integer, Integer>>(); //numer_fiolki, numer_substancji, ilosc_substancji tokenizer = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int m = Integer.parseInt(tokenizer.nextToken()); int k = Integer.parseInt(tokenizer.nextToken()); int[] vials = new int[n]; tokenizer = new StringTokenizer(reader.readLine()); for(int i = 0; i < n; i++) { vials[i] = Integer.parseInt(tokenizer.nextToken()); pairsMap.put(i, new ArrayList<Integer>()); vialsMap.put(i, new HashMap<Integer, Integer>()); if(vials[i] > 0) { vialsMap.get(i).put(i, vials[i]); } } int[][] steps = new int[m][2]; for(int i = 0; i < m; i++) { tokenizer = new StringTokenizer(reader.readLine()); steps[i][0] = Integer.parseInt(tokenizer.nextToken())-1; steps[i][1] = Integer.parseInt(tokenizer.nextToken())-1; } int[][] pairs = new int[k*2][2]; for(int i = 0; i < k; i++) { tokenizer = new StringTokenizer(reader.readLine()); int a = Integer.parseInt(tokenizer.nextToken())-1; int b = Integer.parseInt(tokenizer.nextToken())-1; pairs[i*2][0] = a; pairs[i*2][1] = b; pairs[i*2+1][0] = b; pairs[i*2+1][1] = a; pairsMap.get(a).add(i*2); pairsMap.get(b).add(i*2+1); } for(int i = 0; i < m; i++) { TreeSet<Integer> planedReactions = new TreeSet<Integer>(); int from = steps[i][0]; int to = steps[i][1]; Map<Integer, Integer> smaller = vialsMap.get(from); Map<Integer, Integer> bigger = vialsMap.get(to); if(smaller.size() > bigger.size()) { Map<Integer, Integer> swapTmp = smaller; smaller = bigger; bigger = swapTmp; } for(int c : smaller.keySet()) { int volume = smaller.get(c); List<Integer> reactants = pairsMap.get(c); Iterator<Integer> reactantsIterator = reactants.iterator(); while(reactantsIterator.hasNext()) { int reactionIdx = reactantsIterator.next(); int elem = pairs[reactionIdx][1]; if(bigger.containsKey(elem)) { planedReactions.add(reactionIdx); } } bigger.put(c, volume); } for(Integer reactionIdx : planedReactions) { int a = pairs[reactionIdx][0]; int b = pairs[reactionIdx][1]; Integer aVolume = bigger.get(a); Integer bVolume = bigger.get(b); if(aVolume != null && bVolume != null) { int volume = Math.min(aVolume, bVolume); resHalf += volume; if(aVolume == volume) { bigger.remove(a); } else { bigger.put(a, aVolume-volume); } if(bVolume == volume) { bigger.remove(b); } else { bigger.put(b, bVolume-volume); } } } vialsMap.put(from, null); vialsMap.put(to, bigger); } System.out.println(resHalf*2); } }
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class fio { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer; long resHalf = 0; Map<Integer, List<Integer>> pairsMap = new HashMap<Integer, List<Integer>>(); //numer_substancji, index_reakcji Map<Integer, Map<Integer, Integer>> vialsMap = new HashMap<Integer, Map<Integer, Integer>>(); //numer_fiolki, numer_substancji, ilosc_substancji tokenizer = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int m = Integer.parseInt(tokenizer.nextToken()); int k = Integer.parseInt(tokenizer.nextToken()); int[] vials = new int[n]; tokenizer = new StringTokenizer(reader.readLine()); for(int i = 0; i < n; i++) { vials[i] = Integer.parseInt(tokenizer.nextToken()); pairsMap.put(i, new ArrayList<Integer>()); vialsMap.put(i, new HashMap<Integer, Integer>()); if(vials[i] > 0) { vialsMap.get(i).put(i, vials[i]); } } int[][] steps = new int[m][2]; for(int i = 0; i < m; i++) { tokenizer = new StringTokenizer(reader.readLine()); steps[i][0] = Integer.parseInt(tokenizer.nextToken())-1; steps[i][1] = Integer.parseInt(tokenizer.nextToken())-1; } int[][] pairs = new int[k*2][2]; for(int i = 0; i < k; i++) { tokenizer = new StringTokenizer(reader.readLine()); int a = Integer.parseInt(tokenizer.nextToken())-1; int b = Integer.parseInt(tokenizer.nextToken())-1; pairs[i*2][0] = a; pairs[i*2][1] = b; pairs[i*2+1][0] = b; pairs[i*2+1][1] = a; pairsMap.get(a).add(i*2); pairsMap.get(b).add(i*2+1); } for(int i = 0; i < m; i++) { TreeSet<Integer> planedReactions = new TreeSet<Integer>(); int from = steps[i][0]; int to = steps[i][1]; Map<Integer, Integer> smaller = vialsMap.get(from); Map<Integer, Integer> bigger = vialsMap.get(to); if(smaller.size() > bigger.size()) { Map<Integer, Integer> swapTmp = smaller; smaller = bigger; bigger = swapTmp; } for(int c : smaller.keySet()) { int volume = smaller.get(c); List<Integer> reactants = pairsMap.get(c); Iterator<Integer> reactantsIterator = reactants.iterator(); while(reactantsIterator.hasNext()) { int reactionIdx = reactantsIterator.next(); int elem = pairs[reactionIdx][1]; if(bigger.containsKey(elem)) { planedReactions.add(reactionIdx); } } bigger.put(c, volume); } for(Integer reactionIdx : planedReactions) { int a = pairs[reactionIdx][0]; int b = pairs[reactionIdx][1]; Integer aVolume = bigger.get(a); Integer bVolume = bigger.get(b); if(aVolume != null && bVolume != null) { int volume = Math.min(aVolume, bVolume); resHalf += volume; if(aVolume == volume) { bigger.remove(a); } else { bigger.put(a, aVolume-volume); } if(bVolume == volume) { bigger.remove(b); } else { bigger.put(b, bVolume-volume); } } } vialsMap.put(from, null); vialsMap.put(to, bigger); } System.out.println(resHalf*2); } } |