• 邻接表(spfa模版)


    http://www.cnblogs.com/tonghao/p/4708661.html//数组模拟

    邻接表只是储存图的方法,要求最短路还是要用其他算法。SPFA迪杰斯特拉等等算法。
    #include<stdio.h>
    #include<iostream>
    #include<stdlib.h>
    #include<queue>
    #include<algorithm>
    #include<string.h>
    #define inf 0x3f3f3f
    using namespace std;
    bool vis[2005];
    int dis[2005];
    int T,N;
    struct node
    {
        int v,w;
        struct node*next;
    }*h[2005];
    void LA(int u,int v,int w)
    {
        struct node *p=(struct node *)malloc(sizeof(struct node));
       // p->u=u;
        p->v=v;
        p->w=w;
        p->next=h[u];
        h[u]=p;
    }
    void spfa(int s)
    {
        queue<int>Q;
        memset(vis,false,sizeof(vis));
        Q.push(s);
        dis[s]=0;
        while(!Q.empty()){
            int u=Q.front();
            Q.pop();
            vis[u]=false;
            struct node *p;
            for(p=h[u];p!=NULL;p=p->next)
            {
                int q=p->v;
                if(dis[q]>dis[u]+p->w)//最短路径
                {
                    dis[q]=dis[u]+p->w;
                    if(!vis[q])
                    {
                        vis[q]=true;
                        Q.push(q);
                    }
                }
            }
        }
    }
    int main()
    {
        while(~scanf("%d%d",&T,&N))
        {
            memset(dis,inf,sizeof(dis));
            memset(h,0,sizeof(h));
            for(int i=0;i<T;i++)
            {
                int v,u,w;
                cin>>u>>v>>w;
                LA(u,v,w);
                LA(v,u,w);
            }
            spfa(1);
            cout<<dis[N]<<endl;
        }
    }



  • 相关阅读:
    Queue
    Singly-Linked List
    Array
    HTTP请求详解
    封装element的API
    uniapp登录逻辑
    Selector学习笔记 (未完待续)
    <Scalable IO in Java>学习
    Spring PropertyMapper源码阅读笔记
    leetcode 字节跳动探索系列
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053369.html
Copyright © 2020-2023  润新知