H - Hellcife is on fire Gym - 102448H(多源最短路)

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
Note
It is guaranteed that from any city you can reach any other city using some roads.

題意:
一個點找了火,要a[i]秒才能完全燒着。完全燒着以後,沿着邊可以燒其他點,傳播時間爲邊的邊權。求每個點燒完的時間

思路:
和fire那題一樣,就是多源最短路。只不過這題不是無權邊,所以用了優先隊列

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

typedef long long ll;
const int maxn = 2e5 + 7;
int head[maxn],nex[maxn],to[maxn],val[maxn],tot;
ll cost[maxn],a[maxn];
priority_queue<pair<ll,int> >q;

void add(int x,int y,int z) {
    to[++tot] = y;
    nex[tot] = head[x];
    val[tot] = z;
    head[x] = tot;
}

void bfs() {
    while(!q.empty()) {
        int now = q.top().second; q.pop();
        for(int i = head[now];i;i = nex[i]) {
            int v = to[i],w = val[i];
            if(!a[v]) {
                a[v] = cost[v] + a[now] + w;
                q.push({-a[v],v});
            } else {
                if(a[v] > cost[v] + a[now] + w) {
                    a[v] = cost[v] + a[now] + w;
                    q.push({-a[v],v});
                }
            }
        }
    }
}

int main() {
    int n,m,k;scanf("%d%d%d",&n,&m,&k);
    for(int i = 1;i <= m;i++) {
        int x,y,z;scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);add(y,x,z);
    }
    for(int i = 1;i <= n;i++) {
        scanf("%lld",&cost[i]);
    }
    for(int i = 1;i <= k;i++) {
        int x;scanf("%d",&x);
        a[x] = cost[x];
        q.push({-cost[x],x});
    }
    bfs();
    for(int i = 1;i <= n;i++) {
        printf("%lld ",a[i]);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章