Google 10月份在线笔试ProblemB(个人代码,未必最优,请不吝赐教)
2014-10-25 14:47
281 查看
Problem
Tom is taking metros in the city to go from station to station.The metro system in the city works like this:
There are N metro lines in the city: line 1, line 2, ..., line N.
For each metro i, there are SNi stations. Let's assume they are Si,1,Si,2, ... , Si,SNi.
These stations are ordered from one end point to the other end point. The metro is running in both directions. In other words, the metro is going from Si,1 -> Si,2 -> ... -> Si,SNi,
and Si,SNi -> Si,SNi-1 -> ... -> Si,1. You can take the metro from any station and get off at any station. It takes a certain time to travel from one
station to the next station. It takes Timei,1 minutes to travel from Si,1 to Si,2, Timei,2 minutes to travel from Si,2 to Si,3,
etc. It takes the same time in the other direction.
There are M transfer tunnels. Each transfer tunnel connects two stations of different metro lines. It takes a certain amount of time to travel through a tunnel in either direction. You can get off the metro
at one end of the tunnel and walk through the tunnel to the station at the another end.
When you arrive at a metro station of line i, you need to wait Wi minutes for the next metro.
Now, you are going to travel from one station to another. Find out the shortest time you need.
Input
The first line of the input gives the number of test cases, T. T test cases follow.Each test case starts with an integer N, the number of metro lines. N metros descriptions follow. Each metro description starts with two integers SNi and Wi, the number
of stations and the expected waiting time in minutes. The next line consists of SNi-1 integers,Timei,1, Timei,2, ..., Timei,SNi-1, describing
the travel time between stations.
After the metro descriptions, there is an integer M, the number of tunnels. M lines follow to describe the tunnels. Each tunnel description consists of 5 integers, m1i, s1i, m2i, s2i, tiwhich
means the tunnel is connecting stations Sm1i,s1i and station Sm2i,s2i. The walking time of the tunnel is ti.
The next line contains an integer Q, the number of queries. Each of the next Q lines consists of 4 integers, x1, y1, x2, y2, which mean you are going to travel
from stationSx1,y1 to station Sx2,y2.
Output
For each test case, output one line containing "Case #x:", where x is the test case number (starting from 1), then followed by Q lines, each line containing an integer y which is the shortesttime you need for that query. If it's impossible, output -1 for that query instead.
Limits
1 ≤ T ≤ 100.1 ≤ Wi ≤ 100.
1 ≤ Timei,j ≤ 100.
1 ≤ m1i ≤ N.
1 ≤ s1i ≤ SNm1i.
1 ≤ m2i ≤ N.
1 ≤ s2i ≤ SNm2i.
m1i and m2i will be different.
1 ≤ ti ≤ 100.
1 ≤ Q ≤ 10.
1 ≤ x1 ≤ N.
1 ≤ y1 ≤ SNx1.
1 ≤ x2 ≤ N.
1 ≤ y2 ≤ SNy2.
Station Sx1,y1 and station Sx2,y2 will be different.
Small dataset
1 ≤ N ≤ 10.0 ≤ M ≤ 10.
2 ≤ SNi ≤ 100.
The total number of stations in each case is at most 100.
Large dataset
1 ≤ N ≤ 100.0 ≤ M ≤ 100.
2 ≤ SNi ≤ 1000.
The total number of stations in each case is at most 1000.
Sample
Input | Output |
2 2 5 3 3 5 7 3 4 2 1 1 1 1 1 2 2 2 1 1 1 1 2 4 2 5 3 3 5 7 3 4 2 1 1 1 2 1 2 2 2 1 2 4 1 4 1 1 1 1 1 5 | Case #1: 11 Case #2: 18 |
wait 3 minutes for line 1 and get on it.
take it for 3 minutes and get off at station 2.
take the tunnel and walk for 1 minute to station 2 of line 2.
wait 2 minutes for line 2 and get on it.
take it for 2 minutes and get off at station 4.
The total time is: 3+3+1+2+2=11.
解法:
本题看上去很复杂,其实思想不难,不过考到了很多很细的思维盲点,自己写出的代码出了很多次bug,bug点在代码里都有标注。本题的核心算法其实是很简单的有向带权图的最短路径算法。
具体代码如下,大小集合通用:
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <queue>
using namespace std;
#define MAXMETRO 105
#define STATIONPER 1010
#define ALLSTATIONNUM MAXMETRO*STATIONPER*2
int S[MAXMETRO][STATIONPER];
// 记录路径关系感觉还是用map更好一些,因为有重复路径,重复路径其实最短的那条路径才是有效路径
// 用map快速定位重复路径,替换为最短值,这样在搜索的时候可以避免很多不必要的搜索,
// 但是在搜索的时候遍历map的效率可能会稍微低一些,所以这是一个需要权衡的点
map<int, int> times[ALLSTATIONNUM];
int waitTime[MAXMETRO];
#define TIMESMAX 2147483647
int dp[ALLSTATIONNUM];
int totalNum;
int findShortest()
{
int sl, ss, el, es;
cin >> sl >> ss >> el >> es;
sl--; ss--; el--; es--;
for (int i = 0; i < ALLSTATIONNUM; i++)
{
dp[i] = TIMESMAX;
}
int sidx = S[sl][ss] + totalNum;
int eidx = S[el][es] + totalNum;
// bug::此处初始值为0,因为出发站即虚拟站
dp[sidx] = 0;
queue<int> q;
q.push(sidx);
while (!q.empty())
{
int cur = q.front();
q.pop();
for (map<int, int>::iterator it = times[cur].begin();
it != times[cur].end(); it++)
{
int nstation = it->first;
int time = it->second;
if (dp[nstation] > dp[cur] + time)
{
dp[nstation] = dp[cur] + time;
q.push(nstation);
}
}
}
return dp[eidx] == TIMESMAX ? -1 : dp[eidx];
}
void inputData()
{
// input the line basic information
// bug::每次input的时候一定要clear
for (int i = 0; i < ALLSTATIONNUM; i++)
{
times[i].clear();
}
int lines;
cin >> lines;
totalNum = 0;
for (int i = 0; i < lines; i++)
{
int stationNum;
cin >> stationNum >> waitTime[i];
for (int j = 0; j < stationNum - 1; j++)
{
int timetmp;
cin >> timetmp;
int curidx = totalNum + j;
int nextidx = totalNum + j + 1;
times[curidx].insert(make_pair(nextidx, timetmp));
times[nextidx].insert(make_pair(curidx, timetmp));
}
for (int j = 0; j < stationNum; j++)
{
S[i][j] = totalNum + j;
}
S[i][stationNum] = -1;
totalNum += stationNum;
}
for (int i = 0; i < lines; i++)
{
int idx = 0;
while (S[i][idx] >= 0)
{
int virsidx = totalNum + S[i][idx];
int staidx = S[i][idx];
times[virsidx].insert(make_pair(staidx, waitTime[i]));
times[staidx].insert(make_pair(virsidx, 0));
idx++;
}
}
// input the transform tunnels information
int tunnels;
cin >> tunnels;
for (int i = 0; i < tunnels; i++)
{
int sl, ss, el, es, t;
cin >> sl >> ss >> el >> es >> t;
sl--; ss--; el--; es--;
int sstation = S[sl][ss];
int estation = S[el][es];
// bug::换乘除非还要继续乘车,如果是换乘之后直接到目的地的话是不需要再等一段时间的,所以不能这样直接相加。
// bug::因此需要添加一个虚拟站,这个虚拟站表示从外部走到车站的时间,但是这些车站到其对应的虚拟车站的距离应该是0,如果不将其设置成0的话,那么如果没有进行换乘的话就无法走到目的点了。
// 而换乘其实相当于是从某个地铁站走到了虚拟站,而不是从虚拟站走到虚拟站,
// 如果还需要继续往下走的话,那么就需要走到车站,如果不需要的话,那么就算结束。
// bug::这个换乘站的时间居然是可以重复的,但是重复的值不相同,因为我这里是map,所以会清掉上一次的值,所以插入时需要先查询
int sidx = sstation + totalNum;
int eidx = estation + totalNum;
if (times[sstation].find(eidx) != times[sstation].end())
{
if (times[sstation][eidx] < t)
{
continue;
}else
{
times[sstation][eidx] = t;
times[estation][sidx] = t;
times[sidx][eidx] = t;
times[eidx][sidx] = t;
}
}else
{
times[sstation].insert(make_pair(eidx, t));
times[estation].insert(make_pair(sidx, t));
// bug::可以从换乘虚拟站直接到虚拟站,因为是换乘距离,所以是不需要在实际地铁站等的那个时间,那个是乘车才需要等的时间
times[sidx].insert(make_pair(eidx, t));
times[eidx].insert(make_pair(sidx, t));
}
}
}
int main(int argc, const char * argv[]) {
// insert code here...
int T, idx = 0;
cin >> T;
while (idx < T)
{
idx++;
cout << "Case #" << idx << ": " << endl;
inputData();
int Q;
cin >> Q;
for (int i = 0; i < Q; i++)
{
cout << findShortest() << endl;
}
}
return 0;
}
相关文章推荐
- Google 10月份在线笔试ProblemD(个人代码,未必最优,请不吝赐教)
- Google 10月份在线笔试ProblemC(个人代码,未必最优,请不吝赐教)
- Google 10月份在线笔试ProblemA(个人代码,未必最优,请不吝赐教)
- google要在线笔试----bad horse 二分图
- Google中国2014校园招聘笔试Round B China New Grad Test Problem D. Dragon Maze
- Google2015校园招聘在线笔试题2--2048游戏
- 2013年google 在线笔试第五题被虐记。。
- Google中国2014校园招聘笔试Round A China New Grad Test Problem A. Read Phone Number
- PHP在线生成二维码(google api)的代码
- PHP在线生成二维码代码(google api)实例
- 搜狗2013校园招聘java笔试题分析(个人的一点分析拙见,不到之处请不吝赐教)
- Google 2015 校招第四轮在线技术笔试 解题报告
- A利用ASP小偷和Google实现在线翻译功能的代码
- hiho模拟面试题2 - google在线技术笔试模拟 【补提交卡】
- google 2014在线笔试题
- 2015阿里巴巴秋季校园招聘前端 在线笔试题个人参考答案
- PHP在线生成二维码(google api)的实现代码详解
- PHP在线生成二维码代码(google api)
- 2014 google (在线)测试笔试题解
- Google中国2014校园招聘笔试Round A China New Grad Test Problem E. Spaceship Defence