1459 迷宫游戏

#include<iostream>
#define N 500+10
#define INF 0x0fffffff
using namespace std;

int Mintime, Maxscore;
int n, m, s, e;
int score[N];
int dis[N];
int ans[N];
int visited[N];
int map[N][N];

void init();
void DFS(int s, int e, int time, int sc);
void Dijkstra(int s);

int main()
{
    cin >> n >> m >> s >> e;
    init();
    for (int i = 0; i < n; i++)
        cin >> score[i];
    for (int i = 1; i <= m; i++)
    {
        int a, b, cost;
        cin >> a >> b >> cost;
        map[a][b] = map[b][a] = cost;
    }
    //DFS(s, e, 0, score[s]);
    Dijkstra(s);
    //cout << Mintime << ' ' << Maxscore << endl;
    if (s != e)
        ans[e] += score[s];
    cout << dis[e] << ' ' << ans[e] << endl;
    system("pause");
    return 0;
}
void init()
{
    Mintime = INF;
    Maxscore = 0;
    for (int i = 0; i < n; i++)
    {
        visited[i] = 0;
        ans[i] = 0;
        for (int j = 0; j < n; j++)
            map[i][j] = i == j ? 0 : INF;
    }
}
void DFS(int s, int e, int time, int sc)
{
    if (time > Mintime)
        return;
    if (s == e)
    {
        if (time < Mintime)
        {
            Mintime = time;
            Maxscore = sc;
        }
        if (time == Mintime)
        {
            if (sc > Maxscore)
                Maxscore = sc;
        }
        return;
    }
    else
    {
        visited[s] = 1;
        for (int i = 0; i < n; i++)
        {
            if (visited[i] == 0 && map[s][i] < INF)
            {
                DFS(i, e, time + map[s][i], sc+score[i]);
            }
        }
        visited[s] = 0;
    }
}
void Dijkstra(int s)
{
    for (int i = 0; i < n; i++)
    {
        ans[i] = score[i];
        dis[i] = map[s][i];
    }
    visited[s] = 1;

    for (int i = 0; i < n; i++)
    {
        int Min = INF;
        int p=-1;
        for (int j = 0; j < n; j++)
        {
            if (visited[j] == 0&&dis[j]<Min)
            {
                Min = dis[j];
                p = j;
            }
        }
        if (p == -1)
            return;
        visited[p] = 1;
        for (int j = 0; j < n; j++)
        {
            if (visited[j] == 0 && (dis[p] + map[p][j] < dis[j]))
            {
                dis[j] = dis[p] + map[p][j];
                ans[j] = ans[p] + score[j];
            }
            else if (visited[j] == 0 && (Min + map[p][j] == dis[j]))
            {
                if (ans[j] < ans[p] + score[j])
                    ans[j] = ans[p] + score[j];
            }
        }
    }

}

猜你喜欢

转载自blog.csdn.net/zero_1778393206/article/details/80304113