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, 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

Experiential Summing-up

在模板上稍微改动——新增点权和最短路径条数,分别用 w[i] 和 num[i] 表示。当 dis[u] + g[u][v] < dis[v] 时,不仅仅要更新dis[v],还要更新 num[v] = num[u], w[v] = weight[v] + w[u];如果 dis[u] + e[u][v] == dis[v],还要更新 num[v] += num[u]。

Accepted Code

 1 #include<bits/stdc++.h>
 2 #define INF 0x3ffffff
 3 #define MAXV 510
 4 using namespace std;
 5 
 6 int weight[MAXV];//点权
 7 int w[MAXV];//记录最大点权之和
 8 int num[MAXV];//表示从起点到各点的最短路径的条数
 9 int dis[MAXV];
10 int g[MAXV][MAXV];
11 bool vis[MAXV] = {false};
12 int n, m, a, b, t;
13 
14 void dijkstra()
15 {
16     for(int i = 0; i < n; i ++)
17     {
18         int u = -1, MIN = INF;
19         for(int j = 0; j < n; j ++)
20         {
21             if(vis[j] == false && dis[j] < MIN)
22             {
23                 u = j;
24                 MIN = dis[j];
25             }
26         }
27         if(u == -1) return;
28         vis[u] = true;
29 
30         for(int v = 0; v < n; v ++)
31         {
32             if(vis[v] == false && g[u][v] != INF)
33             {
34                 if(dis[u] + g[u][v] < dis[v])
35                 {
36                     dis[v] = dis[u] + g[u][v];
37                     w[v] = w[u] + weight[v];
38                     num[v] = num[u];
39                 }
40                 else if(dis[u] + g[u][v] == dis[v])
41                 {
42                     num[v] += num[u];//最短路径点权与条数无关,必须写在外面
43                     if(w[v] < w[u] + weight[v]) w[v] = w[u] + weight[v];
44                 }
45             }
46         }
47     }
48 }
49 
50 int main()
51 {
52     cin >> n >> m >> a >> b;
53     //初始化
54     fill(dis, dis + MAXV, INF);
55     fill(w, w + MAXV, 0);//除起点外,所有点的点权之和为0
56     fill(g[0], g[0] + MAXV * MAXV, INF);
57     dis[a] = 0;
58     num[a] = 1;
59     for(int i = 0; i < n; i ++)
60     {//输入点权
61         cin >> t;
62         weight[i] = t;
63     }
64     w[a] = weight[a];//起点的点权之和为本身的点券
65     for(int i = 0; i < m; i ++)
66     {//无向图
67         int u, v, w;
68         cin >> u >> v >> w;
69         g[u][v] = w;
70         g[v][u] = w;
71     }
72     dijkstra();
73     cout << num[b] << " " << w[b];
74     return 0;
75 }

 

posted @ 2023-02-22 10:31  爱吃虾滑  阅读(11)  评论(0编辑  收藏  举报