概述
L3-007. 天梯地图
本题要求你实现一个天梯赛专属在线地图,队员输入自己学校所在地和赛场地点后,该地图应该推荐两条路线:一条是最快到达路线;一条是最短距离的路线。题目保证对任意的查询请求,地图上都至少存在一条可达路线。
输入格式:
输入在第一行给出两个正整数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
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 500, INF = 2147483647;
struct Edge {
int v1, v2, length, time;
Edge() {}
Edge(int v1, int v2, int length, int time) : v1(v1), v2(v2), length(length), time(time) {}
};
struct HeapNode {
int v, w;
HeapNode() {}
HeapNode(int v, int w) : v(v), w(w) {}
bool operator < (const HeapNode oh) const {
return oh.w < w;
}
};
int N, M, s, e;
vector<int> G[maxn + 5], patht, pathd;
vector<Edge> edges;
bool vis[maxn + 5];
int dd[maxn + 5], dt[maxn + 5], dr[maxn + 5], pd[maxn + 5], pt[maxn + 5];
priority_queue<HeapNode> q;
void init1() {
memset(vis, false, sizeof(vis));
memset(pt, -1, sizeof(pt));
while (!q.empty()) q.pop();
for (int i = 0; i < N; i++) {
dt[i] = INF;
dd[i] = INF;
}
}
void init2() {
memset(vis, false, sizeof(vis));
memset(pd, -1, sizeof(pd));
while (!q.empty()) q.pop();
for (int i = 0; i < N; i++) {
dd[i] = INF;
dr[i] = INF;
}
}
void dijkstra1() {
init1();
q.push(HeapNode(s, 0));
dt[s] = dd[s] = 0;
while (!q.empty()) {
HeapNode thn = q.top(); q.pop();
int v = thn.v;
vis[v] = true;
for (int i = 0; i < G[v].size(); i++) {
Edge ee = edges[G[v][i]];
int from = ee.v1, to = ee.v2, length = ee.length, time = ee.time;
if (!vis[to]) {
if (dt[from] < INF) {
if (dt[from] + time < dt[to]) {
dt[to] = dt[from] + time;
dd[to] = dd[from] + length;
pt[to] = from;
q.push(HeapNode(to, dt[to]));
}
else if (dt[from] + time == dt[to] && dd[from] + length < dd[to]) {
dd[to] = dd[from] + length;
pt[to] = from;
q.push(HeapNode(to, dt[to]));
}
}
}
}
}
int x = pt[e];
patht.push_back(e);
while (x != -1) {
patht.push_back(x);
x = pt[x];
}
}
void dijkstra2() {
init2();
q.push(HeapNode(s, 0));
dd[s] = dr[s] = 0;
while (!q.empty()) {
HeapNode thn = q.top(); q.pop();
int v = thn.v;
vis[v] = true;
for (int i = 0; i < G[v].size(); i++) {
Edge ee = edges[G[v][i]];
int from = ee.v1, to = ee.v2, length = ee.length, time = ee.time;
if (!vis[to]) {
if (dd[from] < INF) {
if (dd[from] + length < dd[to]) {
dd[to] = dd[from] + length;
dr[to] = dr[from] + 1;
pd[to] = from;
q.push(HeapNode(to, dd[to]));
}
else if (dd[from] + length == dd[to] && dr[from] + 1 < dr[to]) {
dr[to] = dr[from] + 1;
pd[to] = from;
q.push(HeapNode(to, dd[to]));
}
}
}
}
}
int x = pd[e];
pathd.push_back(e);
while (x != -1) {
pathd.push_back(x);
x = pd[x];
}
}
int main()
{
cin >> N >> M;
for (int i = 0; i < M; i++) {
int v1, v2, oneway, length, time;
scanf("%d%d%d%d%d", &v1, &v2, &oneway, &length, &time);
edges.push_back(Edge(v1, v2, length, time));
G[v1].push_back(edges.size() - 1);
if (!oneway) {
edges.push_back(Edge(v2, v1, length, time));
G[v2].push_back(edges.size() - 1);
}
}
scanf("%d%d", &s, &e);
dijkstra1();
dijkstra2();
bool flag = true;
if (patht.size() == pathd.size()) {
for (int i = 0; i < patht.size(); i++) {
if (patht[i] != pathd[i]) {
flag = false;
break;
}
}
}
else {
flag = false;
}
if (flag) {
printf("Time = %d; ", dt[e]);
printf("Distance = %d: ", dd[e]);
for (int i = pathd.size() - 1; i >= 0; i--) {
printf(i == pathd.size() - 1 ? "%d" : " => %d", pathd[i]);
}
}
else {
printf("Time = %d: ", dt[e]);
for (int i = patht.size() - 1; i >= 0; i--) {
printf(i == patht.size() - 1 ? "%d" : " => %d", patht[i]);
}
puts("");
printf("Distance = %d: ", dd[e]);
for (int i = pathd.size() - 1; i >= 0; i--) {
printf(i == pathd.size() - 1 ? "%d" : " => %d", pathd[i]);
}
}
return 0;
}
最后
以上就是诚心背包为你收集整理的团体程序设计天梯赛 L3-007. 天梯地图 Dijkstra L3-007. 天梯地图的全部内容,希望文章能够帮你解决团体程序设计天梯赛 L3-007. 天梯地图 Dijkstra L3-007. 天梯地图所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复