pat 1030 train plan

一点教训

俩种方法:

  • 一是直接用Dijstra,另一种是Dijstra+DFS的
  • 基本上就是二层条件进行筛选,也很常规,当然,注意pre的初始化
    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
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #define MAX 510
    #define INF 1000000000
    using namespace std;
    int n , m , s , d;
    struct Node
    {
    int dis , cost;
    };
    Node G[MAX][MAX];
    bool vis[MAX] = {false};
    int dist[MAX];
    int w[MAX];
    int pre[MAX];
    void Dijstra(int s)
    {
    fill(dist , dist + MAX , INF);
    fill(w , w + MAX , 0);
    fill(pre , pre + MAX , 0);
    dist[s] = 0;
    int i , j;
    for(i = 0 ; i < n ; ++i)
    {
    int u = -1 , min = INF;
    for(j = 0 ; j < n ; ++j)
    {
    if(vis[j] == false && min > dist[j])
    {
    u = j;
    min = dist[j];
    }
    }
    if(u == -1)
    return ;
    vis[u] = true;
    if(u == d)
    {
    break;
    }
    for(j = 0 ; j < n ; ++j)
    {
    if(vis[j] == false && G[u][j].dis != INF && G[u][j].cost != INF)
    {
    if(dist[u] + G[u][j].dis < dist[j])
    {
    dist[j] = dist[u] + G[u][j].dis;
    w[j] = w[u] + G[u][j].cost;
    pre[j] = u;
    }
    else if(dist[u] + G[u][j].dis == dist[j])
    {
    if(w[j] > w[u] + G[u][j].cost)
    {
    w[j] = w[u] + G[u][j].cost;
    pre[j] = u;
    }
    }
    }
    }
    }
    }
    void DFS(int s , int v)
    {
    if(s == v)
    {
    cout<<v;
    return ;
    }
    DFS(s , pre[v]);
    cout<<" "<<v;
    }
    int main()
    {
    cin>>n>>m>>s>>d;
    int i ;
    Node temp;
    temp.cost = INF;
    temp.dis = INF;
    fill(G[0] , G[0] + MAX * MAX , temp);
    for(i = 0 ; i < m ; ++i)
    {
    int a , b , dis , cost;
    cin>>a>>b>>dis>>cost;
    G[a][b].dis = dis;
    G[a][b].cost = cost;
    G[b][a].dis = dis;
    G[b][a].cost = cost;
    }
    Dijstra(s);
    DFS(s , d);
    cout<<" "<<dist[d]<<" "<<w[d];
    return 0;
    }
    /*
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #define MAX 510
    #define INF 1000000000
    using namespace std;
    int n , m , s , d;
    struct Node
    {
    int dis , cost;
    };
    Node G[MAX][MAX];
    bool vis[MAX] = {false};
    vector<int> path , tempPath;
    vector<int> pre[MAX];
    int dist[MAX];
    int w[MAX];
    void Dijstra(int s)
    {
    fill(dist , dist + MAX , INF);
    fill(w , w + MAX , 0);
    dist[s] = 0;
    int i,j;
    for(i = 0 ; i < n ; ++i)
    {
    int u = -1 , min = INF;
    for(j = 0; j < n ; ++j)
    {
    if(vis[j] == false && min > dist[j])
    {
    u = j;
    min = dist[j];
    }
    }
    if(u == -1)
    return ;
    vis[u] = true;
    for(j = 0; j < n ; ++j)
    {
    if(vis[j] == false && G[u][j].dis != INF && G[u][j].cost != INF)
    {
    if(dist[u] + G[u][j].dis < dist[j])
    {
    dist[j] = dist[u] + G[u][j].dis;
    pre[j].clear();
    pre[j].push_back(u);
    }
    else if(dist[u] + G[u][j].dis == dist[j])
    {
    pre[j].push_back(u);
    }
    }
    }
    }
    }
    int minCost = INF;
    void DFS(int v)
    {
    if(v == s)
    {
    tempPath.push_back(v);//记录最后一个节点,即源节点
    int tempCost = 0;
    for(int i = tempPath.size() -1 ; i > 0 ; --i)//将记录的全部节点进行边权累加
    {
    int id = tempPath[i];//当前节点编号为id
    int idnext = tempPath[i -1];//下一个节点(切记此处i-1可能导致数组越界访问,所以i>0)
    tempCost += G[id][idnext].cost;
    }
    if(tempCost < minCost)//每次递归的时候可能更新此值
    {
    minCost = tempCost;
    path = tempPath;
    }
    tempPath.pop_back();//用完以后要pop
    return;
    }
    tempPath.push_back(v);//记录经过的路径
    for(int i = 0 ; i < pre[v].size() ; ++i)
    {
    DFS(pre[v][i]);
    }
    tempPath.pop_back();//attention
    }
    int main()
    {
    cin>>n>>m>>s>>d;
    Node temp;
    temp.dis = INF;
    temp.cost = INF;
    fill(G[0] , G[0] + MAX * MAX , temp);
    int i;
    for(i = 0 ; i < m ; ++i)
    {
    int a , b , dis , cost;
    cin>>a>>b>>dis>>cost;
    G[a][b].dis = dis;
    G[a][b].cost = cost;
    G[b][a] = G[a][b];
    }
    Dijstra(s);
    DFS(d);
    for(i = path.size() - 1 ; i >= 0 ; --i)
    {
    cout<<path[i]<<" ";
    }
    cout<<dist[d]<<" "<<minCost;
    return 0;
    }*/

热评文章