【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 ...
随机推荐
- app内区域截图利用html2Canvals保存到手机 截屏 (html2Canvals2版本1.0.0)
html2canvas($(], {scale:,logging:false,useCORS:true}).then(function(canvas) { var dataUrl = canvas.t ...
- oracle常见的函数
1.字符函数 -- initcap函数只针对英文 select * from tb_user where user_name = initcap('张三'); -- ltrim 左剪切 select ...
- 常用工具api等链接
java技术驿站 http://cmsblogs.com/ WatchMen(守望者成才网) http://www.watchmen.cn/ JAVA知识分享 http://www.java1234. ...
- tp5 配置 // 视图输出字符串内容替换 'view_replace_str' 的原理
- vb.net 实现多态
1. 新建一个module,设置public 其他类才可以调用 Public Module Module2 Public Interface MyInterface Property stuName ...
- js指定范围指定个数的不重复随机数
今天偶然看到的 比如要生成 1-100范围之内的10个不重复随机数,代码就可以这么写 var arr = []; for (var i = 1; i <=100; i++) { arr.push ...
- Java面向对象编程 -3.3
综合实战 简单Java类 在以后进行项目开发与设计的过程之中,简单Java类都将作为一个重要的组成部分存在,慢慢接触到正规的项目设计后, 简单Java类无处不再,并且有可能产生一系列的变化. 所谓的简 ...
- proto school tutorial: blog: lesson 1
https://proto.school/#/blog/01 我们现在希望:把一个post 和他的作者联系起来. 从之前的教程here中,你可以知道, 一个cid 就是代表一个link 比如: { l ...
- leetcode 0209
目录 ✅ 500. 键盘行 描述 解答 ✅ 905. 按奇偶排序数组 描述 解答 py sorted 参数详解 ✅ 559. N叉树的最大深度 描述 解答 java dfs-like method(r ...
- ipfs 资料汇集
目录 js ipfs u can use wikipeida here js ipfs https://github.com/ipfs/js-ipfs u can use wikipeida here ...