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];
vector<ii> g[maxN];
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});
}
cin >> q;
while (q--){
int st; cin >> st;
for (int i = 0 ; i < 5 ; i++) cin >> c[i];
for (int i = 0 ; i < n ; i++){
for (int mask = 0 ; mask < (1<<5) ; mask++) dp[i][mask] = inf;
}
dp[st][0] = 0;
priority_queue<pair<ll , ii> , vector<pair<ll , ii>> , greater<pair<ll , ii>>> pq;
pq.push({dp[st][0] , {st , 0}});
while (pq.empty() == false){
auto it = pq.top(); pq.pop();
int u = it.se.fi;
int mask = it.se.se;
if (dp[u][mask] != it.fi) continue;
for (auto e : g[u]){
int v = e.fi;
int w = e.se;
if (dp[v][mask] > dp[u][mask] + 1ll * w){
dp[v][mask] = dp[u][mask] + 1ll * w;
pq.push({dp[v][mask] , {v , mask}});
}
for (int i = 0 ; i < 5 ; i++){
if ((mask>>i)&1) continue;
if (c[i] == -1) continue;
if (dp[v][mask ^ (1<<i)] > dp[u][mask] + 1ll * c[i] + (1ll * (10 - i - 1) * w) / 10){
dp[v][mask ^ (1<<i)] = dp[u][mask] + 1ll * c[i] + (1ll * (10 - i - 1) * w) / 10;
pq.push({dp[v][mask ^ (1<<i)] , {v , (mask ^ (1<<i))}});
}
}
}
}
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:78:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(name".INP" , "r" , stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:79:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | 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... |