博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1111 Online Map (30 分)
阅读量:4932 次
发布时间:2019-06-11

本文共 4603 字,大约阅读时间需要 15 分钟。

1111. 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 <= N <= 500), 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

知识点: Dijkstra算法; DFS算法

思路:

第一是求最短路径,有相同的则输出时间最短;利用Dijkstra求解,更新最短路时,如果最短路相同,则比较时间

1 if(!visited[i]){ 2     if(minl[i]>minD+G_l[minV][i]){ 3         minl[i]=minD+G_l[minV][i]; 4         mint[i]=mint[minV]+G_t[minV][i]; 5         pre_shorest[i]=minV; 6     }else if(minl[i]==minD+G_l[minV][i]&& 7             mint[i]>mint[minV]+G_t[minV][i]){ 8         mint[i]=mint[minV]+G_t[minV][i]; 9         pre_shorest[i]=minV;        10     }11 }

第二是求最快路,如果有相同的,输出节点最少的:用Dijkstra算法,设立容器pre来储存每个节点的优选前去节点;然后用DFS来遍历每条路径,选出最少节点的

1 for(int i=0;i
minD+G_t[minV][i]){ 4 mint[i]=mint[minV]+G_t[minV][i]; 5 pre_faster[i].clear(); 6 pre_faster[i].push_back(minV); 7 }else if(mint[i]==minD+G_t[minV][i]){ 8 pre_faster[i].push_back(minV); 9 }10 }11 }

 

最后,vector可以比较,相同的情况特殊处理

 

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 
#include
#include
using namespace std;const int maxn = 550;const int inf = 999999;int n,m;int G_l[maxn][maxn];int G_t[maxn][maxn];int minl[maxn];int mint[maxn];int visited[maxn];int pre_shorest[maxn];vector
pre_faster[maxn];vector
s_path;vector
f_path;vector
f_tmpp;int minsize;int dijkstra_shorest(int start,int end){ fill(pre_shorest, pre_shorest+maxn, -1); fill(minl,minl+maxn,inf); fill(mint,mint+maxn,inf); fill(visited,visited+maxn,0); minl[start] = 0; for(int i=0;i
minD+G_l[minV][i]){ minl[i]=minD+G_l[minV][i]; mint[i]=mint[minV]+G_t[minV][i]; pre_shorest[i]=minV; }else if(minl[i]==minD+G_l[minV][i]&& mint[i]>mint[minV]+G_t[minV][i]){ mint[i]=mint[minV]+G_t[minV][i]; pre_shorest[i]=minV; } } } } int ptr = end; while(ptr != -1){ //printf(" %d\n",ptr); s_path.push_back(ptr); ptr=pre_shorest[ptr]; } return minl[end];}void DFS(int v,int start){ f_tmpp.push_back(v); if(v==start){ if(f_tmpp.size()
minD+G_t[minV][i]){ mint[i]=mint[minV]+G_t[minV][i]; pre_faster[i].clear(); pre_faster[i].push_back(minV); }else if(mint[i]==minD+G_t[minV][i]){ pre_faster[i].push_back(minV); } } } } minsize = inf; DFS(end,start); for(int i=0;i
=0;i--){ printf("%d",s_path[i]); if(i!=0) printf(" -> "); } }else{ printf("Distance = %d: ",D); for(int i=s_path.size()-1;i>=0;i--){ printf("%d",s_path[i]); if(i!=0) printf(" -> "); } printf("\nTime = %d: ",T); for(int i=f_path.size()-1;i>=0;i--){ printf("%d",f_path[i]); if(i!=0) printf(" -> "); } }}

 

转载于:https://www.cnblogs.com/lokwongho/p/9932066.html

你可能感兴趣的文章
51CTO大赛,欢迎投博主一票
查看>>
FlashCS5作成SWC,在Flex4中使用(1)
查看>>
《那些年啊,那些事——一个程序员的奋斗史》——91
查看>>
简单的学生管理系统
查看>>
vue-cli目录结构及说明
查看>>
JS 数据类型转换
查看>>
WeQuant交易策略—RSI
查看>>
osgearth将视点绑定到一个节点上
查看>>
PHP 当前时间秒数+数值,然后再转换成时间。
查看>>
TNS-12537,TNS-12560,TNS-00507 Linux Error: 29: Illegal seek解决
查看>>
IOS开发之Post 方式获取服务器数据
查看>>
Python使用selenium模拟点击(二)
查看>>
go语言生成一张正弦图
查看>>
OOP的几个原则-----LSP:Liskov替换原则(下)
查看>>
DevExpress更新至13.1.7
查看>>
菜谱查询接口文档
查看>>
PID204特种部队
查看>>
P2420 让我们异或吧(倍增)
查看>>
codeforces 880E. Maximum Subsequence(折半搜索+双指针)
查看>>
分享Silverlight/Windows8/WPF/WP7/HTML5一周学习导读(5月14日-5月20日)
查看>>