1030 Travel Plan (30 分)(最短路径 and dfs)

#include<bits/stdc++.h> using namespace std;
const int N=;
const int inf=0x3f3f3f3f;
int mp[N][N];
bool vis[N];
int dis[N];
int n,m,s,D;
int cost[N][N];
vector<int>path[N];
void Dijkstra()
{
fill(vis,vis+N,false);
fill(dis,dis+N,inf);
for(int i=;i<n;i++) dis[i]=mp[s][i];
dis[s]=;
for(int i=;i<n-;i++){
int u=-;
int minn=inf;
for(int j=;j<n;j++){
if(!vis[j]&&dis[j]<minn){
u=j;
minn=dis[j];
}
}
if(u==-) return;
vis[u]=true;
for(int j=;j<n;j++){
if(!vis[j]&&dis[u]+mp[u][j]<=dis[j]){
if(mp[u][j]+dis[u]<dis[j]){
dis[j]=mp[u][j]+dis[u];
path[j].clear();
path[j].push_back(u);
}
else{
path[j].push_back(u);
}
}
} }
}
vector<int>t,p;
int minn=inf;
void dfs(int v)
{ if(v==s){
t.push_back(v);
int sum=;
for(int i=t.size()-;i>;i--){
int a=t[i];
int b=t[i-];
sum+=cost[a][b];
}
if(sum<minn){
minn=sum;
p=t;
}
t.pop_back();
return;
}
t.push_back(v);
for(int i=;i<path[v].size();i++){
dfs(path[v][i]);
}
t.pop_back(); }
int main()
{
fill(cost[],cost[]+N*N,);
fill(mp[],mp[]+N*N,inf);
scanf("%d %d %d %d",&n,&m,&s,&D);
while(m--){
int a,b,c,d;
scanf("%d %d %d %d",&a,&b,&c,&d);
mp[a][b]=mp[b][a]=c;
cost[a][b]=cost[b][a]=d;
}
Dijkstra();
dfs(D);
for(int i=p.size()-;i>=;i--){
printf("%d ",p[i]);
}
printf("%d %d\n",dis[D],minn);
return ;
}
1030 Travel Plan (30 分)(最短路径 and dfs)的更多相关文章
- 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
- 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 A 1030. Travel Plan (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...
- 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 Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]
题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...
- 1030 Travel Plan (30)(30 分)
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
- 1030. Travel Plan (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...
- PAT (Advanced Level) 1030. Travel Plan (30)
先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath& ...
随机推荐
- Angularjs实例应用
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Struts2 第四讲 -- Struts2的基本配置
5.struts2的基本配置 5.1 struts2的访问连接url 在struts1中,通过<action path=“/primer/helloWorldAction.action”> ...
- 在cmd下面执行.py文件时提示ModuleNotFoundError 但是 IDE 不报错
原理是 python 解释器寻找 模块的顺序决定,不细说 简略来讲就是 在 IDE中运行,会自动帮你把项目根目录添加到 PYTHONPATH 中,但是在 cmd 运行需要自己添加. 解决方法: 1. ...
- OI 刷题记录——每周更新
每周日更新 2016.05.29 UVa中国麻将(Chinese Mahjong,Uva 11210) UVa新汉诺塔问题(A Different Task,Uva 10795) NOIP2012同余 ...
- 构建高可靠hadoop集群之0-hadoop用户向导
本文翻译自:http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-hdfs/HdfsUserGuide.html 基于2.8. ...
- 【操作系统作业—lab1】linux shell脚本 遍历目标文件夹和所有文件 | 包括特殊字符文件名的处理
要求:写一个linux bash脚本来查看目标文件夹下所有的file和directory,并且打印出他们的绝对路径. 运行command:./myDir.sh input_path output_ ...
- 微信程序跳转到页面底部 scroll-view
wx.createSelectorQuery().select('#j_page').boundingClientRect(function (rect) { wx.pageScrollTo({ sc ...
- 2.5 进程控制之wait函数
一.绪论 一个进程在终止时会关闭所有文件描述符,释放在用户空间分配的内存,但它的PCB还保留着,内核在其中保存了一些信息:如果 是正常终止则保存着退出状态,如果是异常终止则保存着导致该进程终止的信号是 ...
- Leecode刷题之旅-C语言/python-70爬楼梯
/* * @lc app=leetcode.cn id=70 lang=c * * [70] 爬楼梯 * * https://leetcode-cn.com/problems/climbing-sta ...
- 黑洞数--python
黑洞数:黑洞数又称陷阱数,是类具有奇特转换特性的整数.任何一个数字不全相同整数,经有限“重排求差”操作,总会得某一个或一些数,这些数即为黑洞数.“重排求差”操作即把组成该数的数字重排后得到的最大数减去 ...