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;
}

全部评论

相关推荐

尊嘟假嘟点击就送:加v细说,问题很大
点赞 评论 收藏
分享
10-15 16:27
门头沟学院 C++
LeoMoon:建议问一下是不是你给他付钱😅😅
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务