PAT1030 Travel Plan (30)---DFS
(一)题意
题目链接:https://www.patest.cn/contests/pat-a-practise/1030
1030. Travel Plan (30)
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output
0 2 3 3 40
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
(二)题解
这道题是PAT最很喜欢出的一道求最短路的变形题。解法有很多种,但鉴于我做上一道LeetCode花费了过多的经历,而且还要面对导师催论文的压力,我只写一种解法。
直接用DFS寻找从源点到目的地的所有路径中距离最短且花费最少的路径,用path记录路径。
DFS很方便可以记录路径,是借助深度deep作为路径下标。
代码如下:
#include <bits/stdc++.h>
using namespace std;
#define maxn 510
#define For(I,A,B) for(int I = (A); I < (B); I++)
int n,m,s,d;
bool vis[maxn];
int dist[maxn][maxn];
int cost[maxn][maxn];
int cur_path[maxn],path[maxn];
int mindis,minc,len; void dfs(int node,int dis,int cc,int deep)
{
cur_path[deep] = node;
if(node == d)
{
if(dis < mindis)
{
mindis = dis;
minc = cc;
len = deep;
For(i,,deep + )
path[i] = cur_path[i];
}
else if (dis == mindis && cc < minc)
{
mindis = dis;
minc = cc;
len = deep;
For(i,,deep + )
path[i] = cur_path[i];
}
return;
}
For(i,,n)
{
if(!vis[i] && dist[i][node] != -)
{
vis[i] = ;
dfs(i,dis + dist[i][node],cc + cost[node][i],deep + );
vis[i] = ;
}
}
}
int main()
{
//freopen("1030.in","r",stdin);
while(scanf("%d%d%d%d",&n,&m,&s,&d) != EOF)
{
int t1,t2;
mindis = minc = INT_MAX;
For(i,,n)
For(j,,n)
dist[i][j] = -;
For(i,,m)
{
scanf("%d%d",&t1,&t2);
scanf("%d%d",&dist[t1][t2],&cost[t1][t2]);
dist[t2][t1] = dist[t1][t2];
cost[t2][t1] = cost[t1][t2];
}
vis[s] = ;
dfs(s,,,);
For(i,,len + )
cout<<path[i]<<" ";
cout<<mindis<<" "<<minc<<endl;
}
return ;
}
相似的题目还包括PAT1018(https://www.patest.cn/contests/pat-a-practise/1018)和PAT1003(https://www.patest.cn/contests/pat-a-practise/1003)
PAT1030 Travel Plan (30)---DFS的更多相关文章
- PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS
PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...
- PAT1030. Travel Plan (30)
#include <iostream> #include <limits> #include <vector> using namespace std; int n ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, to ...
- PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]
题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...
- 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
- PAT1030. Travel Plan
//晴神模板,dij+dfs,貌似最近几年PAT的的图论大体都这么干的,现在还在套用摸板阶段....估计把这及格图论题题搞完,dij,dfs,并查集就掌握差不多了(模板还差不多)为何bfs能自己干出来 ...
- 1030. Travel Plan (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...
- PAT A 1030. Travel Plan (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...
随机推荐
- CrawlScript脚本语言实现网络爬虫
前段时间我们学习了几种爬虫技术,我们来回顾一下,webCollector,htmlParser,Jsoup,各有优劣,但是如果能灵活运用,其实都是很不错的.那么,今天呢,我们来学习一种脚本语言,这是一 ...
- java日期转换
在java开发过程中,时间的转换时必须掌握的=========下面把时间转换做个总结,有可能不是很全面 时间格式只有两种 yyyy-MM-DD yyyy/MM/DD 时间的类型:字符串类型.sql类型 ...
- wamp2.4.4 如何配置虚拟主机及反向代理(解决跨域问题)
一.找到安装目录下的httpd.conf文件 1. 删除Include conf/extra/httpd-vhosts.conf前面的#号(开启虚拟主机的配置) 2. 删除LoadModule pro ...
- HackerRank The Chosen One [预处理][gcd]
题解:tags:[预处理][gcd]故事背景:光头钻进了茫茫人海.这是一个典型の通过前缀后缀和来降低复杂度的问题.先用pre数组与suf数组分别维护前缀gcd和后缀gcd.如果 a[i] % gcd( ...
- 手机自动化测试:Appium源码分析之跟踪代码分析九
手机自动化测试:Appium源码分析之跟踪代码分析九 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家 ...
- 老李分享:Android性能优化之内存泄漏2
这种创建Handler的方式会造成内存泄漏,由于mHandler是Handler的非静态匿名内部类的实例,所以它持有外部类Activity的引用,我们知道消息队列是在一个Looper线程中不断轮询处理 ...
- 用java写的一个程序,可以调用windows系统中自带的各种工具,例如截图,便签等
由于图片资源没有上传,所以运行后不会有图片,感兴趣的同学可以@我,我打包上传, package SmallPrograme; import java.awt.*; import java.awt.ev ...
- 跟着刚哥梳理java知识点——包装类(十)
Java为8种基本数据类型都提供了对应的包装器类型 装箱和拆箱: public class Main { public static void main(String[] args) { Intege ...
- 【mysql】关于InnoDB表text blob大字段的优化
最近在数据库优化的时候,看到一些表在设计上使用了text或者blob的字段,单表的存储空间已经达到了近100G,这种情况再去改变和优化就非常难了 一.简介 为了清楚大字段对性能的影响,我们必须要知道i ...
- sql中 datediff的使用
简介:我们在sql中经常要判断年或者月或者日是否相等,我们可以用datediff函数,使用很方便 datediff:判断年或月或日或周.星期.小时.分钟等的差别数使用格式: DATEDIFF(date ...