Guest User

The Parking Slot

a guest
Jun 25th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. import java.io.DataInputStream;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Comparator;
  6. import java.util.PriorityQueue;
  7.  
  8. /**
  9.  * Created by bugkiller on 25/06/18.
  10.  */
  11. class TheParkingSlot {
  12.  
  13.     static ArrayList<Edge> adj[] = new ArrayList[100001];
  14.     static boolean vis[] = new boolean[100001];
  15.     static long d[][] = new long[100001][2];
  16.     private static final int DISTANCE =0, CAPACITY = 1;
  17.     public static void main(String[] args) throws IOException {
  18.         initGraph();
  19.         Reader fr = new Reader();
  20.         int n, m, f, k;
  21.         String s[];
  22.         n = fr.nextInt();
  23.         m = fr.nextInt();
  24.         f = fr.nextInt();
  25.         for (int i = 0; i < n; i++) {
  26.             d[i + 1][DISTANCE] = Long.MAX_VALUE;
  27.             d[i + 1][1] = fr.nextInt();
  28.         }
  29.         for (int i = 0; i < m; i++) {
  30.             int u = fr.nextInt();
  31.             int v = fr.nextInt();
  32.             int w = fr.nextInt();
  33.             adj[u].add(new Edge(v, w));
  34.             adj[v].add(new Edge(u, w));
  35.         }
  36.         k = fr.nextInt();
  37.         dijkstra(n);
  38.         Arrays.sort(d, 1, n + 1, Comparator.comparingLong(o -> o[DISTANCE]));
  39.         System.out.println(solve(n,k,f));
  40.     }
  41.  
  42.     private static String solve(int n, int k, long parkingFee) {
  43.         StringBuilder sbr = new StringBuilder();
  44.         int count = 0;
  45.         for (int i = 1; i <= n; i++) {
  46.             if (count == k) {
  47.                 break;
  48.             }
  49.             for (int j = 0; j < d[i][CAPACITY] && count < k; j++) {
  50.                 count++;
  51.                 sbr.append(parkingFee + d[i][DISTANCE]).append(" ");
  52.             }
  53.         }
  54.         for (; count < k; count++) {
  55.             sbr.append("-1").append(" ");
  56.         }
  57.         sbr.deleteCharAt(sbr.length() - 1);
  58.         return sbr.toString();
  59.     }
  60.  
  61.     private static void dijkstra(int n) {
  62.         PriorityQueue<Edge> queue = new PriorityQueue<>(Comparator.comparingLong(e -> e.w));
  63.         d[1][DISTANCE] = 0;
  64.         queue.add(new Edge(1, 0));
  65.         while (!queue.isEmpty()) {
  66.             Edge u = queue.poll();
  67.             vis[u.v] = true;
  68.             adj[u.v].forEach(edge -> {
  69.                 if (!vis[edge.v]) {
  70.                     d[edge.v][DISTANCE] = Long.min(d[edge.v][DISTANCE], d[u.v][DISTANCE] + edge.w);
  71.                     queue.add(new Edge(edge.v, d[edge.v][DISTANCE]));
  72.                 }
  73.             });
  74.         }
  75.     }
  76.  
  77.     private static void initGraph() {
  78.         for (int i = 0; i < 100001; i++) {
  79.             adj[i] = new ArrayList<Edge>();
  80.         }
  81.     }
  82.     static class Edge {
  83.         int v;
  84.         long w;
  85.  
  86.         Edge(int v, long w) {
  87.             this.v = v;
  88.             this.w = w;
  89.         }
  90.     }
  91.     static class Reader {
  92.         final private int BUFFER_SIZE = 1 << 16;
  93.         private DataInputStream din;
  94.         private byte[] buffer;
  95.         private int bufferPointer, bytesRead;
  96.  
  97.         public Reader()
  98.         {
  99.             din = new DataInputStream(System.in);
  100.             buffer = new byte[BUFFER_SIZE];
  101.             bufferPointer = bytesRead = 0;
  102.         }
  103.  
  104.         public String readLine() throws IOException
  105.         {
  106.             byte[] buf = new byte[64]; // line length
  107.             int cnt = 0, c;
  108.             while ((c = read()) != -1)
  109.             {
  110.                 if (c == '\n')
  111.                     break;
  112.                 buf[cnt++] = (byte) c;
  113.             }
  114.             return new String(buf, 0, cnt);
  115.         }
  116.  
  117.         public int nextInt() throws IOException
  118.         {
  119.             int ret = 0;
  120.             byte c = read();
  121.             while (c <= ' ')
  122.                 c = read();
  123.             boolean neg = (c == '-');
  124.             if (neg)
  125.                 c = read();
  126.             do
  127.             {
  128.                 ret = ret * 10 + c - '0';
  129.             }  while ((c = read()) >= '0' && c <= '9');
  130.  
  131.             if (neg)
  132.                 return -ret;
  133.             return ret;
  134.         }
  135.  
  136.  
  137.         private void fillBuffer() throws IOException
  138.         {
  139.             bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  140.             if (bytesRead == -1)
  141.                 buffer[0] = -1;
  142.         }
  143.  
  144.         private byte read() throws IOException
  145.         {
  146.             if (bufferPointer == bytesRead)
  147.                 fillBuffer();
  148.             return buffer[bufferPointer++];
  149.         }
  150.  
  151.         public void close() throws IOException
  152.         {
  153.             if (din == null)
  154.                 return;
  155.             din.close();
  156.         }
  157.     }
  158. }
Add Comment
Please, Sign In to add comment