Bellman-Ford&&spfa(判负环)
题目链接
题意:有n个农场,m条双向路径u,v,t表示从u农场到v农场要花t时间,w个虫洞u,v,t,表示从u穿越到v时间倒流t。
问从任意一点出发,再回到出发点,能否在出发前时间到达出发点(时间倒流)。
解法:Bellman-Ford算法(O(VE))
算法核心:对所有边进行V-1次松弛操作,每一次松弛操作最少确定一点到源点的最短路径,所以最多v-1次可求出所有点到源点的最短路径。最少一次就可以确定所有点的最短路径,即一条链依次更新。
判负环:如果存在负权环,则不存在最短路径。可以一直进行松弛操作。所以第V次对所有边进行松弛时,可以判断是否存在负环。
学习博客
//#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#include <stdlib.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 100
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j) for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
//ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
const int N = 1e4+9;
const int maxn = 5e2+9;
const double esp = 1e-6;
int dis[maxn] , ans;
int n , m , q ;
struct node{
int u , v , w;
}edge[N];
bool Bellman_Ford(int u){
fill(dis , dis+maxn , INF);
dis[u] = 0 ;
rep(i , 1 , n-1){
int flag = 1 ;
rep(j , 1 , ans){
if(dis[edge[j].u] + edge[j].w < dis[edge[j].v]){
dis[edge[j].v] = dis[edge[j].u] + edge[j].w;
flag = 0 ;
}
}
if(flag) break;//表明所有最短路径已经确定没有可松弛操作
}
rep(i , 1 , ans){
if(dis[edge[i].u] + edge[i].w < dis[edge[i].v]){
return false;
}
}
return true;
}
void init(){
ans = 0 ;
}
void solve(){
init();
scanf("%lld%lld%lld" , &n , &m , &q);
rep(i , 1 , m){
int u , v , w ;
scanf("%lld%lld%lld" , &u , &v , &w);
edge[++ans].u = u , edge[ans].v = v ;
edge[ans].w = w ;
edge[++ans].u = v , edge[ans].v = u ;
edge[ans].w = w ;
}
rep(i , 1 , q){
int u , v , w ;
scanf("%lld%lld%lld" , &u , &v , &w);
edge[++ans].u = u , edge[ans].v = v ;
edge[ans].w = -w ;
}
if(Bellman_Ford(1)){
cout << "NO" << endl;
}else{
cout << "YES" << endl;
}
}
signed main()
{
//ios::sync_with_stdio(false);
int t ;
scanf("%lld" , &t);
while(t--)
solve();
}
spfa算法是对Bellman-Ford的算法的队列优化。(O(kE))k表示节点平均入队列次数,一般k<=2
spfa算法核心:用队列来保存待优化的节点,优化时每次取出队首结点u,并且用结点u当前的最短路径估计值对离开结点u所指向的结点v进行松弛操作,
即判断是否有dis[v]>dis[u]+w(w是连接u与v的边的长度),若有,则更新dis[v]。如果结点v的最短路径估计值有所调整,且结点v不在当前的队列中,
就将结点v放入队尾。这样不断从队列中取出结点来进行松弛操作,直至队列空为止
判断负环:根据bellman算法判断负环条件,可知spfa判断负环条件为,一个节点进入队列超过V次,即存在负环。
//#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#include <stdlib.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 100
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j) for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
//ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
const int N = 1e4+9;
const int maxn = 5e2+9;
const double esp = 1e-6;
int head[maxn] , tol , dis[maxn] , vis[maxn] , ans[maxn];
int n , m , q ;
struct node{
int v , w , next;
}g[N];
void add(int u , int v , int w){
g[++tol] = {v , w , head[u]};
head[u] = tol;
}
bool spfa(int u){
ME(vis , 0);
fill(dis , dis+maxn , INF);
dis[u] = 0 , vis[u] = 1;ans[u]++;
queue<int>q;
q.push(u);
while(!q.empty()){
int a = q.front();q.pop();
vis[a] = 0 ;
for(int i = head[a] ; i ; i = g[i].next){
int v = g[i].v;
int w = g[i].w;
if(dis[a] + w < dis[v]){
dis[v] = dis[a] + w ;
if(!vis[v]){
q.push(v);
vis[v] = 1 ;
ans[v]++;
if(ans[v] > n){
return false;
}
}
}
}
}
return true;
}
void init(){
tol = 0 ;
ME(head , 0);
ME(ans , 0);
}
void solve(){
init();
int u , v , w;
scanf("%lld%lld%lld" , &n , &m , &q);
rep(i , 1 , m){
scanf("%lld%lld%lld" , &u , &v, &w);
add(u , v , w);
add(v , u , w);
}
rep(i , 1 , q){
int u , v , w ;
scanf("%lld%lld%lld" , &u , &v , &w);
add(u , v , -w);
}
if(spfa(1)){
cout << "NO" << endl;
}else{
cout << "YES" << endl;
}
}
signed main()
{
//ios::sync_with_stdio(false);
int t ;
scanf("%lld" , &t);
while(t--)
solve();
}