目录
- [1 介绍](#1 介绍)
- [2 训练](#2 训练)
1 介绍
本博客用来记录使用dijkstra算法或spfa算法求解最短路问题的题目。
2 训练
题目1 :1129热浪
C++代码如下,
cpp
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int N = 2510;
int n, m;
vector<vector<pair<int,int>>> g; //first表示next_node,second表示w
int dist[N];
bool st[N];
int snode, enode;
void dijkstra() {
memset(dist, 0x3f, sizeof dist);
dist[snode] = 0;
priority_queue<pair<int, int>, vector<pair<int,int>>, greater<pair<int,int>>> h;
h.push(make_pair(0, snode));
while (!h.empty()) {
//确定当前结点中,不在集合s且距离结点snode最近的结点。记作cnode
auto t = h.top();
h.pop();
int cdist = t.first, cnode = t.second;
if (st[cnode]) continue; //如果cnode已经被确定是最小路径上的结点了,则跳过
st[cnode] = true; //将它加入到集合中
for (auto [next_node, w] : g[cnode]) {
if (dist[next_node] > cdist + w) {
dist[next_node] = cdist + w;
h.push(make_pair(dist[next_node], next_node));
}
}
}
return;
}
int main() {
cin >> n >> m >> snode >> enode;
g.resize(n + 10);
for (int i = 1; i <= m; ++i) {
int a, b, c;
cin >> a >> b >> c;
g[a].emplace_back(b, c);
g[b].emplace_back(a, c);
}
//求snode到enode的最短距离
dijkstra();
cout << dist[enode] << endl;
return 0;
}
题目2 :1128信使
C++代码如下,
cpp
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int N = 110;
int n, m;
int d[N];
bool st[N];
vector<vector<pair<int,int>>> g;
void dijkstra() {
memset(d, 0x3f, sizeof d);
d[1] = 0;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> hp; //小根堆
hp.push(make_pair(0, 1)); //first表示距离,second表示结点
while (!hp.empty()) {
auto t = hp.top(); //找到未在集合中,距离最小的结点
hp.pop();
int a = t.second;
if (st[a]) continue; //已经用d[a]更新过了,将它放入集合中
st[a] = true;
for (auto [b, w] : g[a]) {
if (d[b] > d[a] +w) { //d[b]此时比较大,用一个更小值来更新它。
d[b] = d[a] + w;
hp.push(make_pair(d[b], b));
}
}
}
return;
}
int main() {
cin >> n >> m;
g.resize(n + 10);
for (int i = 0; i < m; ++i) {
int a, b, c;
cin >> a >> b >> c;
g[a].emplace_back(b, c);
g[b].emplace_back(a, c);
}
dijkstra();
int res = 0; //求最大值
for (int i = 1; i <= n; ++i) res = max(res, d[i]);
if (res == 0x3f3f3f3f) {
res = -1;
}
cout << res << endl;
return 0;
}
题目3 :1127香甜的黄油
C++代码如下,
cpp
#include <iostream>
#include <cstring>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
using namespace std;
const int N = 810;
int cow, n, m;
int cnt[N];
int d[N];
bool st[N];
vector<vector<pair<int,int>>> g;
void spfa(int start) {
//起点为start
memset(d, 0x3f, sizeof d);
memset(st, 0, sizeof st);
d[start] = 0;
queue<int> q;
q.push(start);
st[start] = true; //结点start在队列中
while (!q.empty()) {
int t = q.front();
q.pop();
st[t] = false; //结点t不在队列中了
for (auto [b, w] : g[t]) {
if (d[b] > d[t] + w) {
d[b] = d[t] + w;
if (!st[b]) {
q.push(b);
st[b] = true;
}
}
}
}
return;
}
int main() {
cin >> cow >> n >> m;
g.resize(n + 10);
for (int i = 1; i <= cow; ++i) {
int a;
cin >> a; //每头牛所在的牧场
cnt[a]++;
}
for (int i = 1; i <= m; ++i) {
int a, b, c;
cin >> a >> b >> c;
g[a].emplace_back(b, c);
g[b].emplace_back(a, c);
}
//spfa()算法 //o(m)时间复杂度,不会被超时
long long res = INT_MAX;
for (int i = 1; i <= n; ++i) {
//第i个牧场作为放糖点
spfa(i);
long long t = 0;
for (int j = 1; j <= n; ++j) t += cnt[j] * d[j];
res = min(res, t);
}
cout << res << endl;
return 0;
}
题目4 :1126最小花费
cpp
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int N = 2010;
int n, m;
int snode, enode;
double d[N]; //求最大距离,最大利率
bool st[N]; //是否使用它来更新过
vector<vector<pair<int,int>>> g;
void dijkstra() {
//d的初始化
for (int i = 1; i <= n; ++i) d[i] = 0.0; //初始成0.0
memset(st, 0, sizeof st);
d[snode] = 1.0;
priority_queue<pair<double,int>> hp; //大根堆
hp.push(make_pair(1.0, snode));
while (!hp.empty()) {
auto t = hp.top();
hp.pop();
int a = t.second;
if (st[a]) continue;
st[a] = true;
for (auto [b, w] : g[a]) {
if (d[b] < d[a] * 0.01 * (100 - w)) {
d[b] = d[a] * 0.01 * (100 - w);
hp.push(make_pair(d[b], b));
}
}
}
return;
}
int main() {
cin >> n >> m;
g.resize(n + 10);
for (int i = 1; i <= m; ++i) {
int a, b, c;
cin >> a >> b >> c;
g[a].emplace_back(b, c);
g[b].emplace_back(a, c);
}
cin >> snode >> enode;
dijkstra();
double res = 100.0 / d[enode];
printf("%.8f\n", res);
return 0;
}
题目5: