PAT Advanced 1003 Emergency(最短路)

題目描述

題目地址

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

基本思路

開始以爲是簡單的最短路模板題,結果發現還是有點意思的,在簡單的最短路的基礎上更進一步,基礎圖論起手就是 Dijkstra。最短路基礎算法 原地TP

和基礎最短路不同的是,這裏我們需要計算的是最短路的條數,以及這些最短路中節點權重和最大的值。可以作爲其他類似題目的解題思路。

在基礎的 Dijkstra 算法中我們每次更新對應點最短路當到此點距離小於到其相鄰點距離對應邊權和,這樣就保證每次都是“最短”了,顯然最短路不只一條,那麼何時可以判斷有重複最短路。

在這裏插入圖片描述

簡單來說 d[i]d[i] 表示起始點到i點的最短路大小,到C點的最短路更新在 d[C]<d[B]+costd[C] < d[B] + cost 的時候。我們使用 num[i]num[i] 表示到i點最短路條數,此時到C的最短路只能是從點B過來,所以此時C的最短路數量就是B的最短路數量。 點C最短路條數增長僅當 d[B]+cost==d[C]d[B] + cost == d[C] 時,這時候說明從BCC原來的最短路是一樣的,所以我們需要把到B的最短路數量加給我們的C,得到新的最短路條數

代碼表示如下:

// 更新C點的最短路
// B爲C的所有鄰點
for(B in C_Neighbor) {
	if(d[B]+cost < d[C]) {
  		// 此時更新最短路 通過B到C 所以C的最短路數量等於到B的最短路數量
    	d[C] = d[B]+cost;
        num[C] = num[B];
    }
	else if(d[B]+cost == d[C]) {
    	// 從B方向來的路也是最短路 所以加上
    	num[C] += num[B];
    }
}

對於最短路中的最大節點權值和則比較簡單,當存在更短的路徑時,更新對應點的權值和,當存在相同長度最短路時,比較兩條路的節點權值和,取較大的,最後的邏輯代碼如下:

// 更新C點的最短路
// B爲C的所有鄰點
// sum[i] 表示i點的最短路徑節點權值和最大值
for(B in C_Neighbor) {
	if(d[B]+cost < d[C]) {
  		// 此時更新最短路 通過B到C 所以C的最短路數量等於到B的最短路數量
    	d[C] = d[B]+cost;
        num[C] = num[B];
        sum[C] = sum[B] + weight[C]; // weight[C] C節點的權值
    }
	else if(d[B]+cost == d[C]) {
    	// 從B方向來的路也是最短路 所以加上
    	num[C] += num[B];
        if(sum[C] < sum[B] + weight[C]) {
            sum[C] = sum[B] + weight[C];
        }
    }
}       

AC 代碼

注意其中對應數組變量的初始化

#include <cstdio>
#include <utility>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>

#define LOCAL
#define INF 0x3f3f3f3f
const int maxn = 501;

using namespace std;

typedef pair<int,int> P;    // P.first 爲 起始點到 P.second 的最短路
struct edge {int to, cost;};
vector<edge> G[maxn];   // 存儲圖的鄰接矩陣
int d[maxn];    // 對應點的最短路
int team_num[maxn]; // 每個城市對應點的救援隊數量
int road_sum[maxn]; // 最短路數量
int team_sum[maxn]; // 節點最大權值和


int main() {
#ifdef LOCAL
    freopen("./in.txt", "r", stdin);
#endif
    int n, m, s, t; scanf("%d %d %d %d", &n, &m, &s, &t);
    fill(d, d+maxn, INF); d[s] = 0;
    fill(road_sum, road_sum+maxn, 0);   // 開始全部爲 0
    road_sum[s] = 1;    // 起點到自己有1條路
    for(int i = 0;i < n;i++) {
        scanf("%d", team_num+i);
        team_sum[i] = team_num[i];  // 都可以自己到自己,所以和team_num一樣
    }
    // 讀入圖數據
    int t_s, t_t, t_cost;
    for(int i = 0;i < m;i++) {
        scanf("%d %d %d", &t_s, &t_t, &t_cost);
        G[t_s].push_back(edge{t_t, t_cost});
        G[t_t].push_back(edge{t_s, t_cost});
    }
    priority_queue<P, vector<P>, greater<P> > que;
    que.push(make_pair(0, s));
    while (!que.empty()) {
        P t = que.top(); que.pop();
        if(d[t.second] < t.first) {
            continue;
        }
        for(int i = 0;i < G[t.second].size();i++) {
            edge e = G[t.second][i];
            if(d[e.to] > d[t.second] + e.cost) {
                team_sum[e.to] = team_sum[t.second] + team_num[e.to];
                road_sum[e.to] = road_sum[t.second];
                d[e.to] = d[t.second] + e.cost;
                que.push(make_pair(d[e.to], e.to));
            }
            else if(d[e.to] == d[t.second] + e.cost) {
                road_sum[e.to] += road_sum[t.second];
                if(team_sum[e.to] < team_sum[t.second] + team_num[e.to]) {
                    team_sum[e.to] = team_sum[t.second] + team_num[e.to];
                }
            }
        }
    }
    printf("%d %d", road_sum[t], team_sum[t]);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章