🧀
✨
-
原理:
-
时间复杂度
-
适用于:
最长路与最短路
-
代码模板:
//最短路
struct E{
int y,w;
};
vector<E>e[N];
void bellman_ford(int x){
for(int i=1;i<=n;i++)dis[i]=INT_MAX;
dis[x]=0;
for(int t=1;t<=n-1;t++){//循环次数
bool f=1;
for(int u=1;u<=n;u++){//添加中转边
for(auto i:e[u]){
if(dis[u]+i.w<dis[i.y]){//说明能更短
dis[i.y]=dis[u]+i.w;
f=0;
}
}
}
if(f)break;//优化:当图没有刷新时,就不用更新
}
}只需要改一下初始化和替换条件就是最长路了
//最长路
struct E{
int y,w;
};
vector<E>e[N];
void bellman_ford(int x){
for(int i=1;i<=n;i++)dis[i]=-INT_MAX;
dis[x]=0;
for(int t=1;t<=n-1;t++){//循环次数
bool f=1;
for(int u=1;u<=n;u++){//添加中转边
for(auto i:e[u]){
if(dis[u]+i.w>dis[i.y]){//说明能更长
dis[i.y]=dis[u]+i.w;
f=0;
}
}
}
if(f)break;//优化:当图没有刷新时,就不用更新
}
}✨
-
原理:
bellman-ford
-
时间复杂度
-
适用于:
最长路与最短路
-
代码模板:
//最短路
struct E{
int y,w;
};
vector<E>e[N];
queue<int>q;
void spfa(int x){
for(int i=1;i<=n;i++)dis[i]=INT_MAX;
dis[x]=0;
q.push(x);
v[x]=1;
while(q.size()){
auto u=q.front();
q.pop();
v[u]=0;
for(auto i:e[u]){
if(dis[u]+i.w<dis[i.y]){
dis[i.y]=dis[u]+i.w;
if(!v[i.y])q.push(i.y);
v[i.y]=1;
}
}
}
}//最长路
struct E{
int y,w;
};
vector<E>e[N];
queue<int>q;
void spfa(int x){
for(int i=1;i<=n;i++)dis[i]=INT_MAX;
dis[x]=0;
q.push(x);
v[x]=1;
while(q.size()){
auto u=q.front();
q.pop();
v[u]=0;
for(auto i:e[u]){
if(dis[u]+i.w>dis[i.y]){
dis[i.y]=dis[u]+i.w;
if(!v[i.y])q.push(i.y);
v[i].y=1;
}
}
}
}P3371 【模板】单源最短路径(弱化版)
做法No.2
直接套 bellman-ford 最短路模板
#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
using namespace std;
const int N=1e4+50;
int n,m,s,ans,dis[N],v[N];
struct E{
int y,w;
};
vector<E>e[N];
void bellman_ford(int x){
for(int i=1;i<=n;i++)dis[i]=INT_MAX;
dis[x]=0;
for(int t=1;t<=n-1;t++){//循环次数
bool f=1;
for(int u=1;u<=n;u++){//添加中转边
for(auto i:e[u]){
if(dis[u]+i.w<dis[i.y]){//说明能更短
dis[i.y]=dis[u]+i.w;
f=0;
}
}
}
if(f)break; //优化:当图没有刷新时,就不用更新了
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m>>s;
for(int i=1,x,y,w;i<=m;i++){
cin>>x>>y>>w;
e[x].push_back({y,w});
}
bellman_ford(s);
for(int i=1;i<=n;i++){
cout<<dis[i]<<" ";
}
return 0;
}P1807 最长路
直接套 bellman-ford 最长路模板,当 =_ ,说明无法到达n点,输出 -
#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
using namespace std;
const int N=1e4+50;
int n,m,s,ans,dis[N],v[N];
struct E{
int y,w;
};
vector<E>e[N];
void bellman_ford(int x){
for(int i=1;i<=n;i++)dis[i]=-INT_MAX;
dis[x]=0;
for(int t=1;t<=n-1;t++){
bool f=1;
for(int u=1;u<=n;u++){
for(auto i:e[u]){
if(dis[u]+i.w>dis[i.y]){
dis[i.y]=dis[u]+i.w;
f=0;
}
}
}
if(f)break;
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1,x,y,w;i<=m;i++){
cin>>x>>y>>w;
e[x].push_back({y,w});
}
bellman_ford(1);
if(dis[n]==-INT_MAX)cout<<-1;
else cout<<dis[n];
return 0;
}P10108 [GESP202312 六级] 闯关游戏
套 bellman-ford 最长路模板
注意:
- 要开 ;
- y超过 + 的边全都连到 + 上;
- 循环次数和初始化都要运行 + 次;
- 输出为 到 + 的最大总分;
#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
using namespace std;
const int N=1e4+50;
int n,m,s,ans,dis[N],a[N],b[N];
vector<int>e[N];
void bellman_ford(int x){
for(int i=1;i<=n+1;i++)dis[i]=-1e18;
dis[x]=0;
for(int t=1;t<=n+1;t++){
bool f=1;
for(int u=1;u<=n;u++){
for(auto i:e[u]){
if(dis[u]+b[u]>dis[i]){
dis[i]=dis[u]+b[u];
f=0;
}
}
}
if(f)break;
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
cin>>b[i];
}
//把每一个边都连起来
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int c=min(n+1,i+a[j]);
e[i].push_back(c);
}
}
bellman_ford(1);
cout<<dis[n+1];
return 0;
}P10110 [GESP202312 七级] 商品交易
这道题是用 ,找从 到 最小的步数
最终的答案便为: b 货物的钱数 - a 货物的钱数 + 最小的交易次数(也就是从 到 最小的步数)
#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
using namespace std;
const int N=1e5+50;
int n,m,s,ans,a,b,c[N],v[N];
struct E{
int y,w;
};
vector<int>e[N];
void bfs(int x){
queue<int>q;
q.push(x);
v[x]=1;
while(!q.empty()){
auto u=q.front();
q.pop();
for(auto i:e[u]){
if(!v[i]){
v[i]=v[u]+1;
q.push(i);
}
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m>>a>>b;
a++,b++;//由于题目中编号是从0开始所以整体往右移
for(int i=1;i<=n;i++)cin>>c[i];
for(int i=1,x,y;i<=m;i++){
cin>>x>>y;
x++,y++;//由于题目中编号是从0开始所以整体往右移
e[x].push_back(y);
}
bfs(a);
if(!v[b])cout<<"No solution";
else cout<<c[b]-c[a]+v[b]-1;
return 0;
}P3385 【模板】负环
不能用bellman-ford,因为bellman-ford只能求解是否存在负环(不能求是否能到):如果存在负环更新次数会大于 - 次;
所以只能用spfa,spfa判负环非常经典:如果一个点入队了 次以上说明一定出现了负环。
#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
using namespace std;
const int N=1e5+50;
int t,n,m,ans,dis[N],v[N],I[N];
struct E{
int y,w;
};
vector<E>e[N];
bool spfa(int x){
for(int i=1;i<=n;i++)dis[i]=INT_MAX,v[i]=I[i]=0;
dis[x]=0;
queue<int>q;
q.push(x);
v[x]=I[x]=1;
while(q.size()){
auto u=q.front();
q.pop();
v[u]=0;
for(auto i:e[u]){
if(dis[u]+i.w<dis[i.y]){
dis[i.y]=dis[u]+i.w;
if(!v[i.y]){
I[i.y]++;
if(I[i.y]>n)return 1;
v[i.y]=1;
q.push(i.y);
}
}
}
}
return 0;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
cin>>n>>m;
for(int i=1;i<=n;i++)e[i].clear();
for(int i=1,x,y,w;i<=m;i++){
cin>>x>>y>>w;
if(w<0)e[x].push_back({y,w});
else{
e[x].push_back({y,w});
e[y].push_back({x,w});
}
}
if(spfa(1)){
cout<<"YES\n";
}else{
cout<<"NO\n";
}
}
return 0;
}