QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#297684#7753. Energy Distributionucup-team139Compile Error//C++233.2kb2024-01-04 22:39:262024-01-04 22:39:26

Judging History

你现在查看的是最新测评结果

  • [2024-10-31 10:22:30]
  • hack成功,自动添加数据
  • (/hack/1089)
  • [2024-01-04 22:39:26]
  • 评测
  • [2024-01-04 22:39:26]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

typedef long double T; //oppure modular<mod>
typedef vector<T> vd;
const T eps = 1e-3;
vi col; //globale per ricavare il sottospazio
int solveLinear(vector<vd>& A, vd& b, vd& x) {
    int n = sz(A), m = sz(x), rank = 0, br, bc;
    if (n) assert(sz(A[0]) == m);
    col.resize(m); iota(all(col), 0);
    rep(i,0,n) {
        T v, bv = 0;
        rep(r,i,n) rep(c,i,m)
        if ((v = fabs(A[r][c])) > bv)
            br = r, bc = c, bv = v;
        if (bv <= eps) {
            rep(j,i,n) if (fabs(b[j]) > eps) return -1;
            break;
        }
        swap(A[i], A[br]); swap(b[i], b[br]);
        swap(col[i], col[bc]);
        rep(j,0,n) swap(A[j][i], A[j][bc]);
        bv = T(1)/A[i][i];
        rep(j,i+1,n) {
            T fac = A[j][i] * bv;
            b[j] -= fac * b[i];
            rep(k,i+1,m) A[j][k] -= fac*A[i][k];
        }
        rank++;
    }
    x.assign(m, T(0));
    for (int i = rank; i--;) {
        b[i] = b[i]/A[i][i];
        x[col[i]] = b[i];
        rep(j,0,i) {
            b[j] -= A[j][i] * b[i];
        }
    }
    return rank; // (multiple solutions if rank < m)
}

void solve(int t){
    int n;
    cin>>n;
    
    vector w(n,vector(n,0));
    for(auto &i : w)for(auto &j : i)cin>>j;
    
    T ans = 0.0;
    for(int mask=1;mask<(1<<n);mask++){
        bool ok=true;
        vector<int> nodi;
        
        for(int j=0;j<n;j++){
            if(mask&(1<<j))nodi.push_back(j);
        }
        
        for(auto j : nodi){
            for(auto k : nodi){
                if(j==k)continue;
                assert(w[j][k]==w[k][j]);
                if(w[j][k]==0)ok=false;
            }
        }
        
        if(!ok)continue;
        
        vector<vd> A;
        vd B,x;
        
        x.resize(nodi.size());
        for(auto a : nodi){
            for(auto b : nodi){
                if(a==b)continue;
                
                vd tmp;
                for(auto i : nodi){
                    if(i==a)tmp.push_back(-w[a][b]);
                    else if(i==b)tmp.push_back(w[a][b]);
                    else tmp.push_back(w[a][i]-w[b][i]);
                }
                
                A.push_back(tmp);
                B.push_back(0);
            }
        }
        
        A.push_back(vd(nodi.size(),1));
        B.push_back(1);
        
        int rank = solveLinear(A,B,x);
        if(rank==-1)continue;
        assert(rank==(int)x.size());
        
        T curr=0;
        for(int i=0;i<(int)x.size();i++){
            if(x[i]<=-EPS){
                curr=0.0;
                break;
            }
            for(int j=0;j<i;j++){
                curr+=x[i]*x[j]*w[nodi[i]][nodi[j]];
            }
        }
        ans=max(ans,curr);
    }
    
    cout<<setprecision(20)<<ans<<"\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int t=1;
    //cin>>t;
    for(int i=1;i<=t;i++)solve(i);
    
    return 0;
}

Details

answer.code: In function ‘void solve(int)’:
answer.code:105:23: error: ‘EPS’ was not declared in this scope
  105 |             if(x[i]<=-EPS){
      |                       ^~~