QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#297671#7753. Energy Distributionucup-team139WA 1ms3784kbC++233.1kb2024-01-04 22:26:212024-01-04 22:26:21

Judging History

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

  • [2024-10-31 10:22:30]
  • hack成功,自动添加数据
  • (/hack/1089)
  • [2024-01-04 22:26:21]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3784kb
  • [2024-01-04 22:26:21]
  • 提交

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 double T; //oppure modular<mod>
typedef vector<T> vd;
const double eps = 1e-12;
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;
    
    double 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);
        assert(rank==(int)x.size());
        
        double curr=0;
        for(int i=0;i<(int)x.size();i++){
            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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3784kb

input:

2
0 1
1 0

output:

0.25

result:

ok found '0.2500000', expected '0.2500000', error '0.0000000'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3776kb

input:

3
0 2 1
2 0 2
1 2 0

output:

0.57142857142857139685

result:

ok found '0.5714286', expected '0.5714290', error '0.0000004'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3768kb

input:

3
0 1 2
1 0 1
2 1 0

output:

0.5

result:

ok found '0.5000000', expected '0.5000000', error '0.0000000'

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 3764kb

input:

4
0 3 1 0
3 0 1 0
1 1 0 2
0 0 2 0

output:

1

result:

wrong answer 1st numbers differ - expected: '0.7500000', found: '1.0000000', error = '0.2500000'