#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)的更多相关文章

  1. 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

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

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

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

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

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

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

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

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

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

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

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

  8. 1030 Travel Plan (30)(30 分)

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

  9. 1030. Travel Plan (30)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...

  10. PAT (Advanced Level) 1030. Travel Plan (30)

    先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath& ...

随机推荐

  1. 深入 Struts2 的配置 - 处理多个请求-处理请求结果-模型驱动-异常机制

    转:http://www.java3z.com/cwbwebhome/article/article2/2938.html?id=1631 本部分主要介绍struts.xml的常用配置. 1.1.   ...

  2. HDU 1110 Equipment Box (判断一个大矩形里面能不能放小矩形)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1110 Equipment Box Time Limit: 2000/1000 MS (Java/Oth ...

  3. caffe convert mxnet

    https://github.com/apache/incubator-mxnet/tree/430ea7bfbbda67d993996d81c7fd44d3a20ef846/tools/caffe_ ...

  4. 涉及JSP、Servlet的页面编码问题

    1. JSP页面中,二处的字符编码有何区别 1.<%@ page contentType="text/html;charset=UTF-8" %> 是服务器端java程 ...

  5. mysql快速导入导出数据

    --导入 select * from inhos_genoperation(表名) where UPLOAD_ORG_CODE='***' into outfile '/tmp/inhos_genop ...

  6. onload函数不执行

    原因: <jsp:include page="">通过该标签引入jsp时会导致<body>失效 案例: <jsp:include page=" ...

  7. java中泛型的简单使用

    泛型是在jdk1.5之后引入的,我们可以在类的声明处增加泛型列表,如:<T,E,V>.此处,字符可以是任何标识符,一般采用这3个字母. 1.泛型类声明 class MyCollection ...

  8. java各种业务解决方案总结

    最近有点时间,突然感慨良多,感觉辛苦工作这么久什么都没有,总结了以前的工作,将接触的主要工具列出来,希望给大家解决问题做参考.相关工具都是实践检验过的 1.数据库 (1).内存数据库 redis (2 ...

  9. plupload批量上传分片(后台代码)

    plupload批量上传分片功能, 对于文件比较大的情况下,plupload支持分片上传,后台代码如下: /** * * 方法:upLoadSpecialProgramPictrue * 方法说明:本 ...

  10. C++最接近整数的浮点运算

    Function return ceil 不小于给定值的最接近整数值 floor 不大于给定值的最接近整数 trunc (C++11) 绝对值不大于给定值的最接近整数 round(C++11) 最接近 ...