#include<bits/stdc++.h>
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define INF 1e18
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define endl '\n'
#define pi 3.14159265359
#define TASKNAME "wildboar"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
typedef pair<ii,ii> iv;
const int MAXN = 550;
vector<iii> g[MAXN];
int plan[MAXN];
int n,m,q,l,u,v,w;
long long dist[MAXN][MAXN][MAXN];
bool spec[MAXN];
void dijkstra(){
memset(dist,0x3f,sizeof(dist));
dist[plan[1]][1][0] = 0;
priority_queue<iv,vector<iv>,greater<iv>> pq;
pq.push({{0,plan[1]},{1,0}});
while(!pq.empty()){
iv tp = pq.top();
int du = tp.fi.fi;
int u = tp.fi.se;
int cur = tp.se.fi;
int cur_edge_id = tp.se.se;
pq.pop();
if (du > dist[u][cur][cur_edge_id]) continue;
for(auto pt: g[u]){
int v = pt.se.fi;
int idedge = pt.fi;
int w = pt.se.se;
if (cur_edge_id == idedge) continue;
if (v == plan[cur+1] and dist[v][cur+1][idedge] > dist[u][cur][cur_edge_id] + w){
dist[v][cur+1][idedge] = dist[u][cur][cur_edge_id] + w;
pq.push({{dist[v][cur+1][idedge],v},{cur+1,idedge}});
}
if (v != plan[cur+1] and dist[v][cur][idedge] > dist[u][cur][cur_edge_id] + w){
dist[v][cur][idedge] = dist[u][cur][cur_edge_id] + w;
pq.push({{dist[v][cur][idedge],v},{cur,idedge}});
}
}
}
long long ans = INF;
for(int i=1;i<=m;i++){
ans = min(ans, dist[plan[l]][l][i]);
}
if (ans == INF) cout<<-1<<endl;
else cout<<ans<<endl;
}
main()
{
fast;
// freopen(TASKNAME".inp","r",stdin);
// freopen(TASKNAME".out","w",stdout);
cin>>n>>m>>q>>l;
memset(spec,false,sizeof(spec));
for(int i=1;i<=m;i++){
cin>>u>>v>>w;
g[u].pb({i,{v,w}});
g[v].pb({i,{u,w}});
}
for(int i=1;i<=l;i++){
cin>>plan[i];
spec[plan[i]] = true;
}
for(int i=1;i<=q;i++){
int p,k;
cin>>p>>k;
spec[plan[p]] = false;
plan[p] = k;
spec[plan[p]] = true;
dijkstra();
}
}
Compilation message
wild_boar.cpp:62:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
62 | main()
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
123 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
123 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
123 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
123 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |