PAT甲级——A1111 Online Map【30】
Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (2), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:
V1 V2 one-way length time
where V1
and V2
are the indices (from 0 to N−1) of the two ends of the street; one-way
is 1 if the street is one-way from V1
to V2
, or 0 if not; length
is the length of the street; and time
is the time taken to pass the street.
Finally a pair of source and destination is given.
Output Specification:
For each case, first print the shortest path from the source to the destination with distance D
in the format:
Distance = D: source -> v1 -> ... -> destination
Then in the next line print the fastest path with total time T
:
Time = T: source -> w1 -> ... -> destination
In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.
In case the shortest and the fastest paths are identical, print them in one line in the format:
Distance = D; Time = T: source -> u1 -> ... -> destination
Sample Input 1:
10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5
Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5
Sample Input 2:
7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5
Sample Output 2:
Distance = 3; Time = 4: 3 -> 2 -> 5
#include <iostream>
#include <vector>
using namespace std;
#define inf 999999999
int N, M, start, des, shortest[];//shortest存储最短路径的长度(下标为0)和最快路径的时间(下标为1)
int graph[][][];//图的邻接矩阵
int dis[], past[], num[];////各点最短距离、父节点、num求最短路径时存储时间,求最快路径时存储当前路径上结点个数
bool visit[] = { false };
vector<int>path[];//下标为0存储最短路径,下标为1存储最快路径
void Dijkstra(int k)//k==0求最短路径,k==1求最快路径
{
while (visit[des] == false)
{
int v = -, min = inf;
for (int i = ; i < N; ++i)
{
if (visit[i] == false && min > dis[i])
{
v = i;
min = dis[i];
}
}
if (v == -)break;
visit[v] = true;
for (int i = ; i < N; ++i)
{
if (visit[i] == false && graph[v][i][k] != && dis[i] > dis[v] + graph[v][i][k])
{
dis[i] = dis[v] + graph[v][i][k];//更新距离
past[i] = v;//更新父节点
if (k == )//是求最短路径
num[i] = num[v] + graph[v][i][];//叠加时间
else//求最快路径
num[i] = num[v] + ;//叠加经过的路径节点
}
else if (graph[v][i][k] != && dis[i] == dis[v] + graph[v][i][k])//路径距离相等
{
if (k == && num[i] > num[v] + graph[v][i][])//时间更短
{
past[i] = v;//要经过这个点
num[i] = num[v] + graph[v][i][];//叠加经过的时间
}
else if (k == && num[i] > num[v] + )//经过更少的节点
{
past[i] = v;//则经过该点
num[i] = num[v] + ;//叠加经过的节点
}
}
}
}
shortest[k] = dis[des];//存储答案信息
}
void DFS(int v, int k)//使用DFS寻找出答案
{
if (v == start)//到起点
{
path[k].push_back(v);
return;
}
DFS(past[v], k);//从后向前寻根,故DFS后进行记录路径,则路径则是顺序的
path[k].push_back(v);
}
bool cmp()
{
if (path[].size() != path[].size())
return false;
for (int i = ; i < path[].size(); ++i)
if (path[][i] != path[][i])
return false;
return true;
}
void printPath(int k)//路径打印
{
for (int i = ; i < path[k].size(); ++i)
cout << path[k][i] << (i == path[k].size() - ? "" : " -> ");
cout << endl;
}
int main()
{
cin >> N >> M;
while (M--)
{
int a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
graph[a][b][] = d;
graph[a][b][] = e;
if (c == )//有双向道
{
graph[b][a][] = d;
graph[b][a][] = e;
}
}
cin >> start >> des;
for (int i = ; i < ; ++i)//使用两次Dij+DFS
{
fill(visit, visit + N, false);
fill(dis, dis + N, inf);
fill(num, num + N, inf);
dis[start] = ;
num[start] = ;
Dijkstra(i);
DFS(des,i);//从终点开始寻根
}
if (cmp())//比较路径是否相同
{
printf("Distance = %d; Time = %d: ", shortest[], shortest[]);
printPath();
}
else//不相等
{
printf("Distance = %d: ", shortest[]);
printPath();
printf("Time = %d: ", shortest[]);
printPath();
}
return ;
}
最新文章
- 【C#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法
- Google Code Jam 2016 Round 1B B
- amqp事务
- Hadoop Fsimage 和 editlog
- A little tutorial on CodeFluent Entities with ASP.NET MVC4
- Java容器类接口的选择
- BZOJ3439: Kpm的MC密码
- iframe切换内容页仍然能自适应大小代码(含js)
- C++数据结构之二叉树
- UINavigationItem不显示
- ActivityManager与Proxy模式的运用
- 如何恢复(初始化)android studio所有设置
- highchart
- android PM2.5监控demo开发
- JQuery ajax的使用
- centos 7 mysql 开启binlog
- 容器(container)
- Spring Boot 添加JSP支持【转】
- |和||以及&;和&;&;
- gensim学习笔记