1030 Travel Plan (30)(30 分)

Dijkstra里面计算最短路与最少花费,记录最优路径pre

#include
#include
using namespace std;
const int INF = 1e9 + 10;
const int maxv = 510;
int G[maxv][maxv], d[maxv], c[maxv], cost[maxv][maxv];
bool vis[maxv];
int n, m, s, dd;
int pre[maxv];
vectorpath;
void Dijkstra(int s)
{
    fill(d, d + maxv, INF);
    d[s] = 0;
    fill(c, c + maxv, INF);
    c[s] = 0;
    while (1)
    {
        int u = -1, MIN = INF;
        for (int i = 0; i < n; i++)
        {
            if (!vis[i]&&d[i] < MIN)MIN = d[i], u = i;
        }
        if (u == -1)return;
        vis[u] = true;
        for (int i = 0; i < n; i++)
        {
            if (!vis[i] && G[u][i])
            {
                if (d[u] + G[u][i] < d[i])
                {
                    d[i] = d[u] + G[u][i];
                    pre[i] = u;
                    c[i] = c[u] + cost[u][i];
                }
                else if (d[u] + G[u][i] == d[i] && c[u] + cost[u][i] < c[i])
                {
                    pre[i] = u;
                    c[i] = c[u] + cost[u][i];
                }
            }
        }
    }
}
void DFS(int v)
{
    if (v == s)
    {
        path.push_back(v);
        return;
    }
    DFS(pre[v]);
    path.push_back(v);
}
int main()
{
    scanf("%d%d%d%d", &n, &m, &s, &dd);
    for (int i = 0; i < m; i++)
    {
        int c1, c2, dis, cc;
        scanf("%d%d%d%d", &c1, &c2, &dis, &cc);
        G[c1][c2] = G[c2][c1] = dis;
        cost[c1][c2] = cost[c2][c1] = cc;
    }
    Dijkstra(s);
    DFS(dd);
    for (int i = 0; i < path.size(); i++)
    {
        printf("%d ", path[i]);
    }
    printf("%d %d", d[dd], c[dd]);
    return 0;
}

Dijkstra+DFS模板

#include
#include
using namespace std;
const int maxv = 510;
const int INF = 1e9 + 10;
int G[maxv][maxv], d[maxv], C[maxv][maxv];
int n, m, s, dd, mincost = INF;
bool vis[maxv];
vectorpre[maxv];
vectortemp, path;
void Dijkstra(int s)
{
    fill(d, d + maxv, INF);
    d[s] = 0;
    while (1)
    {
        int u = -1, MIN = INF;
        for (int i = 0; i < n; i++)
        {
            if (!vis[i]&&d[i] < MIN)MIN = d[i], u = i;
        }
        if (u == -1)return;
        vis[u] = true;
        for (int i = 0; i < n; i++)
        {
            if (!vis[i] && G[u][i])
            {
                if (d[u] + G[u][i] < d[i])
                {
                    d[i] = d[u] + G[u][i];
                    pre[i].clear();
                    pre[i].push_back(u);
                }
                else if (d[u] + G[u][i] == d[i])
                {
                    pre[i].push_back(u);
                }
            }
        }
    }
}
void DFS(int v)
{
    if (v == s)
    {
        temp.push_back(s);
        int tempcost = 0;
        for (int i = temp.size() - 1; i > 0; i--)
        {
            int v = temp[i];
            int u = temp[i - 1];
            tempcost += C[u][v];
        }
        if (tempcost < mincost)
        {
            mincost = tempcost;
            path = temp;
        }
        temp.pop_back();
        return;
    }
    temp.push_back(v);
    for (int i = 0; i < pre[v].size(); i++)
        DFS(pre[v][i]);
    temp.pop_back();
}
int main()
{
    scanf("%d%d%d%d", &n, &m, &s, &dd);
    for (int i = 0; i < m; i++)
    {
        int c1, c2, dis, cost;
        scanf("%d%d%d%d", &c1, &c2, &dis, &cost);
        G[c1][c2] = G[c2][c1] = dis;
        C[c1][c2] = C[c2][c1] = cost;
    }
    Dijkstra(s);
    DFS(dd);
    for (int i = path.size() - 1; i >= 0; i--)printf("%d ", path[i]);
    printf("%d %d", d[dd], mincost);
    return 0;
}

你可能感兴趣的:(1030 Travel Plan (30)(30 分))