D. Buy a Ticket

D. Buy a Ticket

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 viui 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

Copy

4 21 2 42 3 76 20 1 25

output

Copy

6 14 1 25 

input

Copy

3 31 2 12 3 11 3 130 10 20

output

Copy

12 10 12 

对于每一个点i,求一个到j最短的距离是的2d(i,j)+aj最小,每天更小的话就用ai。

可以加一个超级源点 ,源点到每个点的距离是ai,然后将每条边的距离翻倍就行了。思路很巧妙。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 2e5+10;
 5 typedef pair<ll,int> P;
 6 vector<P> vs[N];
 7 int n, m, u, v;
 8 ll w, x, d[N];
 9 void dij(int s) {
10     for(int i = 1; i <= n; i ++) {
11         d[i] = 1LL<<60;
12     }
13     d[s] = 0;
14     priority_queue<P, vector<P>, greater<P> >que;
15     que.push(P(0,s));
16     while(que.size()) {
17         P p = que.top();
18         que.pop();
19         int u = p.second;
20         if(d[u] < p.first) continue;
21         for(int i = 0; i < vs[u].size(); i ++) {
22             P x = vs[u][i];
23             if(d[x.second] > d[u] + x.first) {
24                 d[x.second] = d[u] + x.first;
25                 que.push(P(d[x.second],x.second));
26             }
27         }
28     }
29 }
30 int main() {
31     ios_base::sync_with_stdio(false);
32     cin.tie(0);
33     cout.tie(0);
34     cin >> n >> m;
35     for(int i = 1; i <= m; i ++) {
36         cin >> u >> v >> w;
37         vs[u].push_back(P(2*w,v));
38         vs[v].push_back(P(2*w,u));
39     }
40     for(int i = 1; i <= n; i ++) {
41         cin >> x;
42         vs[n+1].push_back(P(x,i));
43     }
44     dij(n+1);
45     for(int i = 1; i <= n; i ++) {
46         printf("%lld%c",d[i]," \n"[i==n]);
47     }
48     return 0;
49 }

原文地址:https://www.cnblogs.com/xingkongyihao/p/8947462.html

时间: 2024-11-03 02:15:54

D. Buy a Ticket的相关文章

Buy the Ticket DP +大数

Buy the Ticket 题目抽象:有m个手持50元的人,n个手持100元的人,售票处没有准备钱.问有多少种不同的方案使得购票不中断.每个人是不同的个体. 分析:简单DP题.画个格子,那么选取的点不越过对角线y = x.  dp[i][j]表示i个100元的人,j个手持50元的人的方案数.     i<=j dp[i][j] = dp[i-1][j]+dp[i][j-1];  i>j  dp[i][j] = 0;  如果对某些点有限制,那么在递推是加条件判断. ans = dp[n][m]

hdu Buy the Ticket

1 import java.math.BigInteger; 2 import java.util.*; 3 public class Main { 4 public static void main(String []args) 5 { 6 Scanner cin=new Scanner(System.in); 7 int n,m,i; 8 int t1=0; 9 while(cin.hasNextBigInteger()) 10 { 11 t1++; 12 m=cin.nextInt();

Buy the Ticket(卡特兰数+递推高精度)

Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1886 Accepted Submission(s): 832   Problem Description The \\\\\\\"Harry Potter and the Goblet of Fire\\\\\\\" will be on show i

Buy the Ticket{HDU1133}

Buy the TicketTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6517 Accepted Submission(s): 2720 Problem DescriptionThe "Harry Potter and the Goblet of Fire" will be on show in the next few day

HDU——1133 Buy the Ticket

Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7152    Accepted Submission(s): 2998 Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the nex

Codeforces 938D Buy a Ticket (转化建图 + 最短路)

题目链接  Buy a Ticket 题意   给定一个无向图.对于每个$i$ $\in$ $[1, n]$, 求$min\left\{2d(i,j) + a_{j}\right\}$ 建立超级源点$n+1$, 对于每一条无向边$(x, y, z)$,$x$向$y$连一条长度为$2z$的边,反之亦然. 对于每个$a_{i}$, 从$i$到$n+1$连一条长度为$a_{i}$的边,反之亦然. 然后跑一边最短路即可. #include <bits/stdc++.h> using namespace

Codeforces 938D Buy a Ticket

Buy a Ticket 题意要求:求出每个城市看演出的最小费用, 注意的一点就是车票要来回的. 题解:dijkstra 生成优先队列的时候直接将在本地城市看演出的费用放入队列里, 然后直接跑就好了,  dis数组存的是, 当前情况下的最小花费是多少. 代码: 1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<queue> 5 #include<vector&g

Codeforces 938 D. Buy a Ticket (dijkstra 求多元最短路)

题目链接:Buy a Ticket 题意: 给出n个点m条边,每个点每条边都有各自的权值,对于每个点i,求一个任意j,使得2×d[i][j] + a[j]最小. 题解: 这题其实就是要我们求任意两点的最短路,但是从点的个数上就知道这题不可以用floyd算法,其实多元最短路可以用dijkstra算.@.@!把所有的点的权值和点放到结构体里面,放入优先队列,其实这样就能保证每次拓展到的点就是这个点的最短路(因为是优先队列,保证拓展到的点这时候的值是最小的),其实就是这个点想通就很简单. 1 #inc

Codeforces 938 D. Buy a Ticket

D. Buy a Ticket time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Musicians of a popular band "Flayer" have announced that they are going to "make their exit" with a world tou