horiuchi #5504(2008/01/28 02:27 GMT) [ Java ] Rating0/0=0.00
単純に全探索で求めてみました。
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
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; public class Sample136 { private final Map<Graph, Integer> feeMap_ = new HashMap<Graph, Integer>(); private final Map<Integer, List<Integer>> routeMap_ = new HashMap<Integer, List<Integer>>(); public String[] calc(Reader reader) throws IOException { List<String> result = new ArrayList<String>(); BufferedReader br = new BufferedReader(reader); br.readLine(); // 最初の行は読み飛ばす(エラーチェックは必要?) for (String line = br.readLine(); line != null; line = br.readLine()) { String[] values = line.split("\\s+"); if (values[0].equals("0") && values.length == 3) { List<Integer> searched = searchGraph(Integer.parseInt(values[1]), Integer.parseInt(values[2]), new HashSet<Integer>()); Collections.sort(searched); result.add(String.valueOf(searched.get(0))); } else if (values[0].equals("1") && values.length == 4) { addGraph(Integer.parseInt(values[1]), Integer.parseInt(values[2]), Integer.parseInt(values[3])); } else { throw new IllegalArgumentException(line); } } return result.toArray(new String[0]); } private void addGraph(int from, int to, int fee) { Graph graph = new Graph(from, to); Integer integer = feeMap_.get(graph); if (integer == null) { feeMap_.put(graph, fee); } else { if (integer.intValue() > fee) { feeMap_.put(graph, fee); } } addRoute(from, to); addRoute(to, from); } private void addRoute(int from, int to) { List<Integer> list = routeMap_.get(from); if (list == null) { list = new ArrayList<Integer>(); routeMap_.put(from, list); } list.add(to); } private List<Integer> searchGraph(int from, int to, Set<Integer> searchedNodes) { List<Integer> result = new ArrayList<Integer>(); List<Integer> list = routeMap_.get(from); if (list == null) return result; searchedNodes.add(from); searchedNodes.add(to); if (list.contains(to)) { result.add( feeMap_.get(new Graph(from, to)) ); } for (int node: list) { if (searchedNodes.contains(node)) continue; List<Integer> searchResult = searchGraph(node, to, new HashSet<Integer>(searchedNodes)); for (int search: searchResult) { result.add( search + feeMap_.get(new Graph(from, node)) ); } } return result; } public static class Graph { public final int from; public final int to; public Graph(int from, int to) { if (from < to) { this.from = from; this.to = to; } else { this.from = to; this.to = from; } } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Graph)) return false; Graph other = (Graph) obj; return this.from == other.from && this.to == other.to; } @Override public int hashCode() { return Integer.valueOf((from - 1) * 100 + (to - 1)).hashCode(); } } public static void main(String[] args) { Sample136 sample = new Sample136(); StringReader reader = new StringReader( "5 16\n" + "1 1 2 343750\n" + "1 1 3 3343\n" + "1 1 4 347392\n" + "1 1 5 5497\n" + "1 2 3 123394\n" + "1 2 4 545492\n" + "1 2 5 458\n" + "1 3 4 343983\n" + "1 3 5 843468\n" + "1 4 5 15934\n" + "0 2 1\n" + "0 4 1\n" + "0 3 2\n" + "0 4 2\n" + "0 4 3\n" + "0 5 3\n" ); try { String[] result = sample.calc(reader); for (String line: result) { System.out.println(line); } } catch (IOException ex) { ex.printStackTrace(); } } }
Rating0/0=0.00-0+
[ reply ]
horiuchi
#5504()
[
Java
]
Rating0/0=0.00
単純に全探索で求めてみました。
Rating0/0=0.00-0+
[ reply ]