社区讨论
官方AC,民间蛙声一片,咋搞的?求调
P8817[CSP-S 2022] 假期计划参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @lo7hdon1
- 此快照首次捕获于
- 2023/10/27 01:51 2 年前
- 此快照最后确认于
- 2023/10/27 01:51 2 年前
懵了,官方数据AC,民间数据几乎全WA,为啥?
BFS求距离,然后枚举所有1ab,将ab对保存在b为索引的数组中,排序后取前三用,为何不对呢?
CPP#include "bits/stdc++.h"
using namespace std;
const int N=2502,M=10005;
int n,m,k,u,v;
int g[N][N],w[N],cnt[N];
struct node{
int i,w;
}p[N][N];
bool cmp(node a,node b){
return a.w>b.w ;
}
int head[N],cnt1;
struct edge{
int to,next;
}e[M<<1];
bool vis[N];
queue <int> qq;
void bfs(int s){
memset(vis,0,sizeof(vis));
g[s][s]=0;
qq.push(s);
while(!qq.empty()){
int u=qq.front();
qq.pop();
vis[u]=1;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(!vis[v] and g[s][v]>g[s][u]+1 ){
g[s][v]=g[s][u]+1;
qq.push(v);
}
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>m>>k;
memset(g,63,sizeof(g));
for(int i=2;i<=n;i++)cin>>w[i];
for(int i=1;i<=m;i++){
cin>>u>>v;
// g[u][v]=1,g[v][u]=1;
cnt1++;
e[cnt1].to=v;
e[cnt1].next=head[u];
head[u]=cnt1;
cnt1++;
e[cnt1].to=u;
e[cnt1].next=head[v];
head[v]=cnt1;
}
for(int k=1;k<=n;k++){
bfs(k);
}
//枚举所有1ab cd1 的ad,存在另一个点的数对中,只要前三即可
for(int j=2;j<=n;j++){
for(int i=2;i<=n;i++){
if(i!=j and g[1][i]<=k+1 and g[i][j]<=k+1){
cnt[j]++;
p[j][cnt[j]].i=i;
p[j][cnt[j]].w=w[j]+w[i];
}
}
sort(p[j]+1,p[j]+1+cnt[j],cmp);
}
//预处理以上之后,可以把下面这个n四方 变成9n平方
int ans=0;
for(int c=2;c<=n;c++){
for(int b=2;b<=n;b++){
if(c==b or g[c][b]>k+1)continue;
for(int ci=1;ci<=3;ci++){
int d=p[c][ci].i;
if(d==b)continue;
for(int bi=1;bi<=3;bi++){
int a=p[b][bi].i;
if(a==c or a==d)continue;
ans=max(ans,p[c][ci].w+p[b][bi].w);
}
}
}
}
cout<<ans;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...