Buy a Ticket (最短路设虚拟节点+Dijkstra优先队列优化)

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 

题意:乐队在每个城市开演唱会,各城市演唱会票价不同。求每个城市的人想要去一次演唱会的最小花费(来回车费+门票费)。

思路:化点权为边权,设一个0虚拟节点,0节点到其他节点的路费为各个节点的门票费,这样问题就转化为0节点到其他n个节点的最短路问题

注意:

1.Dijkstra算法用邻接表写,用邻接矩阵会爆掉(N=200005太大了),痛心

2.图的建立:0节点与其他节点为单向0->n ;其他n个节点之间为双向 

3.     for(int i=1;i<=n;i++)
              dis[i]=Inf;  //Inf=9999999999999999(16个)
        dis[0]=0;

        dis[i]初始化必须用for循环,用memset会出错,因为Inf非常大 

4. for(int i=0;i<=n;i++)
          G[i].clear(); 

每次实例结束都要清空向量

#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cstdio>
using namespace std;
#define Inf 999999999999999999//因为花费数值为ll型 ,设成ll无限大 
typedef long long ll;
const int N=200005;
struct node{
	int v;
	ll w;
	bool operator<(const node other)const
	{
		return w>other.w;
	}
};
int n,m;
ll dis[N];
int vis[N];
vector<node> G[N];
void GetMap()
{
	for(int i=0;i<=n;i++)
	    G[i].clear();
	node t;
	//其他n个节点之间为双向 
	for(int i=0;i<m;i++)
	{
		int a,b;
		ll d;
		cin>>a>>b>>d;
		t.v=b; t.w=2*d;
		G[a].push_back(t);
		t.v=a;
		G[b].push_back(t);
	}
	//0节点与其他节点为单向0->n 
	for(int i=1;i<=n;i++)
	{
		ll d;
		cin>>d;
		t.v=i; t.w=d;
		G[0].push_back(t);
	}
}
void Dijkstra()
{
	priority_queue<node> q;
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;i++)//dis[i]初始化必须用for循环,用memset会出错,因为Inf非常大 
	   dis[i]=Inf;
	dis[0]=0;
	node t;
	t.v=0; t.w=0;
	q.push(t);
	while(!q.empty())
	{
		t=q.top();
		q.pop();
		int v=t.v;
		if(vis[v]) continue;//这个剪枝要有,否则会超时 
		vis[v]=1;
		for(int i=0;i<G[v].size();i++)
		{
			node s=G[v][i];
			if(dis[s.v]>t.w+s.w)
			{
				dis[s.v]=t.w+s.w;
				s.w=dis[s.v];
				q.push(s);
			}
		}
	}
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		GetMap();
		Dijkstra();
		for(int i=1;i<n;i++)
		    cout<<dis[i]<<" ";
		cout<<dis[n]<<endl;
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ksuper&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值