[模板] 最短路

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;
}

最短路

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值