-
Notifications
You must be signed in to change notification settings - Fork 15
/
p292.java
162 lines (113 loc) · 3.59 KB
/
p292.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.util.*;
/**
* @author Rubén Saiz
*/
class State implements Comparable<State> {
int node;
int weight;
public State(int node, int weight) {
this.node = node;
this.weight = weight;
}
@Override
public int compareTo(State state) {
return this.weight - state.weight;
}
}
class Edge {
int from, to;
int weight;
public Edge(int from, int node, int weight) {
this.from = from;
this.to = node;
this.weight = weight;
}
}
class Graph {
HashMap<Integer, LinkedList<Edge>> adyacentList;
int size;
public Graph(int size) {
this.size = size;
adyacentList = new HashMap<>();
for (int i = 1; i <= this.size; i++)
adyacentList.put(i, new LinkedList<Edge>());
}
public void addEdge(int A, int B, int weight) {
this.adyacentList.get(A).addLast(new Edge(A, B, weight));
}
public void clear() {
for (int i = 1; i <= this.size; i++) {
this.adyacentList.get(i).clear();
}
}
public void dijkstra(int start, int[] distances) {
PriorityQueue<State> q = new PriorityQueue<>();
Arrays.fill(distances, Integer.MAX_VALUE);
q.add(new State(start, 0));
distances[start] = 0;
while (!q.isEmpty()) {
State current = q.poll();
if (current.weight > distances[current.node]) continue;
for (Edge node : this.adyacentList.get(current.node)) {
int weight = current.weight + node.weight;
if (weight >= distances[node.to]) continue;
distances[node.to] = weight;
q.add(new State(node.to, weight));
}
}
}
}
public class p292 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int N, C, oficina, paquetes, minCost;
int from, to, weight;
int costGo, costBack;
int[] distances, distances2;
boolean imposible;
while (s.hasNext()) {
N = s.nextInt();
C = s.nextInt();
Graph graph = new Graph(N + 1);
Graph graph2 = new Graph(N + 1);
for (int i = 0; i < C; i++) {
from = s.nextInt();
to = s.nextInt();
weight = s.nextInt();
graph.addEdge(from, to, weight);
graph2.addEdge(to, from, weight);
}
oficina = s.nextInt();
paquetes = s.nextInt();
distances = new int[N + 1];
distances2 = new int[N + 1];
graph.dijkstra(oficina, distances);
graph2.dijkstra(oficina, distances2);
minCost = 0;
imposible = false;
for (int i = 0; i < paquetes; i++) {
int casa = s.nextInt();
if (!imposible) {
costGo = distances[casa];
if (costGo != Integer.MAX_VALUE) {
minCost += costGo;
}
else imposible = true;
if (!imposible) {
costBack = distances2[casa];
if (costBack != Integer.MAX_VALUE) {
minCost += costBack;
}
else imposible = true;
}
}
}
if (imposible) {
System.out.println("Imposible");
}
else {
System.out.println( minCost );
}
}
}
}