[HackerRank] Rust&Murderer 稀疏图最短路径问题

3月份一个偶然的机会刷了一道算法题,当时折腾了好久,趁现在有空赶紧记录一下。
原题地址:Rust&Murderer

题目描述

Detective Rust is investigating a homicide and he wants to chase down the murderer. The murderer knows he would definitely get caught if he takes the main roads for fleeing, so he uses the village roads (or side lanes) for running away from the crime scene.

Rust knows that the murderer will take village roads and he wants to chase him down. He is observing the city map, but it doesn't show the village roads (or side lanes) on it and shows only the main roads.

The map of the city is a graph consisting nodes (labeled to ) where a specific given node represents the current position of Rust and the rest of the nodes denote other places in the city, and an edge between two nodes is a main road between two places in the city. It can be suitably assumed that an edge that doesn't exist/isn't shown on the map is a village road (side lane). That means, there is a village road between two nodes and iff(if and only if) there is no city road between them.

Rust wants to calculate the shortest distance from his position (Node ) to all the other places in the city if he travels using the village roads (side lanes).

Note: The graph/map of the city is ensured to be a sparse graph.

分析

问题本质上就是给定一个无权无向图,从S点出发,只能走不连通的路径(side lane),求S点到每个点的最短路径。

  • 基础思路是:用一个map存储所有的不连通的路径,然后用最短路径算法求出S点到每个点的路径。但是注意原始图是一个稀疏图,存储不连通的路径的话不会卡时间但是会卡空间
  • 因为是无权图,不需要用Dijkstra算法,直接BFS即可。BFS的方法就是用一个队列来存储每一层的结点,就像二叉树的层次遍历那样。
  • 每次执行BFS时,从S出发,每下降一层距离就加1 。 关键是怎么知道某个节点到哪些节点不连通。为了解决卡空间的问题,可以构造一个hashset来存储没有访问过的节点。每次从这个hashset里遍历,看是不是连通。这样就OK了。

代码实现

#include <cmath>
#include <cstdio>
#include <vector>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int caseNumber;
    cin >> caseNumber;
    for (int i = 0; i < caseNumber; i ++){
        int cityNumber;
        int pathNumber;
        cin >> cityNumber >> pathNumber;

        unordered_map<int, unordered_set<int> > mainPath;
        unordered_set<int> unvisitedCities;

        for (int j = 1; j < cityNumber + 1; j ++){
            unvisitedCities.insert(j);
        }
       
        for (int j = 0; j < pathNumber; j ++){
            int src;
            int dst;
            cin >> src >> dst;
            mainPath[src].insert(dst);
            mainPath[dst].insert(src);
        }
       
        int derectiveLocation;
        cin >> derectiveLocation;
        int distance[cityNumber + 1];
        for (int j = 0; j < cityNumber + 1; j ++)
            distance[j] = -1;
        distance[derectiveLocation] = 0;
        queue<int> bfsQueue;

        bfsQueue.push(derectiveLocation);

        while (bfsQueue.size() && unvisitedCities.size()){
            int front = bfsQueue.front();
            unordered_set<int> tempSet(unvisitedCities);
            for (auto p : mainPath[front]){
                tempSet.erase(p);
            }
            for (auto index : tempSet) {
                if (distance[index] == -1) {
                    bfsQueue.push(index);
                    distance[index] = distance[front] + 1;
                }
            }
            for (auto c : tempSet){
                unvisitedCities.erase(c);
            } 
            bfsQueue.pop();
        }
        int realDistance[cityNumber ];
        for (int j = 1; j < derectiveLocation; j ++){
            realDistance[j] = distance[j];
        }
        for (int j = derectiveLocation + 1; j < cityNumber + 1; j ++){
            realDistance[j - 1] = distance[j];
        }
        printf("%d", realDistance[1]);
        for (int j = 2; j < cityNumber; j ++){
            printf(" %d", realDistance[j]);
        }
        printf("\n");
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容

  • 一 德尔终于完成了他的航拍试飞,这时已经是下午五点了。 美娜第一次找到德尔并提出请求他为自己居住了二十年马上就要...
    五色浮元子_阅读 415评论 0 3
  • 关于朋友这个定义,自古以来有太多。肯为你两肋插刀的那是生死之交,肯为你雪中送炭的那是患难之交。肯为你摔琴绝弦终生不...
    苍山一片云阅读 258评论 0 0
  • 反复发作 无法入睡
    秋笑羽阅读 118评论 0 0