Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
51613 heaksicn 最优子图 C++ 解答错误 40 211 MS 2216 KB 1793 2022-07-13 12:16:03

Tests(8/20):


#include<bits/stdc++.h> #define ll long long using namespace std; inline int read(){ int X=0,w=0; char ch=0; while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } inline void write(ll x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } int n; ll k; ll w[501][501]; ll ans; int x[101],y[101]; void dfs(int t,int ctx,int cty){ if(t>n){ ll res=0; for(int i=1;i<=ctx;i++){ for(int j=1;j<=cty;j++){ res+=k-w[x[i]][y[j]]; } } for(int i=1;i<=ctx;i++){ for(int j=i+1;j<=ctx;j++){ res+=w[x[i]][x[j]]; } } for(int i=1;i<=cty;i++){ for(int j=i+1;j<=cty;j++){ res+=w[y[i]][y[j]]; } } ans=max(ans,res); // if(res==139){ // for(int i=1;i<=ctx;i++) cout<<x[i]<<" "; // puts(""); // for(int i=1;i<=cty;i++) cout<<y[i]<<" "; // puts(""); // } return; } if(ctx!=n-1){ x[ctx+1]=t; dfs(t+1,ctx+1,cty); } if(cty!=n-1){ y[cty+1]=t; dfs(t+1,ctx,cty+1); } } void subtask1(){ dfs(1,0,0); write(ans); exit(0); } void subtask2(){ cout<<(n-1)*(n-2)/2; exit(0); } void solution(){ for(int i=1;i<=n;i++){ ll res=0; for(int j=1;j<=n;j++){ if(i==j) continue; for(int x=1;x<=n;x++){ if(x==i||x==j) continue; res+=w[j][x]; } } ll tp=res; for(int j=1;j<=n;j++){ if(i==j) continue; for(int x=1;x<=n;x++){ if(x==i||x==j) continue; res+=(k-w[j][x]); } } ans=max(ans,res); } write(ans); exit(0); } int main(){ n=read(); cin>>k; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++) cin>>w[i][j]; } if(k==1){ subtask2(); } if(n<=20){ subtask1(); }else{ solution(); } return 0; }


测评信息: