ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

buy a ticket

2020-05-27 14:02:54  阅读:378  来源: 互联网

标签:Node city buy ll coins th ticket dis


题目

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 v i to city u i (and from u i to v i), and it costs w i coins to use this route.

Each city will be visited by "Flayer", and the cost of the concert ticket in i-th city is a i 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 img you have to calculate img, 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.

输入

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 v i, u i and w i (1 ≤ v i, u i ≤ n, v i ≠ u i, 1 ≤ w i ≤ 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 a 1, a 2, ... a k (1 ≤ a i ≤ 1012) — price to attend the concert in i-th city.

输出

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).

样例输入1

4 2
1 2 4
2 3 7
6 20 1 25

样例输出1

6 14 1 25 

样例输入2

3 3
1 2 1
2 3 1
1 3 1
30 10 20

样例输出2

12 10 12 

分析

其实这个题还是比较简单的,有点类似挖水井的那道题

我们建一个超级源点,把每个点的点权作为超级源点和该点之间的边权,把原图的边权乘以,再跑最短路就行了,最后输出答案。

代码

/*************************************************************************
	> File Name: h.cpp
	> Author: LiuGeXian
	> Mail: 1019630230@qq.com 
	> Created Time: 2020/5/27 7:15:52
 ************************************************************************/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const ll INF = 1e12 + 10;
struct Node{
	ll pos, dis;
	Node(){}
	Node(ll x, ll y){
		pos = x;
		dis = y;
	}
	bool operator < (const Node &a)const{
		return dis > a.dis;
	}
};
struct Edge{
	ll to, dis, next;
}edge[maxn << 2];
int n, m, vis[maxn], cnt, head[maxn];
ll dis[maxn];
void Add(ll u, ll v, ll w){
	edge[++cnt].to = v;
	edge[cnt].next = head[u];
	edge[cnt].dis = w;
	head[u] = cnt;
}
void Dij(ll s){
	priority_queue<Node> q;
	q.push(Node(s, 0));
	for (int i = 1; i <= n; i++) dis[i] = INF;
	dis[s] = 0;
	while (q.size()){
		ll d = q.top().dis;
		ll p = q.top().pos;
		q.pop();
		if (vis[p]) continue;
		vis[p] = 1;
		for (int i = head[p]; i; i = edge[i].next){
			int v = edge[i].to;
			if (dis[v] > d + edge[i].dis){
				dis[v] = d + edge[i].dis;
				q.push(Node(v, dis[v]));
			}
		}
	}
}
int main(){
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; i++){
		ll u, v, w;
		scanf("%lld%lld%lld", &u, &v, &w);
		Add(u, v, w << 1);
		Add(v, u, w << 1);
	}
	for (int i = 1; i <= n; i++){
		ll x;
		scanf("%lld", &x);
		Add(0, i, x);
		Add(i, 0, x);
	}
	Dij(0);
	for (int i = 1; i <= n; i++)
		printf("%lld ", dis[i]);
	return 0;
}

标签:Node,city,buy,ll,coins,th,ticket,dis
来源: https://www.cnblogs.com/ghosh/p/12972567.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有