PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS
PAT 1030 最短路最小边权 堆优化dijkstra+DFS
1030 Travel Plan (30 分)
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤500) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input:
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output:
0 2 3 3 40
题目大意:求起点到终点的最短路径最短距离和花费,要求首先路径最短,其次花费最少,要输出完整路径
分析:Dijksta + DFS。 Dijkstra记录路径pre数组,然后用dfs求最短的一条mincost以及它的路径path,最后输出path数组和mincost
注意路径path因为是从末端一直压入push_back到path里面的,所以要输出路径的时候倒着输出
关键是DFS的写法,应该是在一个链状的图数据结构上进行搜索,所以先加一个判断条件,如果到了起点就统计总长度,如果更小,更新anscost并更新path.注意这个时候仍然要pop_back(),没准还有一条更短的路径能够通向起点,之后dfs之后pop_back是常规操作.
void dfs(int v)
{
temppath.push_back(v);
if(v==from)
{
int tempcost=0;
for(int i=temppath.size()-1;i>=1;i--)
{
int id=temppath[i];
int nextid=temppath[i-1];
tempcost+=length[id][nextid];
}
if(tempcost<anscost)
{
path=temppath;
anscost=tempcost;
}
temppath.pop_back();//还得继续迭代呢,没准费用更小
return ;
}
for(int i=0;i<pre[v].size();i++)
{
dfs(pre[v][i]);
}
temppath.pop_back();
}
#include <iostream>
#include<bits/stdc++.h>
#define each(a,b,c) for(int a=b;a<=c;a++)
#define de(x) cout<<#x<<" "<<(x)<<endl
using namespace std;
const int maxn=500+5;
const int inf=0x3f3f3f3f;
int dis[maxn];
bool vis[maxn];
vector<int>pre[maxn];
vector<int>path,temppath;
int length[maxn][maxn];
struct Edge
{
int v;
int len;
int cost;
Edge(int v,int len,int cost):v(v),len(len),cost(cost){}
};
vector<Edge>G[maxn];
struct node
{
int v;
int len;
node(int v=0,int len=0):v(v),len(len){}
bool operator<(const node&r)const
{
return len>r.len;
}
};
void dijkstra(int n,int start)
{
each(i,0,n-1)
{
vis[i]=false;
dis[i]=inf;
}
dis[start]=0;
priority_queue<node>Q;
node temp;
Q.push(node(start,0));
while(!Q.empty())
{
temp=Q.top();
Q.pop();
int u=temp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=0;i<(int)G[u].size();i++)
{
int v=G[u][i].v;
int len=G[u][i].len;
int cost=G[u][i].cost;
if(!vis[v]&&dis[v]>dis[u]+len)
{
dis[v]=dis[u]+len;
pre[v].clear();
pre[v].push_back(u);
Q.push(node(v,dis[v]));
}
else if(!vis[v]&&dis[v]==dis[u]+len)///我佛拉
{
pre[v].push_back(u);
///需不需要push呢????????
}
}
}
}
/*
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
*/
int n,m,from,to;
int anscost;
void dfs(int v)
{
temppath.push_back(v);
if(v==from)
{
int tempcost=0;
for(int i=temppath.size()-1;i>=1;i--)
{
int id=temppath[i];
int nextid=temppath[i-1];
tempcost+=length[id][nextid];
}
if(tempcost<anscost)
{
path=temppath;
anscost=tempcost;
}
temppath.pop_back();//还得继续迭代呢,没准费用更小
return ;
}
for(int i=0;i<pre[v].size();i++)
{
dfs(pre[v][i]);
}
temppath.pop_back();
}
int main()
{
anscost=inf;
cin>>n>>m>>from>>to;
while(m--)
{
int a,b,l,cost;
scanf("%d%d%d%d",&a,&b,&l,&cost);
G[a].push_back(Edge(b,l,cost));
G[b].push_back(Edge(a,l,cost));
length[a][b]=length[b][a]=cost;
}
dijkstra(n,from);
dfs(to);
for(int i=path.size()-1;i>=0;i--)
{
printf("%d ",path[i]);
}
printf("%d %d\n",dis[to],anscost);
return 0;
}
PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+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甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
- 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[图论][难]
1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...
- [图算法] 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 ...
- PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径
模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...
- PAT A 1030. Travel Plan (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...
- 1030 Travel Plan (30)(30 分)
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
随机推荐
- chrome下载提示网络错误
问题背景:项目开发测试阶段出现该问题. 复现:开发调试过程中一直没有问题,本地下载excel.Word.pdf 都完美,但是在服务部署到服务器之后,测试环境的chrome就总是下载失败,提示网络错误. ...
- Spring Junit4 接口测试
Junit实现接口类测试 - dfine.sqa - 博客园http://www.cnblogs.com/Automation_software/archive/2011/01/24/1943054. ...
- 真正的能理解CSS中的line-height,height与line-height
https://blog.csdn.net/a2013126370/article/details/82786681 在最近的项目中,常常用到line-height,只是简单的理解为行高但并没有深层次 ...
- 基于spark logicplan的表血缘关系解析实现
随着公司平台用户数量与表数量的不断增多,各种表之间的数据流向也变得更加复杂,特别是某个任务中会对源表读取并进行一系列复杂的变换后又生成新的数据表,因此需要一套表血缘关系解析机制能清晰地解析出每个任务所 ...
- Spring cloud微服务安全实战-5-1单点登录基本架
基于微服务架构,前后端分离实现SSO 前后端分离到底是个什么样的架构. 中间不是用Nginx而是用Node JS. node.js在服务端的页面渲染,这样搜索引擎爬虫 来爬的时候 爬的就是html页面 ...
- 【429】关于ADT的访问权限
在看老师代码的时候,发现ADT中的 struct 有时候写到了 adt.c 里面,有时候写到了 adt.h 里面,其实有些困惑,经过仔细研究,发现写在 adt.h 中的 struct 可以在 test ...
- 123457123456#2#----com.ppGame.XueYingYu76--前拼后广--儿童英语_pp
com.ppGame.XueYingYu76--前拼后广--儿童英语_pp
- Delphi : TStringList的Find,IndexOf和Sort
关键:Find要事先Sort排序,Indexof不用排序. TStringList内部查找相关的数据.待调试代码时才知道痛苦,浪费无数时间后,只得一步步跟踪,才发 现Find方法返回的Index总是错 ...
- Docker之使用Docker-compose搭建LNMP环境
之前有随笔介绍使用Docker-compose搭建LNMP环境(centos6 php5.6) https://www.cnblogs.com/minseo/p/10146982.html 本文介绍D ...
- win7和Ubuntu16.04之间相互远程控制
一.在win7中远程控制ubuntu16.04 步骤: 在ubuntu中通过Desktop share打开允许远程访问 ubuntu中安装xrdp 由于xrdp与unity.gnome桌面不兼容,所以 ...