답안 #255332

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
255332 2020-07-31T17:34:20 Z model_code Relay Marathon (NOI20_relaymarathon) Java 11
17 / 100
6000 ms 277900 KB
import java.io.DataInputStream;
import java.io.IOException;
import java.util.*;
import java.io.*;

public class RelayMarathon {
    static final class Reader {
        final private int BUFFER_SIZE = 1 << 20;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;
        public Reader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }
        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ') {
                c = read();
            }
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            return ret;
        }
        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ') {
                c = read();
            }
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1) {
                buffer[0] = -1;
            }
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead) {
                fillBuffer();
            }
            return buffer[bufferPointer++];
        }
    }

    public static class DirectedEdge {
      int u;
      int w;
      public DirectedEdge(int u, int w) {
        this.u = u;
        this.w = w;
      }
    }

    public static class Edge {
      int u;
      int v;
      int w;
      public Edge(int u, int v, int w) {
        this.u = u;
        this.v = v;
        this.w = w;
      }
    }

    public static class Qitem implements Comparable<Qitem> {
      int first;
      int second;
      public Qitem(int first, int second) {
        this.first = first;
        this.second = second;
      }
      public final int compareTo(Qitem b) {
        if(this.first > b.first) {
          return 1;
        } else if(this.first < b.first) {
          return -1;
        } else {
          return 0;
        }
      }
    }

    public static ArrayList<Edge> E;
    static final int INT_MAX = 1000000003;
    static final int[] S = new int[100005];
    static final int[] dx1 = new int[100005];
    static final int[] dy1 = new int[100005];
    static final int[] d = new int[100005];
    static final int[] p = new int[100005];
    static final int[] skip = new int[100005];
    static final int[] vis = new int[100005];
    static int n, m, k;
    public static ArrayList<DirectedEdge>[] adj;

    static final PriorityQueue<Qitem> Q = new PriorityQueue<>();

    public static void djikstra() {
      while(!Q.isEmpty()) {
        int u = Q.poll().second;
        if(vis[u] == 1)  continue;
        vis[u] = 1;
        for(DirectedEdge nbr : adj[u]) {
          int v = nbr.u, w = nbr.w;
          if(skip[v] == 1)  continue;
          if(d[u] + w < d[v]) {
            d[v] = d[u] + w;
            Q.add(new Qitem(d[v], v));
            p[v] = p[u];
          }
        }
      }
    }

    public static void run(int src) {
      while(!Q.isEmpty()) Q.poll();
      for(int i = 1; i <= n; i++) {
        d[i] = INT_MAX;
        vis[i] = 0;
      }
      d[src] = 0;
      p[src] = src;
      Q.add(new Qitem(0, src));
      djikstra();
    }

    public static Edge run_nearest() {
      while(!Q.isEmpty()) Q.poll();
      for(int i = 1; i <= n; i++) {
        d[i] = INT_MAX;
        vis[i] = 0;
      }
      for(int i = 0; i < k; i++) {
        int u = S[i];
        if(skip[u] == 1)  continue;
        Q.add(new Qitem(0, u));
        d[u] = 0;
        p[u] = u;
      }
      djikstra();
      int ans = INT_MAX, a = -1, b = -1;
      for(Edge e : E) {
        int u = e.u, v = e.v, w = e.w;
        if(skip[u] == 1 || skip[v] == 1)  continue;
        if(d[u] == INT_MAX || d[v] == INT_MAX || p[u] == p[v])  continue;
        if(ans > d[u] + w + d[v]) {
          ans = d[u] + w + d[v];
          a = p[u];
          b = p[v];
        }
      }
      return new Edge(a, b, ans);
    }

    public static void main(String[] args) throws IOException {
        Reader reader = new Reader();
        n = reader.nextInt();
        m = reader.nextInt();
        k = reader.nextInt();

        adj = (ArrayList<DirectedEdge>[]) new ArrayList[n + 2];
        for(int i = 0; i < n + 2; i++) {
          adj[i] = new ArrayList<DirectedEdge>();
        }

        E = new ArrayList<Edge>();

        int u, v, w;
        for(int i = 0; i < m; i++) {
          u = reader.nextInt();
          v = reader.nextInt();
          w = reader.nextInt();
          adj[u].add(new DirectedEdge(v, w));
          adj[v].add(new DirectedEdge(u, w));
          E.add(new Edge(u, v, w));
        }
        for(int i = 0; i < k; i++) {
          u = reader.nextInt();
          S[i] = u;
        }

        Edge tmp = run_nearest();

        int x1 = tmp.u, y1 = tmp.v, d1 = tmp.w;

        int c1 = -1, c2 = -1;

        run(x1);
        for(int i = 1; i <= n; i++) dx1[i] = d[i];
        run(y1);
        for(int i = 1; i <= n; i++) dy1[i] = d[i];

        for(int i = 0; i < k; i++) {
          int c = S[i];
          if(c == x1 || S[i] == y1)  continue;
          if(c1 == -1 || dy1[c] <= dy1[c1]) {
            c2 = c1;
            c1 = c;
          } else if(c2 == -1 || dy1[c] < dy1[c2]) {
            c2 = c;
          }
        }

        int ans = INT_MAX;

        if(dx1[x1] != INT_MAX && dy1[c1] != INT_MAX) {
          if(ans > dx1[c1] + dy1[c2]) {
            ans = dx1[c1] + dy1[c2];
          }
        }

        for(int i = 0; i < k; i++) {
          int x2 = S[i];
          if(x2 == x1 || x2 == y1 || x2 == c1)  continue;
          if(dx1[x2] != INT_MAX && dy1[c1] != INT_MAX) {
            if(ans > dx1[x2] + dy1[c1]) {
              ans = dx1[x2] + dy1[c1];
            }
          }
        }

        skip[x1] = skip[y1] = 1;
        tmp = run_nearest();
        int d2 = tmp.w;
        if(d1 != INT_MAX && d2 != INT_MAX) {
          if(ans > d1 + d2) {
            ans = d1 + d2;
          }
        }

        System.out.println(ans);
    }
}

