L3-007. 天梯地图
时间限制
300 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越
本题要求你实现一个天梯赛专属在线地图,队员输入自己学校所在地和赛场地点后,该地图应该推荐两条路线:一条是最快到达路线;一条是最短距离的路线。题目保证对任意的查询请求,地图上都至少存在一条可达路线。
输入格式:
输入在第一行给出两个正整数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
思路:最短路,不过麻烦的是多条最短路中推荐最优的路线,并输出路径。路径的还原可以不断记录前驱节点,注意的是每个节点的前驱节点可能不止一个,全需要记录,最后dfs搜索最优路径。
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<set>
#include<queue>
using namespace std;
#define N_MAX 500+20
#define INF 0x3f3f3f3f
int n, m;
int s, t;
struct edge {
int to, cost_t, cost_l;
edge() {}
edge(int to,int cost_t,int cost_l):to(to),cost_t(cost_t),cost_l(cost_l) {}
};
struct P {
int first, second;//first距离,second节点编号
P() {}
P(int first,int second):first(first),second(second) {}
bool operator < (const P&b) const{
return first > b.first;
}
};
vector<edge>G[N_MAX];
int d_t[N_MAX], d_l[N_MAX];
vector<int>prev_l[N_MAX];//记录最短路径的前驱结点,每个点都可能会有几个前驱结点
vector<int>prev_t[N_MAX];//记录最短时限路径的前驱结点,同上
int Dist[N_MAX][N_MAX];//邻接矩阵
void dijkstra1(int s) {
priority_queue<P>que;
fill(d_l,d_l+n,INF);
d_l[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if (d_l[v] < p.first)continue;
for (int i = 0; i < G[v].size();i++) {
edge e = G[v][i];
if (d_l[e.to] > d_l[v] + e.cost_l) {
d_l[e.to] = d_l[v] + e.cost_l;
que.push(P(d_l[e.to], e.to));
prev_l[e.to].clear();
prev_l[e.to].push_back(v);
}
else if (d_l[e.to] == d_l[v] + e.cost_l) {
prev_l[e.to].push_back(v);
}
}
}
}
void dijkstra2(int s) {
priority_queue<P>que;
fill(d_t, d_t + n, INF);
d_t[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if (d_t[v] < p.first)continue;
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
if (d_t[e.to] > d_t[v] + e.cost_t) {
d_t[e.to] = d_t[v] + e.cost_t;
que.push(P(d_t[e.to], e.to));
prev_t[e.to].clear();
prev_t[e.to].push_back(v);
}
else if (d_t[e.to] == d_t[v] + e.cost_t) {
prev_t[e.to].push_back(v);
}
}
}
}
int road2[N_MAX];
vector<int>r2;
int min_step = INF;
void dfs2(int x,int step) {//最短距离一样,取节点最少的路径
road2[step] = x;
if (x == s) {//到达起点
if (min_step > step) {
min_step = step;
r2.clear();
for (int i = step; i >= 0; i--)r2.push_back(road2[i]);
}
return;
}
for (int i = 0; i < prev_l[x].size();i++) {
dfs2(prev_l[x][i], step + 1);
}
}
int road1[N_MAX];
vector<int>r1;
int min_dist = INF;
void dfs1(int x, int step,int dist) {//最短时间一样,取最短路径
road1[step] = x;
if (x == s) {//到达起点
if (min_dist > dist) {
min_dist = dist;
r1.clear();
for (int i = step; i >= 0; i--)r1.push_back(road1[i]);
}
return;
}
for (int i = 0; i < prev_t[x].size(); i++) {
int from = prev_t[x][i];
dfs1(from, step + 1,dist+Dist[from][x]);
}
}
int main() {
while (scanf("%d%d",&n,&m)!=EOF) {
for (int i = 0; i < m;i++) {
int from, to, one, L, T;
scanf("%d%d%d%d%d", &from, &to, &one, &L, &T);
G[from].push_back(edge(to, T, L));
Dist[from][to] = L;
if (!one) {
G[to].push_back(edge(from, T, L));
Dist[to][from] = L;
}
}
scanf("%d%d",&s,&t);
dijkstra1(s);
dijkstra2(s);
dfs1(t, 0, 0);
dfs2(t, 0);
if (r1 == r2) {
printf("Time = %d; Distance = %d:",d_t[t],d_l[t]);
for (int i = 0; i < r1.size(); i++)
printf(" %d%s",r1[i],i+1==r1.size()? "\n" : " =>");
}
else {
printf("Time = %d:",d_t[t]);
for (int i = 0; i < r1.size();i++) {
printf(" %d%s", r1[i], i + 1 == r1.size() ? "\n" : " =>");
}
printf("Distance = %d:", d_l[t]);
for (int i = 0; i < r2.size();i++) {
printf(" %d%s", r2[i], i + 1 == r2.size() ? "\n" : " =>");
}
}
}
return 0;
}
来源:oschina
链接:https://my.oschina.net/u/4407318/blog/4241953