PAT甲级1111. Online Map

题意:

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

输入规格:

每个输入文件包含一个测试用例。对于每种情况,

第一行给出两个正整数N(2 <= N <= 500),M分别是地图上街道交叉路口的总数和街道数。然后M行跟随,每个描述一个街道的格式:

V1 V2单程长度时间

其中V1和V2是街道两端的索引(从0到N-1);一-

方式是1,如果街道从V1到V2是单向的,或者如果没有,则为0;长度是街道的长度;时间是通过街道的时间。

最后给出一对源和目的地。

输出规格:

对于每种情况,首先打印距离为D的源到目的地的最短路径,格式如下:

距离= D:源 - > v1 - > ... - >目的地

然后在下一行打印总时间最快的路径T:

时间= T:源 - > w1 - > ... - >目的地

如果最短路径不唯一,则输出最短路径中最快的路径,这被保证是唯一的。如果最快的路径不是唯一的,

输出通过最少交叉路口的路口,这被保证是唯一的。

如果最短和最快的路径相同,请以以下格式打印一行:

距离= D;时间= T:源 - > u1 - > ... - >目的地

思路:

Dijkstra + dfs.写了一个小时,有点慢。

ac代码:

C++

// pat1111.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
#include<unordered_set> using namespace std; int n, m;
int starting, destination;
int mymap[501][501];
int mytime[501][501];
int visit[501];
int len[501];
int vec[501];
int disway[501];
int timeway[501];
int temp[501];
int short_time = -1;
int time_len = -1;
int dlen = 0,time_dis = 0; void Dijkstra()
{
int now = -1; memset(visit, 0, sizeof(visit));
memset(len, -1, sizeof(len));
len[starting] = 0;
while (1)
{
now = -1;
for (int i = 0; i < n; i++)
{
if (!visit[i] && len[i] != -1 && (now == -1 || len[i] < len[now])) now = i;
}
if (now == -1) break;
visit[now] = 1;
for (int i = 0; i < n; i++)
{
if (!visit[i] && mymap[now][i] && (len[i] == -1 || mymap[now][i] + len[now] < len[i] ))
len[i] = mymap[now][i] + len[now];
}
} memset(vec, -1, sizeof(vec));
memset(visit, 0, sizeof(visit));
vec[starting] = 0;
while (1)
{
now = -1;
for (int i = 0; i < n; i++)
{
if (!visit[i] && vec[i] != -1 && (now == -1 || vec[i] < vec[now])) now = i;
}
if (now == -1) break;
visit[now] = 1;
for (int i = 0; i < n; i++)
{
if (!visit[i] && mytime[now][i] && (vec[i] == -1 ||mytime[now][i] + vec[now] < vec[i]))
vec[i] = mytime[now][i] + vec[now];
}
} } void dfs(int cur, int dis, int time, int pos)
{
if (dis > len[destination] && time > vec[destination]) return;
if (dis == len[destination] && cur == destination)
{
if (short_time == -1 || time < short_time)
{
short_time = time;
dlen = pos;
for (int i = 0; i < pos; i++)
disway[i] = temp[i];
}
}
if (time == vec[destination] && cur == destination)
{
if (time_len == -1 || pos < time_len)
{
time_len = pos;
time_dis = dis;
for (int i = 0; i < pos; i++)
timeway[i] = temp[i];
}
} for (int i = 0; i < n; i++)
{
if (!visit[i] && mymap[cur][i])
{
temp[pos] = i;
dfs(i, dis + mymap[cur][i], time + mytime[cur][i], pos + 1);
}
} } int main()
{
//input
scanf("%d %d", &n, &m);
int v1, v2, is_one, l, t;
for (int i = 0; i < m; i++)
{
scanf("%d %d %d %d %d", &v1, &v2, &is_one, &l, &t);
if (is_one)
{
mymap[v1][v2] = l;
mytime[v1][v2] = t;
}
else
{
mymap[v1][v2] = mymap[v2][v1] = l;
mytime[v1][v2] = mytime[v2][v1] = t;
}
}
scanf("%d %d", &starting, &destination); Dijkstra(); memset(visit, 0, sizeof(visit));
//temp[0] = starting;
dfs(starting,0,0,0); if (time_dis == len[destination] && short_time == vec[destination])
{
printf("Distance = %d; Time = %d: %d", time_dis, short_time,starting);
// -> 2 -> 5"
for (int i = 0; i < time_len; i++)
printf(" -> %d", timeway[i]);
printf("\n");
}
else
{
printf("Distance = %d: %d", len[destination], starting);
for (int i = 0; i < dlen; i++)
printf(" -> %d", disway[i]);
printf("\n"); printf("Time = %d: %d", vec[destination], starting);
for (int i = 0; i < time_len; i++)
printf(" -> %d", timeway[i]);
printf("\n");
} return 0;
}

PAT甲级1111. Online Map的更多相关文章

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

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

  2. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  3. PAT甲级——A1131 Subway Map【30】

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  4. PAT甲级1131 Subway Map【dfs】【输出方案】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805347523346432 题意: 告诉你一个地铁线路图,站点都是 ...

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

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

  6. PAT甲级——A1111 Online Map【30】

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

  7. PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]

    题目 Input our current position and a destination, an online map can recommend several paths. Now your ...

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

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

  9. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. 06 Frequently Asked Questions (FAQ) 常见问题解答 (常见问题)

    Frequently Asked Questions (FAQ) Origins 起源 What is the purpose of the project? What is the history ...

  2. vue总结 01基础特性

    最近有时间来总结一下vue的知识: 一.vue.js 被定义成一个开发web界面的前端库,是一个非常轻量的工具.vue.js本身具有响应式和组件化的特点. 我们不需要在维护视图和数据的统一上花费大量的 ...

  3. Python 深拷贝、浅拷贝

    Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的,如果使用的时候不注意,就可能产生意外的结果. 首先,对赋值操作我们要有以下认识: 赋值是将一个对象的地址赋值给一个变量,让变量指向该地址( ...

  4. Python全局变量和局部变量

    全局变量和局部变量 定义在函数内部的变量拥有一个局部作用域,定义在函数外的拥有全局作用域. 局部变量只能在其被声明的函数内部访问,而全局变量可以在整个程序范围内访问.调用函数时,所有在函数内声明的变量 ...

  5. 开源的python机器学习模块

    1. Scikit-learn Scikit-learn 是基于Scipy为机器学习建造的的一个Python模块,他的特色就是多样化的分类,回归和聚类的算法包括支持向量机,逻辑回归,朴素贝叶斯分类器, ...

  6. 使用文本用户界面(NMTUI)进行网络配置

    NetworkManager 文本用户界面(TUI)工具 nmtui 可提供一个文本界面配置由 NetworkManager 控制的网络.该工具包含在 NetworkManager-tui 子软件包中 ...

  7. JQuery中Table标签页和无缝滚动

    HTML代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...

  8. Winfom 插件式(Plugins)/模块化开发框架-动态加载DLL窗体-Devexpress

    插件式(AddIn)架构,不是一个新名词,应用程序采用插件式拼合,可以更好的支持扩展.很多著名的软件都采用了插件式的架构,如常见的IDE:Eclipse,Visual Studio,SharpDeve ...

  9. bzoj 4552

    4552 思路: 二分线段树: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #define ...

  10. spring boot配置使用fastjson

    一.前言 spring boot默认使用jackson来操作json数据,相比于jackson,fastjson更好用,功能也强大,所以这里记录一下在spring boot中配置使用fastjson的 ...