CF 38-D Buy a Ticket(SPFA)

http://codeforces.com/contest/938/problem/D

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

 

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 

 

思路:spfa算法,建一个源点,每个点都与源点连接,然后直接跑spfa就可以了,不过中间用优先队列来控制点的优先顺序就比较优秀,花费多的点一定是要么在自己城市,要么去到更廉价的城市,而且若A城市到更廉价的B城市,则中间遍历的城市,一定也会到最廉价的B城市,

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 200005
#define inf ((1ll<<60-1))
int n,m,x,y,vis[maxn];
ll z,ans[maxn],dis[maxn];
priority_queue<pair<ll,int> > t;//优先队列,大的在前,加负号,绝对值小的在前,满足最小
vector<pair<int,ll> >q[maxn];
void work(int len,int st)
{
	for(int i=1;i<len;i++)
		dis[i]=inf;
	t.push(make_pair(dis[st]=0,st));
	while(t.empty()==0)
	{
		int now=t.top().second;
		t.pop();vis[now]=0;
		for(int i=0;i<q[now].size();i++)
		{
			int v=q[now][i].first;
			ll c=q[now][i].second;
			if(dis[v]>dis[now]+c)//v的花费是dis[v],v到now去看的花费是 now的最小花费+距离的2倍,有优先队列控制由小到大考虑的,所以可以相当于到相邻城市,一个一个考虑的,是合格的。
			{
				dis[v]=dis[now]+c;
				if(!vis[v])
					vis[v]=1,t.push(make_pair(-dis[v],v));
			}
		}
	}
}
int main(void)
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%lld",&x,&y,&z);
		q[x].push_back(make_pair(y,2ll*z));
		q[y].push_back(make_pair(x,2ll*z));
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&z);
		q[n+1].push_back(make_pair(i,z));
	}
	work(n+1,n+1);
	for(int i=1;i<=n;i++)
		printf("%lld ",dis[i]);
	printf("\n");
	return 0;
}
/*
4 2
1 2 4
2 3 7
6 20 1 25
*/

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值