forked from improper4/uva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa00383_ShippingRoutes.java
89 lines (73 loc) · 2.09 KB
/
UVa00383_ShippingRoutes.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
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 319 (383 - Shipping Routes) */
/* SUBMISSION: 09049036 */
/* SUBMISSION TIME: 2011-07-14 18:34:33 */
/* LANGUAGE: 2 */
import java.util.*;
public class UVa00383_ShippingRoutes {
static boolean[][] adj;
static Map<String, Integer> warehouses;
static int M;
static int[] bfs(int s) {
int[] distance = new int[M];
int[] parent = new int[M];
boolean[] visited = new boolean[M];
Arrays.fill(distance, -1);
Arrays.fill(parent, -1);
Queue<Integer> Q = new LinkedList<Integer>();
Q.offer(s);
visited[s] = true;
distance[s] = 0;
while (!Q.isEmpty()) {
int v = Q.poll();
visited[v] = true;
for (int w = 0; w < M; ++w)
if (adj[v][w] && !visited[w]) {
visited[w] = true;
distance[w] = distance[v] + 1;
parent[w] = v;
Q.offer(w);
}
}
return distance;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
System.out.println("SHIPPING ROUTES OUTPUT");
for (int t = 1; t <= T; ++t) {
M = in.nextInt();
int N = in.nextInt();
int P = in.nextInt();
System.out.println();
System.out.println("DATA SET " + t);
System.out.println();
adj = new boolean[M][M];
warehouses = new HashMap<String, Integer>();
for (int i = 0; i < M; ++i)
warehouses.put(in.next(), i);
for (int i = 0; i < N; ++i) {
String from = in.next();
String to = in.next();
adj[warehouses.get(from)][warehouses.get(to)] =
adj[warehouses.get(to)][warehouses.get(from)] = true;
}
for (int i = 0; i < P; ++i) {
int size = in.nextInt();
String start = in.next();
String dest = in.next();
int[] distance = bfs(warehouses.get(start));
int cost = distance[warehouses.get(dest)];
if (cost == -1)
System.out.println("NO SHIPMENT POSSIBLE");
else
System.out.println("$" + (100 * size * cost));
}
}
System.out.println();
System.out.println("END OF OUTPUT");
in.close();
System.exit(0);
}
}