1003 Emergency (25分)

baixiaofei567發表於2020-10-09

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(最好用鄰接矩陣,因為1000點以內都是鄰接矩陣比較方便),其實最短路徑基本dijkstra就行了
鄰接表的Node的結構體最好是定義一下Node的建構函式,這樣push_back的時候比較方便

Node(int _v, int _dis):v(_v),dis(_dis){}//要注意後面沒有分號

鄰接表的圖不用初始化,鄰接矩陣的圖要在main裡面初始化
這題和基礎的dij不同的是,它要你求最短路徑的條數和最短路徑上最大的點權和
所以我們重新定義了兩個陣列,一個是w陣列,用來存源點到某點i最大的點權和,一個是num陣列,用來存源點到某點i的最短路徑條數
在拿出一個點優化路徑時,多了一個選項,本來只有小於原本d[v]然後優化,並使num[v] = num[u],因為到u的路徑就是num[u]條,u到v的路徑只有1條,所以相等;使w[v] += weight[v]
在相等的情況下,不用優化d[v],如果w[v] < w[u] + weight[v],那麼優化一下w[v]。然後不管點權和大於還是小於,路徑條數一定要加上,num[v] += num[u]即可,因為num[v]現在還沒有被源點訪問過,所以加上num[u]會給他一個初始值。
最後輸出num[ed]和w[ed]即可

#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
//cstring是memset的,algorithm是fill的
using namespace std;
const int MAXV = 510;
const int INF = 1000000000;

struct Node{
    int v, dis;
    Node(int _v, int _dis):v(_v),dis(_dis){}
};
vector<Node> Adj[MAXV];
int n, m, st, ed, d[MAXV], weight[MAXV];//d為從原點到i點的最短距離,weight為點權
int w[MAXV], num[MAXV];//w為從源點到i最大點權之和,num為從源點到i的最短路徑條數
bool vis[MAXV] = {false};

void Dijkstra(int s){
    fill(d, d + MAXV, INF);
    memset(num, 0, sizeof(num));
    memset(w, 0, sizeof(w));
    d[s] = 0;
    w[s] = weight[s];
    num[s] = 1;
    for(int i = 0; i < n; i++){
        int u = -1, MIN = INF;
        for(int j = 0; j < n; j++){
            if(vis[j] == false && d[j] < MIN){
                u = j;
                MIN = d[j];
            }
        }
        if(u == -1) return;
        vis[u] = true;
        for(int j = 0; j < Adj[u].size(); j++){
            int v = Adj[u][j].v;
            int dis = Adj[u][j].dis;
            if(vis[v] == false){
                if(dis + d[u] < d[v]){
                    d[v] = dis + d[u];
                    num[v] = num[u];
                    w[v] = w[u] + weight[v];
                }
                else if(dis + d[u] == d[v]){
                    if(w[u] + weight[v] > w[v]){
                        w[v] = w[u] + weight[v];
                    }
                    num[v] += num[u];
                }
            }
        }
        
    }
}

int main(){
    cin >> n >> m >> st >>ed; 
    for(int i = 0; i < n; i++){
        cin >> weight[i];
    }
    int a, b, dis;
    for(int i = 0; i < m; i++){
        cin >> a >> b >> dis;
        Adj[a].push_back(Node(b,dis));
        Adj[b].push_back(Node(a,dis));
    }
    Dijkstra(st);
    cout << num[ed] << " "<< w[ed];
    cout << endl;
    return 0;
}