Пример #1
0
  static void test00() {
    Aron.beg();
    PriorityQueue<Interval> queue = new PriorityQueue<Interval>();
    Stack<Interval> stack = new Stack<Interval>();
    int[] arr1 = {4, 1, 2, 6, 9};
    int[] arr2 = {5, 1, 4, 9, 10};

    for (int i = 0; i < arr1.length; i++) {
      queue.add(new Interval(arr1[i], arr2[i]));
    }
    if (queue.size() > 0) {
      stack.push(queue.remove());
    }
    while (!queue.isEmpty()) {
      Interval top = stack.peek();
      Interval inter = queue.remove();
      if (top.end < inter.begin) stack.push(inter);
      else {
        stack.peek().end = Math.max(stack.peek().end, inter.end);
      }
    }
    while (!stack.empty()) {
      System.out.println("[" + stack.peek().begin + " " + stack.peek().end + "]");
      stack.pop();
    }

    Aron.end();
  }
 public static String next() {
   if (pq.peek() != null) {
     System.out.println("queue pop");
     String curUrl = pq.remove().url;
     if (!visitedUrl.contains(curUrl)) {
       visitedUrl.add(curUrl);
       return curUrl;
     } else return null;
   } else return null;
 }
 // 保存待访问的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();
 }
Пример #4
0
 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();
 }
  /** ***************************************************************************** */
  void addEntry(String s) {
    // System.err.println("TRACX: " + s);
    if (trace_writer != null) trace_writer.println(s);

    char s0 = s.charAt(0);
    if (s0 == 'T') { // THREAD
      if (thread_entries != null) {
        for (TraceEntry te : thread_entries) pending_entries.add(te);
        thread_entries = null;
      }
      StringTokenizer tok = new StringTokenizer(s);
      tok.nextToken();
      int id = Integer.parseInt(tok.nextToken());
      current_thread = thread_map.get(id);
      if (current_thread == null) {
        int tid = Integer.parseInt(tok.nextToken());
        String tnm = tok.nextToken("\n");
        current_thread = new ThreadData(++thread_counter, tid, tnm);
        thread_map.put(id, current_thread);
      }
      thread_entries = new ArrayList<TraceEntry>();
    } else if (s0 == 'D') { // DONE
      if (trace_writer != null) trace_writer.flush();
      if (thread_entries != null) {
        for (TraceEntry te : thread_entries) pending_entries.add(te);
        thread_entries = null;
      }
      StringTokenizer tok = new StringTokenizer(s);
      tok.nextToken();
      long time = Long.parseLong(tok.nextToken());
      if (next_time != 0) {
        int ct = 0;
        // System.err.println("TRACE: Pending size = " + pending_entries.size());
        while (!pending_entries.isEmpty() && pending_entries.peek().getTime() < next_time) {
          TraceEntry te = pending_entries.remove();
          ++ct;
          outputEntry(te);
        }
        if (ct > 0) {
          for (BdynEventUpdater eu : update_listeners) {
            eu.eventsAdded();
          }
        }
        end_time = Math.max(next_time, end_time);
      }
      next_time = time;
    } else if (s0 == 'S') {
      if (cpu_time == null) {
        StringTokenizer tok = new StringTokenizer(s);
        tok.nextToken();
        cpu_time = Boolean.parseBoolean(tok.nextToken());
      }
    } else if (cpu_time != null) {
      TraceEntry te = new TraceEntry(s, current_thread, cpu_time);
      if (thread_entries == null) pending_entries.add(te);
      else {
        thread_entries.add(te);
        if (te.isExit()) {
          int sz = thread_entries.size();
          int loc = te.getEntryLocation();
          if (sz >= 4) {
            TraceEntry t3 = thread_entries.get(sz - 2);
            if (!t3.isExit() && t3.getEntryLocation() == loc) {
              TraceEntry t2 = thread_entries.get(sz - 3);
              if (t2.isExit() && t2.getEntryLocation() == loc) {
                TraceEntry t1 = thread_entries.get(sz - 4);
                if (!t1.isExit()
                    && t1.getEntryLocation() == loc
                    && (te.getTime() - t1.getTime()) < MERGE_TIME) {
                  t1.merge(t2.getTime(), t3.getTime(), te.getTime());
                  thread_entries.remove(sz - 2);
                  thread_entries.remove(sz - 3);
                }
              }
            }
          }
        }
      }
    }
  }
 public static void print() {
   while (pq.peek() != null) {
     System.out.println(pq.remove().url);
   }
 }