基础模板
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e6+10;
int n,m,s;
bool vis[N];
int dis[N];
vector<PII> v[N];
void spfa(int s)
{
queue<int> q;
for(int i=1;i<=n;i++)
{
dis[i] = 1e9;
}
vis[s] = 1;
dis[s] = 0;
q.push(s);
while(q.size())
{
int now = q.front();
q.pop();
vis[now] = 0;
for(auto t:v[now])
{
int spot = t.fi,w = t.se;
if(dis[spot]>dis[now]+w)
{
dis[spot] = dis[now]+w;
if(vis[spot]==0)
{
vis[spot]=1;
q.push(spot);
}
}
}
}
}
int main()
{
IOS;
cin>>n>>m;
while(m--)
{
int a,b,w;
cin>>a>>b>>w;
v[a].pb({b,w});
}
cin>>s;
spfa(s);
}
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e6+10;
int n,m,s;
vector< pair<int,int> > v[N];
int dis[N];
bool vis[N];
void spfa()
{
for(int i=1;i<=n;i++)
{
dis[i] = 1e9;
vis[i] = 0;
}
queue<int> q;
q.push(s);
dis[s] = 0;
vis[s] = 1;
while(q.size())
{
int now = q.front();
q.pop();
vis[now] = 0;
for(auto t:v[now])
{
int spot = t.fi,w =t.se;
if(dis[spot]>dis[now]+w)
{
dis[spot] = dis[now]+w;
if(vis[spot]==0)
{
vis[spot] = 1;
q.push(spot);
}
}
}
}
}
int main()
{
IOS;
cin>>n>>m>>s;
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
v[a].pb({b,c});
}
spfa();
for(int i=1;i<=n;i++)
{
if(dis[i]==1e9) cout<<(1ll<<31)-1<<' ';
else cout<<dis[i]<<" ";
}
}
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e6+10;
int n,m;
bool vis[N];
int dis[N];
int cnt[N];
vector<PII> v[N];
bool spfa()
{
queue<int> q;
for(int i=1;i<=n;i++)
{
dis[i] = 1e9;
vis[i] = 0;
cnt[i] = 0;
}
vis[1] = 1;
dis[1] = 0;
q.push(1);
while(q.size())
{
int now = q.front();
q.pop();
vis[now] = 0;
for(auto t:v[now])
{
int spot = t.fi,w = t.se;
if(dis[spot]>dis[now]+w)
{
cnt[spot] = cnt[now]+1;
if(cnt[spot]>=n) return true;
dis[spot] = dis[now]+w;
if(vis[spot]==0)
{
q.push(spot);
vis[spot]=1;
}
}
}
}
return false;
}
int main()
{
IOS;
int k;
cin>>k;
while(k--)
{
cin>>n>>m;
for(int i=1;i<=max(n,m);i++)
{
v[i].clear();
dis[i] = 1e9;
vis[i] = 0;
cnt[i] = 0;
}
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
if(c>=0) v[a].pb({b,c}),v[b].pb({a,c});
else v[a].pb({b,c});
}
if(spfa()) cout<<"YES"<<"\n";
else cout<<"NO"<<"\n";
}
}
那如果存在非连通的点呢?
bool spfa()
{
queue<int> q;
for(int i=1;i<=n;i++)
{
dis[i] = 1e9;
vis[i] = 0;
cnt[i] = 0;
}
vis[n+1] = 1;
dis[n+1] = 0;
q.push(n+1);
}
int main()
{
for(int i=1;i<=n;i++)
{
v[n+1].pb({i,0});
}
}
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e6+10;
int n,m,d,A,B;
int dis[N];
bool vis[N];
vector<PII> v[N];
map<pair<int,int>,int> mp;
void spfa()
{
for(int i=1;i<=n;i++)
{
dis[i] = 1e9;
vis[i] = 0;
}
queue<int> q;
q.push(A);
dis[A] = 0;
vis[A] = 1;
while(q.size())
{
int now = q.front();
q.pop();
vis[now] = 0;
for(auto t:v[now])
{
int spot = t.fi,w = 0;
if(mp[{now,spot}]==1) w = t.se;
if(dis[spot]>dis[now]+w)
{
dis[spot] = dis[now]+w;
if(vis[spot]==0)
{
vis[spot] = 1;
q.push(spot);
}
}
}
}
}
int main()
{
IOS;
cin>>n>>m;
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
v[a].pb({b,c});
v[b].pb({a,c});
}
cin>>d;
while(d--)
{
int a,b;
cin>>a>>b;
mp[{a,b}] = 1;
mp[{b,a}] = 1;
}
cin>>A>>B;
spfa();
cout<<dis[B];
}
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e6+10;
int n,m;
vector<PII> v1[N];
vector<PII> v2[N];
bool vis1[N];
int dis1[N];
bool vis2[N];
int dis2[N];
int sum=0;
void spfa1(int s)
{
queue<int> q;
vis1[s] = 1;
for(int i=1;i<=n;i++)
{
dis1[i] = 1e9;
vis1[i] = 0;
}
dis1[s] = 0;
q.push(s);
while(q.size())
{
int now = q.front();
q.pop();
vis1[now] = 0;
for(auto t:v1[now])
{
int spot = t.fi,w = t.se;
if(dis1[spot]>dis1[now]+w)
{
dis1[spot] = dis1[now]+w;
if(vis1[spot]==0)
{
vis1[spot] = 1;
q.push(spot);
}
}
}
}
}
void spfa2(int s)
{
queue<int> q;
vis2[s] = 1;
for(int i=1;i<=n;i++)
{
dis2[i] = 1e9;
vis2[i] = 0;
}
dis2[s] = 0;
q.push(s);
while(q.size())
{
int now = q.front();
q.pop();
vis2[now] = 0;
for(auto t:v2[now])
{
int spot = t.fi,w = t.se;
if(dis2[spot]>dis2[now]+w)
{
dis2[spot] = dis2[now]+w;
if(vis2[spot]==0)
{
vis2[spot] = 1;
q.push(spot);
}
}
}
}
}
int main()
{
IOS;
cin>>n>>m;
while(m--)
{
int a,b,l;
cin>>a>>b>>l;
v1[a].pb({b,l});
v2[b].pb({a,l});
}
spfa1(1);
spfa2(1);
for(int i=1;i<=n;i++)
{
sum+=dis1[i];
sum+=dis2[i];
}
cout<<sum;
}
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e6+10;
int n,m,T;
int cnt[N];
bool vis[N];
int dis[N];
vector<PII> v[N];
bool suc = 0;
void spfa(int s)
{
queue<int> q;
for(int i=1;i<=n;i++)
{
dis[i] = 1e9;
vis[i] = 0;
cnt[i] = 0;
}
vis[s] = 1;
dis[s] = 0;
q.push(s);
while(q.size())
{
int now = q.front();
q.pop();
vis[now] = 0;
for(auto t:v[now])
{
int spot = t.fi,w = t.se;
if(dis[spot]>dis[now]-w)
{
cnt[spot] = cnt[now]+1;
if(cnt[spot]>=n)
{
suc= 1;
return;
}
dis[spot] = dis[now]-w;
if(vis[spot]==0)
{
vis[spot]=1;
q.push(spot);
}
}
}
}
}
int main()
{
IOS;
cin>>n>>m;
while(m--)
{
int a,b,w;
cin>>a>>b>>w;
v[a].pb({b,w});
}
int minn=1e9;
spfa(1);
minn = min(minn,dis[n]);
spfa(n);
minn = min(minn,dis[1]);
if(suc) cout<<"Forever love";
else cout<<minn;
}