1131 Subway Map

link

#include <iostream>
#include <cstring>
#include <vector>
#include <unordered_map>
#include <climits>
# define LL long long
using namespace std;

vector<vector<int>> adj(10000);
unordered_map<int,unordered_map<int,int>> line;
vector<int> finalpath;
int transfers;
int mincnt;
int visited[10000];

int countTransfers(vector<int>& path){
    int preline=0;
    int res=0;
    for(int i=0;i<path.size()-1;i++){
        int u=path[i];
        int v=path[i+1];
        int curline=line[u][v];
        if(preline==0){
            preline=curline;
            continue;
        }
        if(curline!=preline){
            preline=curline;
            res++;
        }
    }
    return res;
}

void dfs(int u, int v, int cnt, vector<int>& path){
    if(visited[u]==1) return;
    if(cnt>mincnt) return;
    if(u==v){
        path.push_back(v);
        if(cnt<mincnt){
            mincnt=cnt;
            finalpath=path;
            transfers=countTransfers(path);
        }else if(cnt==mincnt){
            int tmp=countTransfers(path);
            if(tmp<transfers){
                transfers=tmp;
                finalpath=path;
            }
        }
        path.pop_back();
        return;
    }

    visited[u]=1;
    path.push_back(u);
    for(int next:adj[u]){
        dfs(next,v,cnt+1,path);
    }
    visited[u]=0;
    path.pop_back();
}

int main(){
    int N;
    cin>>N;
    for(int i=1;i<=N;i++){
        int cnt;
        cin>>cnt;
        int pre;
        cin>>pre;
        for(int j=1;j<cnt;j++){
            int cur;
            cin>>cur;
            adj[pre].push_back(cur);
            adj[cur].push_back(pre);
            line[cur][pre]=i;
            line[pre][cur]=i;
            pre=cur;
        }
    }

    int K;
    cin>>K;
    for(int i=0;i<K;i++){
        memset(visited,0,sizeof(visited));
        int u,v;
        cin>>u>>v;
        vector<int> path;
        mincnt=INT_MAX;
        dfs(u,v,0,path);
        printf("%d\n", finalpath.size()-1);
        int preline=0;
        int start=u;
        for(int j=0;j<finalpath.size()-1;j++){
            int a=finalpath[j];
            int b=finalpath[j+1];
            int curline=line[a][b];
            if(preline==0){
                preline=curline;
            }else{
                if(preline!=curline){
                    printf("Take Line#%d from %04d to %04d.\n", preline,start,a);
                    start=a;
                    preline=curline;
                }
            }
            if(b==v){
                printf("Take Line#%d from %04d to %04d.\n", curline,start,v);
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/FEIIEF/p/12505763.html

时间: 2024-08-30 17:06:02

1131 Subway Map的相关文章

PAT甲级——1131 Subway Map (30 分)

可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30 分) In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing

1131 Subway Map (30 分)

1131 Subway Map (30 分) In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now you are supposed to help people with your computer skills! Given th

1131 Subway Map(30 分)

In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now you are supposed to help people with your computer skills! Given the starting position of

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

选择一个 HTTP 状态码不再是一件难事 – Racksburg

原文链接:http://racksburg.com/choosing-an-http-status-code/ 打开双语对照阅读 有什么能比 HTTP 响应状态码更简单呢?页面渲染了吗?好极了,返回 200.页面不存在?那么是 404.想要跳转到另一个页面?302 或者可能是 301. 我喜欢把 HTTP 状态码想象成无线电波传输的 10 码1.“呼叫,呼叫,我是 White Chocolate Thunder,发现 200 OK.” —— Aaron Patterson (@tenderlov

选择一个 HTTP 状态码不再是一件难事 – Racksburg《转载》

本文转载自:众成翻译 译者:十年踪迹 链接:http://www.zcfy.cc/article/904 原文:http://racksburg.com/choosing-an-http-status-code/ 有什么能比 HTTP 响应状态码更简单呢?页面渲染了吗?好极了,返回 200.页面不存在?那么是 404.想要跳转到另一个页面?302 或者可能是 301. 我喜欢把 HTTP 状态码想象成无线电波传输的 10 码<sup>1</sup>.“呼叫,呼叫,我是 White

如何选择正确的HTTP状态码

本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2015/12/how-to-choose-http-status-code 众所周知,每一个HTTP响应都会带有一个状态码,不过对于很多开发者来说,平时使用最多的几个状态码无外乎就是200.400.404.500等.那其他众多状态码该应用在何种场景中,什么时候应该使用哪些状态码就成为一个值得我们深入思考的问题了.即便在Facebook这样的公司中,那些聪明的开发者所构建的API也可能

地铁线路项目设计与分析

#地铁线路项目设计与分析 ##一.项目介绍 #####实现一个帮助进行地铁出行路线规划的命令行程序 ##二.项目计划表 | PSP2.1 | Personal Software Process Stages | Time | | --- | --- | --- | | Planning | 计划 | | · Estimate | · 估计这个任务需要多少时间 | 2h | Development | 开发| | · Analysis | · 需求分析 (包括学习新技术) | 6h | · Des

地铁路线规划的简要分析

地铁路线规划的简要分析 github blog 需求分析 1.设计地铁线路的文件存储格式 2.设计一个支持显示地铁线路与计算换乘的程序 3.设计可以通过命令行启动这个程序 4.设计一条路线的查询功能 5.设计支持查询的一个命令行 6.设计两个站点间最短路径的查询功能 7.设计让程序将结果写入文本文件的命令行 8.性能测试 9.PSP表格记录各模块花费时间 PSP 2.1 Personal Software Process Stages Time Planning 计划     · Estimat