Advertisement
knightL

D

Jan 16th, 2015
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.97 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStreamReader;
  3. import java.util.Arrays;
  4. import java.util.PriorityQueue;
  5. import java.util.InputMismatchException;
  6. import java.util.ArrayList;
  7. import java.io.BufferedReader;
  8. import java.io.OutputStream;
  9. import java.io.PrintWriter;
  10. import java.util.AbstractCollection;
  11. import java.util.StringTokenizer;
  12. import java.io.InputStream;
  13.  
  14. /**
  15.  * Built using CHelper plug-in
  16.  * Actual solution is at the top
  17.  */
  18. public class Main {
  19.     public static void main(String[] args) {
  20.         InputStream inputStream = System.in;
  21.         OutputStream outputStream = System.out;
  22.         QuickScanner in = new QuickScanner(inputStream);
  23.         PrintWriter out = new PrintWriter(outputStream);
  24.         TaskD solver = new TaskD();
  25.         solver.solve(1, in, out);
  26.         out.close();
  27.     }
  28. }
  29.  
  30. class TaskD {
  31.     int dstA[];
  32.     int dstB[];
  33.     public void solve(int testNumber, QuickScanner in, PrintWriter out) {
  34.         int n=in.nextInt();
  35.         int m=in.nextInt();
  36.         int a=in.nextInt()-1;
  37.         int b=in.nextInt()-1;
  38.         ArrayList<Edge> adj[]=new ArrayList[n];
  39.         ArrayList<Edge> rAdj[]=new ArrayList[n];
  40.  
  41.         QEdge edges[]=new QEdge[m];
  42.         for(int i=0;i<n;i++)
  43.         {
  44.             adj[i] = new ArrayList<Edge>();
  45.             rAdj[i]=new ArrayList<Edge>();
  46.         }
  47.         for(int i=0;i<m;i++)
  48.         {
  49.             int S,T,len,c;
  50.             S=in.nextInt()-1;
  51.             T=in.nextInt()-1;
  52.             len=in.nextInt();
  53.             c=in.nextInt();
  54.             adj[S].add(new Edge(T,len));
  55.             rAdj[T].add(new Edge(S,len));
  56.             edges[i]=new QEdge(S,T,len,c);
  57.         }
  58.         dstA=new Dijkstra(adj).getDist(a);
  59.         dstB=new Dijkstra(rAdj).getDist(b);
  60.         int q=in.nextInt();
  61.         Query query[]=new Query[q];
  62.         int res[]=new int[q];
  63.         for(int i=0;i<q;i++)
  64.             query[i]=new Query(in.nextInt(),i);
  65.         Arrays.sort(query);
  66.         Arrays.sort(edges);
  67.         int lq=0;
  68.         int le=0;
  69.         int curRes=0;
  70.         while(lq<q)
  71.         {
  72.             while(le<edges.length && edges[le].getVal()<=query[lq].r)
  73.             {
  74.                 curRes+=edges[le].cost;
  75.                 le++;
  76.             }
  77.             res[query[lq].id]=curRes;
  78.             lq++;
  79.         }
  80.         for(int i=0;i<q;i++)
  81.             out.println(res[i]);
  82.     }
  83.     private class Query implements Comparable<Query>
  84.     {
  85.         int r, id;
  86.         Query(int r, int id)
  87.         {
  88.             this.r=r;
  89.             this.id=id;
  90.         }
  91.  
  92.         public int compareTo(Query query)
  93.         {
  94.             return r-query.r;
  95.         }
  96.     }
  97.  
  98.     private class QEdge implements Comparable<QEdge>
  99.     {
  100.         int from,to,len,cost;
  101.         QEdge(int from, int to, int len, int cost)
  102.         {
  103.             this.from=from;
  104.             this.to=to;
  105.             this.len=len;
  106.             this.cost=cost;
  107.         }
  108.  
  109.         Long getVal()
  110.         {
  111.             return (((long)dstA[from])+len)+dstB[to];
  112.         }
  113.  
  114.         public int compareTo(QEdge qEdge)
  115.         {
  116.             return getVal().compareTo(qEdge.getVal());
  117.         }
  118.     }
  119. }
  120.  
  121. class QuickScanner {
  122.     BufferedReader in;
  123.     StringTokenizer token;
  124.     String delim;
  125.     public QuickScanner(InputStream inputStream)
  126.     {
  127.         this.in=new BufferedReader(new InputStreamReader(inputStream));
  128.         this.delim=" \n\t";
  129.         this.token=new StringTokenizer("",delim);
  130.     }
  131.  
  132.     public boolean hasNext()
  133.     {
  134.         while(!token.hasMoreTokens())
  135.         {
  136.             try {
  137.                 String s=in.readLine();
  138.                 if(s==null) return false;
  139.                 token=new StringTokenizer(s,delim);
  140.             } catch (IOException e) {
  141.                 throw new InputMismatchException();
  142.             }
  143.         }
  144.         return true;
  145.     }
  146.  
  147.     public String next()
  148.     {
  149.         hasNext();
  150.         return token.nextToken();
  151.     }
  152.  
  153.     public int nextInt()
  154.     {
  155.         return Integer.parseInt(next());
  156.     }
  157.  
  158. }
  159.  
  160. class Edge
  161. {
  162.     public int to, cost;
  163.     public Edge(int to, int cost)
  164.     {
  165.         this.to=to;
  166.         this.cost=cost;
  167.     }
  168. }
  169.  
  170. class Dijkstra
  171. {
  172.     private class State implements Comparable<State>
  173.     {
  174.         int cur, curd;
  175.         State(int cur, int curd)
  176.         {
  177.             this.cur=cur;
  178.             this.curd=curd;
  179.         }
  180.  
  181.         public int compareTo(State state)
  182.         {
  183.             if(curd!=state.curd)
  184.                 return curd-state.curd;
  185.             return cur-state.cur;
  186.         }
  187.     }
  188.  
  189.     int n;
  190.     ArrayList<Edge> e[];
  191.     public Dijkstra(ArrayList<Edge> e[])
  192.     {
  193.         n=e.length;
  194.         this.e = e;
  195.     }
  196.     public int[] getDist(int s)
  197.     {
  198.         int[] dist=new int[n];
  199.         Arrays.fill(dist,Integer.MAX_VALUE);
  200.         dist[s]=0;
  201.         PriorityQueue<State> q=new PriorityQueue<State>();
  202.         q.add(new State(s,dist[s]));
  203.         while(!q.isEmpty())
  204.         {
  205.             State c=q.poll();
  206.             if(dist[c.cur]!=c.curd) continue;
  207.             for(Edge ed:e[c.cur])
  208.             {
  209.                 int to=ed.to;
  210.                 int nd=c.curd+ed.cost;
  211.                 if(dist[to]>nd)
  212.                 {
  213.                     dist[to]=nd;
  214.                     q.add(new State(to,dist[to]));
  215.                 }
  216.             }
  217.         }
  218.         return dist;
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement