zoukankan      html  css  js  c++  java
  • spfa(模板)

    题目

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<deque>
    #include<cstring>
    using namespace std;
    struct H{
        int cost;//边长
        int to; //指向的店
    };
    vector<H>w[10001];//动态数组
    int dis[100001],f[100001],d[100001];
    int main()
    {
        int m,n,i,j,s;
    
        scanf("%d%d%d",&n,&m,&s);
    
        for(i=1;i<=m;i++)
        {
            int x,y,k;
    
            scanf("%d%d%d",&x,&y,&k);
    
            H e1;
            e1.to=y;
            e1.cost=k;
            w[x].push_back(e1);
        }
    
        for(i=1;i<=n;i++)
         dis[i]=2147483647;//最大值
    
        f[s]=1;dis[s]=0;d[1]=s;
    
        int h=0,t=1;
    
        do
        {
            h++;
            int x=d[h];
            f[x]=0;
            for(i=0;i<w[x].size();i++)//枚举与队首相连接的点,动态数组是在0开始存的
            {
                int p=w[x][i].to;
                if(dis[p]>dis[x]+w[x][i].cost)
                {
                    dis[p]=dis[x]+w[x][i].cost;
                    if(f[p]==0) d[++t]=p,f[p]=1;//如果此点有更新价值,就入队;
                }
            }
        }while(h<t);
    
        for(i = 1;i <= n;i++)
         printf("%d ",dis[i]);
    
        return 0;
    } 

    错误
    DI 1:我刚开始将所有的与队首相连的点都入队,导致了re也就是队爆了,其实只有这个点的最短路径值更新了,才有入队的价值;
    DI 2:题目中说如果s与一个点不相连,就输出最大值,并且给出了最大值为214748364,也就是赋的初值,我却没有看到这句话,赋的初值为999999999。
    mdzz我又犯了老毛病,不看题,不思考。

  • 相关阅读:
    MIT 6.828 JOS学习笔记10. Lab 1 Part 3: The kernel
    Java基础知识点4:继承
    CentOS Installation
    超微主板创建RAID磁盘阵列
    MySQL查询语句
    Psql操作命令
    Let's Encrypt 免费 SSL 证书续期
    Linux系统禁用swap分区
    公共 NTP 服务器地址
    Postgresql配置
  • 原文地址:https://www.cnblogs.com/ht008/p/6819852.html
Copyright © 2011-2022 走看看