Google Code Jam Notes - Spaceship Defence - Java

Problem:

The enemy has invaded your spaceship, and only superior tactics will allow you to defend it! To travel around your spaceship, your soldiers will use two devices: teleporters andturbolifts.

Teleporters allow your soldiers to move instantly between rooms. Every room contains a teleporter, and rooms are color-coded: if a soldier is in a room with some color, she can use the teleporter in that room to immediately move to any other room with the same color.

Turbolifts allow your soldiers to move between rooms more slowly. A turbolift is like an elevator that moves in many directions. Each turbolift moves from one room to one other room, and it takes a certain amount of time to travel. Notes about turbolifts:

  • Turbolifts are not two-way: if a turbolift moves soldiers from room a to room b, the same turbolift cannot move soldiers from room b to room a, although there might be another turbolift that does that.
  • More than one soldier can use the same turbolift, and they do not interfere with each other in any way.

You will be given the locations and destinations of several soldiers. For each soldier, output the minimum amount of time it could take that soldier to travel from his location to his destination.

Input

The first line of the input gives the number of test cases, TT test cases follow.

For every test case:

The first line of every test case contains an integer N, which is the number of rooms in your spaceship. The rooms are numbered from 1 to N. The following N lines each contain a string telling the color of the rooms, from room 1 to room N. The strings only contain characters a-z (the lower-case English letters) and 0-9 (the number 0 to 9), and the length of each string will be less than or equal to 2.

The next line in the test case is an integer M, which indicates the number of turbolifts in your spaceship. The following M lines each contain 3 space-separated integers aibiti, telling us that there is a turbolift that can transport soldiers from room ai to room bi in tiseconds.

The next line in the test case contains an integer S, which is the number of soldiers at your command. The following S lines each contain two integers: the location and destination of one soldier, pj and qj.

Output

For each test case, output one line containing only the string "Case #x:", where x is the number of the test case (starting from 1). On the next S lines, output a single integer: on line j, the smallest number of seconds it could take for a soldier to travel from pj to qj. If there is no path from pj to qj, the integer you output should be -1.

Limits

1 ≤ S ≤ 100.
1 ≤ ai, bi ≤ N.
0 ≤ ti ≤ 1000.
1 ≤ pj, qj ≤ N.

Small dataset

1 ≤ T ≤ 10.
1 ≤ N ≤ 1000.
0 ≤ M ≤ 3000.

Large dataset

T = 1.
1 ≤ N ≤ 80000.
0 ≤ M ≤ 3000.

Sample


Input 
 

Output 
 
3
3
gl
t3
t3
3
1 2 217
3 2 567
1 1 21
2
2 1
2 3
4
ca
bl
bl
8z
0
3
1 2
2 3
1 1
8
re
b7
ye
gr
0l
0l
ye
b7
7
4 1 19
2 4 21
2 5 317
4 5 34
4 7 3
4 8 265
8 6 71
3
4 3
2 6
1 4
Case #1:
-1
0
Case #2:
-1
0
0
Case #3:
3
55
-1

Analysis:
Classical problem, calculate the shortest paths between every nodes. Use Floyd's algorithm, the time complexity O(n^3).

The trick is for the large data set, if we create an array of dimension [80000][80000], it is either too slow or take too much space. Notice the limitation of the room color, there are no more than (26+10)*(26+10) + (26+10) colors, if we calculate the shortest distance in the color space instead of the room number space, the problem is reduced to compute an array of no more than [1400][1400] (1332 would be more accurate). 

My solution:(Your opinion is highly appreciated).

package codeJam.google.com;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author Zhenyi 2013 Dec 22, 2013 16:18:30 PM
 */
public class SpaceshipDefence {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new
		FileReader("C:/Users/Zhenyi/Downloads/E-small-practice.in"));
		FileWriter out = new
		FileWriter("C:/Users/Zhenyi/Downloads/E-small-practice.out");
//		BufferedReader in = new BufferedReader(new FileReader(
//				"C:/Users/Zhenyi/Downloads/E-large-practice.in"));
//		FileWriter out = new FileWriter(
//				"C:/Users/Zhenyi/Downloads/E-large-practice.out");

		int T = new Integer(in.readLine());

		for (int cases = 1; cases <= T; cases++) {
			int N = new Integer(in.readLine());
			int[] roomColor = new int[N];
			int[][] shortestDist = new int[1400][1400];
			for (int i = 0; i < 1400; i++) {
				for (int j = 0; j < 1400; j++) {
					shortestDist[i][j] = Integer.MAX_VALUE;
					if (i == j)
						shortestDist[i][j] = 0;
				}
			}
			HashMap<String, Integer> colorMap = new HashMap<String, Integer>();
			for (int i = 0; i < N; i++) {
				String st = in.readLine();
				if (!colorMap.containsKey(st)) {
					int t = colorMap.size();
					colorMap.put(st, t);
				}
				roomColor[i] = colorMap.get(st);
			}

			Integer M = new Integer(in.readLine());
			for (int i = 0; i < M; i++) {
				String[] st = in.readLine().split("\\s");
				Integer a = new Integer(st[0]) - 1;
				Integer b = new Integer(st[1]) - 1;
				Integer value = new Integer(st[2]);
				if (shortestDist[roomColor[a]][roomColor[b]] > value) {
					shortestDist[roomColor[a]][roomColor[b]] = value;
				}
			}
			for (int k = 0; k <= colorMap.size(); k++) {
				for (int i = 0; i <= colorMap.size(); i++) {
					for (int j = 0; j <= colorMap.size(); j++) {
						if (shortestDist[i][k] != Integer.MAX_VALUE
								&& shortestDist[k][j] != Integer.MAX_VALUE
								&& shortestDist[i][k] + shortestDist[k][j] < shortestDist[i][j]) {
							shortestDist[i][j] = shortestDist[i][k]
									+ shortestDist[k][j];
						}
					}
				}
			}

			Integer s = new Integer(in.readLine());
			out.write("Case #" + cases + ":\n");
			for (int i = 0; i < s; i++) {
				String[] st = in.readLine().split("\\s");
				Integer start = new Integer(st[0]) - 1;
				Integer end = new Integer(st[1]) - 1;
				int result = shortestDist[roomColor[start]][roomColor[end]];
				if (result == Integer.MAX_VALUE) {
					result = -1;
				}
				out.write(result + "\n");
			}
		}
		in.close();
		out.flush();
		out.close();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值