板子——图论专题

最短路

dijkstra

特点
单源最短路,全是正权边。时间复杂度O(m logn)。

代码

#include<bits/stdc++.h>
using namespace std; 

typedef long long ll;
#define endl "\n"
#define IOS ios::sync_with_stdio(false); cin.tie(0)
#define inf 0x3f3f3f3f
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define debug(a) cout<<"\tdebug:"<<a<<endl
#define for1(i, a, b) for(int i = a; i <= b; i++)
#define for2(i, b, a) for(int i = b; i >= a; i--)
const double PI = acos(-1.0);
const int mod = 998244353;
const int eps = 1e-8;
const int N = 10 + 1e5;

void slove();
template<typename T>void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-') f *= -1; ch = getchar();}
    while(isdigit(ch)){x = x*10+ch-48; ch = getchar();} x *= f;
}
template<typename T>void print(T x) 
{
	if(x < 0) putchar('-'), x = -x;
	if(x >= 10) print(x/10);
	putchar(x % 10 + '0');
}

int main()
{
#ifndef ONLINE_JUDGE 
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif
    IOS;
    slove();
    return 0;
}

int n, m, tot, s, t;
int vis[N], head[N], dis[N];

struct edge
{
    int to, next, val;
}e[N];

struct node
{
    int to, val;
    bool operator < (const node&b) const
    {
        return val > b.val;
    }
};

void add(int u, int v, int val)
{
    e[tot].to = v; e[tot].val = val; e[tot].next = head[u]; head[u] = tot++;
}

void dijkstra()
{
    memset(dis, 0x3f, sizeof dis);
    dis[s] = 0;
    priority_queue<node> q;
    q.push(node{s, 1});
    
    while(!q.empty())
    {
        node x = q.top(); q.pop();
        int u = x.to;
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = head[u]; ~i; i = e[i].next)
        {
            int v = e[i].to;
            if(dis[v] > dis[u] + e[i].val)
            {
                dis[v] = dis[u]+e[i].val;
                q.push(node{v, dis[v]});
            }
        }
    }
}

void slove()
{
	memset(head, -1, sizeof head);
    cin>>n>>m>>s>>t;
    while(m--)
    {
        int u, v, w; cin>>u>>v>>w;
        add(u, v, w);
        add(v, u, w);
    }
    dijkstra();
    cout<<dis[t]<<endl;
}

SPFA

特点
单源最短路,可以有负权边,不能有负环。时间复杂度:最好O(m) 最坏O(mn)。

代码

#include<bits/stdc++.h>
using namespace std; 

typedef long long ll;
#define endl "\n"
#define IOS ios::sync_with_stdio(false); cin.tie(0)
#define inf 0x3f3f3f3f
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define debug(a) cout<<"\tdebug:"<<a<<endl
#define for1(i, a, b) for(int i = a; i <= b; i++)
#define for2(i, b, a) for(int i = b; i >= a; i--)
const double PI = acos(-1.0);
const int mod = 998244353;
const int eps = 1e-8;
const int N = 10 + 1e6;

void slove();
template<typename T>void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-') f *= -1; ch = getchar();}
    while(isdigit(ch)){x = x*10+ch-48; ch = getchar();} x *= f;
}
template<typename T>void print(T x) 
{
	if(x < 0) putchar('-'), x = -x;
	if(x >= 10) print(x/10);
	putchar(x % 10 + '0');
}

int main()
{
#ifndef ONLINE_JUDGE 
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif
    IOS;
    slove();
    return 0;
}

int n, m, tot, s, t;
int vis[N], head[N], dis[N];

struct edge
{
    int to, next, val;
}e[N];


void add(int u, int v, int val)
{
    e[tot].to = v; e[tot].val = val; e[tot].next = head[u]; head[u] = tot++;
}

void spfa()
{
    memset(dis, 0x3f, sizeof dis);
    dis[s] = 0;

    queue<pii> q;
    q.push(pii{s, 0});
    vis[s] = 1;

    while(!q.empty())
    {
        pii x = q.front(); q.pop();
        int u = x.first;
        vis[u] = 0;
        for(int i = head[u]; ~i; i = e[i].next)
        {
            int v = e[i].to; 
            if(dis[v] > dis[u] + e[i].val)
            {
                dis[v] = dis[u]+e[i].val;
                if(!vis[v]) vis[v] = 1, q.push(pii{v, dis[v]});
            }
        }
    }
    if(dis[t] == inf) cout<<"impossible"<<endl;
    else cout<<dis[t]<<endl;
}

void slove()
{
	memset(head, -1, sizeof head);
    cin>>n>>m;
    s = 1;
    t = n;
    while(m--)
    {
        int u, v, w; cin>>u>>v>>w;
        add(u, v, w);
    }
    spfa();
}

负环

SPFA

特点
单源最短路,可存在负权,时间复杂度:最优O(m) 最坏O(mn)

代码

#include<bits/stdc++.h>
using namespace std; 

typedef long long ll;
#define endl "\n"
#define IOS ios::sync_with_stdio(false); cin.tie(0)
#define inf 0x3f3f3f3f
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define debug(a) cout<<"\tdebug:"<<a<<endl
#define for1(i, a, b) for(int i = a; i <= b; i++)
#define for2(i, b, a) for(int i = b; i >= a; i--)
const double PI = acos(-1.0);
const int mod = 998244353;
const int eps = 1e-8;
const int N = 10 + 1e6;

void slove();
template<typename T>void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-') f *= -1; ch = getchar();}
    while(isdigit(ch)){x = x*10+ch-48; ch = getchar();} x *= f;
}
template<typename T>void print(T x) 
{
	if(x < 0) putchar('-'), x = -x;
	if(x >= 10) print(x/10);
	putchar(x % 10 + '0');
}

int main()
{
#ifndef ONLINE_JUDGE 
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif
    IOS;
    slove();
    return 0;
}

int n, m, tot, s, t;
int vis[N], head[N], dis[N], cnt[N];

struct edge
{
    int to, next, val;
}e[N];


void add(int u, int v, int val)
{
    e[tot].to = v; e[tot].val = val; e[tot].next = head[u]; head[u] = tot++;
}

int spfa()
{
    memset(dis, 0x3f, sizeof dis);
    dis[s] = 0;

    queue<pii> q;   // second一般没用
    for(int i = 1; i <= n; i++)
    {
        vis[i] = 1;
        q.push(pii{i, 0});
    }

    while(!q.empty())
    {
        pii x = q.front(); q.pop();
        int u = x.first;
        vis[u] = 0;
        for(int i = head[u]; ~i; i = e[i].next)
        {
            int v = e[i].to; 
            if(dis[v] > dis[u] + e[i].val)
            {
                dis[v] = dis[u]+e[i].val;
                cnt[v] = cnt[u]+1;
                if(cnt[v] >= n) return 1;   // 有负环
                if(!vis[v]) vis[v] = 1, q.push(pii{v, dis[v]});
            }
        }
    }
    return 0;
}

void slove()
{
	memset(head, -1, sizeof head);
    cin>>n>>m;
    s = 1;
    t = n;
    while(m--)
    {
        int u, v, w; cin>>u>>v>>w;
        add(u, v, w);
    }
   int ans = spfa();
   if(ans) cout<<"YES"<<endl;
   else cout<<"NO"<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值