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

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, C​1​​ and C​2​​ - 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 c​1​​, c​2​​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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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

思路:本題目根據鄰接矩陣,利用dijkstra算法求最短路徑的題。

需要解決:

  1. dijkstra算法
  2. 多條最短路徑,選擇路徑中,節點和最大的一條
  3. 遍歷路徑

dijkstra算法圖文講解參考:https://blog.csdn.net/lbperfect123/article/details/84281300

多條最短路徑,要對訪問的前一個節點進行記錄,然後用DFS遍歷路徑,求得所有節點和最大的路徑。

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int n, m, c1, c2;
const int inf = 9999999;
int num[501];
vector<vector<int> > pre_city(501);//記錄每個節點的前一個節點,用DFS遍歷可得到路徑
vector<int> tmp_path;//訪問路徑
int e[501][501];//鄰接矩陣
int d[501];/*路徑最小值*/
int visited[501] = {0};
int num_path = 0, maxx = 0;

void input()
{
    cin >> n >> m >> c1 >> c2;
    for(int i = 0; i < n; i++)
    {
        cin >> num[i];
    }
    for(int i = 0; i < m; i++)
    {
        int x,y,w;
        cin >> x >> y >> w;
        e[x][y] = e[y][x] = w;
    }
    //初始化對角線爲0
    for(int i = 0; i < n; i++)
        e[i][i] = 0;
}

void DFS(int start, int ends)
{
    tmp_path.push_back(start);
    if(start == ends)
    {
        num_path++;
        int sum = 0;
        for(int i = tmp_path.size() - 1; i >= 0; i--)
        {
            sum += num[tmp_path[i]];
        }
        if(sum > maxx)
            maxx = sum;
        tmp_path.pop_back();
        return;
    }

    for(int i = 0; i < pre_city[start].size(); i++)
        DFS(pre_city[start][i], ends);
    tmp_path.pop_back();
}

void dijkstra_path()
{
    for(int i = 0; i < n; i++)
    {
        if(e[c1][i] != inf)
        {
            d[i] = e[c1][i];
            pre_city[i].push_back(c1);
        }
    }
    visited[c1] = 1;
    for(int i = 0; i < n; i++)
    {
        /*for1, 查找路徑最小值*/
        int minn = inf, k;
        for(int j = 0; j < n; j++)
        {
            if(!visited[j] && d[j] < minn)
            {
                minn = d[j];
                k = j;
            }
        }
        visited[k] = 1;
        /*for2, 中間點k到未找過的邊集v,是否更小*/
        for(int v = 0; v < n; v++)
        {
            if(!visited[v] && d[v] > d[k] + e[k][v])
            {
                d[v] = d[k] + e[k][v];
                pre_city[v].clear();
                pre_city[v].push_back(k);
            }
            else if(!visited[v] && d[v] == d[k] + e[k][v])
                pre_city[v].push_back(k);
        }
    }

    DFS(c2, c1);
}

void output()
{
    cout << num_path << " " << maxx;
}

int main()
{
    /*1003 Emergency (25 分)*/
    fill(num, num+501, 0);
    fill(e[0], e[0]+501*501, inf);
    fill(d, d+501, inf);
    input();

    dijkstra_path();

    output();
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章