0
点赞
收藏
分享

微信扫一扫

1030 Travel Plan (30分)———— Dijkstra+DFS、SPFA

1 题目

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

2 解析

2.1 题意

给出城市个数、城市间距离、城市间花费、起点和终点,求起点到终点间的最短路径、最短距离和最小花费。

2.2 思路

用Dijkstra算法计算最短距离同时记录最短路径和最小花费。

3 参考代码

3.1 Dijkstra(邻接表):

#include 
#include
#include
#include

using std::fill;
using std::vector;

struct node
{
int v;
int dis;
};

const int MAXV = 600;
const int INF = 0x3fffffff;
vector<node> G[MAXV];
int N, start, end;
bool vis[MAXV] = {false};
int cost[MAXV][MAXV];
int d[MAXV], c[MAXV];
int pre[MAXV];

void Dijkstra(int s){
fill(d, d + MAXV, INF);
fill(c, c + MAXV, INF);
d[s] = 0;
c[s] = 0;

for (int i = 0; i < N; ++i)
{
pre[i] = i;
}

for (int i = 0; i < N; ++i)
{
int u = -1, min = INF;
for (int j = 0; j < N; ++j)
{
if(vis[j] == false && d[j] < min){
u = j;
min = d[j];
}
}

if(u == -1){
return;
}
vis[u] = true;

for (int j = 0; j < G[u].size(); ++j)
{
int v = G[u][j].v;
if(vis[v] == false){
if(d[u] + G[u][j].dis < d[v]){
d[v] = d[u] + G[u][j].dis;
c[v] = c[u] + cost[u][v];
pre[v] = u;
}else if(d[u] + G[u][j].dis == d[v]){
if(c[v] > c[u] + cost[u][v]){
c[v] = c[u] + cost[u][v];
pre[v] = u;
}
}
}
}
}
}

void DFS(int v){
if(v == start){
printf("%d ", v);
return;
}

DFS(pre[v]);
printf("%d ", v);
}

int main(int argc, char const *argv[])
{
int m;
scanf("%d%d%d%d", &N, &m, &start, &end);

int e1, e2, distance;
for (int i = 0; i < m; ++i)
{
scanf("%d%d%d", &e1, &e2, &distance);
scanf("%d", &cost[e1][e2]);
cost[e2][e1] = cost[e1][e2];
node temp;
temp.v = e2;
temp.dis = distance;
G[e1].push_back(temp);
temp.v = e1;
G[e2].push_back(temp);
}

Dijkstra(start);
DFS(end);

printf("%d %d\n", d[end], c[end]);
return 0;
}

3.2 Dijkstra+DFS:

*Dijkstra求最短距离并记录前驱,DFS求最短距离和最小花费

#include 
#include
#include
#include

using std::fill;
using std::vector;

struct node
{
int v;
int dis;
};

const int MAXV = 600;
const int INF = 0x3fffffff;
int G[MAXV][MAXV];
int N, start, end;
bool vis[MAXV] = {false};
int cost[MAXV][MAXV];//花费矩阵
int d[MAXV];//最短距离
int minCost = INF;//最短路径上的最小花费
vector<int> pre[MAXV];//前驱
vector<int> tempPath, path;//临时路径、最优路径

void Dijkstra(int s){
fill(d, d + MAXV, INF);
d[s] = 0;

for (int i = 0; i < N; ++i)
{
int u = -1, min = INF;
for (int j = 0; j < N; ++j)
{
if(vis[j] == false && d[j] < min){
u = j;
min = d[j];
}
}

if(u == -1){
return;
}
vis[u] = true;
for (int v = 0; v < N; ++v)
{
if(vis[v] == false && G[u][v] != INF){
if(d[u] + G[u][v] < d[v]){
d[v] = d[u] + G[u][v];
pre[v].clear();
pre[v].push_back(u);
}else if(d[u] + G[u][v] == d[v]){
pre[v].push_back(u);

}
}
}
}
}

