PAT1030.Travel Plan

题目

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:

1
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:

1
2
3
4
5
6
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:

1
0 2 3 3 40

代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#include <cstdio>
#include <vector>

using namespace std;
const int MAXV=505;
const int INF=1e9;
int G[MAXV][MAXV];
int cost[MAXV][MAXV];
int d[MAXV];
vector<int> pre[MAXV];
vector<int> tmp,res;
bool vis[MAXV]={false};
int n,m,s,ed;
int dis,distmp,total=INF;

void dfs(int now)
{
    if(now==s)
    {
        tmp.push_back(s);
        int u=ed,v;
        int totaltmp=0;
        for(int i=1;i<tmp.size();i++)
        {
            v=tmp[i];
            totaltmp+=cost[u][v];
            u=v;
        }
        if(totaltmp<total)
        {
            total=totaltmp;
            res=tmp;

        }
        tmp.pop_back();
        return;
    }

    tmp.push_back(now);
    for(int i=0;i<pre[now].size();i++)
        dfs(pre[now][i]);
    tmp.pop_back();
}

void dijkstra()
{
    fill(d,d+MAXV,INF);
    d[s]=0;
    pre[s].push_back(s);
    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&&d[u]+G[u][v]<=d[v])
            {
                if(d[u]+G[u][v]==d[v])
                {
                    pre[v].push_back(u);
                }
                else // <
                {
                    pre[v].clear();
                    pre[v].push_back(u);
                    d[v]=d[u]+G[u][v];
                }
            }
        }
    }

}
int main()
{
    fill(G[0],G[0]+MAXV*MAXV,INF);
    fill(cost[0],cost[0]+MAXV*MAXV,INF);
    scanf("%d%d%d%d",&n,&m,&s,&ed);
    int p,q,k,j;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d%d",&p,&q,&k,&j);
        G[p][q]=G[q][p]=k;
        cost[p][q]=cost[q][p]=j;
    }
    dijkstra();
    dfs(ed);
    for(int v=res.size()-1;v>=0;v--)
    {
        printf("%d ",res[v]);
    }
    printf("%d %d\n",d[ed],total);
    return 0;
}
Licensed under CC BY-NC-SA 4.0
Built with Hugo
主题 StackJimmy 设计