Language: C++


import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	
	static final long INF = 10000000000000000L;
	static int n;
	static List<Node>[] Edge;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		n = Integer.parseInt(sc.next());
		
		Edge = new ArrayList[n];
		
		for(int i = 0 ;i < n ;i++) {
			Edge[i] = new ArrayList<>();
		}
		
		for(int i = 0 ;i < n-1 ;i++) {
			int a = Integer.parseInt(sc.next());
			int b = Integer.parseInt(sc.next());
			int cost = Integer.parseInt(sc.next());
			
			Edge[a-1].add(new Node(b-1,cost));
			Edge[b-1].add(new Node(a-1,cost));
		}
		
		int q = Integer.parseInt(sc.next());
		int k = Integer.parseInt(sc.next());
		
		long []d = dijkstra(k-1);
		
		for(int i = 0 ; i < q ;i++) {
			int x = Integer.parseInt(sc.next())-1;
			int y = Integer.parseInt(sc.next())-1;
			
			long ans = d[x] + d[y];
			System.out.println(ans);
		}

	}


static long[] dijkstra(int s){
	
	Queue <Pos> que = new PriorityQueue<Pos>();
	long[]d = new long [n]; //頂点sからの距離
	for(int i = 0 ; i < n ;i++) {
		d[i] = INF;
	}
	BitSet used = new BitSet(n);
	que.add(new Pos(0,s)); //初期値を代入、自身の距離は0
	d[s] = 0 ;				
	
	while(!que.isEmpty()) {
		Pos p = que.poll();		//
		int v = p.getTo();
		if(used.get(p.getTo())) {
			continue;
		}
		used.set(p.getTo());
		for(Node e : Edge[v]) {
			if(d[e.getTo()] > d[v] + e.getCost()) {
				d[e.getTo()] = (long)( d[v] + e.getCost());
				que.add(new Pos(d[e.getTo()],e.getTo()));
			}
		}
	}
	return d;	
}
}

class Pos implements Comparable<Pos>{

private long cost;
private int to;

public Pos (long cost, int to) {
	this.cost = cost;
	this.to = to;
}

public long getCost() {
	return cost;
}

public int getTo() {
	return to;
}

public void setCost(long cost) {
	this.cost = cost;
}

public void setTo(int to) {
	this.to = to;
}

@Override
public int compareTo(Pos o) {
	return Long.compare(cost, o.cost);
}

}
class Node{
private int to;
private int cost;

public Node(int to, int cost) {
	this.to = to;
	this.cost = cost;
}

public int getTo() {
	return to;
}

public int getCost() {
	return cost;
}

public void setTo(int to) {
	this.to = to;
}

public void setCost(int cost) {
	this.cost = cost;
}
}


Compiler output:
a.cc:106:1: error: stray '@' in program
  106 | @Override
      | ^
a.cc:2:1: error: 'import' does not name a type
    2 | import java.util.ArrayList;
      | ^~~~~~
a.cc:2:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:3:1: error: 'import' does not name a type
    3 | import java.util.BitSet;
      | ^~~~~~
a.cc:3:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:4:1: error: 'import' does not name a type
    4 | import java.util.List;
      | ^~~~~~
a.cc:4:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:5:1: error: 'import' does not name a type
    5 | import java.util.PriorityQueue;
      | ^~~~~~
a.cc:5:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:6:1: error: 'import' does not name a type
    6 | import java.util.Queue;
      | ^~~~~~
a.cc:6:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:7:1: error: 'import' does not name a type
    7 | import java.util.Scanner;
      | ^~~~~~
a.cc:7:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:9:1: error: expected unqualified-id before 'public'
    9 | public class Main {
      | ^~~~~~
a.cc:80:22: error: expected initializer before 'Comparable'
   80 | class Pos implements Comparable<Pos>{
      |                      ^~~~~~~~~~
a.cc:113:8: error: expected ':' before 'int'
  113 | private int to;
      |        ^~~~
      |        :
a.cc:114:8: error: expected ':' before 'int'
  114 | private int cost;
      |        ^~~~
      |        :
a.cc:116:7: error: expected ':' before 'Node'
  116 | public Node(int to, int cost) {
      |       ^~~~~
      |       :
a.cc:121:7: error: expected ':' before 'int'
  121 | public int getTo() {
      |       ^~~~
      |       :
a.cc:125:7: error: expected ':' before 'int'
  125 | public int getCost() {
      |       ^~~~
      |       :
a.cc:129:7: error: expected ':' before 'void'
  129 | public void setTo(int to) {
      |       ^~~~~
      |       :
a.cc:133:7: error: expected ':' before 'void'
  133 | public void setCost(int cost) {
      |       ^~~~~
      |       :
a.cc:136:2: error: expected ';' after class definition
  136 | }
      |  ^
      |  ;
a.cc: In constructor 'Node::Node(int, int)':
a.cc:117:14: error: request for member 'to' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
  117 |         this.to = to;
      |              ^~
a.cc:118:14: error: request for member 'cost' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
  118 |         this.cost = cost;
      |              ^~~~
a.cc: In member function 'void Node::setTo(int)':
a.cc:130:14: error: request for member 'to' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
  130 |         this.to = to;
      |              ^~
a.cc: In member function 'void Node::setCost(int)':
a.cc:134:14: error: request for member 'cost' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
  134 |         this.cost = cost;
      |              ^~~~