Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Arrays;
- import java.util.PriorityQueue;
- import java.util.InputMismatchException;
- import java.util.ArrayList;
- import java.io.BufferedReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.util.AbstractCollection;
- import java.util.StringTokenizer;
- import java.io.InputStream;
- /**
- * Built using CHelper plug-in
- * Actual solution is at the top
- */
- public class Main {
- public static void main(String[] args) {
- InputStream inputStream = System.in;
- OutputStream outputStream = System.out;
- QuickScanner in = new QuickScanner(inputStream);
- PrintWriter out = new PrintWriter(outputStream);
- TaskD solver = new TaskD();
- solver.solve(1, in, out);
- out.close();
- }
- }
- class TaskD {
- int dstA[];
- int dstB[];
- public void solve(int testNumber, QuickScanner in, PrintWriter out) {
- int n=in.nextInt();
- int m=in.nextInt();
- int a=in.nextInt()-1;
- int b=in.nextInt()-1;
- ArrayList<Edge> adj[]=new ArrayList[n];
- ArrayList<Edge> rAdj[]=new ArrayList[n];
- QEdge edges[]=new QEdge[m];
- for(int i=0;i<n;i++)
- {
- adj[i] = new ArrayList<Edge>();
- rAdj[i]=new ArrayList<Edge>();
- }
- for(int i=0;i<m;i++)
- {
- int S,T,len,c;
- S=in.nextInt()-1;
- T=in.nextInt()-1;
- len=in.nextInt();
- c=in.nextInt();
- adj[S].add(new Edge(T,len));
- rAdj[T].add(new Edge(S,len));
- edges[i]=new QEdge(S,T,len,c);
- }
- dstA=new Dijkstra(adj).getDist(a);
- dstB=new Dijkstra(rAdj).getDist(b);
- int q=in.nextInt();
- Query query[]=new Query[q];
- int res[]=new int[q];
- for(int i=0;i<q;i++)
- query[i]=new Query(in.nextInt(),i);
- Arrays.sort(query);
- Arrays.sort(edges);
- int lq=0;
- int le=0;
- int curRes=0;
- while(lq<q)
- {
- while(le<edges.length && edges[le].getVal()<=query[lq].r)
- {
- curRes+=edges[le].cost;
- le++;
- }
- res[query[lq].id]=curRes;
- lq++;
- }
- for(int i=0;i<q;i++)
- out.println(res[i]);
- }
- private class Query implements Comparable<Query>
- {
- int r, id;
- Query(int r, int id)
- {
- this.r=r;
- this.id=id;
- }
- public int compareTo(Query query)
- {
- return r-query.r;
- }
- }
- private class QEdge implements Comparable<QEdge>
- {
- int from,to,len,cost;
- QEdge(int from, int to, int len, int cost)
- {
- this.from=from;
- this.to=to;
- this.len=len;
- this.cost=cost;
- }
- Long getVal()
- {
- return (((long)dstA[from])+len)+dstB[to];
- }
- public int compareTo(QEdge qEdge)
- {
- return getVal().compareTo(qEdge.getVal());
- }
- }
- }
- class QuickScanner {
- BufferedReader in;
- StringTokenizer token;
- String delim;
- public QuickScanner(InputStream inputStream)
- {
- this.in=new BufferedReader(new InputStreamReader(inputStream));
- this.delim=" \n\t";
- this.token=new StringTokenizer("",delim);
- }
- public boolean hasNext()
- {
- while(!token.hasMoreTokens())
- {
- try {
- String s=in.readLine();
- if(s==null) return false;
- token=new StringTokenizer(s,delim);
- } catch (IOException e) {
- throw new InputMismatchException();
- }
- }
- return true;
- }
- public String next()
- {
- hasNext();
- return token.nextToken();
- }
- public int nextInt()
- {
- return Integer.parseInt(next());
- }
- }
- class Edge
- {
- public int to, cost;
- public Edge(int to, int cost)
- {
- this.to=to;
- this.cost=cost;
- }
- }
- class Dijkstra
- {
- private class State implements Comparable<State>
- {
- int cur, curd;
- State(int cur, int curd)
- {
- this.cur=cur;
- this.curd=curd;
- }
- public int compareTo(State state)
- {
- if(curd!=state.curd)
- return curd-state.curd;
- return cur-state.cur;
- }
- }
- int n;
- ArrayList<Edge> e[];
- public Dijkstra(ArrayList<Edge> e[])
- {
- n=e.length;
- this.e = e;
- }
- public int[] getDist(int s)
- {
- int[] dist=new int[n];
- Arrays.fill(dist,Integer.MAX_VALUE);
- dist[s]=0;
- PriorityQueue<State> q=new PriorityQueue<State>();
- q.add(new State(s,dist[s]));
- while(!q.isEmpty())
- {
- State c=q.poll();
- if(dist[c.cur]!=c.curd) continue;
- for(Edge ed:e[c.cur])
- {
- int to=ed.to;
- int nd=c.curd+ed.cost;
- if(dist[to]>nd)
- {
- dist[to]=nd;
- q.add(new State(to,dist[to]));
- }
- }
- }
- return dist;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement