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 -> v~1~ -> ... -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w~1~ -> ... -> 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 -> u~1~ -> ... -> 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
给出一些streets的端点v1,v2,如果是one-way的,即单程,那么只能从v1到v2,如果不是单程的,也可以从v2到v1,找出最短的路(不唯一,那就找出其中时间最短的)和时间最短的(不唯一,就找出
经过地点最少的),如果说这两个路径一样,就只输出一次,按照题目给定的格式。
代码:
#include <stdio.h>
#include <string.h>
#define inf 0x3f3f3f3f
int m,n,source,destination,a,b,w,l,t;
int length[][],times[][],dis[],cost[],cost1[],num[],vis[],path1[],path2[];
void getpath1(int x) {
if(x != source) {
getpath1(path1[x]);
printf(" -> ");
}
printf("%d",x);
}
void getpath2(int x) {
if(x != source) {
getpath2(path2[x]);
printf(" -> ");
}
printf("%d",x);
}
int equals(int x) {
if(path1[x] != path2[x])return ;
else if(x == source)return ;
return equals(path1[x]);
}
int main() {
scanf("%d%d",&n,&m);
for(int i = ;i < n;i ++) {
for(int j = ;j < n;j ++) {
length[i][j] = times[i][j] = inf;
}
dis[i] = cost[i] = cost1[i] = inf;
path1[i] = path2[i] = -;
}
for(int i = ;i < m;i ++) {
scanf("%d%d%d%d%d",&a,&b,&w,&l,&t);
if(w) {
length[a][b] = l;
times[a][b] = t;
}
else {
length[a][b] = length[b][a] = l;
times[a][b] = times[b][a] = t;
}
}
scanf("%d%d",&source,&destination);
dis[source] = cost[source] = cost1[source] = ;
while() {
int t = -,mi = inf;
for(int i = ;i < n;i ++) {
if(!vis[i] && mi > dis[i]) {
mi = dis[i];
t = i;
}
}
if(t == -)break;
vis[t] = ;
for(int i = ;i < n;i ++) {
if(vis[i] || length[t][i] == inf)continue;
if(dis[i] > dis[t] + length[t][i]) {
path1[i] = t;
dis[i] = dis[t] + length[t][i];
cost1[i] = cost1[t] + times[t][i];
}
else if(dis[i] == dis[t] + length[t][i] && cost1[i] > cost1[t] + times[t][i]) {
cost1[i] = cost1[t] + times[t][i];
path1[i] = t;
}
}
}
memset(vis,,sizeof(vis));
while() {
int t = -,mi = inf;
for(int i = ;i < n;i ++) {
if(!vis[i] && mi > cost[i]) {
mi = cost[i];
t = i;
}
}
if(t == -)break;
vis[t] = ;
for(int i = ;i < n;i ++) {
if(vis[i] || times[t][i] == inf)continue;
if(cost[i] > cost[t] + times[t][i]) {
path2[i] = t;
cost[i] = cost[t] + times[t][i];
num[i] = num[t] + ;
}
else if(cost[i] == cost[t] + times[t][i] && num[i] > num[t] + ) {
num[i] = num[t] + ;
path2[i] = t;
}
}
}
printf("Distance = %d",dis[destination]);
if(!equals(destination)) {
printf(": ");
getpath1(destination);
printf("\n");
}
else {
printf("; ");
}
printf("Time = %d: ",cost[destination]);
getpath2(destination);
}

1111 Online Map (30)(30 分)的更多相关文章

  1. 1111 Online Map (30 分)

    1111 Online Map (30 分) Input our current position and a destination, an online map can recommend sev ...

  2. 1111 Online Map (30 分)

    1111. Online Map (30)Input our current position and a destination, an online map can recommend sever ...

  3. 【刷题-PAT】A1111 Online Map (30 分)

    1111 Online Map (30 分) Input our current position and a destination, an online map can recommend sev ...

  4. PAT 1111 Online Map[Dijkstra][dfs]

    1111 Online Map(30 分) Input our current position and a destination, an online map can recommend seve ...

  5. PAT甲级——1131 Subway Map (30 分)

    可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30  ...

  6. PAT甲级——1111 Online Map (单源最短路经的Dijkstra算法、priority_queue的使用)

    本文章同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90041078   1111 Online Map (30 分) ...

  7. PAT甲级1111. Online Map

    PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...

  8. etectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30))

    # 函数原型detectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30)) # gray需要识别的图片 # 1.03:表示每次图像尺寸减小 ...

  9. c# 时间格式处理,获取格式: 2014-04-12T12:30:30+08:00

    C#  时间格式处理,获取格式: 2014-04-12T12:30:30+08:00 一.获取格式: 2014-04-12T12:30:30+08:00 方案一:(局限性,当不是当前时间时不能使用) ...

  10. java.time.format.DateTimeParseException: Text '2019-10-11 12:30:30' could not be parsed at index 10

    java.time.format.DateTimeParseException: Text '2019-10-11 12:30:30' could not be parsed at index 10 ...

随机推荐

  1. HDU 小明系列故事——师兄帮帮忙 高速幂

    小明系列故事--师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  2. Java 加载器

    类的加载是由类加载器完成的,类加载器包括: 根加载器( BootStrap ).扩展加载器( Extension ).系统加载器( System )和用户自定义类加载器( java.lang.Clas ...

  3. MySQL 优化、设计规则浅谈

    当数据量大,数据库相应慢时都会针对数据库进行优化.这时都是要针对具体情况,具体业务需求进行优化的. 但是有些步骤和规则应该适合各种情况的.这里综合网上找的资料简单分析一下. 第一优化你的sql和索引: ...

  4. ngui 输入事件处理

    NGUI不仅提供了图形接口,还提供了输入事件接口!事件接口是通过UICamera来实现的. Unity3d 为我们提供的原装的input尽管非常方便,但真正跨平台使用时(尤其是跨手机与Pc机时)仍然不 ...

  5. Spring Boot从入门到实战:整合Web项目常用功能

    在Web应用开发过程中,一般都涵盖一些常用功能的实现,如数据库访问.异常处理.消息队列.缓存服务.OSS服务,以及接口日志配置,接口文档生成等.如果每个项目都来一套,则既费力又难以维护.可以通过Spr ...

  6. Zend API:深入 PHP 内核

    Introduction Those who know don't talk. Those who talk don't know. Sometimes, PHP "as is" ...

  7. ip获取位置

    $ip = $_SERVER["REMOTE_ADDR"]; $url = "http://ip.taobao.com/service/getIpInfo.php?ip= ...

  8. hibernate QBC查询

    HQL运算符 QBC运算符 含义 = Restrictions.eq() 等于equal <>  Restrictions.ne() 不等于not equal >  Restrict ...

  9. A charge WIFI point base on airbase-ng+dhcp+lamp+wiwiz

    Make wifi as a hot point Make a script echo $0 $1 case $1 in "start") sleep 1 ifconfig wla ...

  10. 【BZOJ4212】神牛的养成计划 Trie树+可持久化Trie树

    [BZOJ4212]神牛的养成计划 Description Hzwer成功培育出神牛细胞,可最终培育出的生物体却让他大失所望...... 后来,他从某同校女神 牛处知道,原来他培育的细胞发生了基因突变 ...