#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll nx = 5e3+5, inf = 4e18;
ll n,e,k,q,t[nx],p[6],dist[nx][1<<5];
vector<pair<ll,ll>> adj[nx]; priority_queue<tuple<ll,ll,ll>, vector<tuple<ll,ll,ll>>, greater<tuple<ll,ll,ll>>> pq;
ll Dijkstra(ll p[])
{
for(int i = 0; i < n; i++) for(int j = 0; j < 32; j++) dist[i][j] = inf;
while(!pq.empty()) pq.pop();
int start = p[0];
if(start < 0) return -1;
dist[start][0] = 0;
pq.push({0,start,0});
while(!pq.empty())
{
auto [d, u, state] = pq.top();
pq.pop();
if(d > dist[u][state]) continue;
for(auto [v, w] : adj[u])
{
if(w != inf && d + w < dist[v][state])
{
dist[v][state] = d+w;
pq.push({dist[v][state], v, state});
}
for(int i = 1; i <= 5; i++)
{
if((1 << (i-1)) & state) continue;
if(p[i] == -1) continue;
ll nw = state | (1<<(i-1));
if(w != inf && w * (10 - i) / 10 + d + p[i] < dist[v][nw])
{
dist[v][nw] = w * (10 - i) / 10 + d + p[i];
pq.push({dist[v][nw], v, nw});
}
}
}
}
ll res = inf;
for(int i = 0; i < k; i++)
{
ll node = t[i];
for(int j = 0; j < (1<<5); j++)
{
res = min(res, dist[t[i]][j]);
}
}
return (res == inf ? -1 : res);
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>e>>k;
for(int i = 0; i < k; i++) cin>>t[i];
for(int i = 0; i < e; i++)
{
ll u,v,w; cin>>u>>v>>w;
adj[u].push_back({v,w});
}
cin>>q;
for(int i = 0; i < q; i++)
{
cin>>p[0]>>p[1]>>p[2]>>p[3]>>p[4]>>p[5];
cout<<Dijkstra(p)<<"\n";
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |