QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#297671 | #7753. Energy Distribution | ucup-team139 | WA | 1ms | 3784kb | C++23 | 3.1kb | 2024-01-04 22:26:21 | 2024-01-04 22:26:21 |
Judging History
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'