【最短路】·SPFA算法实现

 感谢caioj scy老师通俗易懂的讲解,非常可惜小白菜要注销了,学oi太晚,还妹有来得及做多少里面的题,遗憾。

(题目是caioj1088)

 

#include<cstdio>
#include<cstring>
using namespace std;
struct bian//表示有向边的结构体,构建编目录
{
    int x,y,d,next;// x表示出发点,y表示终点,next表示和x相连的上一条边的编号
};
 
bian a[210000]; 

int len,n,last[11000];//a的个数是边的个数,last的个数是点的个数。
 
//last[i]表示最后一条和点i相连的边的编号。
int d[11000];//d[i]表示目前i和出发点的最短距离
int list[11000],head,tail;//list用来存排队准备更新其他人的点,head表示头,tail表示尾
void ins(int x,int y,int d)// ins函数的功能是建立一条边
{
    len++;//全局增加一条有向边,len一开始为0
    a[len].x=x; a[len].y=y;a[len].d=d;//边的赋值
    a[len].next=last[x]; last[x]=len;//边的联系  (顺序不可以变,是为了遍历所有与某一点的边) 
}

bool v[11000];// v[i]等于true表示点i在队列list中,等于false表示点i不再队列list中
int main()
{ //freopen("a.in","r",stdin); //freopen("a.out","w",stdout); int x,y,c,m,st,ed; scanf("%d%d",&n,&m); len=0; memset(last,0,sizeof(last));//注意构图之前一定要初始化,不然后果很严重! for(int i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&c);//题目给出的是无向边,而我们的边目录是有向边 ins(x,y,c);//建立正向边 ins(y,x,c);//建立反向边 } //1:初始化d,这样后面才有更新的必要 st=1;ed=n; for(int i=1;i<=n;i++) d[i]=999999999; d[st]=0;//出发点为0 //2:初始化v, 一开始所有点都没有 memset(v,false,sizeof(v)); v[st]=true;//点1作为出发点已经进入list //3:初始化队列list list[1]=st; head=1;tail=2;//队列的头是有人的,队列的尾tail指的位置是没人的 while(head!=tail)//只要头不等于尾,就表示还有人需要更新别人 { x=list[head];//取出准备好更新别人的人x for(int k=last[x]; k ; k=a[k].next )//重点理解!k首相等于和x相连的最后一条边的编号。那么倒数第二条和x相连的边的编号是多少呢?在a[最后一条].next可以找到 { y=a[k].y; if(d[y]>d[x]+a[k].d)//更新是一定要的 { d[y]=d[x]+a[k].d; if(v[y]==false)//如果被更新了,那么要考虑进入队列(考虑是否去更新别人) { v[y]=true; list[tail]=y; tail++; if(tail==n+1) tail=1;//如果tail超过队列的最后一个,则变为第一个 } } } //x已经完成更新别人的任务,就要退出队列list,那么需要做什么呢? list[head]=0; head++; if(head==n+1) head=1; //如果head超过队列的最后一个,则变为第一个 v[x]=false; } printf("%d\n",d[n]);//最后输出终点到出发点的距离 return 0; }

 

posted @ 2019-08-21 16:25  QUEKI嶺冬  阅读(237)  评论(0)    收藏  举报
/*! Color themes for Google Code Prettify | MIT License | github.com/jmblog/color-themes-for-google-code-prettify */ .pln{color:#4d4d4c}ol.linenums{margin-top:0;margin-bottom:0;color:#8e908c}li.L0,li.L1,li.L2,li.L3,li.L4,li.L5,li.L6,li.L7,li.L8,li.L9{padding-left:1em;background-color:#fff;list-style-type:decimal!important;}@media screen{.str{color:#718c00}.kwd{color:#8959a8}.com{color:#8e908c}.typ{color:#4271ae}.lit{color:#f5871f}.pun{color:#4d4d4c}.opn{color:#4d4d4c}.clo{color:#4d4d4c}.tag{color:#c82829}.atn{color:#f5871f}.atv{color:#3e999f}.dec{color:#f5871f}.var{color:#c82829}.fun{color:#4271ae}} /*下面是我设置背景色,字体大小和字体*/ .cnblogs-markdown code{ background:#fff!important; } .cnblogs_code,.cnblogs_code span,.cnblogs-markdown .hljs{ font-size:16px!important; } .syntaxhighlighter a, .syntaxhighlighter div, .syntaxhighlighter code, .syntaxhighlighter table, .syntaxhighlighter table td, .syntaxhighlighter table tr, .syntaxhighlighter table tbody, .syntaxhighlighter table thead, .syntaxhighlighter table caption, .syntaxhighlighter textarea { font-size: 16px!important; } .cnblogs_code, .cnblogs_code span, .cnblogs-markdown .hljs{ font-family:consolas, "Source Code Pro", monaco, monospace !important; } //以上是代码高亮 /* 文字特效 */