Compilation message

Note: RelayMarathon.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 13988 KB Output is correct
2 Correct 88 ms 13992 KB Output is correct
3 Correct 79 ms 13904 KB Output is correct
4 Correct 76 ms 13936 KB Output is correct
5 Correct 81 ms 13892 KB Output is correct
6 Correct 104 ms 13952 KB Output is correct
7 Correct 97 ms 14108 KB Output is correct
8 Correct 97 ms 14244 KB Output is correct
9 Correct 79 ms 13740 KB Output is correct
10 Correct 82 ms 14072 KB Output is correct
11 Correct 83 ms 13992 KB Output is correct
12 Correct 100 ms 14052 KB Output is correct
13 Correct 96 ms 13832 KB Output is correct
14 Correct 99 ms 14176 KB Output is correct
15 Correct 95 ms 14396 KB Output is correct
16 Correct 82 ms 13940 KB Output is correct
17 Correct 90 ms 14072 KB Output is correct
18 Correct 88 ms 13928 KB Output is correct
19 Correct 99 ms 14080 KB Output is correct
20 Correct 90 ms 13968 KB Output is correct
21 Correct 109 ms 14200 KB Output is correct
22 Correct 114 ms 13976 KB Output is correct
23 Correct 91 ms 14044 KB Output is correct
24 Correct 77 ms 14052 KB Output is correct
25 Correct 87 ms 13968 KB Output is correct
26 Correct 87 ms 14060 KB Output is correct
27 Correct 80 ms 13668 KB Output is correct
28 Correct 96 ms 14184 KB Output is correct
29 Correct 112 ms 14180 KB Output is correct
30 Correct 99 ms 14276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 13988 KB Output is correct
2 Correct 88 ms 13992 KB Output is correct
3 Correct 79 ms 13904 KB Output is correct
4 Correct 76 ms 13936 KB Output is correct
5 Correct 81 ms 13892 KB Output is correct
6 Correct 104 ms 13952 KB Output is correct
7 Correct 97 ms 14108 KB Output is correct
8 Correct 97 ms 14244 KB Output is correct
9 Correct 79 ms 13740 KB Output is correct
10 Correct 82 ms 14072 KB Output is correct
11 Correct 83 ms 13992 KB Output is correct
12 Correct 100 ms 14052 KB Output is correct
13 Correct 96 ms 13832 KB Output is correct
14 Correct 99 ms 14176 KB Output is correct
15 Correct 95 ms 14396 KB Output is correct
16 Correct 82 ms 13940 KB Output is correct
17 Correct 90 ms 14072 KB Output is correct
18 Correct 88 ms 13928 KB Output is correct
19 Correct 99 ms 14080 KB Output is correct
20 Correct 90 ms 13968 KB Output is correct
21 Correct 109 ms 14200 KB Output is correct
22 Correct 114 ms 13976 KB Output is correct
23 Correct 91 ms 14044 KB Output is correct
24 Correct 77 ms 14052 KB Output is correct
25 Correct 87 ms 13968 KB Output is correct
26 Correct 87 ms 14060 KB Output is correct
27 Correct 80 ms 13668 KB Output is correct
28 Correct 96 ms 14184 KB Output is correct
29 Correct 112 ms 14180 KB Output is correct
30 Correct 99 ms 14276 KB Output is correct
31 Correct 104 ms 14152 KB Output is correct
32 Correct 111 ms 14040 KB Output is correct
33 Correct 121 ms 14296 KB Output is correct
34 Correct 92 ms 13892 KB Output is correct
35 Correct 82 ms 13872 KB Output is correct
36 Correct 129 ms 15156 KB Output is correct
37 Correct 124 ms 15504 KB Output is correct
38 Correct 97 ms 14192 KB Output is correct
39 Correct 398 ms 28692 KB Output is correct
40 Correct 166 ms 16956 KB Output is correct
41 Correct 106 ms 14308 KB Output is correct
42 Correct 160 ms 16908 KB Output is correct
43 Correct 145 ms 15172 KB Output is correct
44 Correct 128 ms 14280 KB Output is correct
45 Correct 96 ms 13892 KB Output is correct
46 Correct 585 ms 33368 KB Output is correct
47 Correct 176 ms 17168 KB Output is correct
48 Correct 384 ms 28328 KB Output is correct
49 Correct 533 ms 30056 KB Output is correct
50 Correct 111 ms 14356 KB Output is correct
51 Correct 121 ms 14408 KB Output is correct
52 Correct 124 ms 14300 KB Output is correct
53 Correct 259 ms 23816 KB Output is correct
54 Correct 472 ms 30756 KB Output is correct
55 Correct 101 ms 14084 KB Output is correct
56 Correct 96 ms 14068 KB Output is correct
57 Correct 103 ms 14232 KB Output is correct
58 Correct 590 ms 34668 KB Output is correct
59 Correct 86 ms 13952 KB Output is correct
60 Correct 112 ms 14720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 572 ms 37848 KB Output is correct
2 Correct 172 ms 19468 KB Output is correct
3 Execution timed out 6217 ms 277900 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 13988 KB Output is correct
2 Correct 88 ms 13992 KB Output is correct
3 Correct 79 ms 13904 KB Output is correct
4 Correct 76 ms 13936 KB Output is correct
5 Correct 81 ms 13892 KB Output is correct
6 Correct 104 ms 13952 KB Output is correct
7 Correct 97 ms 14108 KB Output is correct
8 Correct 97 ms 14244 KB Output is correct
9 Correct 79 ms 13740 KB Output is correct
10 Correct 82 ms 14072 KB Output is correct
11 Correct 83 ms 13992 KB Output is correct
12 Correct 100 ms 14052 KB Output is correct
13 Correct 96 ms 13832 KB Output is correct
14 Correct 99 ms 14176 KB Output is correct
15 Correct 95 ms 14396 KB Output is correct
16 Correct 82 ms 13940 KB Output is correct
17 Correct 90 ms 14072 KB Output is correct
18 Correct 88 ms 13928 KB Output is correct
19 Correct 99 ms 14080 KB Output is correct
20 Correct 90 ms 13968 KB Output is correct
21 Correct 109 ms 14200 KB Output is correct
22 Correct 114 ms 13976 KB Output is correct
23 Correct 91 ms 14044 KB Output is correct
24 Correct 77 ms 14052 KB Output is correct
25 Correct 87 ms 13968 KB Output is correct
26 Correct 87 ms 14060 KB Output is correct
27 Correct 80 ms 13668 KB Output is correct
28 Correct 96 ms 14184 KB Output is correct
29 Correct 112 ms 14180 KB Output is correct
30 Correct 99 ms 14276 KB Output is correct
31 Correct 104 ms 14152 KB Output is correct
32 Correct 111 ms 14040 KB Output is correct
33 Correct 121 ms 14296 KB Output is correct
34 Correct 92 ms 13892 KB Output is correct
35 Correct 82 ms 13872 KB Output is correct
36 Correct 129 ms 15156 KB Output is correct
37 Correct 124 ms 15504 KB Output is correct
38 Correct 97 ms 14192 KB Output is correct
39 Correct 398 ms 28692 KB Output is correct
40 Correct 166 ms 16956 KB Output is correct
41 Correct 106 ms 14308 KB Output is correct
42 Correct 160 ms 16908 KB Output is correct
43 Correct 145 ms 15172 KB Output is correct
44 Correct 128 ms 14280 KB Output is correct
45 Correct 96 ms 13892 KB Output is correct
46 Correct 585 ms 33368 KB Output is correct
47 Correct 176 ms 17168 KB Output is correct
48 Correct 384 ms 28328 KB Output is correct
49 Correct 533 ms 30056 KB Output is correct
50 Correct 111 ms 14356 KB Output is correct
51 Correct 121 ms 14408 KB Output is correct
52 Correct 124 ms 14300 KB Output is correct
53 Correct 259 ms 23816 KB Output is correct
54 Correct 472 ms 30756 KB Output is correct
55 Correct 101 ms 14084 KB Output is correct
56 Correct 96 ms 14068 KB Output is correct
57 Correct 103 ms 14232 KB Output is correct
58 Correct 590 ms 34668 KB Output is correct
59 Correct 86 ms 13952 KB Output is correct
60 Correct 112 ms 14720 KB Output is correct
61 Correct 572 ms 37848 KB Output is correct
62 Correct 172 ms 19468 KB Output is correct
63 Execution timed out 6217 ms 277900 KB Time limit exceeded
64 Halted 0 ms 0 KB -