Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class GG {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner in = new Scanner(System.in);
  8.         int V = in.nextInt();
  9.  
  10.         int[] vals = new int[V+1];
  11.         int[] dist = new int[V+1];
  12.         int[] itms = new int[V+1];
  13.         boolean[] vis = new boolean[V+1];
  14.         Vector<Pair>[] adjList = new Vector[V+1];
  15.         for(int i = 1; i <= V; i++) {
  16.             vals[i] = in.nextInt();
  17.             adjList[i] = new Vector<Pair>();
  18.             vis[i] = false;
  19.             dist[i] = Integer.MAX_VALUE;
  20.             itms[i] = 0;
  21.         }
  22.  
  23.         itms[1] = vals[1];
  24.         dist[1] = 0;
  25.  
  26.         int E = in.nextInt();
  27.         for(int i = 0; i < E ; i++) {
  28.             int u = in.nextInt();
  29.             int v = in.nextInt();
  30.             int w = in.nextInt();
  31.  
  32.             adjList[u].add(new Pair(v, w));
  33.             adjList[v].add(new Pair(u, w));
  34.         }
  35.  
  36.         PriorityQueue<Triple> q = new PriorityQueue();
  37.         q.offer(new Triple(1, dist[1], itms[1]));
  38.  
  39.         while(!q.isEmpty()) {
  40.             Triple state = q.poll();
  41.             int u  = state.f;
  42.             int d  = state.s;
  43.             int it = state.t;
  44.  
  45.  
  46.  
  47.             for(Pair p : adjList[u]) {
  48.                 int v = p.f;
  49.                 int w = p.s;
  50.  
  51.                 if(dist[v] > dist[u] + w) {
  52.                     dist[v] = Math.min(dist[u] + w, dist[v] );
  53.                     itms[v] = vals[v] + itms[u];
  54.                     q.offer(new Triple(v, dist[v], itms[v]));
  55.                        
  56.                 }else if(dist[v] == (dist[u] + w)) {
  57.                     if(itms[v] < itms[u] + vals[v]) {
  58.                         itms[v] = itms[u] + vals[v];
  59.                         q.offer(new Triple(v, dist[v], itms[v]));
  60.                     }
  61.                    
  62.                 }
  63.  
  64.  
  65.                
  66.                
  67.             }
  68.         }
  69.  
  70.    
  71.             if(dist[V] == Integer.MAX_VALUE) System.out.println("Impossible");
  72.             else System.out.println(dist[V]+" "+itms[V]);
  73.  
  74.        
  75.  
  76.        
  77.        
  78.     }
  79. }
  80.  
  81. class Pair implements Comparable<Pair>{
  82.     int f, s;
  83.  
  84.     public Pair(int f, int s) {
  85.         this.f = f;
  86.         this.s = s;
  87.     }
  88.  
  89.     @Override
  90.     public int compareTo(Pair p) {
  91.         return this.s - p.s;
  92.     }
  93. }
  94.  
  95. class Triple implements Comparable<Triple> {
  96.  
  97.     int f, s, t;
  98.  
  99.     public Triple(int f, int s, int t) {
  100.         this.f = f;
  101.         this.s = s;
  102.         this.t = t;
  103.     }
  104.  
  105.     @Override
  106.     public int compareTo(Triple t) {
  107.         return this.s - t.s;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement