제출 #1045499

#제출 시각아이디문제언어결과실행 시간메모리
1045499vjudge1Evacuation plan (IZhO18_plan)C++17
100 / 100
325 ms57148 KiB
#include<bits/stdc++.h> #define maxn 10000007 #define N 100005 #define mod 111539786 #define BIT(x, i) ((x >> i) & 1) #define er erase #define ll long long #define fuck make_pair #define meme(a,b) memset(a,b,sizeof(a)) #define faster ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define sz(a) (int) a.size() #define fot(i,a,b) for(int i = a ; i <= b ; i++) #define super_fot(i,a,b,j) for(int i = a ; i <= b ; i+=j) #define fort(i,a,b) for(int i = a ; i < b ; i++) #define fod(i,b,a) for(int i = b ; i >= a ; i--) #define super_fod(i,b,a,j) for(int i = b ; i >= a ; i-=j) #define fodt(i,b,a) for(int i = b ; i > a ; i--) #define lb lower_bound #define ub upper_bound using namespace std; const ll oo = 1e18; ll n, m, x, y, w, k, q; ll a[N], p[N], parent[N], s[N], id[N]; vector<pair<ll,ll>>adj[N]; set<ll>sz[N]; ll dist[N]; bool mark[N]; bool cmp(ll a, ll b) { return dist[a] > dist[b]; } void dijstra() { fot(i,1,n) { dist[i] = oo; } priority_queue<pair<ll,ll>,vector<pair<ll,ll>>, greater<pair<ll,ll>> >q; fot(i,1,k) { dist[a[i]]=0; q.push({0,a[i]}); } while(!q.empty()) { ll u = q.top().second; q.pop(); if(mark[u]) continue; mark[u] = true; for(auto v: adj[u]) { ll it = v.first; ll w = v.second; if(dist[it] > dist[u] + w) { dist[it] = dist[u] + w; q.push({dist[it],it}); } } } } ll find_set(ll u) { if(u==parent[u]) return u; return parent[u] = find_set(parent[u]); } void union_set(ll u, ll v) { u = find_set(u); v = find_set(v); if(u==v) return ; if(s[u]<s[v]) swap(u,v); parent[v] = u; s[u]+=s[v]; for(auto it: sz[v]) { if(sz[u].find(it)!=sz[u].end()) { sz[u].erase(it); p[it]=w; } else { sz[u].insert(it); } } } void nmd() { cin>>n>>m; while(m--) { cin>>x>>y>>w; adj[x].push_back({y,w}); adj[y].push_back({x,w}); } cin>>k; fot(i,1,k) { cin>>a[i]; } dijstra(); cin>>q; fot(i,1,q) { cin>>x>>y; sz[x].insert(i); sz[y].insert(i); } fot(i,1,n) { id[i] = i ; parent[i] = i; s[i] = i; } sort(id+1,id+n+1,cmp); memset(mark,0,sizeof(mark)); fot(i,1,n) { x = id[i]; mark[x] = true; w = dist[x]; for(auto it : adj[x]) { y = it.first; if(mark[y]) { union_set(x,y); } } } fot(i,1,q) { cout<<p[i]<<"\n"; } } int main(){ faster; nmd(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...