(一)题意

题目链接: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的更多相关文章

  1. PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS

    PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...

  2. PAT1030. Travel Plan (30)

    #include <iostream> #include <limits> #include <vector> using namespace std; int n ...

  3. [图算法] 1030. Travel Plan (30)

    1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...

  4. 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 ...

  5. 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 ...

  6. 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

    题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...

  7. PAT1030. Travel Plan

    //晴神模板,dij+dfs,貌似最近几年PAT的的图论大体都这么干的,现在还在套用摸板阶段....估计把这及格图论题题搞完,dij,dfs,并查集就掌握差不多了(模板还差不多)为何bfs能自己干出来 ...

  8. 1030. Travel Plan (30)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...

  9. PAT A 1030. Travel Plan (30)【最短路径】

    https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...

随机推荐

  1. 构建高性能web站点-阅读笔记(一)

    看完前9章,也算是看完一半了吧,总结一下. 郭欣这个名字或许并不响亮,但是这本书写的确实真好!百度一下他的名字也能够看到他是某些公司的创始人和投资者,当然他本人必定是大牛无疑. 从网页的动静分离到网络 ...

  2. Spring+SpringMVC+MyBatis+easyUI整合优化篇(四)单元测试实例

    日常啰嗦 前一篇文章<Spring+SpringMVC+MyBatis+easyUI整合优化篇(三)代码测试>讲了不为和不能两个状态,针对不为,只能自己调整心态了,而对于不能,本文会结合一 ...

  3. IOS开发创建开发证书及发布App应用(二)——创建证书

    2. 创建证书 证书分为两种,一种是开发者证书,主要是用来真机调试的 另一种就是发布证书,就是用来发布应用的, 最好是两种都要下载,不然编译时候可能报错,我猜想可能苹果怕你没用真机调试 创建证书分为两 ...

  4. iOS关于友盟分享弹不出面板问题

    在程序代理类中声明 [NSThread sleepForTimeInterval:10];//设置启动页面时间 [self.window makeKeyAndVisible]; [[UMSocialM ...

  5. 深入浅出数据结构C语言版(6)——游标数组及其实现

    在前两次博文中,我们由表讲到数组,然后又由数组的缺陷提出了指针式链表(即http://www.cnblogs.com/mm93/p/6576765.html中讲解的带有next指针的链表).但是指针式 ...

  6. 第1章1zabbix快速入门

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; t ...

  7. Spring Hiernate整合

    Spring整合Hibernate 一.整合目标 1.由IoC容器管理Hibernate的SessionFactory2.让Hibernate使用Spring的声明式事务 二.整合步骤 先加入Hibe ...

  8. JSP自定义不带属性和标签体的简单标签

    1. 新建HelloTag类 2. 添加额外的Jar包 (1). 右键项目 -> Build Path -> Configure Build Path -> Libraries -& ...

  9. 《连载 | 物联网框架ServerSuperIO教程》- 18.集成OPC Client,及使用步骤

    1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...

  10. 六行python代码的爱心曲线

    前些日子在做绩效体系的时候,遇到了一件囧事,居然忘记怎样在Excel上拟合正态分布了,尽管在第二天重新拾起了Excel中那几个常见的函数和图像的做法,还是十分的惭愧.实际上,当时有效偏颇了,忽略了问题 ...