CF938D Buy a Ticket

1522713-20191007170231801-17633395.png

4 2
1 2 4
2 3 7
6 20 1 25
6 14 1 25 
3 3
1 2 1
2 3 1
1 3 1
30 10 20
12 10 12 

思路:

建立虚点把点券转化成边权

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define int long long
#define maxn 850500
using namespace  std ;
int read() {
    int x = 0 , f = 1 ; char s = getchar() ;
    while(s > '9' || s < '0') {if(s == '-') f = -1 ; s = getchar() ;}
    while(s <='9' && s >='0') {x = x * 10 + (s-'0'); s = getchar() ;}
    return x*f ;    
}
int n , m ;
struct dy{ 
    int x , y ,z  ,next ;
}a[maxn] ;
int head[maxn] , vis[maxn] , dis[maxn] , t ;
void add(int x ,int y ,int z) {
    a[++t].x = x ;
    a[t].y = y ;
    a[t].z = z ;
    a[t].next = head[x] ;
    head[x] = t ;
}
typedef pair<int,int>pa ;
priority_queue<pa,vector<pa>,greater<pa> >q ;
void dij(int s) {
    memset(dis,0x3f,sizeof(dis)) ;
    memset(vis,0,sizeof(vis)) ;
    dis[s] = 0 , vis[s] = 0 ;
    q.push(make_pair(dis[s],s)) ;
    while(!q.empty()) {
        int u = q.top().second ;
        q.pop() ;
        if(vis[u]) continue ;
        vis[u] = 1 ;
        for(int i = head[u] ; i ; i = a[i].next ) {
            int v = a[i].y ;
            if(dis[v] > dis[u] + a[i].z ) {
                dis[v] = dis[u] + a[i].z ;
                q.push(make_pair(dis[v],v)) ;
            }
        }
    }
}
signed main () {
    n = read() , m = read() ;
    while(m --) {
        int x = read() , y = read() , z = read() ;
        add(x,y,z*2) ;
        add(y,x,z*2) ;
    }
    for(int i = 1 ; i <= n ; i ++) {
        int x = read() ;
        add(0,i,x) ;
        add(i,0,x) ;
    }dij(0) ;
    for(int i = 1 ; i <= n ; i ++) {
        printf("%I64d ",dis[i]) ;
    }
    return 0 ;
}

转载于:https://www.cnblogs.com/lyt020321/p/11631226.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值