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>
using namespace std;
typedef long long ll;
const int MX=505;
int N,M,Q;
set<array<int,3>> adj[MX];
const int inf=2e9;
pair<int,int> dist[MX];
pair<int,int> bfs(int S, int T) {
queue<int> q;
q.push(S);
for(int i=1;i<=N;i++) dist[i]={-1,-1};
dist[S]={inf,inf};
while(!q.empty()) {
auto v=q.front(); q.pop();
if(v==T) break;
for(auto [u,w,ii]:adj[v]) {
if(dist[u].first!=-1) continue;
dist[u]=min(make_pair(w,ii),dist[v]);
q.push(u);
}
}
return dist[T];
}
pair<int,int> BFS(int S, int T) {
queue<int> q;
q.push(S);
for(int i=1;i<=N;i++) dist[i]={-1,-1};
dist[S]={0,0};
while(!q.empty()) {
auto v=q.front(); q.pop();
if(v==T) break;
for(auto [u,w,ii]:adj[v]) {
if(dist[u].first!=-1) continue;
dist[u]=max(make_pair(w,ii),dist[v]);
q.push(u);
}
}
return dist[T];
}
array<int,3> e[100000+5];
set<array<int,4>> E;
int L[100000+5], R[100000+5];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>N>>M;
for(int i=0;i<M;i++) {
int u,v,w;
cin>>u>>v>>w;
e[i]={w,u,v};
}
sort(e,e+M);
for(int i=0;i<M;i++) {
auto [w,u,v]=e[i];
auto [ww,ii]=bfs(u,v);
auto [zz,uu,vv]=e[ii];
if(ww!=-1) {
adj[uu].erase({vv,ww,ii});
adj[vv].erase({uu,ww,ii});
E.erase({ww,uu,vv,ii});
}
adj[u].insert({v,w,i});
adj[v].insert({u,w,i});
E.insert({w,u,v,i});
if(ww!=-1)
L[i]=(w+ww),assert(ww<=w);
else
L[i]=0;
}
E.clear();
for(int i=1;i<=N;i++) adj[i].clear();
for(int i=M-1;i>=0;i--) {
auto [w,u,v]=e[i];
auto [ww,ii]=BFS(u,v);
auto [zz,uu,vv]=e[ii];
if(ww!=-1) {
adj[uu].erase({vv,ww,ii});
adj[vv].erase({uu,ww,ii});
E.erase({ww,uu,vv,ii});
}
adj[u].insert({v,w,i});
adj[v].insert({u,w,i});
E.insert({w,u,v,i});
if(ww!=-1)
R[i]=(w+ww),assert(ww>=w);
else
R[i]=2e9+1;
}
vector<pair<int,int>> add, del;
for(int i=0;i<M;i++) {
add.push_back({L[i],i});
del.push_back({R[i],i});
}
sort(add.begin(),add.end());
sort(del.begin(),del.end());
int pa=-1,pd=-1;
set<int> curEdges;
cin>>Q;
for(int i=0;i<Q;i++) {
ll x;
cin>>x;
while(pa+1<add.size() && add[pa+1].first<=2*x) {
curEdges.insert(add[pa+1].second);
pa++;
}
while(pd+1<del.size() && del[pd+1].first<=2*x) {
curEdges.erase(del[pd+1].second);
pd++;
}
ll res=0;
for(auto y:curEdges) res+=abs(e[y][0]-x);
cout<<res<<'\n';
}
}
Compilation message (stderr)
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:139:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | while(pa+1<add.size() && add[pa+1].first<=2*x) {
| ~~~~^~~~~~~~~~~
reconstruction.cpp:144:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
144 | while(pd+1<del.size() && del[pd+1].first<=2*x) {
| ~~~~^~~~~~~~~~~
# | 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... |