周游世界是件浪漫事,但规划旅行路线就不一定了…… 全世界有成千上万条航线、铁路线、大巴线,令人眼花缭乱。所以旅行社会选择部分运输公司组成联盟,每家公司提供一条线路,然后帮助客户规划由联盟内企业支持的旅行路线。本题就要求你帮旅行社实现一个自动规划路线的程序,使得对任何给定的起点和终点,可以找出最顺畅的路线。所谓“最顺畅”,首先是指中途经停站最少;如果经停站一样多,则取需要换乘线路次数最少的路线。

输入格式:

输入在第一行给出一个正整数N(≤),即联盟公司的数量。接下来有N行,第i行(,)描述了第i家公司所提供的线路。格式为:

M S[1] S[2] ⋯ S[M]

其中M(≤)是经停站的数量,S[i](,)是经停站的编号(由4位0-9的数字组成)。这里假设每条线路都是简单的一条可以双向运行的链路,并且输入保证是按照正确的经停顺序给出的 —— 也就是说,任意一对相邻的S[i]和S[i+1](,)之间都不存在其他经停站点。我们称相邻站点之间的线路为一个运营区间,每个运营区间只承包给一家公司。环线是有可能存在的,但不会不经停任何中间站点就从出发地回到出发地。当然,不同公司的线路是可能在某些站点有交叉的,这些站点就是客户的换乘点,我们假设任意换乘点涉及的不同公司的线路都不超过5条。

在描述了联盟线路之后,题目将给出一个正整数K(≤),随后K行,每行给出一位客户的需求,即始发地的编号和目的地的编号,中间以一空格分隔。

输出格式:

处理每一位客户的需求。如果没有现成的线路可以使其到达目的地,就在一行中输出“Sorry, no line is available.”;如果目的地可达,则首先在一行中输出最顺畅路线的经停站数量(始发地和目的地不包括在内),然后按下列格式给出旅行路线:

Go by the line of company #X1 from S1 to S2.
Go by the line of company #X2 from S2 to S3.
......

其中Xi是线路承包公司的编号,Si是经停站的编号。但必须只输出始发地、换乘点和目的地,不能输出中间的经停站。题目保证满足要求的路线是唯一的。

输入样例:

4
7 1001 3212 1003 1204 1005 1306 7797
9 9988 2333 1204 2006 2005 2004 2003 2302 2001
13 3011 3812 3013 3001 1306 3003 2333 3066 3212 3008 2302 3010 3011
4 6666 8432 4011 1306
4
3011 3013
6666 2001
2004 3001
2222 6666

输出样例:

2
Go by the line of company #3 from 3011 to 3013.
10
Go by the line of company #4 from 6666 to 1306.
Go by the line of company #3 from 1306 to 2302.
Go by the line of company #2 from 2302 to 2001.
6
Go by the line of company #2 from 2004 to 1204.
Go by the line of company #1 from 1204 to 1306.
Go by the line of company #3 from 1306 to 3001.
Sorry, no line is available.
深搜寻找最佳路线。 代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstdlib>
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int> pa;
int n,m,k;
vector<pa> z[];///邻接表记录邻接点以及属于第几家公司
int l[],path[];///l记录某个站属于第几家公司 如果是中转站值为-1 path记录dfs过程中的站点
bool vis[];
int ans[],cnt,lnum;
vector<int> s;
int line(int a,int b) {///返回a->b属于第几家公司的路线
for(int i = ;i < z[a].size();i ++) {
if(z[a][i].first == b) return z[a][i].second;
}
}
void dfs(int st,int en,int snum,int tnum,int lastline) {
path[snum] = st;
if(st == en) {
if(snum < cnt || snum == cnt && tnum < lnum) {
cnt = snum;
lnum = tnum;
for(int i = ;i <= cnt;i ++) {
ans[i] = path[i];
}
}
return;
}
for(int i = ;i < z[st].size();i ++) {
int d = z[st][i].first;
if(vis[d]) continue;
vis[d] = true;
if(l[st] == -) {
int e = line(path[snum],d);
if(e != lastline) {
dfs(d,en,snum + ,tnum + ,e);
}
else dfs(d,en,snum + ,tnum,lastline);
}
else dfs(d,en,snum + ,tnum,lastline);
vis[d] = false;
}
}
int main() {
scanf("%d",&n);
for(int i = ;i < n;i ++) {
scanf("%d",&m);
int a = -,b;
scanf("%d",&a);
if(l[a]) l[a] = -;
else l[a] = i + ;
if(z[a].empty()) s.push_back(a);
for(int j = ;j < m;j ++) {
scanf("%d",&b);
if(l[b]) l[b] = -;
else l[b] = i + ;
if(z[b].empty()) s.push_back(b);
z[a].push_back(pa(b,i + ));
z[b].push_back(pa(a,i + ));
a = b;
}
}
scanf("%d",&k);
while(k --) {
int a,b;
cnt = inf;
lnum = inf;
scanf("%d%d",&a,&b);
dfs(a,b,,,);
if(cnt == inf) {
printf("Sorry, no line is available.\n");
}
else {
printf("%d\n",cnt);
for(int i = ;i <= cnt;i ++) {
if(i == cnt || l[ans[i]] == - && line(ans[i - ],ans[i]) != line(ans[i],ans[i + ])) {
printf("Go by the line of company #%d from %04d to %04d.\n",line(ans[i - ],ans[i]),a,ans[i]);
a = ans[i];
}
}
}
}
}

