static void sweep(ArrayList<Integer> indexes) { PriorityQueue<Event> pq = new PriorityQueue<Event>(); for (int i = 0; i < indexes.size(); i++) { pq.offer(new Event(lo[val[indexes.get(i)]] + 1, 1, indexes.get(i))); pq.offer(new Event(hi[val[indexes.get(i)]], -1, indexes.get(i))); } TreeSet<Integer> active = new TreeSet<Integer>(); while (!pq.isEmpty()) { Event curr = pq.poll(); if (curr.type == 1) active.add(curr.index); else if (curr.type == -1) { active.remove(curr.index); Integer lower = active.lower(curr.index); if (lower != null && lower > lo[val[curr.index]]) { Interval add = new Interval(lower, curr.index); Interval prev = intervals.floor(add); Interval next = intervals.ceiling(add); boolean intersectPrev = true; boolean intersectNext = true; if (prev != null) { if (Math.max(add.l, prev.l) < Math.min(add.r, prev.r)) { if (add.r - add.l <= prev.r - prev.l) { intervals.remove(prev); intersectPrev = false; } } else { intersectPrev = false; } } else { intersectPrev = false; } if (next != null) { if (Math.max(add.l, next.l) < Math.min(add.r, next.r)) { if (add.r - add.l <= next.r - next.l) { intervals.remove(next); intersectNext = false; } } else { intersectNext = false; } } else { intersectNext = false; } if (!intersectPrev && !intersectNext) intervals.add(add); } } } }
static void solve(Scanner sc) throws IOException { int h = sc.nextInt(); int w = sc.nextInt(); int k = sc.nextInt(); PriorityQueue<Node> pq = new PriorityQueue<Node>(); for (int i = 0; i < h; i++) { String s = sc.next(); for (int j = 0; j < w; j++) { grid[i][j] = s.charAt(j); dis[i][j] = grid[i][j] == '#' ? 0 : inf; if (grid[i][j] == 'S') pq.offer(new Node(i, j, 0)); } } // Dijkstra // There is always a solution according to problem statement while (!pq.isEmpty()) { Node z = pq.poll(); int x = z.x; int y = z.y; int d = z.dis; if (d > dis[x][y]) continue; if (x == 0 || x == h - 1 || y == 0 || y == w - 1) { System.out.println(d + 1); break; } for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; // if(nx<0||nx>=h||ny<0||ny>=w) // continue; int nd = d + 1 + (grid[nx][ny] == '@' ? k : 0); if (nd < dis[nx][ny]) { dis[nx][ny] = nd; pq.offer(new Node(nx, ny, nd)); } } } }
// 保存待访问的URL及其优先级 private static void SavePriorQueue(String filepath) throws Exception { BufferedWriter bw = new BufferedWriter(new FileWriter(filepath)); PriorityQueue<UrlValue> temp = new PriorityQueue<UrlValue>(); UrlValue cur = null; while (pq.peek() != null) { cur = pq.remove(); temp.offer(cur); bw.write(cur.url + " " + cur.value); bw.newLine(); } pq = temp; bw.flush(); bw.close(); }
// 清除所有优先队列的数据,设置优先种子 private static void SetSeeds(String filepath) throws Exception { pq.clear(); BufferedReader br = new BufferedReader(new FileReader(filepath)); String line = null; UrlValue cur = null; visitedPrint(); while ((line = br.readLine()) != null) { line = line.trim(); // System.out.println(line); if (!line.equals("")) { if (!visitedUrl.contains(line)) { cur = new UrlValue(); cur.url = line; cur.value = 1; pq.offer(cur); } else { System.out.println("contain"); } } } br.close(); }
// 加载待访问的url private static void SetPriorQueue(String filePath) throws Exception { BufferedReader br = new BufferedReader(new FileReader(filePath)); Scanner sc = null; String line = null; String url = null; UrlValue cur = null; while ((line = br.readLine()) != null) { line = line.trim(); if (!line.equals("")) { sc = new Scanner(line); url = sc.next(); if (!visitedUrl.contains(url)) { cur = new UrlValue(); cur.url = url; cur.value = sc.nextDouble(); pq.offer(cur); } } } br.close(); }
public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new FileReader(new File(args[0]))); BufferedWriter bw = new BufferedWriter(new FileWriter(args[1])); int T = Integer.parseInt(br.readLine()); for (int t = 1; t <= T; t++) { br.readLine(); List<List<Station>> graph = new ArrayList<>(); int N = Integer.parseInt(br.readLine()); for (int i = 0; i < N; i++) { graph.add(new ArrayList<>()); String[] line1 = br.readLine().split(" "); int SN = Integer.parseInt(line1[0]); int W = Integer.parseInt(line1[1]); for (int j = 0; j < SN; j++) { graph.get(i).add(new Station(i, j, W)); } String[] dist = br.readLine().split(" "); for (int j = 0; j < dist.length; j++) { int d = Integer.parseInt(dist[j]); Station s1 = graph.get(i).get(j); Station s2 = graph.get(i).get(j + 1); s1.neighbors.put(s2, d); s2.neighbors.put(s1, d); } } int tunnelNum = Integer.parseInt(br.readLine()); for (int i = 0; i < tunnelNum; i++) { String[] tunnel = br.readLine().split(" "); int l1 = Integer.parseInt(tunnel[0]) - 1; int s1 = Integer.parseInt(tunnel[1]) - 1; int l2 = Integer.parseInt(tunnel[2]) - 1; int s2 = Integer.parseInt(tunnel[3]) - 1; int d = Integer.parseInt(tunnel[4]); Station sta1 = graph.get(l1).get(s1); Station sta2 = graph.get(l2).get(s2); sta1.neighbors.put(sta2, d + sta2.waitTime); // add wait time to distance sta2.neighbors.put(sta1, d + sta1.waitTime); Station sta11 = sta1.cloneForTunnel(); Station sta22 = sta2.cloneForTunnel(); graph.get(sta1.lineNo).add(sta11); graph.get(sta2.lineNo).add(sta22); for (Station st : sta11.neighbors.keySet()) { sta2.neighbors.put( st, d + sta11.neighbors.get( st)); // don't wait, sta11 means go xsfrom sta2 to sta1 than go to other // tunnel } for (Station st : sta22.neighbors.keySet()) { sta1.neighbors.put(st, d + sta22.neighbors.get(st)); } } int queryNum = Integer.parseInt(br.readLine()); bw.write("Case: #" + t + ":"); bw.newLine(); for (int i = 0; i < queryNum; i++) { String[] query = br.readLine().split(" "); int l1 = Integer.parseInt(query[0]) - 1; int s1 = Integer.parseInt(query[1]) - 1; int l2 = Integer.parseInt(query[2]) - 1; int s2 = Integer.parseInt(query[3]) - 1; Station sta1 = graph.get(l1).get(s1); Station sta2 = graph.get(l2).get(s2); PriorityQueue<Station> pq = new PriorityQueue<>(); sta1.dist = sta1.waitTime; for (List<Station> line : graph) { for (Station st : line) { pq.offer(st); } } Set<Station> visited = new HashSet<>(); visited.add(sta1); boolean done = false; while (!done && !pq.isEmpty()) { Station st = pq.poll(); for (Station neb : st.neighbors.keySet()) { if (!visited.contains(neb)) { neb.dist = st.dist + st.neighbors.get(neb); if (neb == sta2) { // reference to same object done = true; break; } visited.add(neb); pq.remove(neb); // because need to let pq update this item's priority level pq.offer(neb); } } } int ret = sta2.dist == Integer.MAX_VALUE ? -1 : sta2.dist; bw.write("" + ret); bw.newLine(); } } bw.close(); br.close(); }
public static void main(String[] args) throws Exception { int V, E, s, u, v, w; /* // Graph in Figure 4.17 5 7 2 2 1 2 2 3 7 2 0 6 1 3 3 1 4 6 3 4 5 0 4 1 */ Scanner sc = new Scanner(System.in); V = sc.nextInt(); E = sc.nextInt(); s = sc.nextInt(); AdjList.clear(); for (int i = 0; i < V; i++) { ArrayList<IntegerPair> Neighbor = new ArrayList<IntegerPair>(); AdjList.add(Neighbor); // add neighbor list to Adjacency List } for (int i = 0; i < E; i++) { u = sc.nextInt(); v = sc.nextInt(); w = sc.nextInt(); AdjList.get(u).add(new IntegerPair(v, w)); // first time using weight } // Dijkstra routine ArrayList<Integer> dist = new ArrayList<>(); // INF = 1*10^9 not MAX_INT to avoid overflow dist.addAll(Collections.nCopies(V, INF)); dist.set(s, 0); PriorityQueue<IntegerPair> pq = new PriorityQueue<IntegerPair>( 1, new Comparator<IntegerPair>() { // overriding the compare method public int compare(IntegerPair i, IntegerPair j) { return i.dfs - j.dfs; } }); pq.offer(new IntegerPair(s, 0)); // sort based on increasing distance while (!pq.isEmpty()) { // main loop IntegerPair top = pq.poll(); // greedy: pick shortest unvisited vertex int d = top.dfs; u = top.vn; if (d > dist.get(u)) continue; // We want to process vertex u only once! Iterator it = AdjList.get(u).iterator(); while (it.hasNext()) { // all outgoing edges from u IntegerPair p = (IntegerPair) it.next(); v = p.vn; int weight_u_v = p.dfs; if (dist.get(u) + weight_u_v < dist.get(v)) { // if can relax // (note: Record SP spanning tree here if needed. This is similar) dist.set(v, dist.get(u) + weight_u_v); // relax pq.offer(new IntegerPair(v, dist.get(v))); // (as printpath in BFS) // enqueue this neighbor regardless whether it is already in pq or not } } } for (int i = 0; i < V; i++) // index + 1 for final answer System.out.printf("SSSP(%d, %d) = %d\n", s, i, dist.get(i)); }
public static void add(UrlValue url) { if (url != null && !url.url.trim().equals("") && !visitedUrl.contains(url.url)) { url.url = url.url.trim(); pq.offer(url); } }