void DFS(int v){
if(v == start){
tempPath.push_back(v);
int tempCost = 0;
for (int i = tempPath.size() - 1; i > 0 ; --i)
{
int id = tempPath[i], idNext = tempPath[i - 1];
tempCost += cost[id][idNext];
}

if(tempCost < minCost){
minCost = tempCost;
path = tempPath;
}

tempPath.pop_back();
return;
}

tempPath.push_back(v);
for (int i = 0; i < pre[v].size(); ++i)
{
DFS(pre[v][i]);
}
tempPath.pop_back();
}

int main(int argc, char const *argv[])
{
int m;
scanf("%d%d%d%d", &N, &m, &start, &end);

fill(G[0], G[0] + MAXV * MAXV, INF);

int u, v, distance;
for (int i = 0; i < m; ++i)
{
scanf("%d%d", &u, &v);
scanf("%d%d", &G[u][v], &cost[u][v]);
G[v][u] = G[u][v];
cost[v][u] = cost[u][v];
}

Dijkstra(start);
DFS(end);

for (int i = path.size() - 1; i >= 0; --i)
{
printf("%d ", path[i]);
}

printf("%d %d\n", d[end], minCost);
return 0;
}

3.3 SPFA

#include 
#include
#include
#include
#include
#include

using std::set;
using std::queue;
using std::fill;
using std::vector;

struct node
{
int v;
int dis;
node(int _v, int _dis): v(_v) , dis(_dis){}
};

const int MAXN = 520;
const int INF = 0x3fffffff;
vector<node> G[MAXN];
vector<int> tempPath, Path;
vector<int> pre[MAXN];
int N;
int start, end;
bool inq[MAXN];
int d[MAXN];
int cost[MAXN][MAXN];
int minCost = INF;
// int num[MAXN];

void SPFA(int s){
// memset(num, 0, sizeof(num));
memset(inq, false , sizeof(inq));
fill(d, d + MAXN, INF);

queue<int> q;
q.push(s);
inq[s] = true;
d[s] = 0;
// num[s]++;

while(!q.empty()){
int u = q.front();
q.pop();

inq[u] = false;

for (int j = 0; j < G[u].size(); ++j)
{
int v = G[u][j].v;
int dis = G[u][j].dis;
if(d[v] > d[u] + dis){
d[v] = d[u] + dis;

pre[v].clear();
pre[v].push_back(u);

if(!inq[v]){
q.push(v);
inq[v] = true;
// num[v]++;
// if(num[v] >= N) return;
}
}else if(d[v] == d[u] + dis){

pre[v].push_back(u);

if(!inq[v]){
q.push(v);
inq[v] = true;
// num[v]++;
// if(num[v] >= N) return;
}
}
}
}
}

void DFS(int v){
if(v == start){
tempPath.push_back(v);
int tempCost = 0;
for (int i = tempPath.size() - 1; i > 0; --i)
{
int id = tempPath[i], idNext = tempPath[i - 1];
tempCost += cost[id][idNext];
}

if(tempCost <minCost){
minCost = tempCost;
Path = tempPath;
}

tempPath.pop_back();
return;
}

tempPath.push_back(v);
for (int i = 0; i < pre[v].size(); ++i)
{
DFS(pre[v][i]);
}
tempPath.pop_back();
}

int main(int argc, char const *argv[])
{
fill(cost[0], cost[0] + MAXN * MAXN, INF);

int m, u, v, distance;
scanf("%d%d%d%d", &N, &m, &start, &end);

for (int i = 0; i < m; ++i)
{
scanf("%d%d%d", &u, &v, &distance);
scanf("%d", &cost[u][v]);
cost[v][u] = cost[u][v];
G[u].push_back(node(v,distance));
G[v].push_back(node(u,distance));
}

SPFA(start);

DFS(end);

for (int i = Path.size() - 1; i >= 0; --i)
{
printf("%d ", Path[i]);
}

printf("%d %d\n", d[end], minCost);
return 0;
}


举报

相关推荐

0 条评论