再次做,本来只想记录每个站点都属于哪些公司,但是有个测试点不对,两个站点即使相邻,可能也会同时属于不同的公司。。应该是这样,记录两个相邻站点属于的公司就对了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int> pa;
int n,m;
vector<pa> mp[];
vector<int> ans,path,lans,line;
bool vis[];
int min_,tmin_;
int check(int a,int b) {
for(int i = ;i < mp[a].size();i ++) {
if(mp[a][i].first == b) return mp[a][i].second;
}
}
void dfs(int now,int en,int num,int tnum,int last) {
if(now == en) {
if(num < min_ || num == min_ && tnum < tmin_) {
min_ = num;
tmin_ = tnum;
ans = path;
lans = line;
}
return;
}
for(int i = ;i < mp[now].size();i ++) {
if(vis[mp[now][i].first]) continue;
int d = check(now,mp[now][i].first);
vis[mp[now][i].first] = true;
if(last != d) {
line.push_back(d);
path.push_back(now);
dfs(mp[now][i].first,en,num + ,tnum + ,d);
path.pop_back();
line.pop_back();
}
else dfs(mp[now][i].first,en,num + ,tnum,last);
vis[mp[now][i].first] = false;
}
}
int main() {
int a,b,k;
scanf("%d",&n);
for(int i = ;i < n;i ++) {
scanf("%d",&m);
scanf("%d",&a);
for(int j = ;j < m;j ++) {
scanf("%d",&b);
mp[a].push_back(pa(b,i + ));
mp[b].push_back(pa(a,i + ));
a = b;
}
}
scanf("%d",&k);
for(int i = ;i < k;i ++) {
tmin_ = min_ = inf;
path.clear();
line.clear();
scanf("%d%d",&a,&b);
vis[a] = true;
dfs(a,b,,,);
vis[a] = false;
if(min_ == inf) {
printf("Sorry, no line is available.\n");
}
else {
printf("%d\n",min_);
if(a != b) ans.push_back(b);
for(int j = ;j < ans.size();j ++) {
printf("Go by the line of company #%d from %04d to %04d.\n",lans[j - ],ans[j - ],ans[j]);
}
}
}
return ;
}

L3-014 周游世界 (30 分)的更多相关文章

  1. PTA 07-图5 Saving James Bond - Hard Version (30分)

    07-图5 Saving James Bond - Hard Version   (30分) This time let us consider the situation in the movie ...

  2. PTA 社交网络图中结点的“重要性”计算(30 分)

    7-12 社交网络图中结点的“重要性”计算(30 分) 在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络中相互连接的结点之间蔓延的一种相互 ...

  3. L3-015 球队“食物链” (30 分)

    L3-015 球队“食物链” (30 分)   某国的足球联赛中有N支参赛球队,编号从1至N.联赛采用主客场双循环赛制,参赛球队两两之间在双方主场各赛一场. 联赛战罢,结果已经尘埃落定.此时,联赛主席 ...

  4. PAT A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  5. 04-树6 Complete Binary Search Tree(30 分)

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

  6. PTA 7-2 二叉搜索树的结构(30 分)

    7-2 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大 ...

  7. 1127 ZigZagging on a Tree (30 分)

    1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...

  8. 【PAT】1053 Path of Equal Weight(30 分)

    1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight W​i​​ assigned t ...

  9. 【PAT】1091 Acute Stroke(30 分)

    1091 Acute Stroke(30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the s ...

随机推荐

  1. python中的shallow copy 与 deep copy

    今天在写代码的时候遇到一个奇葩的问题,问题描述如下: 代码中声明了一个list,将list作为参数传入了function1()中,在function1()中对list进行了del()即删除了一个元素. ...

  2. 什么是ASCII码文本文件

    标准ASCII码方式(也称文本方式)存储的文件,更确切地说,英文.数字等字符存储的是ASCII码.文本文件中除了存储文件有效字符信息(包括能用ASCII码字符表示的回车.换行等信息)外,不能存储其他任 ...

  3. ansible之template模块

    趁着最近在搞ansible,现在学习了一波template模块的用法: 1.使用template模块在jinja2中引用变量,先来目录结构树 [root@master ansible]# tree . ...

  4. windows下利用批处理脚本监控程序

    1.要监控的程序为使用cygwin环境编译的exe可执行文件hello.exe,源码如下: #include <stdio.h> #include <unistd.h> voi ...

  5. Spring ApplicationListener使用方法及二次调用问题解决

    使用场景 在一些业务场景中,当容器初始化完成之后,需要处理一些操作,比如一些数据的加载.初始化缓存.特定任务的注册等等.这个时候我们就可以使用Spring提供的ApplicationListener来 ...

  6. vim 安装Vundle.vim

    1.下载 git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim 2.配置vimrc set no ...

  7. Morgan logger

    var logger = require("morgan"); res.render = function(view, options, fn){ options = option ...

  8. Android之微信支付

    Android开发中,大多数电商APP都会有支付这么模块,此博客就讲一下微信支付,代码不多,很简单就可以完成,支付宝支付请看博客 Android支付之支付宝封装类 先来看看效果图 微信支付首先要去微信 ...

  9. codefroce385E矩阵快速幂

    状态变化  (x,y,dx,dy,i) 表示i时刻熊站在(x,y)处速度向量(dx,dy)下一个状态是 ( 2x+y+dx+i , x+2y+dy+i , x+y+dx , x+y+dy , i+1 ...

  10. UVA-11613 Acme Corporation (最大费用最大流+拆点)

    题目大意:有一种商品X,其每每单位存放一个月的代价I固定.并且已知其每月的最大生产量.生产每单位的的代价.最大销售量和销售单价,还已知每个月生产的X能最多能存放的时间(以月为单位).问只考虑前m个月, ...