dijkstra
不能求带负权的最短路。
朴素版
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 510;
int g[N][N];
int dist[N];
bool st[N];
int n,m;
int dijkstra()
{
memset(dist,0x3f,sizeof dist);
dist[1]=0;
for(int i=0;i<n;i++)
{
int t=-1;
for(int j=1;j<=n;j++)
{
if(!st[j]&&(t==-1||dist[t]>dist[j]))
t=j;
}
st[t]=true;
for(int j=1;j<=n;j++)
dist[j]=min(dist[j],dist[t]+g[t][j]);
}
if(dist[n]==0x3f3f3f3f) return -1;
else return dist[n];
}
int main()
{
cin >> n>>m;
memset(g,0x3f,sizeof g);
for(int i=1;i<=m;i++)
{
int a,b,c;
cin >> a>>b>>c;
g[a][b]=min(g[a][b],c);
}
cout << dijkstra()<<endl;
return 0;
}
堆优化版
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 1e6+10;
int h[N],e[N],ne[N],w[N],idx;
int dist[N];
bool st[N];
int n,m;
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
int dijkstra()
{
priority_queue<PII,vector<PII>,greater<PII>> heap;
memset(dist,0x3f,sizeof dist);
dist[1]=0;
heap.push({0,1});
while(heap.size())
{
auto t=heap.top();
heap.pop();
int ver=t.y;
if(st[ver]) continue;
st[ver]=true;
for(int i=h[ver];~i;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[ver]+w[i])
{
dist[j]=dist[ver]+w[i];
heap.push({dist[j],j});
}
}
}
if(dist[n]==0x3f3f3f3f) return -1;
return dist[n];
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while (m -- )
{
int a,b,c;
scanf("%d%d%d", &a, &b, &c);
add(a,b,c);
}
printf("%d\n",dijkstra());
return 0;
}
spfa
不仅可以求带负权的连通块,还可以判断是否有负环
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 100010;
int h[N],ne[N],e[N],w[N],idx;
bool st[N];
int dist[N];
int n,m;
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
int spfa()
{
memset(dist,0x3f,sizeof dist);
dist[1]=0;
queue<int> q;
q.push(1);
st[1]=true;
while(q.size())
{
auto t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];~i;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t]+w[i])
{
dist[j]=dist[t]+w[i];
if(!st[j])
{
st[j]=true;
q.push(j);
}
}
}
}
return dist[n];
}
int main()
{
cin >> n>>m;
memset(h, -1, sizeof h);
while (m -- )
{
int a,b,c;
cin >> a>>b>>c;
add(a, b, c);
}
int t=spfa();
if(t==0x3f3f3f3f) puts("impossible");
else cout << t<<endl;
return 0;
}
判断负环
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2010,M=10010;
int h[N],w[M],e[M], ne[M], idx;
int dist[N],cnt[N];
bool st[N];
int n,m;
void add(int a, int b, int c) // 添加一条边a->b,边权为c
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
bool spfa()
{
queue<int> q;
for (int i = 1; i <= n; i ++ )
{
st[i]=true;
q.push(i);
}
while(q.size())
{
auto t=q.front();
q.pop();
st[t]=false;
for (int i = h[t];~i; i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t]+w[i])
{
dist[j]=dist[t]+w[i];
cnt[j]=cnt[t]+1;
if(cnt[j]>=n) return true;
if(!st[j])
{
st[j]=true;
q.push(j);
}
}
}
}
return false;
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while (m -- )
{
int a,b,c;
scanf("%d%d%d", &a,&b,&c);
add(a, b, c);
}
if(spfa()) puts("Yes");
else puts("No");
return 0;
}
floyd
多源最短路问题。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 210,INF=1e9;
int g[N][N];
int n,m,q;
void floyd()
{
for (int k = 1; k <=n; k ++ )
for (int i = 1; i <=n; i ++ )
for (int j = 1; j <= n; j ++ )
g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
}
int main()
{
cin >> n>>m>>q;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
{
if(i==j) g[i][j]=0;
else g[i][j]=INF;
}
while (m -- )
{
int a,b,w;
cin >> a>>b>>w;
g[a][b]=min(g[a][b],w);
}
floyd();
while(q--)
{
int x,y;
cin >> x>>y;
if(g[x][y]>INF/2) puts("impossible");
else cout << g[x][y]<<endl;
}
return 0;
}