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, 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 Specification:
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
#include <stdio.h>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <set>
using namespace std;
//表示路径信息
struct Road
{
Road(int t, int l, int c, int n) : target_city(t), length(l), count(c), team_number(n) {}
int target_city; //目标点
int length; //路径长度
int count; //路径数量
int team_number; //该路径的搜救队伍数量
};
struct City
{
int team_number; //该城市的搜救队伍数量
vector<Road> roads; //该城市包含的路径
};
static int MAX = 1000000000; // 极大值
vector<City> cities;
vector<bool> visited;
int M, N, C1, C2;
void Dijkstra()
{
int visited_num{0};
//访问M次 即访问所有的点
while (visited_num < M)
{
auto min_road = Road(-1, 10000000, 1, 0);
//找到未访问的点中,距离起点最近的点
for (auto road : cities[C1].roads)
{
if (!visited[road.target_city] && road.length < min_road.length)
{
min_road = road;
}
}
// 访问该点
if (min_road.target_city != -1)
{
visited[min_road.target_city] = true;
visited_num++;
}
// 若有不连通点,则跳出循环
else
{
break;
}
for (auto road : cities[min_road.target_city].roads)
{
// 若路径不存在,或目标点已访问,则跳过
if (road.length == MAX || visited[road.target_city])
continue;
// 比较新产生的路径距离和之前已有距离的大小
auto distance = min_road.length + road.length;
if (distance < cities[C1].roads[road.target_city].length)
{
// 新的路径距离较小,则替代新的路径
auto &r = cities[C1].roads[road.target_city];
r.count = min_road.count;
r.length = distance;
r.team_number = min_road.team_number + cities[road.target_city].team_number;
}
else if (distance == cities[C1].roads[road.target_city].length)
{
//距离相等,则增加count值,并比较更新所能找到的营救队伍数量
auto &r = cities[C1].roads[road.target_city];
r.count += min_road.count;
if (r.team_number < min_road.team_number + cities[road.target_city].team_number)
{
r.team_number = min_road.team_number + cities[road.target_city].team_number;
}
}
}
}
}
int main()
{
std::cin >> M >> N >> C1 >> C2;
for (auto i = 0; i < M; i++)
{
City c;
std::cin >> c.team_number;
for (auto p = 0; p < M; p++)
{
c.roads.push_back(Road(p, MAX, 0, c.team_number));
}
cities.push_back(c);
visited.push_back(false);
}
for (auto i = 0; i < N; i++)
{
int source_id;
int target_city;
int length;
std::cin >> source_id >> target_city >> length;
// 主要是无向图,所以正反都要
cities[source_id].roads[target_city].length = length;
cities[source_id].roads[target_city].team_number += cities[target_city].team_number;
cities[target_city].roads[source_id].length = length;
cities[target_city].roads[source_id].team_number += cities[source_id].team_number;
}
//第二个测试点的关键,要把自己到自己的路径包含进去
cities[C1].roads[C1].length = 0;
cities[C1].roads[C1].count = 1;
Dijkstra();
std::cout << cities[C1].roads[C2].count << " " << cities[C1].roads[C2].team_number;
}