SPFA算法模版+邻接表实现

http://hi.baidu.com/jipyjyxfkubhtxq/item/e3ea343dd7767bd06d15e9b0

[Zz]SPFA算法模版+邻接表实现

SPFA即shotest path faster algorithm,由意思就可以看出该算法效率比较高。

其实SPFA就是bellman-ford算法的一个优化。

具体做法是用一个队列保存待松弛的点,然后对于每个出队的点依次遍历每个与他有边相邻的点(用邻接表效率较高),如果该点可以松弛并且队列中没有该点则将它加入队列中,如此迭代直到队列为空。

据说平均效率是O(E),可见对边稀疏的图用此算法效果是相当可观的。

 

若要判负环路,则记录一个点的入队次数,若超过边数,则有负权环。

 

SPFA算法模版+邻接表实现#include
SPFA算法模版+邻接表实现 #include
SPFA算法模版+邻接表实现 using namespace std;
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现 const long MAXN=10000;
SPFA算法模版+邻接表实现 const long lmax=0x7FFFFFFF;
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现 typedef struct   
SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现{
SPFA算法模版+邻接表实现         long v;
SPFA算法模版+邻接表实现         long next;
SPFA算法模版+邻接表实现         long cost;
SPFA算法模版+邻接表实现 }Edge;
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现 Edge e[MAXN];
SPFA算法模版+邻接表实现 long p[MAXN];
SPFA算法模版+邻接表实现 long Dis[MAXN];
SPFA算法模版+邻接表实现 bool vist[MAXN];
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现 queue q;
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现 long m,n;//点,边
SPFA算法模版+邻接表实现 void init()
SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现{
SPFA算法模版+邻接表实现         long i;
SPFA算法模版+邻接表实现         long eid=0;
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现         memset(vist,0,sizeof(vist));
SPFA算法模版+邻接表实现         memset(p,-1,sizeof(p));
SPFA算法模版+邻接表实现         fill(Dis,Dis+MAXN,lmax);
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现         while (!q.empty())
SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现        SPFA算法模版+邻接表实现{
SPFA算法模版+邻接表实现                 q.pop();
SPFA算法模版+邻接表实现         }
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现         for (i=0;i
SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现        SPFA算法模版+邻接表实现{
SPFA算法模版+邻接表实现                 long from,to,cost;
SPFA算法模版+邻接表实现                 scanf("%ld %ld %ld",&from,&to,&cost);
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现                 e[eid].next=p[from];
SPFA算法模版+邻接表实现                 e[eid].v=to;
SPFA算法模版+邻接表实现                 e[eid].cost=cost;
SPFA算法模版+邻接表实现                 p[from]=eid++;
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现                 //以下适用于无向图
SPFA算法模版+邻接表实现                 swap(from,to);
SPFA算法模版+邻接表实现                
SPFA算法模版+邻接表实现                 e[eid].next=p[from];
SPFA算法模版+邻接表实现                 e[eid].v=to;
SPFA算法模版+邻接表实现                 e[eid].cost=cost;
SPFA算法模版+邻接表实现                 p[from]=eid++;
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现         }
SPFA算法模版+邻接表实现 }
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现 void print(long End)
SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现{
SPFA算法模版+邻接表实现         //若为lmax 则不可达
SPFA算法模版+邻接表实现         printf("%ld\n",Dis[End]);       
SPFA算法模版+邻接表实现 }
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现 void SPF()
SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现{
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现         init();
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现         long Start,End;
SPFA算法模版+邻接表实现         scanf("%ld %ld",&Start,&End);
SPFA算法模版+邻接表实现         Dis[Start]=0;
SPFA算法模版+邻接表实现         vist[Start]=true;
SPFA算法模版+邻接表实现         q.push(Start);
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现         while (!q.empty())
SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现        SPFA算法模版+邻接表实现{
SPFA算法模版+邻接表实现                 long t=q.front();
SPFA算法模版+邻接表实现                 q.pop();
SPFA算法模版+邻接表实现                 vist[t]=false;
SPFA算法模版+邻接表实现                 long j;
SPFA算法模版+邻接表实现                 for (j=p[t];j!=-1;j=e[j].next)
SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现                SPFA算法模版+邻接表实现{
SPFA算法模版+邻接表实现                         long w=e[j].cost;
SPFA算法模版+邻接表实现                         if (w+Dis[t]
SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现                        SPFA算法模版+邻接表实现{
SPFA算法模版+邻接表实现                                 Dis[e[j].v]=w+Dis[t];
SPFA算法模版+邻接表实现                                 if (!vist[e[j].v])
SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现                                SPFA算法模版+邻接表实现{
SPFA算法模版+邻接表实现                                         vist[e[j].v]=true;
SPFA算法模版+邻接表实现                                         q.push(e[j].v);
SPFA算法模版+邻接表实现                                 }
SPFA算法模版+邻接表实现                         }
SPFA算法模版+邻接表实现                 }
SPFA算法模版+邻接表实现         }
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现         print(End);
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现 }
SPFA算法模版+邻接表实现
SPFA算法模版+邻接表实现 int main()
SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现{
SPFA算法模版+邻接表实现         while (scanf("%ld %ld",&m,&n)!=EOF)
SPFA算法模版+邻接表实现 SPFA算法模版+邻接表实现        {
SPFA算法模版+邻接表实现                 SPF();
SPFA算法模版+邻接表实现         }
SPFA算法模版+邻接表实现         return 0;
SPFA算法模版+邻接表实现 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值