SPFA
使用SPFA,测试点4卡了很久,后来看了别人写的SPFA,发现区别在49-52行:是否把v加入队列中,我把这一段代码写在上一个判断括号中,也就是weight[v]更新,才将v加入队列;事实上不管weight[v]是否更新,v都需要加入队列(因为prev[v]和num[v]更新了)。
#include <deque>
#include <iostream>
#include <set>
#include <vector>
const int INF = 1000000000;
struct node {
int city;
int length;
node(int _city, int _length) : city(_city), length(_length) {}
};
int N, M, src, dst, c1, c2, L, u, v, len;
std::vector<int> num, w, dist, weight;
std::vector<std::vector<node>> adj;
std::deque<int> que;
std::vector<std::set<int>> prev;
std::vector<bool> inque;
void SPFA(int s) {
que.push_back(s);
inque[s] = true;
while (!que.empty()) {
u = que.front();
que.pop_front();
inque[u] = false;
for (int i = 0; i < adj[u].size(); ++i) {
v = adj[u][i].city;
len = adj[u][i].length;
if (dist[u] + len < dist[v]) {
prev[v].clear();
prev[v].insert(u);
num[v] = num[u];
dist[v] = dist[u] + len;
weight[v] = weight[u] + w[v];
if (!inque[v]) {
que.push_back(v);
inque[v] = true;
}
} else if (dist[u] + len == dist[v]) {
prev[v].insert(u);
num[v] = 0;
for (auto it = prev[v].begin(); it != prev[v].end(); ++it) {
num[v] += num[*it];
}
if (weight[u] + w[v] > weight[v]) {
weight[v] = weight[u] + w[v];
}
if (!inque[v]) {
que.push_back(v);
inque[v] = true;
}
}
}
}
}
int main() {
std::cin >> N >> M >> src >> dst;
num.resize(N, 0);
w.resize(N, 0);
dist.resize(N, INF);
weight.resize(N, 0);
adj.resize(N);
prev.resize(N);
inque.resize(N, false);
for (int i = 0; i < N; ++i) {
std::cin >> w[i];
}
for (int i = 0; i < M; ++i) {
std::cin >> c1 >> c2 >> L;
adj[c1].push_back(node(c2, L));
adj[c2].push_back(node(c1, L));
}
weight[src] = w[src];
dist[src] = 0;
num[src] = 1;
SPFA(src);
std::cout << num[dst] << " " << weight[dst];
return 0;
}
Bellman-Ford
#include <cstdio>
#include <set>
#include <vector>
const int MAXN = 500;
const int INF = 1000000000;
struct node {
int id;
int length;
node(int _id, int _length) : id(_id), length(_length) {}
};
int N, M, src, dst, c1, c2, L, v, len;
std::vector<std::vector<node>> adj;
std::vector<int> teamNbr, dist, totalNbr, num;
std::vector<std::set<int>> prev;
bool bellman(int s) {
for (int i = 0; i < N - 1; ++i) {
for (int j = 0; j < N; ++j) {
for (int k = 0; k < adj[j].size(); ++k) {
v = adj[j][k].id;
len = adj[j][k].length;
if (dist[j] + len < dist[v]) {
prev[v].clear();
prev[v].insert(j);
dist[v] = dist[j] + len;
totalNbr[v] = totalNbr[j] + teamNbr[v];
num[v] = num[j];
} else if (dist[j] + len == dist[v]) {
prev[v].insert(j);
num[v] = 0;
for (auto it = prev[v].begin(); it != prev[v].end(); ++it) {
num[v] += num[*it];
}
if (totalNbr[j] + teamNbr[v] > totalNbr[v]) {
totalNbr[v] = totalNbr[j] + teamNbr[v];
}
}
}
}
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < adj[i].size(); ++j) {
v = adj[i][j].id;
len = adj[i][j].length;
if (dist[i] + len < dist[v]) {
return false;
}
}
}
return true;
}
int main() {
scanf("%d %d %d %d", &N, &M, &src, &dst);
adj.resize(N);
teamNbr.resize(N);
dist.resize(N, INF);
totalNbr.resize(N, 0);
prev.resize(N);
num.resize(N, 0);
for (int i = 0; i < N; ++i) {
scanf("%d", &teamNbr[i]);
}
totalNbr[src] = teamNbr[src];
dist[src] = 0;
num[src] = 1;
for (int i = 0; i < M; ++i) {
scanf("%d %d %d", &c1, &c2, &L);
adj[c1].push_back(node(c2, L));
adj[c2].push_back(node(c1, L));
}
if (bellman(src)) {
printf("%d %d", num[dst], totalNbr[dst]);
}
return 0;
}
Dijkstra
#include <cstdio>
#include <iostream>
#include <vector>
const int MAXN = 500;
const int INF = 1000000000;
int N, M, src, dst, c1, c2, L;
std::vector<int> dist, weight, num, totalAmount, flag;
int roadLength[MAXN][MAXN];
void dijkstra(int s) {
dist[s] = 0;
totalAmount[s] = weight[s];
num[s] = 1;
int pivot, currMinDist, currMaxAmount;
for (int i = 0; i < N; ++i) {
pivot = -1;
currMinDist = INF;
currMaxAmount = 0;
for (int j = 0; j < N; ++j) {
if (flag[j] == -1 && dist[j] < currMinDist) {
pivot = j;
currMinDist = dist[j];
currMaxAmount = totalAmount[j];
} else if (flag[j] == -1 && dist[j] == currMinDist &&
totalAmount[j] > currMaxAmount) {
pivot = j;
currMaxAmount = totalAmount[j];
}
}
if (pivot == -1) {
return;
}
flag[pivot] = 1;
for (int j = 0; j < N; ++j) {
if (flag[j] == -1 && roadLength[pivot][j] < INF &&
dist[pivot] + roadLength[pivot][j] < dist[j]) {
num[j] = num[pivot];
dist[j] = dist[pivot] + roadLength[pivot][j];
totalAmount[j] = totalAmount[pivot] + weight[j];
} else if (flag[j] == -1 && roadLength[pivot][j] < INF &&
dist[pivot] + roadLength[pivot][j] == dist[j]) {
num[j] += num[pivot];
if (totalAmount[pivot] + weight[j] > totalAmount[j]) {
totalAmount[j] = totalAmount[pivot] + weight[j];
}
}
}
}
}
int main() {
scanf("%d %d %d %d", &N, &M, &src, &dst);
weight.resize(N);
dist.resize(N, INF);
num.resize(N, 0);
totalAmount.resize(N, 0);
flag.resize(N, -1);
for (int i = 0; i < N; ++i) {
scanf("%d", &weight[i]);
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
roadLength[i][j] = INF;
}
}
for (int i = 0; i < M; ++i) {
scanf("%d %d %d", &c1, &c2, &L);
roadLength[c1][c2] = L;
roadLength[c2][c1] = L;
}
dijkstra(src);
printf("%d %d", num[dst], totalAmount[dst]);
return 0;
}
题目如下:
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 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 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.