lanqiao oj 1122 蓝桥王国

简介: lanqiao oj 1122 蓝桥王国

用户登录

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std ;
const long long  N = 3e5 + 2 , INF = 0x3f3f3f3f3f3f3f3fLL;
int n , m ;
struct edge{
    int from , to ; long long w ;
    edge(int a ,int b , long long c){
        from = a , to = b , w = c ;
    }
};
vector<edge> e[N] ;
struct s_node{
    int id ; long long n_dis ;
    s_node(int b , long long c ){
        id = b ; n_dis = c ;
    }
    bool operator < (const s_node &a ) const {
        return n_dis > a.n_dis ;
    }
};
int pre[N] ;
void print_path(int s , int t){
    if(s == t) {
        printf("%d",s) ; return ;
    }
    print_path(s,pre[t]) ;
    printf("%d",t) ; 
}
long long dis[N] ;
void dijkstra(){
    int s = 1 ;
    bool done[N] ;
    for(int i = 1 ; i <= n ; i++) {
        dis[i] = INF ; done[i] = false ;
    }
    dis[s] = 0 ;
    priority_queue<s_node> q ;
    q.push(s_node(s,dis[s])) ;
    while(!q.empty()){
        s_node u = q.top() ;
        q.pop() ;
        if(done[u.id]) continue ;
        done[u.id] = true ;
        for(int i = 0 ; i < e[u.id].size() ; i ++){
            edge y = e[u.id][i] ; 
            if(done[y.to]) continue ;
            if(dis[y.to] > y.w + u.n_dis){
                dis[y.to] = y.w + u.n_dis ;
                q.push(s_node(y.to , dis[y.to])) ;
                pre[y.to] = u.id ;
            }
        }
    }
}
int main(){
    cin >> n >> m ;
    for(int i = 1 ; i <= m ; i ++){
        int a, b ,c ;
        cin >> a >> b >> c ;
        e[a].push_back(edge(a,b,c)) ;
    }
    dijkstra() ;
    for(int i = 1; i <=n ;i ++){
        if(dis[i] >= INF) cout << "-1 " ;
        else printf("%lld ",dis[i]) ; 
    }
}
目录
相关文章
|
3天前
lanqiao OJ 22年省赛 扫雷
lanqiao OJ 22年省赛 扫雷
12 1
|
3天前
lanqiao OJ 525 传球游戏
lanqiao OJ 525 传球游戏
11 2
|
3天前
lanqiao OJ 89 路径之谜
lanqiao OJ 89 路径之谜
7 1
|
1天前
lanqiao oj 1135 蓝桥幼儿园(并查集)
lanqiao oj 1135 蓝桥幼儿园(并查集)
5 0
|
2天前
|
BI
lanqiao OJ 蜗牛
lanqiao OJ 蜗牛
6 0
|
1天前
lanqiao oj 1121 蓝桥公园(floyd)
lanqiao oj 1121 蓝桥公园(floyd)
6 0
|
1天前
lanqiao OJ 健身
lanqiao OJ 健身
5 0
|
2天前
lanqiao OJ 2097 青蛙过河
lanqiao OJ 2097 青蛙过河
6 0
|
3天前
lanqiao OJ 234 大胖子走迷宫
lanqiao OJ 234 大胖子走迷宫
5 0
|
5月前
|
算法 C++
小唐蓝桥的做题心得
小唐蓝桥的做题心得