【luogu P2169 正则表达式】 题解

题目链接:https://www.luogu.org/problemnew/show/P2169
tarjan缩点 + SPFA
缩完点之后加边注意别写错。
也可以不用建两个图,可以在一张图上判断是否强连通再SPFA.

#include <queue>
#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000000 + 10;
const int inf = 0x7fffffff;
struct edge{
    int from, to, next, len;
}e1[maxn<<2], e2[maxn<<2];
int head1[maxn], head2[maxn], cnt1, cnt2;
int dfn[maxn], low[maxn], dis[maxn], tim;
bool vis1[maxn], vis2[maxn];
int n, m, color[maxn], num;
stack<int> s;
queue<int> q; 
void add1(int u, int v, int w)
{
    e1[++cnt1].from = u;
    e1[cnt1].len = w;
    e1[cnt1].next = head1[u];
    e1[cnt1].to = v;
    head1[u] = cnt1;
}
void add2(int u, int v, int w)
{
    e2[++cnt2].from = u;
    e2[cnt2].len = w;
    e2[cnt2].next = head2[u];
    e2[cnt2].to = v;
    head2[u] = cnt2;
}
void tarjan(int x)
{
    dfn[x] = low[x] = ++tim;
    s.push(x); vis1[x] = 1;
    for(int i = head1[x]; i != -1; i = e1[i].next)
    {
        int v = e1[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[x] = min(low[x], low[v]);
        }
        else if(vis1[v])
        {
            low[x] = min(low[x], low[v]);
        }
    }
    if(low[x] == dfn[x])
    {
        color[x] = ++num;
        vis1[x] = 0;
        while(s.top() != x)
        {
            color[s.top()] = num;
            vis1[s.top()] = 0;
            s.pop();
        }
        s.pop();
    }
}
void SPFA()
{
    while(!q.empty())
    {
        int now = q.front(); q.pop();
        vis2[now] = 0;
        for(int i = head2[now]; i != -1; i = e2[i].next)
        {
            int v = e2[i].to;
            if(dis[v] > dis[now] + e2[i].len)
            {
                dis[v] = dis[now] + e2[i].len;
                if(!vis2[v])
                {
                    q.push(v);
                    vis2[v] = 1;
                }
            }
        }
    }
}
int main()
{
    memset(head1, -1, sizeof(head1));
    memset(head2, -1, sizeof(head2));
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; i++) dis[i] = inf;
    for(int i = 1; i <= m; i++)
    {
        int u, v, w;
        scanf("%d%d%d",&u,&v,&w);
        add1(u,v,w);
    }
    for(int i = 1; i <= n; i++) 
    if(!dfn[i]) tarjan(i);
    for(int i = 1; i <= n; i++)
        for(int j = head1[i]; j != -1; j = e1[j].next)
        {
            int v = e1[j].to;
            if(color[v] != color[i])
            add2(color[i], color[v], e1[j].len);
        }
    q.push(color[1]);
    dis[color[1]] = 0;
    vis2[color[1]] = 1;
    SPFA();
    printf("%d",dis[color[n]]);
}

转载于:https://www.cnblogs.com/MisakaAzusa/p/9368210.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值