L3-007 天梯地图 (30 分)
本题要求你实现一个天梯赛专属在线地图,队员输入自己学校所在地和赛场地点后,该地图应该推荐两条路线:一条是最快到达路线;一条是最短距离的路线。题目保证对任意的查询请求,地图上都至少存在一条可达路线。
输入格式:
输入在第一行给出两个正整数 N ( 2 ≤ N ≤ 500 ) N(2 \leq N \leq 500) N(2≤N≤500)和 M M M,分别为地图中所有标记地点的个数和连接地点的道路条数。随后 M M M行,每行按如下格式给出一条道路的信息:
V
1
V
2
o
n
e
−
w
a
y
l
e
n
g
t
h
t
i
m
e
V_1\ V_2\ one-way\ length\ time
V1 V2 one−way length time
其中
V
1
V_1
V1和
V
2
V_2
V2是道路的两个端点的编号(从
0
0
0到
N
−
1
N-1
N−1);如果该道路是从
V
1
V_1
V1到
V
2
V_2
V2的单行线,则
o
n
e
−
w
a
y
one-way
one−way为
1
1
1,否则为
0
0
0;
l
e
n
g
t
h
length
length是道路的长度;
t
i
m
e
time
time是通过该路所需要的时间。最后给出一对起点和终点的编号。
输出格式:
首先按下列格式输出最快到达的时间T和用节点编号表示的路线:
T
i
m
e
=
T
Time\ =\ T
Time = T: 起点
=
>
=>
=> 节点
1
=
>
1\ =>
1 => …
=
>
=>
=> 终点
然后在下一行按下列格式输出最短距离
D
D
D和用节点编号表示的路线:
D
i
s
t
a
n
c
e
=
D
Distance\ =\ D
Distance = D: 起点 => 节点
1
=
>
1\ =>
1 => …
=
>
=>
=> 终点
如果最快到达路线不唯一,则输出几条最快路线中最短的那条,题目保证这条路线是唯一的。而如果最短距离的路线不唯一,则输出途径节点数最少的那条,题目保证这条路线是唯一的。
如果这两条路线是完全一样的,则按下列格式输出:
T i m e = T ; D i s t a n c e = D Time\ =\ T;\ Distance\ =\ D Time = T; Distance = D: 起点 = > => => 节点 1 = > 1\ => 1 => … = > => => 终点
输入样例1:
10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
5 4 0 2 3
5 9 1 1 4
0 6 0 1 1
7 3 1 1 2
8 3 1 1 2
2 5 0 2 2
2 1 1 1 1
1 5 0 1 3
1 4 0 1 1
9 7 1 1 3
3 1 0 2 5
6 3 1 2 1
5 3
输出样例1:
Time = 6: 5 => 4 => 8 => 3
Distance = 3: 5 => 1 => 3
输入样例2:
7 9
0 4 1 1 1
1 6 1 3 1
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 3 1
3 2 1 2 1
4 5 0 2 2
6 5 1 2 1
3 5
输出样例2:
Time = 3; Distance = 4: 3 => 2 => 5
题意
- 就是求一条长度为最短长度路径的路径,最短长度路径相同的情况下求经过的节点数最少的那一条;然后再求一条长度为最短时间路径,最短时间相同的情况下求路径长度最短的那一条
题解
- 比如求最短时间的路径,先做一次狄杰斯特拉,求出 d i s dis dis数组,那么最短时间相同的所有路径中又要求一个最短长度路径,那么很容易想到再做一次狄杰斯特拉 Q W Q QWQ QWQ,显然不是在原图上跑啦,需要在最短长度路径的限制下跑,也就是增加限制 d i s [ t o ] = d i s [ f r o m ] + d i s t a n c e [ f r o m ] [ t o ] dis[to]=dis[from]+distance[from][to] dis[to]=dis[from]+distance[from][to]另外一条需要求的同理
- 注意下标从 0 0 0开始
- 代码很丑,懂了原理就好
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=505;
int n,m,path1[maxn],path2[maxn],tot1=0,tot2=0,dis_s[maxn],dis_s_t[maxn],dis_t[maxn],dis_t_s[maxn];
int pres[maxn],pret[maxn];
struct node{
int to,w,t;
node(int a=0,int b=0,int c=0){
to=a;w=b;t=c;
}
};
vector<node> vec[maxn];
int dijkstra_s(int s,int t)
{
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > que;
memset(dis_s,0x3f,sizeof(dis_s));
dis_s[s]=0;
que.push(make_pair(0,s));
while(!que.empty()){
pair<int,int> cur=que.top();que.pop();
if(dis_s[cur.second]<cur.first) continue;
for(int i=0;i<vec[cur.second].size();i++){
node nxt=vec[cur.second][i];
if(dis_s[nxt.to]>dis_s[cur.second]+nxt.w){
dis_s[nxt.to]=dis_s[cur.second]+nxt.w;
que.push(make_pair(dis_s[nxt.to],nxt.to));
}
}
}
return dis_s[t];
}
void dijkstra_s_t(int s,int t)
{
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > que;
memset(dis_s_t,0x3f,sizeof(dis_s_t));
memset(pres,-1,sizeof(pres));
dis_s_t[s]=0;
que.push(make_pair(0,s));
while(!que.empty()){
pair<int,int> cur=que.top();que.pop();
if(dis_s_t[cur.second]<cur.first) continue;
for(int i=0;i<vec[cur.second].size();i++){
node nxt=vec[cur.second][i];
if(dis_s_t[nxt.to]>dis_s_t[cur.second]+1&&dis_s[nxt.to]==dis_s[cur.second]+nxt.w){
dis_s_t[nxt.to]=dis_s_t[cur.second]+1;
que.push(make_pair(dis_s_t[nxt.to],nxt.to));
pres[nxt.to]=cur.second;
}
}
}
while(t!=-1) {path1[++tot1]=t;t=pres[t];}
}
int dijkstra_t(int s,int t)
{
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > que;
memset(dis_t,0x3f,sizeof(dis_t));
dis_t[s]=0;
que.push(make_pair(0,s));
while(!que.empty()){
pair<int,int> cur=que.top();que.pop();
if(dis_t[cur.second]<cur.first) continue;
for(int i=0;i<vec[cur.second].size();i++){
node nxt=vec[cur.second][i];
if(dis_t[nxt.to]>dis_t[cur.second]+nxt.t){
dis_t[nxt.to]=dis_t[cur.second]+nxt.t;
que.push(make_pair(dis_t[nxt.to],nxt.to));
}
}
}
return dis_t[t];
}
void dijkstra_t_s(int s,int t)
{
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > que;
memset(dis_t_s,0x3f,sizeof(dis_t_s));
memset(pret,-1,sizeof(pret));
dis_t_s[s]=0;
que.push(make_pair(0,s));
while(!que.empty()){
pair<int,int> cur=que.top();que.pop();
if(dis_t_s[cur.second]<cur.first) continue;
for(int i=0;i<vec[cur.second].size();i++){
node nxt=vec[cur.second][i];
if(dis_t_s[nxt.to]>dis_t_s[cur.second]+nxt.w&&dis_t[nxt.to]==dis_t[cur.second]+nxt.t){
dis_t_s[nxt.to]=dis_t_s[cur.second]+nxt.w;
que.push(make_pair(dis_t_s[nxt.to],nxt.to));
pret[nxt.to]=cur.second;
}
}
}
while(t!=-1) {path2[++tot2]=t;t=pret[t];}
}
int main()
{
scanf("%d %d",&n,&m);int s,t;
for(int i=1,u,v,t,w,k;i<=m;i++){
scanf("%d %d %d %d %d",&u,&v,&k,&w,&t);
vec[u].push_back(node(v,w,t));
if(!k) vec[v].push_back(node(u,w,t));
}
scanf("%d %d",&s,&t);
int min_s=dijkstra_s(s,t);dijkstra_s_t(s,t);
int min_t=dijkstra_t(s,t);dijkstra_t_s(s,t);
if(tot1==tot2){
bool ok=true;
for(int i=tot1;i>=1;i--){
if(path1[i]!=path2[i]){
ok=false;
break;
}
}
if(ok){
printf("Time = %d; Distance = %d: ",min_t,min_s);
for(int i=tot1;i>=1;i--) {
printf("%d",path1[i]);
if(i==1) printf("\n");
else printf(" => ");
}
return 0;
}
}
printf("Time = %d: ",min_t);
for(int i=tot2;i>=1;i--) {
printf("%d",path2[i]);
if(i==1) printf("\n");
else printf(" => ");
}
printf("Distance = %d: ",min_s);
for(int i=tot1;i>=1;i--){
printf("%d",path1[i]);
if(i==1) printf("\n");
else printf(" => ");
}
}