Exemple #1
0
int main() {
	scanf("%d%d%d%d%d%d", &N, &M, &A, &B, &K, &G);
	edge.resize(N + 1);
	arr.resize(G);
	dist.resize(N + 1);
	fill(dist.begin(), dist.end(), INF);
	for (int i = 0; i < G; i++)
		scanf("%d", &arr[i]);
	int a, b, d;
	for (int i = 0; i < M; i++) {
		scanf("%d%d%d", &a, &b, &d);
		edge[a].push_back({ b,d });
		edge[b].push_back({ a,d });
		mat[a][b] = d;
		mat[b][a] = d;
	}
	int sum = 0;
	for (int i = 1; i < G; i++) {
		int start = arr[i - 1];
		int finish = arr[i];
		sum += mat[start][finish];
		gdist[start][finish] = sum;
		gdist[finish][start] = sum;
	}
	Pq pq;
	pq.push({ 0,A });
	dist[A] = 0;
	while (!pq.empty()) {
		int now = pq.top().second;
		int cost = -pq.top().first;
		pq.pop();
		if (dist[now] < cost)
			continue;
		for (auto i : edge[now]) {
			int there = i.first;
			int there_cost = cost + i.second + add(now, there, cost);
			if (dist[there] > there_cost) {
				dist[there] = there_cost;
				pq.push({ -there_cost,there });
			}
		}
	}
	printf("%d\n", dist[B]);
	return 0;
}