1003. Emergency (25) 最短路问题

Dijkstra算法可以解决最短路问题,针对单源有向图,属于贪心算法的一种,要求权值非负。

算法实现步骤:
初始化:起点dist[s]=0
其他dist[i]=INF
节点集合V[i]=0
注:C++中初始化最大值,如果设置为FFFFFFFF其实有问题,就像在这个问题中,如果将最大值加一,则出现了负数,所以#define INF 0x3f3f3f3f

初始化:起点dist[s]=0
                其他dist[i]=INF
                节点集合V[i]=0
*注:C++中初始化最大值,如果设置为FFFFFFFF其实有问题,就像在这个问题中,如果将最大值加一,则出现了负数,所以#define INF 0x3f3f3f3f*

循环n次,找到不属于V的k且dist[k]最小的节点,V[k]=1
对于不属于V的i,更新:
dist[i] = min(dist[i], dist[k] + weight[k][i]);

那么我们来解决PAT上1003. Emergency (25)这个问题,题目如下:

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output
2 4

作为最短路的变形题目,我们要注意对于maxTeam和countShst的记录:
如果s-k-i路线最短,那么i的最短路个数就等于k的最短路个数,且i的maxTeam就等于k的maxTeam带上自己的人数。
如果说s-k-i长度与s-i相等,那么最短路个数等于k的最短路个数加上i的最短路个数,这个时候需要判断下走哪条路maxTeam比较大。

代码实现:

#include 
#include 
#include 
#include 
using namespace std;

#define INF 0x3F3F3F3F;

int N, M, S, E, C1, C2;
int teams[502] = { 0 };
int weight[502][502] = { 0 };
int dist[502];
bool mark[502] = { false };
int countShst[502];
int maxTeam[502] = { 0 };


void dijkstra();
int main(void)
{

    cin >> N;
    cin >> M >> S >> E;

    //memset(dist, INF, sizeof(dist));
    //memset(mark, 0, sizeof(mark));
    //memset(countShst, 1, sizeof(countShst));
    for (int i = 0; i < 502; i++){
        dist[i] = INF;
        mark[i] = 0;
        maxTeam[i] = 0;
        countShst[i] = 0;
        for (int j = 0; j < 502; j++)
            weight[i][j] = weight[j][i] = INF;
    }

    for (int i = 0; i < N; i++)
    {
        cin >> teams[i];
        maxTeam[i] = teams[i];
    }

    for (int i = 0; i < M; i++)
    {
        cin >> C1 >> C2;
        cin >> weight[C1][C2];  
        weight[C2][C1] = weight[C1][C2];
        //cout << "weight" << weight[C2][C1];
    }

    dist[S] = 0;
    dijkstra();
    //cout << "shortest: " << dist[E] << " MaxTeams: " << maxTeam[E]<<" "<
    cout << countShst[E] << " " << maxTeam[E] << endl;
    system("pause");
    return 0;
}

void dijkstra()
{
    int k = S, dmin;
    //mark[k] = true;
    //int flag = 0;
    countShst[S] = 1;

    for (int i = 0; i < N; i++)
    {
        //int k = -1;
        //dist[S] = INF;
        dmin = INF;
        for (int j = 0; j < N; j++)
        {
            if (!mark[j] && dist[j]//update
        mark[k] = true;
        for (int i = 0; i < N; i++)
        {
            if (!mark[i])
            {

                if (weight[k][i] != 0 && dist[i] > dist[k] + weight[k][i])
                {
                    maxTeam[i] = maxTeam[k] + teams[i];
                    countShst[i] = countShst[k];
                }
                else if (weight[k][i] != 0 && dist[i] == dist[k] + weight[k][i])
                {
                    countShst[i] += countShst[k];
                    if (maxTeam[i] < maxTeam[k] + teams[i])
                        maxTeam[i] = maxTeam[k] + teams[i];
                }

                dist[i] = min(dist[i], dist[k] + weight[k][i]);

            }

        }

    }
}   

你可能感兴趣的:(算法,dijkstra,c语言,PAT-最短路)