求单源最短路,使用邻接矩阵保存边,模板。
const int maxn = 1e4 + 5;
const int INF = 0x3f3f3f3f;
int mp[maxn][maxn];
int dis[maxn];
bool vis[maxn];
int times[maxn];
bool spfs(int rt){
queue<int> q;
for(int i=0;i<n;i++)
dis[i] = INF;
memset(vis,0,sizeof(vis));
memset(times,0,sizeof(times));
q.push(rt);
vis[rt] = 1;
times[rt] = 1;
int u;
while(!q.emtpy()){
u = q.front();
q.pop();
for(int i=0;i<n;i++){
if(dis[u] + dis[u][i] < dis[i]){
dis[i] = dis[u] + dis[u][i];
if(vis[i] == 0){
vis[i] = 1;
q.push(i);
times[i]++;
if(times[i] == n)
return 0;
}
}
}
}
return 1;
}