记录编号 583470 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 正则表达式 最终得分 100
用户昵称 Gravatar┭┮﹏┭┮ 是否通过 通过
代码语言 C++ 运行时间 1.709 s
提交时间 2023-10-15 15:59:20 内存使用 10.25 MiB
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
//tarjan求强连通分量+缩点+最短路(spfa或dijkstra) 
#define P pair<int,int>
#define make(x,y) make_pair(x,y)
const int N = 2e5+10,M = 1e6+10;
int n,m;
struct made{
    int ver,nx,ed;
}e[M],re[M];//原图 以及 缩点重建图
int hd[N],rhd[N],tot,cnt,num,top;
int low[N],dfn[N],st[N],color[N];
bool v[N];//tarjan 
map<P,int>mp;//判重边 
void add(int x,int y,int z){
    tot++;
    e[tot].ver = y,e[tot].ed = z,e[tot].nx = hd[x],hd[x] = tot;
}//原图 
int radd(int x,int y,int z){
    tot++;
    re[tot].ver = y,re[tot].ed = z,re[tot].nx = rhd[x],rhd[x] = tot;
    return tot;
}//缩点图 
void tarjan(int x) {
    low[x] = dfn[x] = ++cnt;
    st[++top] = x,v[x] = 1;
    for(int i = hd[x];i;i = e[i].nx){
        int y = e[i].ver;
        if(!dfn[y])tarjan(y),low[x] = min(low[x],low[y]) ;
        else if(v[y])low[x] = min(low[x],dfn[y]);
    }
    if(low[x] == dfn[x]){
        num++;int y = 0;
        do{
            y = st[top--];
            v[y] = 0,color[y] = num;
        }while(x != y);
    }
}//正常tarjan 
void build(int x){
    for(int i = hd[x];i;i = e[i].nx){
        int y = e[i].ver;
        int rx = color[x],ry = color[y];
        int f = mp[make(rx,ry)];
        if(rx == ry)continue;
        if(f){
            re[f].ed = min(re[f].ed,e[i].ed);//最优 
            continue;
        }//判重边 
        mp[make(rx,ry)] = radd(rx,ry,e[i].ed);
    }
}//重建缩点 
int d[N];
priority_queue<P,vector<P>,greater<P> >q;
void dijkstra(int x){
    memset(v,0,sizeof(v));
    memset(d,0x3f,sizeof(d));
    d[x] = 0;
    q.push(make(d[x],x));
    while(!q.empty()){
        int x = q.top().second;q.pop();
        if(v[x])continue;
        v[x] = 1;
        for(int i = rhd[x];i;i = re[i].nx){
            int y = re[i].ver,z = re[i].ed;
            if(d[y] > d[x] + z){
                d[y] = d[x] + z;
                q.push(make(d[y],y));
            } 
        }
    }
}//dijkstra求最短路 
int main(){
    freopen("regexp.in","r",stdin);
    freopen("regexp.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= m;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);
    }
    for(int i = 1;i <= n;i++)
        if(!color[i])tarjan(i);// 
    // 
    tot = 0;
    for(int i = 1;i <= n;i++)
        build(i);// 
    dijkstra(color[1]);// 
    printf("%d\n",d[color[n]]);
    
    return 0;
    
}