1003 Emergency

 

 

#include <iostream>
#include <vector>

using namespace std;

#define MaxInt 32767
#define MVNum 500

typedef int ver_type;
typedef int arc_type;
typedef struct {
    ver_type vers[MVNum];
    arc_type arcs[MVNum][MVNum];
    int ver_num, arc_num;
}am_gragh;

typedef struct {
    am_gragh g;
    int c1, c2;
}rescue_task;

void init_am_gragh(am_gragh &g, int ver_num, int arc_num)
{
    g.ver_num = ver_num;
    g.arc_num = arc_num;
    for (int i = 0; i < g.ver_num; ++i)
        for (int j = 0; j < g.ver_num; ++j) {
            g.arcs[i][j] = MaxInt;
            g.arcs[j][i] = MaxInt;
        }
}
void shortest_path_dij(am_gragh& g, int v0, vector<int>& shortesr_num, vector<int>& rescue_num)
{
    int n = g.ver_num;
    vector<bool> s(n);
    vector<int> d(n);
    for (int i = 0; i < n; ++i) {
        s[i] = false;
        d[i] = g.arcs[v0][i];
        shortesr_num[i] = 1;
        rescue_num[i] = g.vers[v0] + g.vers[i];
    }
    s[v0] = true;
    d[v0] = 0;
    rescue_num[v0] = g.vers[v0];
    for (int i = 1; i < n; ++i) {
        int min = MaxInt;
        int k = v0;
        for (int w = 0; w < n; ++w) {
            if ((!s[w]) && (min > d[w])) {
                k = w;
                min = d[w];
            }
        }
        s[k] = true;
        for (int w = 0; w < n; ++w) {
            if(!s[w]) {
                if (d[k] + g.arcs[k][w] < d[w]) {
                    d[w] = d[k] + g.arcs[k][w];
                    rescue_num[w] = rescue_num[k] + g.vers[w];
                    shortesr_num[w] = shortesr_num[k];
                }
                else if (d[k] + g.arcs[k][w] == d[w]) {
                    if((rescue_num[k] + g.vers[w]) > rescue_num[w])
                        rescue_num[w] = rescue_num[k] + g.vers[w];
                    shortesr_num[w] += shortesr_num[k];
                }
            }
        }
    }
}
int main(void)
{
    rescue_task rescue;
    int n,m;
    cin >> n >> m;
    cin >> rescue.c1 >> rescue.c2;
    init_am_gragh(rescue.g, n, m);
    for (int i = 0; i < n; ++i)
        cin >> rescue.g.vers[i];

    for (int k = 0; k < m; ++k) {
        int i, j;
        cin >> i >> j;
        cin >> rescue.g.arcs[i][j];
        rescue.g.arcs[j][i] = rescue.g.arcs[i][j];
    }

    vector<int> shortesr_num(n);
    vector<int> rescue_num(n);
    shortest_path_dij(rescue.g, rescue.c1, shortesr_num, rescue_num);
    cout << shortesr_num[rescue.c2] << ' ' << rescue_num[rescue.c2] << endl;
    return 0;
}

/*
 *输入:
 第一行:4个正整数(<500):
     第一个N:城市数量(序号从0开始)
     第二个M:道路数量
     第三个C1:你所在城市
     第四个C2:需要救援的城市
 第二行:N个数:表示第i个城市的救援队的数量
 剩余的M行:每一行3个数:第一第二个数为城市序号,第三个数为这个两个城市之间的距离

 输出:
 一行;两个数:C1到C2的最短距离,汇聚的救援队的数量
 */
/*
例:
输入
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
输出
2 4
*/

 

posted @ 2022-11-21 21:53  行路难,多歧路  阅读(34)  评论(0编辑  收藏  举报