5-14 周游世界(24分)
2017-05-14 17:02
281 查看
周游世界是件浪漫事,但规划旅行路线就不一定了…… 全世界有成千上万条航线、铁路线、大巴线,令人眼花缭乱。所以旅行社会选择部分运输公司组成联盟,每家公司提供一条线路,然后帮助客户规划由联盟内企业支持的旅行路线。本题就要求你帮旅行社实现一个自动规划路线的程序,使得对任何给定的起点和终点,可以找出最顺畅的路线。所谓“最顺畅”,首先是指中途经停站最少;如果经停站一样多,则取需要换乘线路次数最少的路线。
输入在第一行给出一个正整数NN(\le
100≤100),即联盟公司的数量。接下来有NN行,第ii行(i=1,
\cdots , Ni=1,⋯,N)描述了第ii家公司所提供的线路。格式为:
MM S[1]
S[2] \cdots⋯ S[MM]
其中MM(\le
100≤100)是经停站的数量,S[ii](i=1,
\cdots , Mi=1,⋯,M)是经停站的编号(由4位0-9的数字组成)。这里假设每条线路都是简单的一条可以双向运行的链路,并且输入保证是按照正确的经停顺序给出的
—— 也就是说,任意一对相邻的S[ii]和S[i+1i+1](i=1,
\cdots , M-1i=1,⋯,M−1)之间都不存在其他经停站点。我们称相邻站点之间的线路为一个运营区间,每个运营区间只承包给一家公司。环线是有可能存在的,但不会不经停任何中间站点就从出发地回到出发地。当然,不同公司的线路是可能在某些站点有交叉的,这些站点就是客户的换乘点,我们假设任意换乘点涉及的不同公司的线路都不超过5条。
在描述了联盟线路之后,题目将给出一个正整数KK(\le
10≤10),随后KK行,每行给出一位客户的需求,即始发地的编号和目的地的编号,中间以一空格分隔。
处理每一位客户的需求。如果没有现成的线路可以使其到达目的地,就在一行中输出“Sorry, no line is available.”;如果目的地可达,则首先在一行中输出最顺畅路线的经停站数量(始发地和目的地不包括在内),然后按下列格式给出旅行路线:
其中
这题快把我整死了。弄了一下午。各种问题
路径的存储,内存不够。。。。
地图用vector存储
路径找到之后感觉特麻烦。。。输出也是个问题,零零散散弄了一下午过了24分...(满分30)
哎..实在想不通了#include<iostream>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
const int inf=1<<20;
static int len[10000],jiedao[10000][10000]={0};
vector<int> map[10000];
int path[10000]={0};
int n,m,x,y;
void Dijstra(int x){
priority_queue<int> que;
for(int i=1000;i<=9999;i++) len[i]=inf,path[i]=0;
que.push(x);
len[x]=0;
while(que.size()){
int num=que.top();
que.pop();
for(int i=0;i<map[num].size();i++){
if(len[map[num][i]]>len[num]+1){
len[map[num][i]]=len[num]+1;
path[map[num][i]]=num;
que.push(map[num][i]);
}
}
}
}
int main(){
cin>>n;
for(int j=1;j<=n;j++){
cin>>m;
int num[105];
for(int i=0; i<m; i++) {
cin>>num[i];
}
for(int i=0;i<m-1;i++){
map[num[i]].push_back(num[i+1]);
map[num[i+1]].push_back(num[i]);
jiedao[num[i]][num[i+1]]=j;
jiedao[num[i+1]][num[i]]=j;
}
}
int nn;
cin>>nn;
while(nn--){
cin>>x>>y;
if(x==y) continue;
Dijstra(x);
int ss=y;
if(len[y]==inf){
cout<<"Sorry, no line is available."<<endl;
continue;
}
cout<<len[y]<<endl;
stack<int> s;
while(y!=x&&y!=0){
s.push(y);
y=path[y];
}
int a=x,k=0;
int b=s.top();
s.pop();
int f=jiedao[a][b];
a=b;
int pre=x;
while(s.size()){
b=s.top();
s.pop();
if(jiedao[a][b]!=f){
printf("Go by the line of company #%d from %4d to %4d.\n",f,pre,a);
f=jiedao[a][b];
pre=a;
}
a=b;
}
printf("Go by the line of company #%d from %4d to %4d.\n",f,pre,ss);
}
return 0;
}
输入格式:
输入在第一行给出一个正整数NN(\le100≤100),即联盟公司的数量。接下来有NN行,第ii行(i=1,
\cdots , Ni=1,⋯,N)描述了第ii家公司所提供的线路。格式为:
MM S[1]
S[2] \cdots⋯ S[MM]
其中MM(\le
100≤100)是经停站的数量,S[ii](i=1,
\cdots , Mi=1,⋯,M)是经停站的编号(由4位0-9的数字组成)。这里假设每条线路都是简单的一条可以双向运行的链路,并且输入保证是按照正确的经停顺序给出的
—— 也就是说,任意一对相邻的S[ii]和S[i+1i+1](i=1,
\cdots , M-1i=1,⋯,M−1)之间都不存在其他经停站点。我们称相邻站点之间的线路为一个运营区间,每个运营区间只承包给一家公司。环线是有可能存在的,但不会不经停任何中间站点就从出发地回到出发地。当然,不同公司的线路是可能在某些站点有交叉的,这些站点就是客户的换乘点,我们假设任意换乘点涉及的不同公司的线路都不超过5条。
在描述了联盟线路之后,题目将给出一个正整数KK(\le
10≤10),随后KK行,每行给出一位客户的需求,即始发地的编号和目的地的编号,中间以一空格分隔。
输出格式:
处理每一位客户的需求。如果没有现成的线路可以使其到达目的地,就在一行中输出“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.
这题快把我整死了。弄了一下午。各种问题
路径的存储,内存不够。。。。
地图用vector存储
路径找到之后感觉特麻烦。。。输出也是个问题,零零散散弄了一下午过了24分...(满分30)
哎..实在想不通了#include<iostream>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
const int inf=1<<20;
static int len[10000],jiedao[10000][10000]={0};
vector<int> map[10000];
int path[10000]={0};
int n,m,x,y;
void Dijstra(int x){
priority_queue<int> que;
for(int i=1000;i<=9999;i++) len[i]=inf,path[i]=0;
que.push(x);
len[x]=0;
while(que.size()){
int num=que.top();
que.pop();
for(int i=0;i<map[num].size();i++){
if(len[map[num][i]]>len[num]+1){
len[map[num][i]]=len[num]+1;
path[map[num][i]]=num;
que.push(map[num][i]);
}
}
}
}
int main(){
cin>>n;
for(int j=1;j<=n;j++){
cin>>m;
int num[105];
for(int i=0; i<m; i++) {
cin>>num[i];
}
for(int i=0;i<m-1;i++){
map[num[i]].push_back(num[i+1]);
map[num[i+1]].push_back(num[i]);
jiedao[num[i]][num[i+1]]=j;
jiedao[num[i+1]][num[i]]=j;
}
}
int nn;
cin>>nn;
while(nn--){
cin>>x>>y;
if(x==y) continue;
Dijstra(x);
int ss=y;
if(len[y]==inf){
cout<<"Sorry, no line is available."<<endl;
continue;
}
cout<<len[y]<<endl;
stack<int> s;
while(y!=x&&y!=0){
s.push(y);
y=path[y];
}
int a=x,k=0;
int b=s.top();
s.pop();
int f=jiedao[a][b];
a=b;
int pre=x;
while(s.size()){
b=s.top();
s.pop();
if(jiedao[a][b]!=f){
printf("Go by the line of company #%d from %4d to %4d.\n",f,pre,a);
f=jiedao[a][b];
pre=a;
}
a=b;
}
printf("Go by the line of company #%d from %4d to %4d.\n",f,pre,ss);
}
return 0;
}
相关文章推荐
- PTA 5-14 周游世界 (2017cccc初赛L3-2)
- 团体程序设计天梯赛-练习集 -- L3-014. 周游世界(dijkstra)
- [转贴]用Google Earth周游世界
- 团体程序设计天梯赛 周游世界(最短路)
- CCCC-GPLT L3-014. 周游世界 团体程序设计天梯赛
- L3-014. 周游世界+dfs_2018_3_17
- 天梯赛补题 - 周游世界(记录路径的最短路)
- 同学,其实你可以不用周游世界……
- 团体程序设计天梯赛L3-014 周游世界
- 天梯赛 大区赛 L3-014.周游世界 (Dijkstra)
- 如何在网上周游世界?
- 同学,其实你可以不用周游世界……
- C语言之算法初步(骑士周游世界)
- 天梯赛 大区赛 L3-014.周游世界 (Dijkstra)
- 旅游新时尚:点点鼠标就可“周游世界”
- 《Head First设计模式》 读书笔记14 真实世界中的模式
- 阳光动力2号能够环球航行而不能周游世界
- PAT-天梯赛习题集-L3-014-周游世界
- ART世界探险(14) - 快速编译器和优化编译器
- PAT L3-014. 周游世界