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 int long long
#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;
const int MAXN = 501;
vector<iii> g[MAXN];
int plan[MAXN];
int n,m,q,l,u,v,w,dist[MAXN][MAXN][MAXN];
bool spec[MAXN];
void dijkstra(){
memset(dist,0x3f,sizeof(dist));
dist[plan[1]][1][0] = 0;
priority_queue<array<int,4>,vector<array<int,4>>,greater<array<int,4>>> pq;
pq.push({0,plan[1],1,0});
while(!pq.empty()){
array<int,4> tp = pq.top();
int du = tp[0];
int u = tp[1];
int cur = tp[2];
int cur_edge_id = tp[3];
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});
}
}
}
int 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 (stderr)
wild_boar.cpp:61:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
61 | main()
| ^~~~
# | 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... |