1. 程式人生 > >Buy a Ticket(多源最短路轉單源最短路)(Dijkstra)

Buy a Ticket(多源最短路轉單源最短路)(Dijkstra)

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 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
題意:有n個城市,m條路(雙向聯通),在每個城市將會有一場精彩的演唱會,每個城市的票價不一樣,每條路的路費不一樣,你在每個城市都有一個朋友,他們都想看演唱會,求每個朋友的花費最小值(票價+來回路費)
思路:多源最短路,Floyd演算法肯定不行,時間複雜度太高
建立一個獨立源點,將點權轉化為與這個點連線的邊權,把多源最短路問題轉化為單源最短路問題,用Dijkstra演算法。
我們可以把路費看做兩點的距離(儲存時乘2儲存,把來回轉化為單向),點權看做該點到自己建的那個點的距離,最後求的就是自己建的那個點到1~n這n個點的最短路(反向思考)

#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
const int maxn=2e5+10;
const long long Inf=999999999999999999;//很不專業的無窮大
struct node{//鄰接表存圖用
	int v;//後繼結點
	long long w;//權值
	node(){}
	node(int vv,long long ww){
         v=vv;
	     w=ww;
	}
};
struct Node{//優先順序佇列用
	int u;//前驅結點
	long long w;
	Node(){}
	Node(int uu,long long ww){
		u=uu;
		w=ww;
	}
	bool operator<(const Node other)const{
	     return w>other.w;  
	}
};
long long dis[maxn];
vector<node>G[maxn];//鄰接表
int n,m;
int vis[maxn];//標記陣列
void dj(){                  //Dijkstra             
	priority_queue<Node>q;
	for(int i=0;i<maxn;i++){//初始化
		dis[i]=Inf;//一開始初始化為無窮大
		vis[i]=0;//標記清零
	}
	dis[0]=0;//0->0的距離為0
	q.push(Node(0,dis[0]));
	while(!q.empty()){
		Node temp=q.top();
		q.pop();
		int u=temp.u;
		if(vis[u]) continue;
		vis[u]=1;
		for(int i=0;i<G[u].size();i++){
			node tmp=G[u][i];
			int v=tmp.v;
			long long w=tmp.w;
			if(dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				q.push(Node(v,dis[v]));
			}
		}
	}
}
int main(){
	scanf("%d%d",&n,&m);
	int u,v;
	long long w;
	for(int i=1;i<=m;i++){
		scanf("%d%d%lld",&u,&v,&w);
		G[u].push_back(node(v,w*2));//w*2,把來回變作單向
		G[v].push_back(node(u,w*2));
	}
	for(int i=1;i<=n;i++){
		scanf("%lld",&w);
		G[0].push_back(node(i,w));
	}
	dj();
	for(int i=1;i<n;i++)
		printf("%lld ",dis[i]);
	printf("%lld\n",dis[n]);
	return 0;
}