Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
34801 swzzzz 【S】T2 C++ 内存超限 50 190 MS 264692 KB 1934 2024-11-14 17:00:43

Tests(5/10):


#include<bits/stdc++.h> using namespace std; #define N 3003 int n,q; pair<int,int>pos[N]; long long dis(int i,int j){ long long tmp1=abs(pos[i].first-pos[j].first), tmp2=abs(pos[i].second-pos[j].second); return tmp1*tmp1*tmp1+tmp2*tmp2*tmp2; } struct edge { int from,to; long long w; }; bool operator<(edge x,edge y){ return x.w>y.w; } long long e[N][N]; int fa[N]; int query(int x){ if(fa[x]==x) return x; fa[x]=query(fa[x]); return fa[x]; } void merge(int x,int y){ x=query(x),y=query(y); if(x==y) return; fa[x]=y; } bool vis[N][N]; long long kruskal(){ memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) fa[i]=i; priority_queue<edge>q; for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ q.push({i,j,e[i][j]}); } } long long ans=0,cnt=0; while (cnt<n-1) { edge tmp=q.top(); q.pop(); if(query(tmp.from)!=query(tmp.to)){ vis[tmp.from][tmp.to]=1; merge(tmp.from,tmp.to); cnt++; ans+=tmp.w; } } return ans; } int main(){ //freopen("logistics.in","r",stdin); //freopen("logistics.out","w",stdout); ios::sync_with_stdio(0); cin>>n>>q; for(int i=1;i<=n;i++) cin>>pos[i].first>>pos[i].second; for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ e[i][j]=dis(i,j); } } long long ans=kruskal(); cout<<ans<<endl; long long u,v,w; for(int i=1;i<=q;i++){ cin>>u>>v>>w; if(u>v) swap(u,v); if(w>=e[u][v]){ cout<<ans<<endl; continue; } if(vis[u][v]){ cout<<ans-e[u][v]+w<<endl; e[u][v]=w; continue; } e[u][v]=w; ans=kruskal(); cout<<ans<<endl; } return 0; }


测评信息: