【刷题-PAT】A1111 Online Map (30 分)
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
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
分析:dijkstra + dfs,先找出最短路径,再dfs出最短路径,根据题目中tie的处理方式选择出最终的路径
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<unordered_map>
#include<set>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
const int nmax = 510, inf = (1 << 31) - 1;
struct node{
int v, len, t;
};
vector<node>G[nmax];
int d[nmax], td[nmax], t[nmax], preV[nmax];
vector<int>pret[nmax];
bool visv[nmax] = {false}, vist[nmax] = {false};
void dij(int s, int n){
fill(d, d + nmax, inf);
fill(td, td + nmax, inf);
fill(t, t + nmax, inf);
d[s] = 0, td[s] = 0, t[s] = 0;
for(int i = 0; i < n; ++i){
int u = -1, MIN = inf;
for(int j = 0; j < n; ++j){
if(visv[j] == false && d[j] < MIN){
MIN = d[j];
u = j;
}
}
if(u == -1)return;
visv[u] = true;
for(int j = 0; j < G[u].size(); ++j){
int v = G[u][j].v;
if(visv[v] == false){
if(d[u] + G[u][j].len < d[v]){
d[v] = d[u] + G[u][j].len;
td[v] = td[u] + G[u][j].t;
preV[v] = u;
}else if(d[u] + G[u][j].len == d[v] && td[u] + G[u][j].t < td[v]){
td[v] = td[u] + G[u][j].t;
preV[v] = u;
}
}
}
u = -1, MIN = inf;
for(int j = 0; j < n; ++j){
if(vist[j] == false && t[j] < MIN){
MIN = t[j];
u = j;
}
}
if(u == -1)return;
vist[u] = true;
for(int j = 0; j < G[u].size(); ++j){
int v = G[u][j].v;
if(vist[v] == false){
if(t[u] + G[u][j].t < t[v]){
t[v] = t[u] + G[u][j].t;
pret[v].clear();
pret[v].push_back(u);
}else if(t[u] + G[u][j].t == t[v]){
pret[v].push_back(u);
}
}
}
}
}
vector<int>pathv;
void dfs1(int s, int e){
pathv.push_back(s);
if(s == e)return;
dfs1(preV[s], e);
}
vector<int>patht, temp;
int intermin = inf;
void dfs2(int s, int e){
temp.push_back(s);
if(s == e){
if(temp.size() < intermin){
intermin = temp.size();
patht = temp;
}
return;
}
for(int i = 0; i < pret[s].size(); ++i){
dfs2(pret[s][i], e);
temp.pop_back();
}
}
void Print(vector<int> &path){
for(int i = path.size() - 1; i >= 0; --i){
printf("%d", path[i]);
if(i > 0)printf(" -> ");
else printf("\n");
}
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif // ONLINE_JUDGE
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i < m; ++i){
int v1, v2, tag, len, t;
scanf("%d%d%d%d%d", &v1, &v2, &tag, &len, &t);
G[v1].push_back({v2, len, t});
if(tag == 0)G[v2].push_back({v1, len, t});
}
int s, e;
scanf("%d%d", &s, &e);
dij(s, n);
dfs1(e, s);
dfs2(e, s);
bool flag = false;
if(patht.size() == pathv.size()){
int i = 0;
while(i < patht.size() && pathv[i] == patht[i])i++;
if(i == patht.size())flag = true;
}
printf("Distance = %d", d[e]);
if(flag == false){
printf(": ");
Print(pathv);
}else{
printf("; ");
}
printf("Time = %d: ", t[e]);
Print(patht);
return 0;
}
【刷题-PAT】A1111 Online Map (30 分)的更多相关文章
- 牛客网刷题(纯java题型 1~30题)
牛客网刷题(纯java题型 1~30题) 应该是先extend,然后implement class test extends A implements B { public static void m ...
- 【刷题-PAT】A1135 Is It A Red-Black Tree (30 分)
1135 Is It A Red-Black Tree (30 分) There is a kind of balanced binary search tree named red-black tr ...
- 【刷题-PAT】A1119 Pre- and Post-order Traversals (30 分)
1119 Pre- and Post-order Traversals (30 分) Suppose that all the keys in a binary tree are distinct p ...
- 【刷题-PAT】A1095 Cars on Campus (30 分)
1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each gate we ...
- PAT-1111 Online Map (30分) 最短路+dfs
明天就要考PAT,为了应付期末已经好久没有刷题了啊啊啊啊,今天开了一道最短路,状态不是很好 1.没有读清题目要求,或者说没有读完题目,明天一定要注意 2.vis初始化的时候从1初始化到n,应该从0开始 ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- [PAT] 1147 Heaps(30 分)
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)
1147 Heaps (30 分) In computer science, a heap is a specialized tree-based data structure that sati ...
- PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
随机推荐
- PowerShell配置文件后门
PowerShell 配置文件是在 PowerShell 启动时运行的脚本. 在某些情况下,攻击者可以通过滥用PowerShell配置文件来获得持久性和提升特权.修改这些配置文件,以包括任意命 ...
- windows10下vscode+cmake编译Qt5代码(2)
概述 本文依赖 前文 本文将介绍使用cmake语法屏蔽运行Qt exe的cmd窗口 解决办法 配置VS项目属性, 缺点: cmake重新 configure后,项目属性会重置,再次运行还将出现cmd弹 ...
- 【LeetCode】771. Jewels and Stones 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 数组count 字典Counter 日期 题目地址 ...
- D. Water Tree
D. Water Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...
- web安全之xss跨站脚本攻击
实验(一) 一.预备知识 1.HTML基础知识 2.phpstudy运用 3.xss的分类 二.实验环境 1.火狐浏览器.Chrome浏览器 2.phpstudy 三.环境搭建 反射型xss环 ...
- 使用AVPlayer自定义支持全屏的播放器(五)—Swift重构版本
前言 很早之前开源了一个简单的视频播放器,由于年久失修,效果惨目忍睹,最近特意花时间对其进行了深度重构.旧版本后期不再维护,新版本使用Swift实现,后续会增加更多功能.不想看文字的请自行下载代码-- ...
- Spring Boot 使用 Filter
Filter 是 JavaEE 中 Servlet 规范的一个组件,位于包javax.servlet 中,它可以在 HTTP 请求到达 Servlet 之前,被一个或多个Filter处理. 1. 编写 ...
- 第五个知识点 复杂性为NP类是什么意思
第五个知识点 复杂性为NP类是什么意思 原文地址:http://bristolcrypto.blogspot.com/2014/11/52-things-number-5-what-is-meant- ...
- [opencv]利用minAreaRect计算平面矩形的旋转角度
#include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include ...
- [opencv]approxDP多边形逼近获取四边形轮廓信息
#include "opencv2/opencv.hpp" #include <iostream> #include <math.h> #include & ...