到处出错❌ | 1131 Subway Map (30 分)

1131 Subway Map (30 分)

给定地铁线路,找出最佳路径。最佳两个标尺:

  1. 总经过站数最少
  2. 少换乘
DFS遍历即可。

关键是判断换乘以及用什么方法来存A站-B站属于哪条线路。

直接line[10000][10000]怕是要💥,所以一开始写的是map<pair<int, int>, int> stop_line;并且规定线路存的时候是id小的站放在pair.first, id大的放在second。

然而看了柳神题解后发现:可以进一步降维啊。已知是0000-9999,那就可以用一个int aaaabbbb来存线路了,高四位代表站A,低四位表示站B,map<int, int> stop2_line;

  • ⬇️这是一开始的写法(绕圈判断❌
    25分,测试点3运行超时。
    因为想到维护visited防止绕圈,但是把防止绕圈想成了防止走一样的路,明明是一条路再次走过某站就算绕圈了呜呜呜我在想什么……)
    ⚠️目测降维什么的不是超时的主因,绕圈才是。
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>

#define MAXN 10000
#define INF 0x3fffffff
using namespace std;
vector<int> graph[MAXN];
map<int, int> stop2_line;
map<int, bool> visited;
vector<int> path, temp_path;
int nline, trans_time, min_stops;
int nq, st, ed;

int get_road_id(int c1, int c2) {
    return 10000 * min(c1, c2) + max(c1, c2);
}

int getTransfer() {
    int res = 0, curr_line = -1, len = temp_path.size();
    for (int i = 0; i < len - 1; i++) {
        int line = stop2_line[get_road_id(temp_path[i], temp_path[i + 1])];
        if (line != curr_line) {
            res++;
            curr_line = line;
        }
    }
    return res;
}

void DFS(int root, int stops) {
    if (root == ed) {
        temp_path.emplace_back(root);
        if (stops < min_stops || (stops == min_stops && getTransfer() < trans_time)) {
            path = temp_path;
            min_stops = stops;
            trans_time = getTransfer();
        }
        temp_path.pop_back();
        return;
    }
    temp_path.emplace_back(root);
    for (auto item:graph[root]) {
        int id = get_road_id(root, item);
        if (!visited[id]) {
            visited[id] = true;
            DFS(item, stops + 1);
            visited[id] = false;
        }
    }
    temp_path.pop_back();
}

int main() {
    scanf("%d", &nline);
    for (int i = 1; i <= nline; ++i) {
        int span, curr, next;
        scanf("%d%d", &span, &curr);
        for (int j = 1; j < span; ++j) {
            scanf("%d", &next);
            graph[curr].emplace_back(next);
            graph[next].emplace_back(curr);
            stop2_line[get_road_id(curr, next)] = i;
            curr = next;
        }
    }
    scanf("%d", &nq);
    for (int i = 0; i < nq; ++i) {
        scanf("%d%d", &st, &ed);
        trans_time = INF, min_stops = INF;
        path.clear();
        DFS(st, 1);
        int len = path.size() - 1;
        printf("%d\n", len);
        int mm = min(st, path[1]), MM = max(st, path[1]);
        if (trans_time == 1) {
            printf("Take Line#%d from %04d to %04d.\n", stop2_line[mm * 10000 + MM], st, ed);
        } else {
            int line = stop2_line[mm * 10000 + MM], temp = st;
            for (int j = 2; j <= len; ++j) {

                if (stop2_line[get_road_id(path[j - 1], path[j])] != line) {
                    printf("Take Line#%d from %04d to %04d.\n", line, temp, path[j - 1]);
                    line = stop2_line[get_road_id(path[j - 1], path[j])];
                    temp = path[j - 1];
                }
            }
            printf("Take Line#%d from %04d to %04d.\n", line, temp, ed);
        }
    }
    return 0;
}
  • 改了之后= =,30分,重复经过相同的站即绕圈 = =


    关键代码
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>

