学完了看了遍老师的代码回来就会了,感觉不错
#include<iostream>
#include<cstring>using namespace std;
struct line
{
int to;
int w;
int next;
}edge[10000];
int g[100];
int n,h,t,ans;
int tot;
void add1(int a, int b, int c) //a是当前边的起点,b是当前边的终点,c是权值
{
tot++; //当前边数
edge[tot].to = b; //当前边的终点
edge[tot].w = c; //当前边的权值
edge[tot].next = g[a]; //当然边的下一条边为a点所指的边
g[a] = tot; //a点指向当前边
}
bool rejudge[100];
int distant[100];
int q[100];
void spfa(int a, int b)
{
memset(distant, 99999, sizeof(int) * 100);
for (int i = 1;i <= n;i++)
rejudge[i] = false;
h = 0;
t = 1;
q[t] = a;
distant[a] = 0;
while (h != t)
{
h = h%n + 1; //q[h]为当前要储存的点
int x = q[h]; //点出队
int temp = g[x]; //取出点所指向的第一条边
while (temp != 0)
{
if (distant[edge[temp].to] > distant[x] + edge[temp].w) //relax
{
distant[edge[temp].to] = distant[x] + edge[temp].w;
if (!rejudge[edge[temp].to]) //判重
{
t = t%n + 1; //入队
q[t] = edge[temp].to;
rejudge[edge[temp].to] = true;
}
}
temp = edge[temp].next; //遍历当前节点的下一条边
}
rejudge[x] = false; //确保入队
}
return distant[b]; //返回终点值
}