This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(5e3)+7;
const ll inf = ll(1e18)+7;
int n , m , k , q , en[maxN] , c[5];
ll dp[maxN][1<<5] , d[maxN];
vector<ii> g[maxN] , rev_g[maxN];
vector<int> nxt[maxN] , topo;
bool vis[maxN];
void dfs(int u){
vis[u] = 1;
for (int v : nxt[u]){
if (vis[v] == 0){
dfs(v);
}
}
topo.push_back(u);
}
void reset(){
for (int u = 0 ; u < n ; u++){
d[u] = inf;
nxt[u].clear();
vis[u] = 0;
for (int mask = 0 ; mask < (1<<5) ; mask++) dp[u][mask] = inf;
}
topo.clear();
}
void solve(){
cin >> n >> m >> k;
for (int i = 0 ; i < k ; i++){
cin >> en[i];
}
for (int i = 0 ; i < m ; i++){
int u , v , w;
cin >> u >> v >> w;
g[u].push_back({v , w});
rev_g[v].push_back({u , w});
}
cin >> q;
while (q--){
int st; cin >> st;
for (int i = 0 ; i < 5 ; i++) cin >> c[i];
reset();
d[st] = 0;
priority_queue<lli , vector<lli> , greater<lli>> pq;
pq.push({d[st] , st});
while (pq.empty() == 0){
auto it = pq.top(); pq.pop();
int u = it.se;
if (d[u] != it.fi) continue;
for (auto e : g[u]){
int v = e.fi;
int w = e.se;
if (d[v] > d[u] + 1ll * w){
d[v] = d[u] + 1ll * w;
pq.push({d[v] , v});
}
}
}
for (int u = 0 ; u < n ; u++){
for (auto e : g[u]){
int v = e.fi;
int w = e.se;
if (d[u] + 1ll * w == d[v]){
nxt[u].push_back(v);
}
}
}
dfs(st);
reverse(all(topo));
dp[st][0] = 0;
for (int v : topo){
for (auto e : rev_g[v]){
int u = e.fi;
int w = e.se;
for (int mask = 0 ; mask < (1<<5) ; mask++){
dp[v][mask] = min(dp[v][mask] , dp[u][mask] + 1ll * w);
for (int i = 0 ; i < 5 ; i++){
if ((mask>>i)&1) continue;
if (c[i] == -1) continue;
dp[v][mask ^ (1<<i)] = min(dp[v][mask ^ (1<<i)] , dp[u][mask] + 1ll * c[i] + (1ll * (10 - i - 1) * w) / 10);
}
}
}
}
ll ans = inf;
for (int i = 0 ; i < k ; i++){
for (int mask = 0 ; mask < (1<<5) ; mask++){
ans = min(ans , dp[en[i]][mask]);
}
}
if (ans == inf) ans = -1;
cout << ans << "\n";
}
}
#define name "A"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | freopen(name".INP" , "r" , stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | freopen(name".OUT" , "w" , stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |