(一)题意

题目链接: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. WebStorm里启动electron项目

    WebStorm里启动electron项目,其实很简单 一.第一步打开下面的窗口 二.然后输入electron .,然后敲下 回车键,然后等会项目界面就会出现了. PS:electron 和 点之间有 ...

  2. 性能测试分享:MYSQL死锁

    poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-845052 ...

  3. poj 3270 Cow Sorting (置换入门)

    题意:给你一个无序数列,让你两两交换将其排成一个非递减的序列,每次交换的花费交换的两个数之和,问你最小的花费 思路:首先了解一下什么是置换,置换即定义S = {1,...,n}到其自身的一个双射函数f ...

  4. POI 3.8读取2003与2007格式EXCEL(xls、xlsx)

    废话少说直接上代码,记得是poi3.8版本啊.方法入口唯一,自动判断格式,使用接口引用,自动选择执行方法. 方法入口: public static ArrayList<String[]> ...

  5. 自动生成数学题型二(框架struts2)题型如((a+b)*c=d)

    1. 生成题目 1.1 生成单个题目 public static String[] twoOperatorAndOperator(int num1, int num2) { double first ...

  6. 转账示例(一):Dao层面实现(本例采用QueryRunner来执行sql语句,数据源为C3P0)

    缺点:Dao层面把Service层面的操作完成了,不利于后期的代码修改和重构 1.自行创建C3P0Util account数据库 2.jar包 3.Dao层面 接口: package com.lear ...

  7. vue获取dom元素内容

    通过ref来获取dom元素 在vue官网上对ref的解释 ref 被用来给元素或子组件注册引用信息.引用信息将会注册在父组件的 $refs 对象上.如果在普通的 DOM 元素上使用,引用指向的就是 D ...

  8. java复习(5)---接口、继承、多态

    Java作为完全面向对象语言,接口.继承和多态是三个非常重要的概念. 1.继承. (1)关键字: extends (2)子类用super()调用父类构造函数,用super().方法 调用父类的成员方法 ...

  9. 【BFS + Hash】拼图——携程2017春招编程题2

    写在前面 前天参加了携程的网测--还是感觉自己太!渣!了!    _(:з」∠)_ 时光匆匆啊,已经到了开始思考人生的时候了(算了不矫情了)--总之写个博客来督促一下自己.之前太懒了,很多时候都是输在 ...

  10. 如何有效快速提高Java服务端开发人员的技术水平?

    我相信很多工作了3-5年的开发人员都会经常问自己几个问题: 1.为什么总是感觉技术没有质的提高? 2.如何能够有效和快速的提高自身的技术水平? 3.如何进入到一个牛逼的大公司,认识牛逼的人? 这篇文章 ...