PAT 1003 Emergency

  1. 1. Emergency
  2. 2. 思路

Emergency

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

1
2
3
4
5
6
7
8
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

1
2 4

题目大意:给定一张图,找出从起点到终点带权最短路径的个数,并输出其中最大的点权

思路

这道题大体上是单源最短路径的变体,dijkstra跑不了了.

dijkstra算法可以解决非负权最短路径的问题.算法的大体思路如下:

  • 对每个顶点,存储一个最短路径估计.起点为0,其余为无穷大.

  • 每次从未被访问的节点中找出最短路径估计最短的那个节点,访问它,对它的所有邻接顶点,进行松弛操作.设当前顶点为u,邻接节点为v,边的权值为w,松弛定义如下:

1
2
3
4
5
6
7
relax(u,v,w)
{
if(u.estimate+w<v.estimate)
{
v.estimate = u.estimate+w;
}
}

可见松弛是减小最短路径估计的过程.

  • 松弛完成后,继续从未被访问的顶点中找出最短路径估计最小的节点,松弛其邻接节点,直到所有节点都访问完毕.

伪码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const int INF 99999999
void dijkstra()
{
int estimate[v];
bool visited[v];
fill(estiamte,estimate+v,INF);
fill(visited,visited+v,false);
estimate[source]=0;
while(true)
{
int minn=INF;
int min=-1;
for(int i=0;i<v;i++)
{
if(!visited[i]&&estimate[i]<minn)
{
minn=estimate[i];
min=i;
}
}
if(min==-1) break;//finished
visited[min]=true;
for(int i=0;i<v;i++)
{
if(graph[min][i]!=-1)
{
if(estimate[min]+graph[min][i]<estimate[i])
{
estimate[i]=estimate[min]+graph[min][i];
}
}
}
}
}

题目要求求出最短路径的数量和其中最大点权的值.
estimate[min]+graph[min][i]<estimate[i]成立时,表示当前到i的路线是新的最短路径预期,所以到i的路线数量和到min的路线数量一致;
estimate[min]+graph[min][i]==estimate[i]时,表明除了经过min到i的路线,还有其他同样短的路径.这时到i的最短路径个数应该加上从起点到min的个数.

接下来考虑点权.当estimate[min]+graph[min][i]<estimate[i]成立时,表示到i的路径是新的最短路径预期,从起点到i的点权和应该为到min的点权和加上i的点权;
当当estimate[min]+graph[min][i]==estimate[i]时,点权和应该是i现有点权和,min点权和+i点权两者中较大的那个.

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
using namespace std;
int v;
int e;
int g[500][500] ;
int source;
int dest;
int v_data[500] ;
int dist[500];
int path[500];
int num[500];
int rescue[500];
int dijkstra()
{
bool visit[500]={false};
dist[source]=0;
num[source]=1;
rescue[source]=v_data[source];
while(true)
{
// find vertex with minimal estimate
int min_index=-1;
int min=10000000;
for(int i=0;i<v;i++)
{
if(min>dist[i]&&visit[i]==false)
{
min = dist[i];
min_index = i;
}
}
if(min_index==-1)
{
break;
}
visit[min_index]=true;
for(int i=0;i<v;i++)
{
int weight = g[min_index][i];
if(weight!=-1)
{
if(visit[i]==false)
{
if(dist[min_index]+weight<dist[i])
{
dist[i]=dist[min_index]+weight;
path[i]=min_index;
num[i]=num[min_index];
rescue[i]=rescue[min_index]+v_data[i];
} else if(dist[min_index]+weight==dist[i])
{
num[i]+=num[min_index];
if(rescue[i]<rescue[min_index]+v_data[i])
{
rescue[i]=rescue[min_index]+v_data[i];
}
}
}
}
}
// for each adj vertex, relax
}
}
int main()
{
cin>>v>>e>>source>>dest;
fill(path,path+v,-1);
fill(dist,dist+v,1000000);
fill(num,num+v,0);
fill(rescue,rescue+v,0);
for(int i=0;i<v;i++)
{
for(int j=0;j<v;j++)
g[i][j]=-1;
}
for(int i=0;i<v;i++)
{
cin>>v_data[i];
}
int p1,p2,l;
for(int i=0;i<e;i++)
{
cin>>p1>>p2>>l;
g[p1][p2]=g[p2][p1]=l;
}
dijkstra();
cout<<num[dest]<<" "<<rescue[dest]<<endl;
}