目录
1.只能使用bellman-ford算法的情况:
给出最多走的边数
例题:原题链接(853. 有边数限制的最短路 - AcWing题库)
ac代码:
#include <iostream>
#include <cstring>
#include <vector>
#include <utility>
#include <queue>
using namespace std;
const int V = 505;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> PII;
vector<PII> rod[V];
int dis[V];//距离
int last[V];
int n, m, mn;
void bf(int s)
{
int tmp=0;
memset(dis, 0x3f, sizeof(dis));
dis[s] = 0;
for (int i = 1; i <= mn; i++) {//走V-1次到达终点(V-1可是一个其他变量)
memcpy(last, dis, sizeof dis);这句话用于防止串联,即在遍历时使用了本次遍历更新出来的值
//后面两个循环遍历了所有的边
for (int j = 1; j <= n; j++) {//遍历所有点
for (int k = 0; k < rod[j].size(); k++) {//遍历所选点j,与其他点之间距离
if (dis[rod[j][k].first] > last[j] + rod[j][k].second) {
dis[rod[j][k].first] = rod[j][k].second + last[j];//松弛过程
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m >> mn;
for (int i = 1; i <= m; i++) {
int a, b, z;
cin >> a >> b >> z;
rod[a].push_back({ b,z });
}
bf(1);
if (dis[n] > (INF / 2)) cout << "impossible" << endl;
else cout << dis[n] << endl;
}
2.spfa优化
优化后时间也可能卡至与bellman-ford用时相同甚至更多
例题:原题链接(851. spfa求最短路 - AcWing题库)
ac代码:
#include <iostream>
#include <cstring>
#include <vector>
#include <utility>
#include <queue>
using namespace std;
const int V = 1e5 + 5;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> PII;
vector<PII> rod[V];
int dis[V];//距离
bool vis[V];//标记
int n, m;
void bf(int s)
{
memset(dis, 0x3f, sizeof(dis));
dis[s] = 0;
queue<int>que;
que.push(s);//起点入队
vis[s] = 1;//进行标记
while (que.size()) {
int tmp = que.front();
que.pop();
vis[tmp] = 0;//去标记,在相邻点松弛后该点可能被更新,即可再次松弛
for (int i = 0; i < rod[tmp].size(); i++) {//遍历相邻点
if (dis[rod[tmp][i].first] > rod[tmp][i].second + dis[tmp]) {
dis[rod[tmp][i].first] = rod[tmp][i].second + dis[tmp];//松弛
if (!vis[rod[tmp][i].first]) {
que.push(rod[tmp][i].first);//可松弛点经过松弛后,入队
vis[rod[tmp][i].first] = 1;//进行标记
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, z;
cin >> a >> b >> z;
rod[a].push_back({ b,z });
}
bf(1);
if (dis[n] > (INF / 2)) cout << "impossible" << endl;
else cout << dis[n] << endl;
}
非vector实现
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int w[N], v[N], h[N], ne[N], cnt, dis[N];
bool vis[N];
int n, m;
void add(int a, int b, int z)
{
v[++cnt] = b, w[cnt] = z, ne[cnt] = h[a], h[a] = cnt;
}
void spfa(int s)
{
memset(dis, 0x3f, sizeof(dis));
dis[s] = 0;
queue<int>que;
que.push(s);
vis[s] = 1;
while (que.size()) {
int tmp = que.front();
que.pop();
vis[tmp] = 0;
for (int i = h[tmp]; i; i = ne[i]) {
if (dis[v[i]] > dis[tmp] + w[i]) {
dis[v[i]] = dis[tmp] + w[i];
if (!vis[v[i]]) {
que.push(v[i]);
vis[v[i]] = 1;
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, z;
cin >> a >> b >> z;
add(a, b, z);
}
spfa(1);
if (dis[n] > (INF / 2)) cout << "impossible" << endl;
else cout << dis[n] << endl;
}
3.spfa求是否有负环
例题:
原题链接:(852. spfa判断负环 - AcWing题库)
ac代码:
#include <iostream>
#include <cstring>
#include <vector>
#include <utility>
#include <queue>
using namespace std;
const int V = 1e5 + 5;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> PII;
vector<PII> rod[V];
int dis[V];//距离
int cnt[V];
bool vis[V];//标记
int n, m;
bool bf()
{//不进行dis初始化,因为不判断路径
queue<int>que;
for (int i = 1; i <= n; i++) {//应遍历所有点保证遍历所有边
vis[i] = 1;
que.push(i);
}
while (que.size()) {
int tmp = que.front();
que.pop();
vis[tmp] = 0;//去标记,在相邻点松弛后该点可能被更新,即可再次松弛
for (int i = 0; i < rod[tmp].size(); i++) {//遍历相邻点
if (dis[rod[tmp][i].first] > rod[tmp][i].second + dis[tmp]) {
dis[rod[tmp][i].first] = rod[tmp][i].second + dis[tmp];//松弛
cnt[rod[tmp][i].first] = cnt[tmp] + 1;//走的次数
if (cnt[rod[tmp][i].first] >= n) return 1;//1到x走的次数,无环应为n-1,多了说明有
if (!vis[rod[tmp][i].first]) {
que.push(rod[tmp][i].first);//可松弛点经过松弛后,入队
vis[rod[tmp][i].first] = 1;//进行标记
}
}
}
}
return 0;
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, z;
cin >> a >> b >> z;
rod[a].push_back({ b,z });
}
if (bf()) cout << "Yes" << endl;
else cout << "No" << endl;
}