Codeforces Educational 38 D. Buy a Ticket ( 建图+堆优化Dij

题目描述

Musicians of a popular band “Flayer” have announced that they are going to “make their exit” with a world tour. Of course, they will visit Berland as well.

There are n cities in Berland. People can travel between cities using two-directional train routes; there are exactly m routes, i-th route can be used to go from city vi to city ui (and from ui to vi), and it costs wi coins to use this route.

Each city will be visited by “Flayer”, and the cost of the concert ticket in i-th city is ai coins.

You have friends in every city of Berland, and they, knowing about your programming skills, asked you to calculate the minimum possible number of coins they have to pay to visit the concert. For every city i you have to compute the minimum number of coins a person from city i has to spend to travel to some city j (or possibly stay in city i), attend a concert there, and return to city i (if j ≠ i).

Formally, for every you have to calculate , where d(i, j) is the minimum number of coins you have to spend to travel from city i to city j. If there is no way to reach city j from city i, then we consider d(i, j) to be infinitely large.

输入

The first line contains two integers n and m (2 ≤ n ≤ 2·105, 1 ≤ m ≤ 2·105).

Then m lines follow, i-th contains three integers vi, ui and wi (1 ≤ vi, ui ≤ n, vi ≠ ui, 1 ≤ wi ≤ 1012) denoting i-th train route. There are no multiple train routes connecting the same pair of cities, that is, for each (v, u) neither extra (v, u) nor (u, v) present in input.

The next line contains n integers a1, a2, … ak (1 ≤ ai ≤ 1012) — price to attend the concert in i-th city.

输出

Print n integers. i-th of them must be equal to the minimum number of coins a person from city i has to spend to travel to some city j (or possibly stay in city i), attend a concert there, and return to city i (if j ≠ i).

样例

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

题意

给出每个点的点权和一些边权(并不一定是连通图 计算每个点都经过的最优解
例如样例一点 123 1 2 3 连接 那么明显我们知道选点权为 1 1 3 的点作为汇点最小了 同理样例二中间点 2 2 为汇点
明显的多源最短路+贪心问题 2e5的数据跑floyed妥妥超时 考虑nlogn的算法
建立一个独立源点 将点权转化为与这个点连接的边权 变成连通图跑最短路QAQ ( cf把spfa卡的不要不要的

AC代码

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

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))

const LL MAXN = 2e5+11;
const LL INF = ~0ull>>1;
struct qnode
{
    LL v, c;
    qnode(LL _v=0, LL _c=0):v(_v),c(_c){}
    bool operator <(const qnode &r)const {
        return c > r.c;
    }
};
struct edge
{
    LL v, cost;
    edge(LL _v=0, LL _cost=0):v(_v),cost(_cost){}
};
vector<edge> E[MAXN];
bool vis[MAXN];
LL dis[MAXN];

void Dij(LL s,LL n)
{
    CLR(vis,false);
    for(LL i = 1; i <= n; i++) dis[i] = INF;
    priority_queue<qnode> que;
    while(!que.empty()) que.pop();
    dis[s] = 0;
    que.push(qnode(s,0));
    qnode tmp;
    while(!que.empty()) {
        tmp = que.top();
        que.pop();
        LL u = tmp.v;
        if(vis[u]) continue;
        vis[u] = true;
        for(LL i = 0; i < E[u].size(); i++) {
            LL v = E[u][i].v;
            LL cost = E[u][i].cost;
            if(!vis[v] && dis[v] > dis[u]+cost) {
                dis[v] = dis[u]+cost;
                que.push(qnode(v,dis[v]));
            }
        }
    }
}
void addedge(LL u, LL v, LL w)
{
    E[u].push_back(edge(v,w));
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);
    LL n, m;
    LL x, y, z;
    cin >> n >> m;
    for(LL i = 0; i < m; i++) {
        cin >> x >> y >> z;
        addedge(x,y,z*2);
        addedge(y,x,z*2);
    }
    for(LL i = 1; i <= n; i++) {
        cin >> x;
        addedge(i,n+1,x);
        addedge(n+1,i,x);
    }
    Dij(n+1,n+1);
    for(LL i = 1; i <= n; i++) cout << dis[i] << ' ';
    cout << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值