1030 Travel Plan(Dijksta +DFS)
0、题目
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
1、大致题意
求起点到终点的最短路径最短距离和花费,要求首先路径最短,其次花费最少,要输出完整路径
2、基本思路
Dijksta
+ DFS
。 Dijkstra
记录路径 pre
数组,然后用 dfs
求最短的一条
m
i
n
c
o
s
t
min_{cost}
mincost 以及它的路径 path
,最后输出 path
数组和
m
i
n
c
o
s
t
min_{cost}
mincost
注意,路径 path
因为是从末端一直压入 push_back
到 path
里面的,所以要输出路径的时候倒着输出
3、解题过程
3.1 第一份代码(18/30)
写出第一份 Dijksta
+ DFS
的代码,很遗憾只对了第一个测试用例
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const long long inf=0x7fffffff;
int n,m,s,e,cnt,end_cost;
int head[10100],vis[10100],dis[10100];
vector<int> pre[10100];
vector<int> path;
struct Edge {
int v,w,cost,next;
} edge[501000];
void addEdge(int u, int v,int w,int cost) {
edge[++cnt].v=v;
edge[cnt].w=w;
edge[cnt].cost=cost;
edge[cnt].next=head[u];
head[u]=cnt;
}
struct node {
int dis;
int pos;
bool operator <( const node &x )const {
return x.dis < dis;
}
}ntmp;
priority_queue<node>q;
void dijkstra(int s) {
for(int i=1; i<=n; i++) { //这行有问题
dis[i]=inf;
vis[i]=0;
}
ntmp.dis=0;
ntmp.pos=s;
q.push(ntmp);
dis[s]=0;
while(!q.empty()) {
node u=q.top();
q.pop();
int pos=u.pos;
if(vis[pos]) {
continue;
}
vis[pos]=1;
for(int i=head[pos]; i; i=edge[i].next) {
int v=edge[i].v;
if(dis[v]>=dis[pos]+edge[i].w) {
if(dis[v]>dis[pos]+edge[i].w) {
pre[v].clear();
dis[v]=dis[pos]+edge[i].w;
}
pre[v].push_back(pos);
if(!vis[v]) {
ntmp.dis=dis[v];
ntmp.pos=v;
q.push((ntmp));
}
}
}
}
}
void dfs(int pos,int cost,vector<int> p) {
p.push_back(pos);
if(pos==s) {
if(end_cost>cost) {
end_cost=cost;
path=p;
}
return;
}
int tmp;
for(int i=0; i<pre[pos].size(); i++) {
for(int j=head[pos]; j; j=edge[j].next) {
if(edge[j].v==pre[pos][i]) {
tmp=edge[j].cost;
break;
}
}
cost+=tmp;
dfs(pre[pos][i],cost,p);
cost-=tmp;
}
return;
}
int main() {
scanf("%d%d%d%d",&n,&m,&s,&e);
memset(edge,0,sizeof(edge));
int u,v,w,cost;
cnt=0;
for(int i=0; i<m; i++) {
scanf("%d%d%d%d",&u,&v,&w,&cost);
addEdge(u,v,w,cost);
addEdge(v,u,w,cost);
}
dijkstra(s);
end_cost=inf;
vector<int>p;
dfs(e,0,p);
for(int i=path.size()-1; i>=0; i--) {
cout<<path[i]<<" ";
}
cout<<dis[e]<<" "<<end_cost;
return 0;
}
3.2 预处理的错误
经过排查,找到了一个测试用例,如下:
输入:
4 5 1 2
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
输出:
1 0 2 3 40
主要是在 点0
的位置没有初始化,然后出错。这个原因也很简单,就是因为我使用的Dijksta
板子,基本上都是以 0
为起始位置的,所以不用考虑 点0
,以后还是注意这些细节。
3.3 AC代码
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const long long inf=0x7fffffff;
int n,m,s,e,cnt,end_cost;
int head[10100],vis[10100],dis[10100];
vector<int> pre[10100];
vector<int> path;
struct Edge {
int v,w,cost,next;
} edge[501000];
void addEdge(int u, int v,int w,int cost) {
edge[++cnt].v=v;
edge[cnt].w=w;
edge[cnt].cost=cost;
edge[cnt].next=head[u];
head[u]=cnt;
}
struct node {
int dis;
int pos;
bool operator <( const node &x )const {
return x.dis < dis;
}
}ntmp;
priority_queue<node>q;
void dijkstra(int s) {
for(int i=0; i<=n; i++) { //注意这行
dis[i]=inf;
vis[i]=0;
}
ntmp.dis=0;
ntmp.pos=s;
q.push(ntmp);
dis[s]=0;
while(!q.empty()) {
node u=q.top();
q.pop();
int pos=u.pos;
if(vis[pos]) {
continue;
}
vis[pos]=1;
for(int i=head[pos]; i; i=edge[i].next) {
int v=edge[i].v;
if(dis[v]>=dis[pos]+edge[i].w) {
if(dis[v]>dis[pos]+edge[i].w) {
pre[v].clear();
dis[v]=dis[pos]+edge[i].w;
}
pre[v].push_back(pos);
if(!vis[v]) {
ntmp.dis=dis[v];
ntmp.pos=v;
q.push((ntmp));
}
}
}
}
}
void dfs(int pos,int cost,vector<int> p) {
p.push_back(pos);
if(pos==s) {
if(end_cost>cost) {
end_cost=cost;
path=p;
}
return;
}
int tmp;
for(int i=0; i<pre[pos].size(); i++) {
for(int j=head[pos]; j; j=edge[j].next) {
if(edge[j].v==pre[pos][i]) {
tmp=edge[j].cost;
break;
}
}
dfs(pre[pos][i],cost+tmp,p);
}
return;
}
int main() {
scanf("%d%d%d%d",&n,&m,&s,&e);
memset(edge,0,sizeof(edge));
int u,v,w,cost;
cnt=0;
for(int i=0; i<m; i++) {
scanf("%d%d%d%d",&u,&v,&w,&cost);
addEdge(u,v,w,cost);
addEdge(v,u,w,cost);
}
dijkstra(s);
end_cost=inf;
vector<int>p;
dfs(e,0,p);
for(int i=path.size()-1; i>=0; i--) {
cout<<path[i]<<" ";
}
cout<<dis[e]<<" "<<end_cost;
return 0;
}