D. Buy a Ticket
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy
4 2
1 2 4
2 3 7
6 20 1 25
output
6 14 1 25 
input

Copy
3 3
1 2 1
2 3 1
1 3 1
30 10 20
output
12 10 12 
题目大意:每个点有点权a[i],定义d[i][j]为i到j的最短路.对于每一个i,求一个任意的j,使得2*d[i][j] + a[j]最小.
分析:这道题应该属于那种想一会就能想到的题.
   题目让我们求的实际上是多源最短路.怎么求?floyd? 其实可以dijkstra来求.现将所有的点以及点权放到结构体里,并且加入到优先队列中.每扩展到一个点,这个点的答案就被确定了,因为是优先队列,每次都会找路径长度最小的扩展.然后再把这个点能扩展到的点以及路径长度放到优先队列中,不断处理,直到所有点的答案被确定.
这道题利用了dijkstra每次取最短距离的点更新和可以处理多源最短路的特点,以前做过的一道类似的题:hdu6166
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
const ll maxn = ;
ll n,m,head[maxn],to[maxn * ],nextt[maxn * ],w[maxn * ],tot = ,vis[maxn],ans[maxn];
priority_queue <pair<ll,ll>,vector<pair<ll,ll> >,greater<pair<ll,ll> > > q; void add(ll x,ll y,ll z)
{
w[tot] = z;
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} int main()
{
scanf("%I64d%I64d",&n,&m);
for (ll i = ; i <= m; i++)
{
ll a,b,c;
scanf("%I64d%I64d%I64d",&a,&b,&c);
add(a,b,*c);
add(b,a,*c);
}
for (ll i = ; i <= n; i++)
{
ll t;
scanf("%I64d",&t);
q.push(make_pair(t,i));
}
while (!q.empty())
{
pair <ll,ll> u = q.top();
q.pop();
if (vis[u.second])
continue;
vis[u.second] = ;
ans[u.second] = u.first;
for (ll i = head[u.second];i;i = nextt[i])
{
ll v = to[i];
q.push(make_pair(u.first + w[i],v));
}
}
for (ll i = ; i <= n; i++)
printf("%I64d ",ans[i]); return ;
}

Codeforces 938.D Buy a Ticket的更多相关文章

  1. Codeforces 938 D. Buy a Ticket (dijkstra 求多元最短路)

    题目链接:Buy a Ticket 题意: 给出n个点m条边,每个点每条边都有各自的权值,对于每个点i,求一个任意j,使得2×d[i][j] + a[j]最小. 题解: 这题其实就是要我们求任意两点的 ...

  2. Codeforces 938D Buy a Ticket (转化建图 + 最短路)

    题目链接  Buy a Ticket 题意   给定一个无向图.对于每个$i$ $\in$ $[1, n]$, 求$min\left\{2d(i,j) + a_{j}\right\}$ 建立超级源点$ ...

  3. Codeforces 938D Buy a Ticket

    Buy a Ticket 题意要求:求出每个城市看演出的最小费用, 注意的一点就是车票要来回的. 题解:dijkstra 生成优先队列的时候直接将在本地城市看演出的费用放入队列里, 然后直接跑就好了, ...

  4. Buy the Ticket{HDU1133}

    Buy the TicketTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...

  5. 【HDU 1133】 Buy the Ticket (卡特兰数)

    Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on sh ...

  6. 【高精度练习+卡特兰数】【Uva1133】Buy the Ticket

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  7. Buy the Ticket(卡特兰数+递推高精度)

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...

  8. hdu 1133 Buy the Ticket(Catalan)

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. HDUOJ---1133(卡特兰数扩展)Buy the Ticket

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

随机推荐

  1. 高可用Kubernetes集群-8. 部署kube-scheduler

    十.部署kube-scheduler kube-scheduler是Kube-Master相关的3个服务之一,是有状态的服务,会修改集群的状态信息. 如果多个master节点上的相关服务同时生效,则会 ...

  2. Fulfilling Work: The Shippers More entrepreneurs hire 'fulfillment' outfits to store and ship their products

    By Stu Woo June 23, 2011 Brett Teper faced a logistical problem when he and a partner founded ModPro ...

  3. 深度系统(deepin)与win10双系统切换设置

    之前在win10下安装了深度系统,我不知道其他人在双系统切换的时候是否需要更改BIOS参数,我根据我的实际情况给出双系统切换设置的解决方案. 1.开机后进入选项System setup 2.按照下图选 ...

  4. Requests库入门——应用实例-网络图片的爬取与保存(好看的小姐姐≧▽≦)

    在B站学习这一节的时候,弹幕最为激烈,不管大家是出于什么目的都想体验一下网络爬虫爬取图片的魅力,毕竟之前的实例实话说都是一些没有太大作用的信息. 好了,直接上代码: import requests i ...

  5. C#中委托的理解

    请注意,这只是个人关于C#中委托的一点点理解,参考了一些博客,如有不周之处,请指出,谢谢! 委托是一种函数指针,委托是方法的抽象,方法是委托的实例.委托是C#语言的一道坎,明白了委托才能算是C#真正入 ...

  6. “献给爱读书的中国人”——Amazon Kindle软件测评

    “献给爱读书的中国人” ——Amazon Kindle软件测评 前不久我在网上看到了一篇印度工程师旅居上海时发表的一篇文章,题目叫做<令人忧虑:不阅读的中国人>,大致讲述的是世界上人们在飞 ...

  7. Java compiler level does not match the version of the installed Java project facet. map解决方法

    右键项目"Properties",在弹出的"Properties"窗口左侧,单击"Project Facets",打开"Proje ...

  8. Java对象创建过程补遗

    一.static修饰的东东是属于这个类的,是所有的该类的实例共享的,因此它们的初始化先于实例对象的初始化. 二.Java中没有静态构造方法,但是有静态代码块.当类中同时存在静态代码块和静态成员变量声明 ...

  9. 运维堡垒机----Gateone

    简介: 运维堡垒机的理念起源于跳板机.2000年左右,高端行业用户为了对运维人员的远程登录进行集中管理,会在机房里部署跳板机.跳板机就是一台服务器,维护人员在维护过程中,首先要统一登录到这台服务器上, ...

  10. Android Studio -导入项目 gradle处理

    如果导入 android studio 项目, 那么一定要注意 需要合适的gradle版本,具体方法为: 首先导入步骤: 打开android studio ==> File ==> New ...