【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 ...
随机推荐
- Python 高级特性介绍 - 迭代的99种姿势 与协程
Python 高级特性介绍 - 迭代的99种姿势 与协程 引言 写这个笔记记录一下一点点收获 测试环境版本: Python 3.7.4 (default, Sep 28 2019, 16:39:19) ...
- CentOS6.5_x64卸载系统原有MySQL
1.查看系统是否存在MySQL的版本 rpm -qa | grep mysql 2.删除老版本的开头文件和库(rpm -e --nodeps XXX) rpm -e --nodeps mysql-5. ...
- java篇 之 继承
this代表正在使用类的对象(的引用) java支持重载:允许在同一个类中使用相同的方法名(重载类型只区分参数列表,包括参数 顺序,参数个数,参数数据类型,与方法返回类型无关) 匹配: 方法名 参数列 ...
- 用for循环创建对象
以下代码Demo: public class TestDemo { public static void main(String[] args) { //以创建5个student为例 int coun ...
- yii2.0 ajax
2.0用的参数是_csrf token = "<?php echo \Yii::$app->request->getCsrfToken()?>", $.aj ...
- EF Expression 扩展
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; na ...
- jmeter csv 插件讲解
1.变量名称 name,pwd 格式表示因为文本中分割默认是逗号所以变量设置也是按此格式如果想按其他格式可以在分隔符栏自定义 2.忽略首行: 有的csv读取你希望读取的数据有header如: user ...
- ASP.NET Core搭建多层网站架构【6-注册跨域、网站核心配置】
2020/01/29, ASP.NET Core 3.1, VS2019, NLog.Web.AspNetCore 4.9.0 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站 ...
- Fluent_Python_Part4面向对象,10-seq-hacking,序列的修改、散列和切片
第四部分第10章,序列的修改.散列和切片 中文电子书P423 这一章接第1章.第9章,以第9章定义的Vector2d类为基础,定义表示多为向量的Vector类.这个类的行为与Python中标准的不可变 ...
- mysql的一些不常用语句
今天写项目,用的ThinkPHP,写的时候有点费劲,原因嘛 无非是对框架或者mysql的一些知识的遗忘. 1.为了配合ThinkPHP中的控制器,需要修改数据库中表名,一时想不起来,幸好有百度,问题很 ...