题目描述:
The kingdom of Hellcife has several cities that are connected by some roads. One day, the king of Hellcife, Bair Jolsonaro, decided to set fire on some of these cities, just for fun. It’s known that the i-th city has a number Ti, indicating that if the fire reaches that city at time X, then at time X+Ti this city will be completely burnt. After a city is completely burnt, the fire begins to spread across all the roads that are connected to this city, with speed of one meter per second.
Your task is simple: you have to find, for each city, the amount of time it takes for the city to be completely burnt, knowing that at time 0 the king Bair Jolsonaro set fire on some cities.
Input
The first line of input contains three numbers N, M and K (1≤N,M,K≤105), indicating the number of cities in Hellcife, the number of roads in Hellcife, and the number of cities Bair Jolsonaro initially set fire, respectively.
Each of the following M lines contains three integers Ai, Bi, and Ci (1≤Ai,Bi≤N, Ai≠Bi, 1≤Ci≤105), indicating that there is a road between cities Ai and Bi with length of Ci meters.
The next line of input contains N numbers Ti (1≤i≤N, 1≤Ti≤105).
The last line of the input contains K numbers Xi (1≤i≤K, 1≤Xi≤N), indicating that the Xi-th city was chosen by Bair Jolsonaro to be set on fire in the beginning.
Output
The output consists of N lines, the i-th of them indicates the amount of time it takes for the i-th city to be completely burnt.
Examples
Input
5 5 1
1 2 1
2 3 4
3 4 5
4 5 10
1 5 13
1 2 3 4 5
1
Output
1
4
11
20
19
Input
5 18 1
1 3 14877
2 1 81271
1 2 50743
5 1 46485
2 5 41563
5 4 72606
1 2 88401
5 3 56633
2 1 25722
3 1 78857
2 3 95527
5 4 66046
1 4 87400
4 2 49102
3 2 3043
5 3 32836
3 2 13703
4 1 86008
31551 94560 84171 16742 55756
2
Output
151833
94560
181774
160404
191879
Input
7 12 2
6 3 61451
3 5 48225
3 6 18732
5 3 86896
1 5 73979
4 3 49294
3 1 2794
1 5 3449
7 2 86351
4 6 59862
2 1 38972
3 7 20293
36685 6614 81280 91835 68491 81662 10505
2 1
Output
36685
6614
120759
261888
108625
221153
103470
分析:多源最短路问题,由于数据较大,图要用链式前向星来存,不熟悉链式前向星可看该博客,用优先队列来存储pair类型数据,pair储存的分别是燃烧到节点的时间以及节点编号。注意用dis[]数组来存燃烧到每个节点的时间时,不能提前初始化为无穷,那样会超时,只能在主函数外定义,这样自动初始为0,然后在优先队列中存的时间为负值。
代码:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 200010
struct node
{
int to;
int next;
int w;
} edge[N];
int head[N];
long long int t[N];
int n,m,k;
int cnt;
void init()
{
for(int i=1; i<=N; i++)
{
head[i]=-1;
}
}
void build(int u,int v,int w)
{
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
long long int dis[N];
priority_queue<pair<long long,int>>que;
void dij()
{
while(!que.empty())
{
int now=que.top().second;
que.pop();
for(int i=head[now];~i;i=edge[i].next)
{
int v=edge[i].to;
int w=edge[i].w;
if(!dis[v])
{
dis[v]=t[v]+dis[now]+w;
que.push({-dis[v],v});
}
else
{
if(dis[v]>t[v]+dis[now]+w)
{
dis[v]=t[v]+dis[now]+w;
que.push({-dis[v],v});
}
}
}
}
}
int main()
{
scanf("%d %d %d",&n,&m,&k);
init();
int u,v,w;
while(m--)
{
scanf("%d %d %d",&u,&v,&w);
build(u,v,w);
build(v,u,w);
}
for(int i=1; i<=n; i++)
{
scanf("%lld",&t[i]);
}
int key;
for(int i=1; i<=k; i++)
{
scanf("%d",&key);
dis[key]=t[key];
que.push({-t[key],key});
}
dij();
for(int i=1;i<=n;i++)
{
printf("%lld\n",dis[i]);
}
return 0;
}