其他题目:【C语言】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 Nintegers, 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问题,附带点权的累计;
Dijkstra问题并不会单纯的出一道简单的最短路径生成,在PAT的考试中,一般存在多条最短路径,而最终结果也不会单纯的考你最短路径是什么,而是一共有三种考法:
1.最短路径的同时,有最佳的点权方案(本题);
2.最短路径的同时,有最佳的边权方案(如高速收费站);
3.同时输出多条最短路径;
Dijkstra思想就是单次选取点和点之间的最短路径,并从这条最短路径到达的点出发,更新所有点的最短路径,全部循环完后,即可得到所有最短路径。
因为点的数量少于1000 所以使用邻接矩阵写,大于1000则用邻接表。
代码:
#include <cstring>
#include <cstdio>//不用iostream scanf等在vs中需要单独设置使用
#include <algorithm>//因为需要使用fill,所以使用C++文件
using namespace std;
bool vis[1000];//记录该点能否到达//c语言在c99后才可以使用布尔型 类型名_Bool
int g[1000][1000];//邻接矩阵记录点和点间能不能通
int d[1000], h[1000];//记录当前到各点的最短距离
int hand[1000];//记录城市收集物品数量
int num[1000];//最短路径的条数
int citnum, roadnum, first, last;//城市数 路数量 出发点 到达点
const int INF = 1000000000;
void Dijkstra(int s)
{
int i, j, min, u;
fill(d, d + 1000, INF);
d[s] = 0;
h[s] = hand[s];
num[s] = 1;
for (i = 0; i < citnum; i++)//选取直接能到达的最近点
{
u = -1; min = INF;
for (j = 0; j < citnum; j++)
{
if (vis[j] == false && d[j] < min)
{
u = j;
min = d[j];
}
}
if (u == -1) return;
vis[u] = true;
for (j = 0; j < citnum; j++)//从这个点出发 更新所有最短路径 不同考法只需改变这里即可
{
if (vis[j] == false && g[u][j] != INF)//找出是单条或多条最短路
{
if (d[u] + g[u][j] < d[j])
{
d[j] = d[u] + g[u][j];
h[j] = h[u] + hand[j];
num[j] = num[u];
}
else if (d[u] + g[u][j] == d[j])
{
num[j] += num[u];
if (h[u] + hand[j] > h[j])
h[j] = h[u] + hand[j];
}
}
}
}
}
int main()
{
fill(g[0], g[0] + 1000000, INF);
int i, dist;
scanf("%d%d%d%d", &citnum, &roadnum, &first, &last);
for (i = 0; i < citnum; i++)
scanf("%d", &hand[i]);
for (i = 0; i < roadnum; i++)
{
int startcity, endcity;
scanf("%d%d%d", &startcity, &endcity, &dist);
g[startcity][endcity] = dist;
g[endcity][startcity] = dist;
}
Dijkstra(first);
printf("%d %d", num[last], h[last]);
return 0;
}