zoukankan      html  css  js  c++  java
  • [模板] 最短路

    SPFA求最短路+判断负环 


    
    int head[MAXN] = {0}, tot = 0;
    
    void init(){ mem(head); tot = 0; }
    
    struct node { int from, to, val, nex; } edge[MAXN << 1];
    
    int add(int x, int y, int z)
    {
    	edge[++tot].to = y;
    	edge[tot].val = z;
    	edge[tot].nex = head[x];
    	head[x] = tot;
    	return tot;
    }
    
    int dis[MAXN], cnt[MAXN] = {0};
    
    bitset <MAXN> used;
    
    bool spfa(int x)
    {
    	memset(dis, 63, sizeof(dis));
    	used.reset();
    	mem(cnt);
    		
    	queue <int> Q;
    	Q.push(x);
    	
    	used[x] = true;
    	dis[x] = 0;
    	cnt[x] = 1;
    	
    	while(!Q.empty())
    	{
    		int now = Q.front();
    		Q.pop();
    		used[now] = false;
    		
    		for(int it = head[now]; it; it = edge[it].nex)
    		{
    			int to = edge[it].to, val = edge[it].val;
    			if(dis[to] > dis[now] + val)
    			{
    				dis[to] = dis[now] + val;
    				cnt[to] = cnt[now] + 1; //记录松弛次数
    				
    				if(cnt[to] > n) //松弛次数>n必有负环
    					return false; 
    					
    				if(!used[to])
    					Q.push(to), used[to] = true;
    			}
    		}	
    	}
    	
    	return true;
    }

    动态规划-floyd
    
    for(int k = 1; k <= len; k++)     //枚举中转点k
            for(int i = 1; i <= len; i++)     //枚举每个位置
                for(int j = 1; j <= len; j++)    //枚举每个位置
                    dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);    //比较更新

    1.链式向前星构图

    https://blog.csdn.net/Zeolim/article/details/81741443

    2.利用小顶堆性质贪心BFS求得最短路 

    https://www.bilibili.com/video/av25829980?from=search&seid=15079366380719599656

    3.STL PQ使用

    https://blog.csdn.net/c20182030/article/details/70757660

    #pragma GCC optimize(2)
    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <cctype>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    #include <ctime>
    #include <vector>
    #include <fstream>
    #include <list>
    #include <iomanip>
    #include <numeric>
    using namespace std;
    typedef long long ll;
    
    const int MAXN = 1e6 + 10;
    
    int inf = 0x3f3f3f3f;
    
    ll n, m, s, ans[MAXN];
    
    struct EDGE
    {
        ll next, to, val;
    }edge[MAXN];
    
    int head[MAXN] = {0}, cnt = 0;
    
    void add(int x, int y, int z)
    {
        edge[++cnt].next = head[x];
        edge[cnt].to = y;
        edge[cnt].val = z;
        head[x] = cnt;
    }
    
    struct bfsnode
    {
        ll now, dis;
        
        bfsnode(ll x, ll y)
        {
            now = x;
            dis = y;
        }
    
        bool operator < (const bfsnode ret) const
        {
            return dis > ret.dis;
        }
    
    };
    
    priority_queue <bfsnode> PQ;
    
    bool finded[MAXN];
    
    void bfs(bfsnode x)
    {
    	PQ.push(x);
    	
        while(! PQ.empty())
        {
            bfsnode B = PQ.top();
    
            PQ.pop();
    
            if( !finded[B.now] )
            {
                finded[B.now] = true;
    
                ans[B.now] = min(ans[B.now], B.dis);
    
                for(int i = head[B.now]; i != 0; i = edge[i].next)
                {
                    PQ.push( bfsnode(edge[i].to, B.dis + edge[i].val) );
                }
            }
        }
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
    
        cin.tie(0);     cout.tie(0);
    
        cin>>n>>m>>s;
    
        ll a, b, c;
    	
    	memset(ans, 0x3f, sizeof(ans));
    	
        for(int i = 0; i < m; i++)
        {
            cin>>a>>b>>c;
    
            add(a, b, c);
        }
    
        bfs(bfsnode(s, 0));
    
        for(int i = 1; i <= n; i++)
            ans[i] >= inf ? printf("2147483647 ") : printf("%d ", ans[i]);
    
        return 0;
    }

    最短路

  • 相关阅读:
    盒子跟随鼠标移动而移动(兼容IE8)
    JavaScript捕获鼠标坐标
    JavaScript判断滚动条是否滚动到底部
    JavaScript自定义getStyle方法获取元素样式
    JavaScript实现简单的图片切换功能
    SpeedReader
    JavaScript定时器
    远程登陆ubantu服务器 .bashrc文件每次打开终端都需要source的问题
    阿里云服务器Ubantu16.04升级python3.6
    阿里云服务器ubantu创建新用户登录显示问题
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270473.html
Copyright © 2011-2022 走看看