【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意:
输入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)的更多相关文章
- 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 A 1030. Travel Plan (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...
- PAT 甲级 1030 Travel Plan
https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 A traveler's map gives ...
- 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
- PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS
PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)
1080 Graduate Admission (30 分) It is said that in 2011, there are about 100 graduate schools ready ...
- 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 ...
随机推荐
- 远程连接本地mongodb 数据库
绑定本地IP即可 start mongod --dbpath D:\mongodb\data\db --bind_ip 192.168.31.143
- Python短文本自动识别个体是否有自杀倾向【新手必学】
我们以微博树洞为例,讲解了怎么自动爬取单个微博的评论.今天我们就要用上这些数据做一个自杀倾向分类器,这样的分类器如果应用得当,将可以帮助成千上万误入歧途的人们挽回生命. 为了简化问题,我们将短文本分为 ...
- HGAME 2020 misc
week1 每日推荐 拿到Wireshark capture file后,按常规思路,用foremost命令拿到一个加密的压缩包,之后文件->导出对象->http,看到最大的一个文件里面最 ...
- ping命令基于ICMP协议的返回信息分析
Ping是潜水艇人员的专用术语,表示回应的声纳脉冲,在网络中 Ping 是一个十分好用的 TCP/IP 工具.它主要的功能是用来检测网络的连通情况和分析网络速度.可以利用 PING 命令检查网络连通状 ...
- 疫情下的在线上课方案:QQ直播+蓝墨云班课
目录 疫情下的在线上课方案:QQ群视频(腾讯课堂)+蓝墨云班课 使用QQ进行直播 材料 QQ直播步骤 其他问题 使用蓝墨云班课加强学习效果 教材问题 我的直播-小学生硬笔书法基础 我的直播 - C程序 ...
- Educational Codeforces Round 73
唉,又是掉分的一场比赛... A. 2048 Game 题意:给出一个数组,问能不能通过一系列操作(将数组中的两个数相加变成另一个数),使得数组中包含2048,数组中的数全是2的指数,可以则输出YES ...
- Java日期时间API系列19-----Jdk8中java.time包中的新的日期时间API类,ZonedDateTime与ZoneId和LocalDateTime的关系,ZonedDateTime格式化和时区转换等。
通过Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类中时间范围示意图:可以很清晰的看出ZonedDateTime相当于LocalDateTime+ZoneI ...
- LeetCode练题——53. Maximum Subarray
1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...
- Spring 的 Bean 生命周期,11 张高清流程图及代码,深度解析
在网上已经有跟多Bean的生命周期的博客,但是很多都是基于比较老的版本了,最近吧整个流程化成了一个流程图.待会儿使用流程图,说明以及代码的形式来说明整个声明周期的流程.注意因为代码比较多,这里的流程图 ...
- JavaScirpt - 模块的写法
传送门 http://www.ruanyifeng.com/blog/2012/10/javascript_module.html 1. 原始写法 function f1() { // do sth. ...