7-9 tourism planning (25 points) Dijkstra single source shortest path algorithm

Posted by rossmurphy on Tue, 31 Dec 2019 15:23:58 +0100

7-9 tourism planning (25 points)

With a self driving tour map, you will know the length of the expressway between cities and the toll charges for the highway. Now you need to write a program to help the tourists to find the shortest path between the departure and the destination. If there are several paths that are the shortest, you need to output the cheapest one.

Input format:

Input Description: the first line of the input data gives four positive integers N, m, S, D, where N (2 ≤ N ≤ 500) is the number of cities, by the way, assume that the number of cities is 0~(N − 1); m is the number of highways; S is the number of cities where the departure is; D is the number of cities where the destination is. In the following M lines, each line gives the information of a highway, namely: City 1, city 2, highway length, toll amount, separated by spaces in the middle, and the numbers are integers and no more than 500. The input guarantees the existence of the solution.

Output format:

The length of the output path and the total charge amount in a line are separated by spaces, and no extra spaces are allowed at the end of the output.

Input example:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Output example:

3 40

On the basis of dijkstra algorithm, a minimum cost is added when the path length is equal. Just add a d[u]+edge[u][j]==d[j] in the relaxation operation.

The code is as follows:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=505;
const int INF=0x3f3f3f3f;
int n,m,s,d;
//Whether to judge in the set
int vis[maxn];
//Shortest path to source
int dj[maxn];
//Cost between two points
int spend[maxn][maxn];
//Minimum cost with origin
int sp[maxn];
//Distance between two points
int edge[maxn][maxn];
//Initialization function
void init ()
{
    memset (vis,0,sizeof(vis));
    for (int i=0;i<n;i++)
       for (int j=0;j<n;j++)
           i==j? edge[i][j]=0,spend[i][j]=0:edge[i][j]=INF,spend[i][j]=INF;
}
//shortest path
void djst ()
{
    for (int i=0;i<n;i++)
    {
        dj[i]=edge[s][i];
        sp[i]=spend[s][i];
    }
    while (1)
    {
       int k=-1;
       int maxx=INF;
       for (int i=0;i<n;i++)
       {
           if(maxx>dj[i]&&!vis[i])
           {
               maxx=dj[i];
               k=i;
           }
       }
       if(k==-1)
        break;
        vis[k]=1;
       for (int i=0;i<n;i++)
       {
           //Relaxation operation
           if(dj[k]+edge[k][i]<dj[i]&&!vis[i])
           {
               dj[i]=dj[k]+edge[k][i];
               sp[i]=sp[k]+spend[k][i];
           }
           //Take the minimum cost
           if(dj[k]+edge[k][i]==dj[i]&&!vis[i])
           {
               sp[i]=min(sp[i],sp[k]+spend[k][i]);
           }
       }
    }
}
int main()
{
    scanf("%d%d%d%d",&n,&m,&s,&d);
    init ();
    for (int i=0;i<m;i++)
    {
        int x,y,len1,spend1;
        scanf("%d%d%d%d",&x,&y,&len1,&spend1);
        edge[x][y]=edge[y][x]=len1;
        spend[x][y]=spend[y][x]=spend1;
    }
    djst();
    printf("%d %d\n",dj[d],sp[d]);
    return 0;
}