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
#include<iostream>
#include<iomanip>
using namespace std;

const int Max_int = 0x7fffffff;
int team_number[500];
int map[500][500];

struct city{
    int dist;
    int visited;
    int min_road_num;
    int team_num;
}city[500];

void Dijkstra(int city_start,int city_end,int city_num){

    for(int i = 0;i < city_num; i++){

        city[i].dist = Max_int;
        city[i].visited = 0;
        city[i].min_road_num = 0;
        city[i].team_num = 0;
    } //init

    city[city_start].dist = 0;
    city[city_start].min_road_num = 1;
    city[city_start].team_num = team_number[city_start];

    for(int i = 0; i < city_num; i ++){
        
        int Min = Max_int;
        int flag = -1;

        for(int i = 0; i < city_num; i++){

            if((city[i].visited == 0) && (city[i].dist < Min)){

                Min = city[i].dist;
                flag = i;
            }
            
        }

        if(flag == -1)
            break;

        city[flag].visited = 1;

        for(int j = 0; j < city_num; j++){

            if((city[j].visited == 0) && (map[flag][j] < Max_int)){

                if(city[j].dist > city[flag].dist + map[flag][j]){

                    city[j].dist = city[flag].dist + map[flag][j];
                    city[j].min_road_num = city[flag].min_road_num;
                    city[j].team_num = city[flag].team_num + team_number[j];

                }else if(city[j].dist == (city[flag].dist + map[flag][j])){

                    city[j].min_road_num += city[flag].min_road_num;
                    if(city[j].team_num < city[flag].team_num + team_number[j]){
                        city[j].team_num = city[flag].team_num + team_number[j];
                    }


                }
            }
        }


    }
    cout << city[city_end].min_road_num << " " << city[city_end].team_num;

}


int main(){
    int city_number;
    int road_number;
    int city_start;
    int city_end;


    cin >> city_number >> road_number >> city_start >> city_end;
    for(int i = 0; i < city_number; i++){

        cin >> team_number[i];
    }
    for(int i = 0; i < city_number; i++){
        for(int j = 0; j < city_number; j++){

            map[i][j] = Max_int;
        }
    }
    int m,n,l;
    for(int i = 0; i < road_number; i++){

        cin >> m >> n >> l;
        map[m][n] = l;
        map[n][m] = l;

    }

    //输出测试
    // for(int i = 0; i < city_number; i++){

    //     for(int j = 0; j < city_number; j++)
    //         cout << setiosflags(ios::left) << setw(10) << map[i][j] << " ";
    //     cout << endl;
    // }

    // for(int i = 0; i < city_number; i++){

    //     cout << team_number[i] << " ";
    // }

    Dijkstra(city_start ,city_end , city_number);

    return 0;
}
发布了11 篇原创文章 · 获赞 0 · 访问量 81

猜你喜欢

转载自blog.csdn.net/zbchenchanghao/article/details/103863876
今日推荐