#define MAXN 10000
#define INF 0x3fffffff
using namespace std;
vector<int> graph[MAXN];
map<int, int> stop2_line;
bool visited[MAXN];
vector<int> path, temp_path;
int nline, trans_time, min_stops;
int nq, st, ed;

int get_road_id(int c1, int c2) {
    return 10000 * min(c1, c2) + max(c1, c2);
}

int getTransfer() {
    int res = 0, curr_line = -1, len = temp_path.size();
    for (int i = 0; i < len - 1; i++) {
        int line = stop2_line[get_road_id(temp_path[i], temp_path[i + 1])];
        if (line != curr_line) {
            res++;
            curr_line = line;
        }
    }
    return res;
}

void DFS(int root, int stops) {
    if (root == ed) {
        temp_path.emplace_back(root);
        if (stops < min_stops || (stops == min_stops && getTransfer() < trans_time)) {
            path = temp_path;
            min_stops = stops;
            trans_time = getTransfer();
        }
        temp_path.pop_back();
        return;
    }
    temp_path.emplace_back(root);
    for (auto item:graph[root]) {
        if (!visited[item]) {
            visited[item] = true;
            DFS(item, stops + 1);
            visited[item] = false;
        }
    }
    temp_path.pop_back();
}

int main() {
    scanf("%d", &nline);
    for (int i = 1; i <= nline; ++i) {
        int span, curr, next;
        scanf("%d%d", &span, &curr);
        for (int j = 1; j < span; ++j) {
            scanf("%d", &next);
            graph[curr].emplace_back(next);
            graph[next].emplace_back(curr);
            stop2_line[get_road_id(curr, next)] = i;
            curr = next;
        }
    }
    scanf("%d", &nq);
    for (int i = 0; i < nq; ++i) {
        scanf("%d%d", &st, &ed);
        trans_time = INF, min_stops = INF;
        path.clear();
        visited[st] = true;
        DFS(st, 1);
        visited[st] = false;
        int len = path.size() - 1;
        printf("%d\n", len);
        int mm = min(st, path[1]), MM = max(st, path[1]);
        if (trans_time == 1) {
            printf("Take Line#%d from %04d to %04d.\n", stop2_line[mm * 10000 + MM], st, ed);
        } else {
            int line = stop2_line[mm * 10000 + MM], temp = st;
            for (int j = 2; j <= len; ++j) {
                if (stop2_line[get_road_id(path[j - 1], path[j])] != line) {
                    printf("Take Line#%d from %04d to %04d.\n", line, temp, path[j - 1]);
                    line = stop2_line[get_road_id(path[j - 1], path[j])];
                    temp = path[j - 1];
                }
            }
            printf("Take Line#%d from %04d to %04d.\n", line, temp, ed);
        }
    }
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 专业考题类型管理运行工作负责人一般作业考题内容选项A选项B选项C选项D选项E选项F正确答案 变电单选GYSZ本规程...
    小白兔去钓鱼阅读 10,374评论 0 13
  • 一、基础知识:1、JVM、JRE和JDK的区别:JVM(Java Virtual Machine):java虚拟机...
    杀小贼阅读 2,539评论 0 4
  • 选择题部分 1.(),只有在发生短路事故时或者在负荷电流较大时,变流器中才会有足够的二次电流作为继电保护跳闸之用。...
    skystarwuwei阅读 14,300评论 0 7
  • 小CC的家离学校有1000多公里,坐火车要数十个小时。每年春运之时,小CC总要绞尽脑汁寻找最合适的换乘路线。 小C...
    Goshowwww阅读 723评论 0 0
  • 两年多的里皮闹剧唯一值得总结的教训是,不要被一些理想主义的观念左右重大决策。如今的下场原本是在两年前闹剧开场之前就...
    一路向前之缘来如此阅读 318评论 0 0

友情链接更多精彩内容