1131(★、※)Subway Map
思路: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的更多相关文章
- Java集合(十)实现Map接口的HashMap
Java集合(十)继承Map接口的HashMap 一.HashMap简介(基于JDK1.8) HashMap是基于哈希表(散列表),实现Map接口的双列集合,数据结构是“链表散列”,也就是数组+链表 ...
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
- [SoapUI] Post请求Body里面限制特殊字符(&、%),Groovy脚本里特殊字符需要添加“\”转义($)。
SoapUI的Post请求,在body里面不能包含(&.%),如果含有这些特殊字符,请求会报错:在添加的Groovy脚本里有些特殊字符需要使用“\”转义($),不然也会报错.
- 从头认识java-15.1 填充容器(3)-填充Map
这一章节我们来讨论一下填充容器的还有一个方面Map.之前的两个章节我们都是用list来作为容器.这一章节我们使用Map. 还有在这里解释一下为什么一直都使用生成器这个东西,事实上他就是建造者设计模式, ...
- Javase之集合体系(4)之Map集合
集合体系之Map集合 ##Map<K,V>( 接口 ) 特点:将键映射到值对象,一个映射不能包含重复的键:每个键只能映射一个值 Map集合与Collection集合的区别 Map集合存 ...
- 【Go入门教程2】内置基础类型(Boolean、数值、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值
这小节我们将要介绍如何定义变量.常量.Go内置类型以及Go程序设计中的一些技巧. 定义变量 Go语言里面定义变量有多种方式. 使用var关键字是Go最基本的定义变量方式,与C语言不同的是Go把变量类型 ...
- 【Android】RxJava的使用(三)转换——map、flatMap
前两篇Android RxJava的使用(一)基本用法.Android RxJava的使用(二)Action介绍了RxJava的基本用法,对Rxjava还不了解的请先看以上两篇.这篇为大家讲解RxJa ...
- 【Go入门教程4】变量(var),常量(const),内置基础类型(Boolean、数值 byte,int,rune、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值
这小节我们将要介绍如何定义变量.常量.Go 内置类型以及 Go 程序设计中的一些技巧. 定义变量 Go 语言里面定义变量有多种方式. 使用 var 关键字是 Go 最基本的定义变量方式,与 C 语言不 ...
- (转)百度Map API
转自 http://blog.sina.com.cn/s/blog_6079f38301013sb3.html 一.与地图操作相关的接口哦! (这些接口的开启都是写在执行成功的回调函数那里) map ...
随机推荐
- PAT 1073 Scientific Notation
1073 Scientific Notation (20 分) Scientific notation is the way that scientists easily handle very ...
- 八大排序算法——插入排序(动图演示 思路分析 实例代码java 复杂度分析)
一.动图演示 二.思路分析 例如从小到大排序: 1. 从第二位开始遍历, 2. 当前数(第一趟是第二位数)与前面的数依次比较,如果前面的数大于当前数,则将这个数放在当前数的位置上,当前数的下标-1 ...
- 学生信息管理系统(C语言)
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct student ...
- libdl.so 动态库加载、查找
使用libdl.so库 动态库加载原理 动态库中函数的查找已经封装成 libdl.so,有4个函数: dlopen : 打开一个动态库 dlsym : 在打开的动态库里找一个函数 dlclo ...
- 跟随我在oracle学习php(9)
三目运算符:表达式? 表达式: 表达式: 自增在前在后没有影响 参与表达式需要注意 在前先计算,在后最后加1. + 字符串拼接. 字符串转数字:从左到右第一个不是数字的位置结束 取整 parseInt ...
- [HDU6146]Pokémon GO
Problem 有一个2n的方格矩阵 在一个格子上可以往旁边8个方向走(如果有格子),求有多少方案把2n走完 Solution 我们用Fi表示从一个角出发走遍所有格子回到这一列另外一点的方案数 显然, ...
- bfs两种记录路径方法
#include<cstdio> #include<queue> using namespace std; struct sss { int x,y; }ans[][]; ][ ...
- ui自动化:python+appium----环境搭建
前言: appium可以说是app最火的一个自动化框架,它的主要优势是支持android和ios,另外脚本支持java和python.以下为python+appium的安装教程... 环境准备... ...
- 官网下载MySQL最新版本的安装包
下载地址:http://www.mysql.com/downloads/ 1.选择下载社区版本 MySQL Community Edition (GPL)Community (GPL) Downloa ...
- erlang-gb_tree,gb_set
gb_tree, gb_set, 均为一个二叉树.具体怎么实现,这边不在累赘,官方有手册, how to use ? 才是我们的重点 1. 初始化 1> gb_trees:empty().{0, ...