-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathADATRIP.cpp
55 lines (55 loc) · 1.6 KB
/
ADATRIP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Ivan Carvalho
// Solution to https://www.spoj.com/problems/ADATRIP/
#include <cstdio>
#include <stack>
#include <vector>
#define MAXN 500010
#define MP make_pair
using namespace std;
typedef pair<int, int> ii;
vector<ii> grafo[MAXN];
stack<int> pilha[10 * MAXN];
int processado[MAXN], atual, qtd, tamanho, n, m, q, respatual, respqtd;
int main() {
scanf("%d %d %d", &n, &m, &q);
for (int i = 1; i <= m; i++) {
int u, v, peso;
scanf("%d %d %d", &u, &v, &peso);
grafo[u].push_back(MP(v, peso));
grafo[v].push_back(MP(u, peso));
}
for (int vez = 1; vez <= q; vez++) {
int ini;
scanf("%d", &ini);
pilha[0].push(ini);
tamanho = 1;
atual = 0;
qtd = 0;
respatual = 0;
respqtd = 0;
while (tamanho > 0) {
while (pilha[atual].empty()) {
atual++;
qtd = 0;
}
while (!pilha[atual].empty()) {
int v = pilha[atual].top();
pilha[atual].pop();
tamanho--;
if (processado[v] == vez) continue;
processado[v] = vez;
respatual = atual;
qtd++;
respqtd = qtd;
for (int i = 0; i < grafo[v].size(); i++) {
int u = grafo[v][i].first, peso = grafo[v][i].second;
if (processado[u] == vez) continue;
pilha[atual + peso].push(u);
tamanho++;
}
}
}
printf("%d %d\n", respatual, respqtd);
}
return 0;
}