邻接表简易定义
//定义简易邻接表
struct edge {
int to,cost;
};
vector <edge> adjmap[maxn];
int dis[maxn];
int vis[maxn];
int path[maxn];
int inque[maxn];
SPFA不完整实现
bool SPFA(int s,int totalnode) {
queue<int> q;
dis[s] = 0;
q.push(s);
inque[s] = 1;
//统计入队次数
while(!q.empty()) {
int u = q.front();
q.pop();
vis[u] = 0;
//队头元素出队 消除标记
//遍历定点u邻接表
for(int v=0; v<adjmap[u].size(); v++) {
int to = adjmap[u][v].to;
if(dis[u]+adjmap[u][v].cost<dis[to]) {
dis[to] = dis[u]+adjmap[u][v].cost;//relax
if(!vis[to]) {
//定点to不在队列内
vis[to] = 1;//标记
inque[to]++;//统计次数
q.push(to);//入队
if(inque[to]>totalnode) {
//超过如对次数上限 说明有负数环
return false;
}
}
}
}
}
return true;
}