当前位置 : 主页 > 编程语言 > c语言 >

CodeForces - 938D 多源最短路 && Dijkstra 优先队列

来源:互联网 收集:自由互联 发布时间:2023-10-08
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-di

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.

Input
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.

Output
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).

Examples
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

定义最小值: 2* dis( i, j )+ val[ j ] ;
求每个点的最小值;
考虑从J 出发,放在优先级队列中,跑最短路;
注意: 此题卡 spfa

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>

typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 200005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-5
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}


ll n, m, head[maxn], to[maxn * 2], nxt[maxn * 2];
ll w[maxn * 2], tot = 1;
ll vis[maxn], ans[maxn];

priority_queue< pll, vector<pll>, greater< pll > >q;

void addedge(ll u,ll v,ll val){
    w[tot]=val;
    to[tot]=v;
    nxt[tot]=head[u];
    head[u]=tot++;
}


int main()
{
    ios::sync_with_stdio(false);
     cin>>n>>m;
     for(ll i=1;i<=m;i++){
        ll a,b,c;
        cin>>a>>b>>c;
        addedge(a,b,2*c);
        addedge(b,a,2*c);
     }
     for(ll i=1;i<=n;i++){
        ll t;
        cin>>t;
        q.push(make_pair(t,i));
     }  
     while(!q.empty()){
        pll u=q.top();
        q.pop();
        if(vis[u.second])continue;
        vis[u.second]=1;
        ans[u.second]=u.first;
        for(ll i=head[u.second];i;i=nxt[i]){
            ll v=to[i];
            q.push(make_pair(u.first+w[i],v));
         }
     }
     for(ll i=1;i<=n;i++){
        cout<<ans[i]<<' ';
     }
}
上一篇:Codeforces Round #459 (Div. 2) D. MADMAX DAG&&博弈
下一篇:没有了
网友评论