(一)题意

题目链接: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. flask框架+pygal+sqlit3搭建图形化业务数据分析平台

    一. 前言 先说下主要的框架和主要的图形库的特点:(个人见解) Django:python开发的一个重量级的web框架,集成了MVC和ORM等技术,设计之初是为了使开发复杂的.数据库驱动的网站变得简单 ...

  2. 【C++】模拟实现auto_ptr

    看了<Effctive C++>,里面提到用对象去管理资源,可以有效防止内存泄漏. 结合auto_ptr特性,稍微思考了一下,实现了一个简单的auto_ptr (因为代码量小,就不分文件了 ...

  3. centOS下调整swap

    [root@localhost /]# mkdir swap [root@localhost /]# cd swap [root@localhost swap]# dd if=/dev/zero of ...

  4. POPTEST老李分享修改dns ip的vbs代码

    POPTEST老李分享修改dns ip的vbs代码   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨 ...

  5. 老李分享:Web Services 组件 2

    WSDL 是一种基于 XML 的语言,它用来对 web service 及其如何访问进行描述. WSDL 表示 web service 描述语言(Web Services Description La ...

  6. 老李分享: Oracle Performance Tuning Overview 翻译下

    1.2性能调优特性和工具 Effective data collection and analysis isessential for identifying and correcting perfo ...

  7. (iOS)私有API的使用(原创)

    最近在做企业级程序,需要搞设备的udid等信息,但是ios7把udid私有化了,不公开使用.所以研究了一下ios的私有api. 调查了一下文章,发现这方面的文章不多,国内更是不全,高手们都懒得写基础教 ...

  8. Nest客户端的基本使用方法

    通过Nuget安装好Nest的相关Dll,之后我们就可以开始了, 1.初始化Nest客户端 string indexName = "customer"; Uri uri = new ...

  9. huffman压缩解压文件【代码】

    距离上次写完哈夫曼编码已经过去一周了,这一周都在写huffman压缩解压,哎,在很多小错误上浪费了很多时间调bug.其实这个程序的最关键部分不是我自己想的,而是借鉴了某位园友的代码,但是,无论如何,自 ...

  10. C#---------------继承和多态初步

    继承:在程序中,如果一个类A:类B,这种机制就是继承. 子类可以继承父类的所有内容(成员)吗? 解析: 1.私有成员(属性和方法) 2.构造函数 3.final修饰过的方法,子类不能进行重写 3.访问 ...