7-15 天梯地图 (30 分)
本题要求你实现一个天梯赛专属在线地图,队员输入自己学校所在地和赛场地点后,该地图应该推荐两条路线:一条是最快到达路线;一条是最短距离的路线。题目保证对任意的查询请求,地图上都至少存在一条可达路线。
输入格式:
输入在第一行给出两个正整数N
(2 ≤ N
≤ 500)和M
,分别为地图中所有标记地点的个数和连接地点的道路条数。随后M
行,每行按如下格式给出一条道路的信息:
V1 V2 one-way length time
其中V1
和V2
是道路的两个端点的编号(从0到N
-1);如果该道路是从V1
到V2
的单行线,则one-way
为1,否则为0;length
是道路的长度;time
是通过该路所需要的时间。最后给出一对起点和终点的编号。
输出格式:
首先按下列格式输出最快到达的时间T
和用节点编号表示的路线:
Time = T: 起点 => 节点1 => ... => 终点
然后在下一行按下列格式输出最短距离D
和用节点编号表示的路线:
Distance = D: 起点 => 节点1 => ... => 终点
如果最快到达路线不唯一,则输出几条最快路线中最短的那条,题目保证这条路线是唯一的。而如果最短距离的路线不唯一,则输出途径节点数最少的那条,题目保证这条路线是唯一的。
如果这两条路线是完全一样的,则按下列格式输出:
Time = T; Distance = D: 起点 => 节点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
代码如下。2份代码 仅打印路径不同。
#include<iostream>
#include<cstring>
#include<math.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int N, E;
int len[505][505], Time[505][505];;
int time_d[505];
int dist[505];
bool vis[505];
int tpath[505], dpath[505];
int num[505];
int a, b;
void tDijskstra(int x){
memset(time_d, INF, sizeof(time_d));
memset(dist, INF, sizeof(dist));
memset(tpath, -1, sizeof(tpath));
memset(vis, 0, sizeof(vis));
time_d[x] = 0;
dist[x] = 0;
while (1){
int k = -1, minn = INF;
for(int i = 0; i < N; i++){
if( !vis[i] && minn > time_d[i]){
minn = time_d[i];
k = i;
}
}
if(k == -1) break;
vis[k] = 1;
for(int i = 0; i < N;i++){
if(!vis[i]){
if(time_d[i] > time_d[k] + Time[k][i]){
time_d[i] = time_d[k] + Time[k][i];
dist[i] = dist[k] + len[k][i];
tpath[i] = k;
}
else if(time_d[i] == time_d[k] + Time[k][i]){
if(dist[i] > dist[k] + len[k][i]){
dist[i] = dist[k] + len[k][i];
tpath[i] = k;
}
}
}
}
}
}
void dDijskstra(int x){
memset(num, 0, sizeof(num));
memset(dist, INF, sizeof(dist));
memset(dpath, -1, sizeof(dpath));
memset(vis, 0, sizeof(vis));
dist[x] = 0;
while (1){
int k = -1, minn = INF;
for(int i = 0; i < N; i++){
if( !vis[i] && minn > dist[i]){
minn = dist[i];
k = i;
}
}
if(k == -1) break;
vis[k] = 1;
for(int i = 0; i < N;i ++){
if(!vis[i]){
if(dist[i] > dist[k] + len[k][i]){
dist[i] = dist[k] + len[k][i];
num[i] = num[k] + 1;
dpath[i] = k;
}
else if(dist[i] == dist[k] + len[k][i]){
if(num[i] > num[k] + 1){
num[i] = num[k] + 1;
dpath[i] = k;
}
}
}
}
}
}
int judge(int b){
if(tpath[b] != dpath[b]){
return 0;
}
else if(b != -1){
return judge(tpath[b]);
}
else{
return 1;
}
}
void print(int path[], int x){
if(x == -1){
return ;
}
else{
print(path, path[x]);
cout << x << " => ";
}
}
void show(int flag){
if(flag){
cout << "Time = " << time_d[b] << "; ";
cout << "Distance = " << dist[b] << ": ";
print(dpath, dpath[b]);
cout << b;
}
else{
cout << "Time = " << time_d[b] << ": ";
print(tpath, tpath[b]);
cout << b << endl;
cout << "Distance = " << dist[b] << ": ";
print(dpath, dpath[b]);
cout << b;
}
}
int main(){
cin >> N >> E;
memset(Time, INF, sizeof(Time));
memset(len, INF, sizeof(len));
for(int i = 1; i <= E; i++){
int v1, v2, one, l, t;
cin >> v1 >> v2 >> one >> l >> t;
if(one == 1){
Time[v1][v2] = min(Time[v1][v2], t);
len[v1][v2] = min(len[v1][v2], l);
}
else{
Time[v2][v1] = Time[v1][v2] = min(Time[v1][v2], t);
len[v2][v1] = len[v1][v2] = min(len[v1][v2], l);
}
}
cin >> a >> b;
tDijskstra(a);
dDijskstra(a);
int flag = judge(b);
show(flag);
return 0;
}
#include<iostream>
#include<cstring>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int INF = 0x3f3f3f3f;
int N, E;
int a, b;
int mp[505][505];
int tim[505][505];
bool vis[505];
int time_d[505];
int dist[505];
int distpath[505];
int timepath[505];
int num[505];
void show(int y){
stack<int> sta1;
stack<int> sta2;
string s1 = "";
string s2 = "";
int temp1 = timepath[y];
int temp2 = distpath[y];
sta1.push(y);
s1 += y + '0';
sta2.push(y);
s2 += y + '0';
while(temp1 != -1){
s1 += temp1 + '0';
sta1.push(temp1);
temp1 = timepath[temp1];
}
while(temp2 != -1){
s2 += temp2 + '0';
sta2.push(temp2);
temp2 = distpath[temp2];
}
if(s1 == s2){
cout << "Time = " << time_d[y] << "; ";
cout << "Distance = " << dist[y] << ": ";
cout << sta2.top() ;
sta2.pop();
while(!sta2.empty()){
cout << " => " << sta2.top();
sta2.pop();
}
}
else
{
cout << "Time = " << time_d[y] << ": ";
cout << sta1.top();
sta1.pop();
while(!sta1.empty()){
cout << " => " << sta1.top();
sta1.pop();
}
cout << endl;
cout << "Distance = " << dist[y] << ": ";
cout << sta2.top();
sta2.pop();
while(!sta2.empty()){
cout << " => " << sta2.top();
sta2.pop();
}
}
}
void timeDijskstra(int x){
memset(vis, 0, sizeof(vis));
memset(dist, INF, sizeof(dist));
memset(time_d, INF, sizeof(time_d));
time_d[x] = 0;
timepath[x] = -1;
dist[x] = 0;
while(1){
int k = -1, minn = INF;
for(int i = 0; i < N; i++){
if(!vis[i] && minn > time_d[i]){
k = i;
minn = time_d[i];
}
}
if(k == -1) break;
vis[k] = 1;
for(int i = 0; i < N; i++){
if(!vis[i]){
if(time_d[i] > tim[k][i] + time_d[k]){
dist[i] = mp[k][i] + dist[k];
timepath[i] = k;
time_d[i] = time_d[k] + tim[k][i];
}
else if(time_d[i] == tim[k][i] + time_d[k]){
if(dist[i] > mp[k][i] + dist[k]){
timepath[i] = k;
dist[i] = mp[k][i] + dist[k];
}
}
}
}
}
}
void disDijskstra(int x){
memset(vis, 0, sizeof(vis));
memset(dist, INF, sizeof(dist));
memset(num, INF, sizeof(num));
dist[x] = 0;
while(1){
int k = -1, minn = INF;
for(int i = 0; i < N; i++){
if(!vis[i] && minn > dist[i]){
k = i;
minn = dist[i];
}
}
if(k == -1) break;
vis[k] = 1;
for(int i = 0; i < N; i++){
if(!vis[i]){
if(dist[i] > dist[k] + mp[k][i]){
dist[i] = dist[k] + mp[k][i];
distpath[i] = k;
num[i] = num[k] + 1;
}
else if(dist[i] == dist[k] + mp[k][i]){
if(num[i] > num[k] + 1){
num[i] = num[k] + 1;
distpath[i] = k;
}
}
}
}
}
show(b);
}
int main(){
cin >> N >> E;
memset(mp, INF, sizeof(mp));
memset(tim, INF, sizeof(tim));
memset(distpath, -1, sizeof(distpath));
memset(timepath, -1, sizeof(timepath));
for(int i = 0; i < E; i++){
int way, l, t, v1, v2;
cin >> v1 >> v2 >> way >> l >> t;
if(way == 1){
mp[v1][v2] = min(mp[v1][v2], l);
tim[v1][v2] = min(tim[v1][v2], t);
}
else {
mp[v1][v2] = mp[v2][v1] = min(mp[v1][v2], min(mp[v2][v1], l));
tim[v1][v2] = tim[v2][v1] = min(tim[v1][v2], min(tim[v2][v1], t));
}
}
cin >> a >> b;
timeDijskstra(a);
disDijskstra(a);
return 0;
}