Google 10月份在线笔试ProblemB(个人代码,未必最优,请不吝赐教)

本文介绍了一个Google在线笔试题目,涉及城市地铁系统的时间优化问题。内容包括地铁线路、站点、等待时间和转乘隧道的描述,以及如何计算从一个站点到另一个站点的最短时间。解题思路是利用有向带权图的最短路径算法解决。

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,2Timei,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, TT 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,1Timei,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, m1is1im2is2itiwhich 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, x1y1x2y2, 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 shortest time 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

In the first case, you are going to travel from station 1 of metro line 1 to station 4 of metro line 2. The best way is:

  • 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;
}


内容概要:本文介绍了ENVI Deep Learning V1.0的操作教程,重点讲解了如何利用ENVI软件进行深度学习模型的训练与应用,以实现遥感图像中特定目标(如集装箱)的自动提取。教程涵盖了从数据准备、标签图像创建、模型初始化与训练,到执行分类及结果优化的完整流程,并介绍了精度评价与通过ENVI Modeler实现一键化建模的方法。系统基于TensorFlow框架,采用ENVINet5(U-Net变体)架构,支持通过点、线、面ROI或分类图生成标签数据,适用于多/高光谱影像的单一类别特征提取。; 适合人群:具备遥感图像处理基础,熟悉ENVI软件操作,从事地理信息、测绘、环境监测等相关领域的技术人员或研究人员,尤其是希望将深度学习技术应用于遥感目标识别的初学者与实践者。; 使用场景及目标:①在遥感影像中自动识别和提取特定地物目标(如车辆、建筑、道路、集装箱等);②掌握ENVI环境下深度学习模型的训练流程与关键参数设置(如Patch Size、Epochs、Class Weight等);③通过模型调优与结果反馈提升分类精度,实现高效自动化信息提取。; 阅读建议:建议结合实际遥感项目边学边练,重点关注标签数据制作、模型参数配置与结果后处理环节,充分利用ENVI Modeler进行自动化建模与参数优化,同时注意软硬件环境(特别是NVIDIA GPU)的配置要求以保障训练效率。
内容概要:本文系统阐述了企业新闻发稿在生成式引擎优化(GEO)时代下的全渠道策略与效果评估体系,涵盖当前企业传播面临的预算、资源、内容与效果评估四大挑战,并深入分析2025年新闻发稿行业五大趋势,包括AI驱动的智能化转型、精准化传播、首发内容价值提升、内容资产化及数据可视化。文章重点解析央媒、地方官媒、综合门户和自媒体四类媒体资源的特性、传播优势与发稿策略,提出基于内容适配性、时间节奏、话题设计的策略制定方法,并构建涵盖品牌价值、销售转化与GEO优化的多维评估框架。此外,结合“传声港”工具实操指南,提供AI智能投放、效果监测、自媒体管理与舆情应对的全流程解决方案,并针对科技、消费、B2B、区域品牌四大行业推出定制化发稿方案。; 适合人群:企业市场/公关负责人、品牌传播管理者、数字营销从业者及中小企业决策者,具备一定媒体传播经验并希望提升发稿效率与ROI的专业人士。; 使用场景及目标:①制定科学的新闻发稿策略,实现从“流量思维”向“价值思维”转型;②构建央媒定调、门户扩散、自媒体互动的立体化传播矩阵;③利用AI工具实现精准投放与GEO优化,提升品牌在AI搜索中的权威性与可见性;④通过数据驱动评估体系量化品牌影响力与销售转化效果。; 阅读建议:建议结合文中提供的实操清单、案例分析与工具指南进行系统学习,重点关注媒体适配性策略与GEO评估指标,在实际发稿中分阶段试点“AI+全渠道”组合策略,并定期复盘优化,以实现品牌传播的长期复利效应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值