思路:DFS遍历

 #include <iostream>
#include <map>
#include <vector>
#include <cstdio>
using namespace std; const int INF = 0x7fffffff;
const int maxn = ; struct Node{
int line, s, e;
Node(int xl, int xs, int xe) :line(xl), s(xs), e(xe) {}
}; struct WhichLine {
int u, v;
WhichLine(int _u, int _v) :u(_u), v(_v) {}
friend bool operator <(WhichLine a,WhichLine b) {//const whichLine &a, const whichLine &b
                                 //如果只是whichLine &a, whichLine &b报错
if (a.u != b.u) return a.u < b.u;
else return a.v < b.v;
}
};
map<WhichLine, int> searchLine;//判断两个站点在哪条线上
vector<int> G[maxn];//图的邻接表
bool visit[maxn];//判断是否已经访问数组
int minStation, minTransfer;//最少的站点,最少的换乘
vector<Node> ans, temp;//最终结果路径和中间记录路径
int st, ed; void DFS(int head, int now, int stationCnt, int pre) {
if (now == ed) {
if (stationCnt < minStation || stationCnt == minStation && temp.size() < minTransfer) {
minStation = stationCnt;
minTransfer = temp.size();
ans = temp;
ans.push_back(Node(searchLine[WhichLine(pre, now)], head, now));
return;
}
} visit[now] = true;
for (auto next : G[now]) {
if (visit[next]) continue;
if (pre != now && searchLine[WhichLine(pre, now)] != searchLine[WhichLine(now, next)]) { temp.push_back(Node(searchLine[WhichLine(pre, now)], head, now));
DFS(now, next, stationCnt + , now);
temp.pop_back();
}
else DFS(head, next, stationCnt + , now);
} visit[now] = false;
} int main(){
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i){
int k, u, v;
scanf("%d", &k);
for (int j = ; j < k; j++) {
scanf("%d", &v);
if (j > ) {
searchLine[WhichLine(u, v)] = i;//两个相邻节点的线路
searchLine[WhichLine(v, u)] = i;
G[u].push_back(v);
G[v].push_back(u);
}
u = v;
}
}
int q;
scanf("%d", &q);
while (q--){
scanf("%d %d", &st, &ed);
minStation = minTransfer = INF;
DFS(st, st, , st);
printf("%d\n", minStation);
for (auto x : ans) printf("Take Line#%d from %04d to %04d.\n", x.line, x.s, x.e);
}
return ;
}

1131(★、※)Subway Map的更多相关文章

  1. Java集合(十)实现Map接口的HashMap

    Java集合(十)继承Map接口的HashMap 一.HashMap简介(基于JDK1.8) HashMap是基于哈希表(散列表),实现Map接口的双列集合,数据结构是“链表散列”,也就是数组+链表 ...

  2. .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法

    .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...

  3. [SoapUI] Post请求Body里面限制特殊字符(&、%),Groovy脚本里特殊字符需要添加“\”转义($)。

    SoapUI的Post请求,在body里面不能包含(&.%),如果含有这些特殊字符,请求会报错:在添加的Groovy脚本里有些特殊字符需要使用“\”转义($),不然也会报错.

  4. 从头认识java-15.1 填充容器(3)-填充Map

    这一章节我们来讨论一下填充容器的还有一个方面Map.之前的两个章节我们都是用list来作为容器.这一章节我们使用Map. 还有在这里解释一下为什么一直都使用生成器这个东西,事实上他就是建造者设计模式, ...

  5. Javase之集合体系(4)之Map集合

    集合体系之Map集合 ##Map<K,V>( 接口 ) 特点:将键映射到值对象,一个映射不能包含重复的键:每个键只能映射一个值 Map集合与Collection集合的区别 ​ Map集合存 ...

  6. 【Go入门教程2】内置基础类型(Boolean、数值、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值

    这小节我们将要介绍如何定义变量.常量.Go内置类型以及Go程序设计中的一些技巧. 定义变量 Go语言里面定义变量有多种方式. 使用var关键字是Go最基本的定义变量方式,与C语言不同的是Go把变量类型 ...

  7. 【Android】RxJava的使用(三)转换——map、flatMap

    前两篇Android RxJava的使用(一)基本用法.Android RxJava的使用(二)Action介绍了RxJava的基本用法,对Rxjava还不了解的请先看以上两篇.这篇为大家讲解RxJa ...

  8. 【Go入门教程4】变量(var),常量(const),内置基础类型(Boolean、数值 byte,int,rune、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值

    这小节我们将要介绍如何定义变量.常量.Go 内置类型以及 Go 程序设计中的一些技巧. 定义变量 Go 语言里面定义变量有多种方式. 使用 var 关键字是 Go 最基本的定义变量方式,与 C 语言不 ...

  9. (转)百度Map API

    转自  http://blog.sina.com.cn/s/blog_6079f38301013sb3.html 一.与地图操作相关的接口哦! (这些接口的开启都是写在执行成功的回调函数那里) map ...

随机推荐

  1. Django框架简介-开头

    一.MVC框架和MTV框架(了解即可) MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制 ...

  2. Matlab:高阶常微分三种边界条件的特殊解法(隐式Euler)

    函数文件1: function b=F(f,x0,u,h) b(1,1)=x0(1)-h*x0(2)-u(1); b(2,1)=x0(2)+h*x0(1)^2-u(2)-h*f; 函数文件2: fun ...

  3. MySQL造数据脚本-亲试

    DELIMITER $$CREATE DEFINER=`root`@`192.168.2.254` PROCEDURE `pjzzspdz_fpmx_initdata12101245`()BEGIN ...

  4. Grafana + Prometheus 监控PostgreSQL

    效果图 部署环境 服务器名称 IP地址 部署业务 备注 部署agent sht-sgmhadoopcm-01 172.16.101.54 PostgreSQL 监控服务器.被监控服务器 node_ex ...

  5. linux如何让一个程序崩溃后自动重启

    思路:  写一个脚本 监控程序的运行状态  没有运行启动运行 已运行不做操作. 如果在控制台启动脚本 注意必须  nohup sh xxx.sh & while true do ps -ef ...

  6. vue中提示toFixed不是函数

     vue中toFixed获取小数点后两位 错误提示:.toFixed is not a function解决办法:Number(_this.group_cash).toFixed(2) 转自:http ...

  7. 一次完整的http事务的过程

    1.域名解析 2.发起TCP三次握手 3.建立TCP连接以后发起http请求 4.服务器端响应请求,浏览器得到html代码 5.浏览器解析html代码并请求html中的资源 6.浏览器对页面进行渲染呈 ...

  8. windows线程池

    #define _WIN32_DCOM #include <SDKDDKVer.h> #include <direct.h> #include <thr/threads. ...

  9. python之路--迭代器和生成器

    迭代: 迭代器协议: 1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前退) 2.可迭代 ...

  10. angualr Material Icons

    首先需要项目引入 angualr meterial icons的资源库 图标资源链接 https://klarsys.github.io/angular-material-icons/ <md- ...