链式向前星and spfa模板

#include"cstdio"
#include"queue"
#include"algorithm"
#include"cstring"
#define MAXN 0x7fffffff
using namespace std;




//cur 给每条边一个标号
//E[i].to表示第i条边指向那个点 ,E[i].next表示第i条边的下一条边
//head[from]表示以from 为结点的第一条边的序号 
struct Edge {
int to;
int w;
int next;
} E[1000000];
int head[100000];
int cur=0;
int dist[100000];
bool mark[100000];
void add(int from,int to,int w) {
E[cur].to=to;
E[cur].w=w;
E[cur].next=head[from];
head[from]=cur++;
}


void init() {
memset(E,-1,sizeof(E));
memset(head,-1,sizeof(head));
cur=0;
for(int i=0; i<100000; i++) {
dist[i]=MAXN;
}
memset(mark,false,sizeof(mark));
}


void spfa(int start) {
queue que;
mark[start]=true;
dist[start]=0;
que.push(start);
while(!que.empty()) {//u当前边的头,v当前边的尾 ,mark标记尾结点是否已经入队 
int u=que.front();
que.pop();
mark[u]=false;
for(int i=head[u]; ~i; i=E[i].next) {
int v=E[i].to;
if(dist[u]+E[i].w dist[v]=dist[u]+E[i].w;
if(!mark[v]) {
mark[v]=true;
que.push(v);
}
}
}
}
}

你可能感兴趣的:(链式向前星and spfa模板)