Pagini recente » Cod sursa (job #98674) | Cod sursa (job #983236) | Cod sursa (job #399447) | Cod sursa (job #3236575) | Cod sursa (job #2693423)
#include <iostream>
#include <algorithm>
#include <fstream>
#include <cstring>
#include <vector>
#include <set>
using namespace std;
const int NMAX = 100005;
const int INF = 0x3f3f3f3f;
vector<pair<int, int>> G[NMAX];
int muchie;
long long dist[NMAX];
bool isEqual(const pair<int, int>& element)
{
return element.first == muchie;
}
int main()
{
ifstream fin("tempest.in");
ofstream fout("tempest.out");
int teste;
fin >> teste;
while(teste > 0)
{
int n, m;
fin >> n >> m;
int source, destination;
fin >> source >> destination;
for (int i = 0; i < m; ++i)
{
int from, to, cost;
fin >> from >> to >> cost;
G[from].push_back(make_pair(to, cost));
G[to].push_back(make_pair(from, cost));
}
memset(dist, INF, sizeof dist);
int n_muchii;
long long d_max = 0;
fin >> n_muchii;
int start = source;
for(int i=0; i<n_muchii; i++)
{
fin >> muchie;
//cout<<muchie;
vector<pair<int, int>>::iterator st = find_if(G[start].begin(), G[start].end(), isEqual);
d_max += G[start][st - G[start].begin()].second;
start = muchie;
}
dist[destination] = 0;
set<pair<int, int>> h;
h.insert(make_pair(0, destination));
while (!h.empty())
{
int node = h.begin()->second;
int d = h.begin()->first;
h.erase(h.begin());
for (vector<pair<int, int>>::iterator it = G[node].begin(); it != G[node].end(); ++it)
{
int to = it->first;
int cost = it->second;
if (dist[to] > dist[node] + cost)
{
if (dist[to] != INF)
{
h.erase(h.find(make_pair(dist[to], to)));
}
dist[to] = dist[node] + cost;
h.insert(make_pair(dist[to], to));
}
}
}
for (int i = 1; i <= n; ++i)
{
if(dist[i] <= d_max)
fout << i << ' ';
}
fout << '\n';
teste--;
}
return 0;
}