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 ;
}

最新文章

  1. 【C#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法
  2. Google Code Jam 2016 Round 1B B
  3. amqp事务
  4. Hadoop Fsimage 和 editlog
  5. A little tutorial on CodeFluent Entities with ASP.NET MVC4
  6. Java容器类接口的选择
  7. BZOJ3439: Kpm的MC密码
  8. iframe切换内容页仍然能自适应大小代码(含js)
  9. C++数据结构之二叉树
  10. UINavigationItem不显示
  11. ActivityManager与Proxy模式的运用
  12. 如何恢复(初始化)android studio所有设置
  13. highchart
  14. android PM2.5监控demo开发
  15. JQuery ajax的使用
  16. centos 7 mysql 开启binlog
  17. 容器(container)
  18. Spring Boot 添加JSP支持【转】
  19. |和||以及&amp;和&amp;&amp;
  20. gensim学习笔记

热门文章

  1. vue 学习五 深入了解components(父子组件之间的传值)
  2. Batch - FOR /F Delims 和 Tokens 用法
  3. NX二次开发-删除经典工具栏UF_UI_remove_toolbar
  4. 安装JDK9之后eclipse无法启动问题解决办法
  5. Shiro术语
  6. jquery操作html元素之(添加元素)
  7. 如何提高阿里云cdn命中率?原命中率极低。
  8. 深夜Python - 第1夜 - for 迷 in 迷思
  9. python 13 字符编码
  10. python语法基础(类)