本人即将迎来第二次csp考试,准备提前刷刷题,刷题之前看到了之前做的PAT甲级的题目,又重新浏览了一遍,特来记录。
以下是题目:
这里给出本小菜鸡的小代码,如有大佬们有更好的解题思路可以一起讨论丫。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <iomanip>
using namespace std;
int n, m, c1, c2;
const int inf = 999999999;
int val[505]; //权值
int map[505][505]; //两点间距
int dis[505]; //到起点距离
int flag[505]; //是否被标记
int sum[505]; //最短路数和
int mval[505]; //救援人数和
void dijkstra() {
for (int i = 0; i < n; i++) {
int sign = c1, min_dis = inf;
for (int j = 0; j < n; j++) {
if (flag[j] == 0 && min_dis > dis[j]) {
min_dis = dis[j];
sign = j;
}
}
flag[sign] = 1;
for (int j = 0; j < n; j++) {
if (flag[j] == 0 && dis[j] > dis[sign] + map[sign][j]) {
dis[j] = dis[sign] + map[sign][j];
sum[j] = sum[sign];
mval[j] = mval[sign] + val[j];
}
else if (flag[j] == 0 && dis[j] == dis[sign] + map[sign][j]) {
sum[j] += sum[sign];
if (mval[j] < mval[sign] + val[j]) {
mval[j] = mval[sign] + val[j];
}
}
}
}
}
int main() {
cin >> n >> m >> c1 >> c2;
for (int i = 0; i < n; i++)
cin >> val[i];
for (int i = 0; i < 505; i++)
for (int j = 0; j < 505; j++)
if (i == j) map[i][j] = 0;
else map[i][j] = inf;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
map[a][b] = c;
map[b][a] = c;
}
for (int i = 0; i < 505; i++) {
dis[i] = inf;
flag[i] = 0;
sum[i] = 0;
mval[i] = 0;
}
//起点为c1
dis[c1] = 0;
flag[c1] = 1;
sum[c1] = 1;
mval[c1] = val[c1];
dijkstra();
cout << sum[c2] << " " << mval[c2];
return 0;
}