题意:

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

trick:

找了半小时bug终于发现是给dis数组赋初值时范围是1~N,这道题点是从0~N-1的,故初次只通过了第0组数据,初始化改一下边界即可AC。

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int n,m,s,d;
int a[];
typedef struct road{
int x,y,z;
};
vector<road>edg[];
vector<pair<int,int> >pre[];
vector<pair<int,int> >tmp_path,path;
int dis[];
bool vis[];
int ans=1e9;
void spfa(){
for(int i=;i<n;++i)
dis[i]=1e9;
dis[s]=;
vis[s]=;
queue<int>q;
q.push(s);
while(!q.empty()){
int x=q.front();
vis[x]=;
q.pop();
for(int i=;i<edg[x].size();++i){
int v=edg[x][i].x;
int w=edg[x][i].y;
int val=edg[x][i].z;
if(dis[v]>dis[x]+w){
dis[v]=dis[x]+w;
pre[v].clear();
pre[v].push_back({x,a[val]});
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
else if(dis[v]==dis[x]+w)
pre[v].push_back({x,a[val]});
}
}
}
void dfs(int x,int y){
if(x==s)
if(y<ans){
ans=y;
path=tmp_path;
}
tmp_path.push_back({x,y});
for(int i=;i<pre[x].size();++i)
dfs(pre[x][i].first,y+pre[x][i].second);
tmp_path.pop_back();
}
int main(){
cin>>n>>m>>s>>d;
int u,v,w;
for(int i=;i<=m;++i){
cin>>u>>v>>w>>a[i];
edg[u].push_back({v,w,i});
edg[v].push_back({u,w,i});
}
spfa();
dfs(d,);
cout<<s<<" ";
for(int i=path.size()-;i>=;--i)
cout<<path[i].first<<" ";
cout<<dis[d]<<" "<<ans;
return ;
}

【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)的更多相关文章

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

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

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

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

  4. PAT 甲级 1030 Travel Plan

    https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 A traveler's map gives ...

  5. 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)

    A traveler's map gives the distances between cities along the highways, together with the cost of ea ...

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

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

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

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

  8. PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)

    1080 Graduate Admission (30 分)   It is said that in 2011, there are about 100 graduate schools ready ...

  9. PAT 甲级 1072 Gas Station (30 分)(dijstra)

    1072 Gas Station (30 分)   A gas station has to be built at such a location that the minimum distance ...

随机推荐

  1. CI 框架多表关联查询

    public function getCollectData($limit, $page, $search_user, $search_phone, $orderfield, $ordertype) ...

  2. ‘\0’的ASCII码

    1.'\0'的ASCII码为0 2.用串口发送字符串时,可以通过'\0'判断字符串是否结束,但发送数字数组的时候不能通过'\0'判断数组是否结束,因为数字0与'\0'的ASCII码值相同.

  3. vue 每20秒刷新1次接口的实现方法

    实现代码: setInterval(() => { setTimeout(fun, ) }, ) 备注: setInterval 放在内层 长时间会影响性能,造成页面卡顿甚至崩溃, 内层配合se ...

  4. 谈谈对Spring IOC的理解(转载)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  5. 页面布局 Wrap 组件 和 RaisedButton按钮

    一.RaisedButton 定义一个按钮 Flutter 中通过 RaisedButton 定义一个按钮.RaisedButton 里面有很多的参数,只讲简单的进行使用. return Raised ...

  6. 命令行选项解析函数getopt()

    1.定义: int getopt(int argc, char * const argv[], const char *optstring); 2.描述: getopt是用来解析命令行选项参数的,但是 ...

  7. 机器学习 — 从mnist数据集谈起

    做了一些简单机器学习任务后,发现必须要对数据集有足够的了解才能动手做一些事,这是无法避免的,否则可能连在干嘛都不知道,而一些官方例程并不会对数据集做过多解释,你甚至连它长什么样都不知道... 以skl ...

  8. python csv 数据切割定制jmeter数据

    需求压测随机抽取10w数据中自定义区间的指定数量数据进行压测: jmeter csv/txt配置: 需要获取{data: [${myList}]  }  jmeter需要数据类型 获取展读取csv数据 ...

  9. 【笔记3-用户模块】从0开始 独立完成企业级Java电商网站开发(服务端)

    数据表结构设计 关系设计 为什么不用外键? 分库分表有外键会非常麻烦,清洗数据也很麻烦.数据库内置触发器也不适合采用. 查业务问题的后悔药--时间戳 create_time 数据创建时间 update ...

  10. ROS及Ubuntu学习记录

    1.ROS安装时出现 “无法获得锁的解决方法” 参考:https://www.jianshu.com/p/6868ed4b4aec 2.运行rviz时 要先在一个终端窗口运行roscore 3.虚拟机 ...