import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.util.TreeSet; public class fio { public static void main(String[] args) { try{ BufferedReader buffReader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(buffReader.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int m = Integer.parseInt(tokenizer.nextToken()); int k = Integer.parseInt(tokenizer.nextToken()); tokenizer = new StringTokenizer(buffReader.readLine()); int[] weights = new int[n]; long weightsSumStart = 0; for(int i=0; i<n; i++){ weights[i] = Integer.parseInt(tokenizer.nextToken()); weightsSumStart += weights[i]; } int[][] canisterOrder = new int[m][2]; for(int j=0; j<m; j++){ tokenizer = new StringTokenizer(buffReader.readLine()); canisterOrder[j][0] = Integer.parseInt(tokenizer.nextToken())-1; canisterOrder[j][1] = Integer.parseInt(tokenizer.nextToken())-1; } ArrayList<TreeSet<Integer>> canisterRreactions = new ArrayList<TreeSet<Integer>>(n); for(int i=0; i<n; i++){ canisterRreactions.add(new TreeSet<Integer>()); } int[][] reactionCompounds = new int[k][2]; for(int l=0; l<k; l++){ tokenizer = new StringTokenizer(buffReader.readLine()); int fistCompound = Integer.parseInt(tokenizer.nextToken())-1; int secondCompound = Integer.parseInt(tokenizer.nextToken())-1; reactionCompounds[l][0] = fistCompound; reactionCompounds[l][1] = secondCompound; canisterRreactions.get(fistCompound).add(l); canisterRreactions.get(secondCompound).add(l); } for(int j=0; j<m; j++){ int fistCanister = canisterOrder[j][0]; int secondCanister = canisterOrder[j][1]; TreeSet<Integer> fistReactions = canisterRreactions.get(fistCanister); TreeSet<Integer> secondReactions = canisterRreactions.get(secondCanister); TreeSet<Integer> base; TreeSet<Integer> added; if(fistReactions.size() > secondReactions.size()){ base = fistReactions; added = secondReactions; }else{ base = secondReactions; added = fistReactions; } for(int reaction : added){ int fistCompound = reactionCompounds[reaction][0]; int secondCompound = reactionCompounds[reaction][1]; int reactionWeight = Math.min(weights[fistCompound], weights[secondCompound]); if(reactionWeight > 0){ if(base.remove(reaction)){ weights[fistCompound] -= reactionWeight; weights[secondCompound] -= reactionWeight; }else{ base.add(reaction); } } } canisterRreactions.set(fistCanister, null); canisterRreactions.set(secondCanister, base); } long weightsSumEnd = 0; for(int i=0; i<n; i++){ weightsSumEnd += weights[i]; } System.out.println(weightsSumStart - weightsSumEnd); }catch(Exception e){ e.printStackTrace(); } } }
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 | import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.util.TreeSet; public class fio { public static void main(String[] args) { try{ BufferedReader buffReader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(buffReader.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int m = Integer.parseInt(tokenizer.nextToken()); int k = Integer.parseInt(tokenizer.nextToken()); tokenizer = new StringTokenizer(buffReader.readLine()); int[] weights = new int[n]; long weightsSumStart = 0; for(int i=0; i<n; i++){ weights[i] = Integer.parseInt(tokenizer.nextToken()); weightsSumStart += weights[i]; } int[][] canisterOrder = new int[m][2]; for(int j=0; j<m; j++){ tokenizer = new StringTokenizer(buffReader.readLine()); canisterOrder[j][0] = Integer.parseInt(tokenizer.nextToken())-1; canisterOrder[j][1] = Integer.parseInt(tokenizer.nextToken())-1; } ArrayList<TreeSet<Integer>> canisterRreactions = new ArrayList<TreeSet<Integer>>(n); for(int i=0; i<n; i++){ canisterRreactions.add(new TreeSet<Integer>()); } int[][] reactionCompounds = new int[k][2]; for(int l=0; l<k; l++){ tokenizer = new StringTokenizer(buffReader.readLine()); int fistCompound = Integer.parseInt(tokenizer.nextToken())-1; int secondCompound = Integer.parseInt(tokenizer.nextToken())-1; reactionCompounds[l][0] = fistCompound; reactionCompounds[l][1] = secondCompound; canisterRreactions.get(fistCompound).add(l); canisterRreactions.get(secondCompound).add(l); } for(int j=0; j<m; j++){ int fistCanister = canisterOrder[j][0]; int secondCanister = canisterOrder[j][1]; TreeSet<Integer> fistReactions = canisterRreactions.get(fistCanister); TreeSet<Integer> secondReactions = canisterRreactions.get(secondCanister); TreeSet<Integer> base; TreeSet<Integer> added; if(fistReactions.size() > secondReactions.size()){ base = fistReactions; added = secondReactions; }else{ base = secondReactions; added = fistReactions; } for(int reaction : added){ int fistCompound = reactionCompounds[reaction][0]; int secondCompound = reactionCompounds[reaction][1]; int reactionWeight = Math.min(weights[fistCompound], weights[secondCompound]); if(reactionWeight > 0){ if(base.remove(reaction)){ weights[fistCompound] -= reactionWeight; weights[secondCompound] -= reactionWeight; }else{ base.add(reaction); } } } canisterRreactions.set(fistCanister, null); canisterRreactions.set(secondCanister, base); } long weightsSumEnd = 0; for(int i=0; i<n; i++){ weightsSumEnd += weights[i]; } System.out.println(weightsSumStart - weightsSumEnd); }catch(Exception e){ e.printStackTrace(); } } } |