SPFA
#include <deque>
#include <iostream>
#include <set>
#include <vector>
const int INF = 1000000000;
struct node {
int city;
int distance;
int cost;
node(int _city, int _distance, int _cost)
: city(_city), distance(_distance), cost(_cost) {}
};
int N, M, S, D, c1, c2, d, c, u, v, len, fee;
std::vector<int> dist, cost;
std::deque<int> deq;
std::vector<std::vector<node>> adj;
std::vector<bool> inque;
std::vector<int> prev;
void SPFA(int s) {
dist[s] = 0;
cost[s] = 0;
deq.push_back(s);
inque[s] = true;
while (!deq.empty()) {
u = deq.front();
deq.pop_front();
inque[u] = false;
for (int i = 0; i < adj[u].size(); ++i) {
v = adj[u][i].city;
len = adj[u][i].distance;
fee = adj[u][i].cost;
if (dist[u] + len < dist[v] ||
(dist[u] + len == dist[v] && cost[u] + fee < cost[v])) {
dist[v] = dist[u] + len;
cost[v] = cost[u] + fee;
prev[v] = u;
if (!inque[v]) {
deq.push_back(v);
inque[v] = true;
}
}
}
}
}
void printRoute(int d) {
if (prev[d] != -1) {
printRoute(prev[d]);
std::cout << " " << d;
} else {
std::cout << d;
}
}
int main() {
std::cin >> N >> M >> S >> D;
dist.resize(N, INF);
cost.resize(N, INF);
adj.resize(N);
inque.resize(N, false);
prev.resize(N, -1);
for (int i = 0; i < M; ++i) {
std::cin >> c1 >> c2 >> d >> c;
adj[c1].push_back(node(c2, d, c));
adj[c2].push_back(node(c1, d, c));
}
SPFA(S);
printRoute(D);
std::cout << " " << dist[D] << " " << cost[D];
return 0;
}
Dijkstra
#include <algorithm>
#include <iostream>
#include <vector>
const int MAXN = 500;
const int INF = 1000000000;
int N, M, src, dst, c1, c2, currMinDist, currMinCost, pivot;
std::vector<int> prev, totalDist, totalCost;
bool visited[MAXN];
int distance[MAXN][MAXN];
int cost[MAXN][MAXN];
void dijkstra(int s) {
totalDist[s] = 0;
totalCost[s] = 0;
for (int i = 0; i < N; ++i) {
currMinDist = INF;
currMinCost = INF;
pivot = -1;
for (int j = 0; j < N; ++j) {
if (!visited[j] &&
(totalDist[j] < currMinDist ||
(totalDist[j] == currMinDist && totalCost[j] < currMinCost))) {
pivot = j;
currMinCost = totalCost[j];
currMinDist = totalDist[j];
}
}
if (pivot == -1) {
return;
}
visited[pivot] = true;
for (int j = 0; j < N; ++j) {
if (!visited[j] &&
(totalDist[pivot] + distance[pivot][j] < totalDist[j] ||
(totalDist[pivot] + distance[pivot][j] == totalDist[j] &&
totalCost[pivot] + cost[pivot][j] < totalCost[j]))) {
totalDist[j] = totalDist[pivot] + distance[pivot][j];
totalCost[j] = totalCost[pivot] + cost[pivot][j];
prev[j] = pivot;
}
}
}
}
void printCity(int d) {
if (prev[d] != -1) {
printCity(prev[d]);
std::cout << " " << d;
} else {
std::cout << d;
}
}
int main() {
std::cin >> N >> M >> src >> dst;
prev.resize(N, -1);
totalDist.resize(N, INF);
totalCost.resize(N, INF);
std::fill(visited, visited + sizeof(visited), false);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
distance[i][j] = INF;
cost[i][j] = INF;
}
}
for (int i = 0; i < M; ++i) {
std::cin >> c1 >> c2;
std::cin >> distance[c1][c2] >> cost[c1][c2];
distance[c2][c1] = distance[c1][c2];
cost[c2][c1] = cost[c1][c2];
}
dijkstra(src);
printCity(dst);
std::cout << " " << totalDist[dst] << " " << totalCost[dst];
return 0;
}
题目如下:
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤500) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.