1. 程式人生 > >洛谷P3265 [JLOI2015]裝備購買 [線性基]

洛谷P3265 [JLOI2015]裝備購買 [線性基]

消元 其他 4th 二進制 pro 16px turn const include

  題目傳送門

裝備購買

格式難調,題面就不放了。


  分析:

  一句話,有$n$件物品,每件物品有$m$個屬性和一個花費值,如果一個裝備的屬性值可以由其他裝備的屬性值改變系數後組合得到那就不買,求購買最多裝備情況下的最小花費。

  這是一道實數線性基的模板,實數線性基和平常常見的二進制線性基區別不大,只是用到了高斯消元的思想來實現,具體還是看代碼吧。

  Code:

//It is made by HolseLee on 4th Oct 2018
//Luogu.org P3265
#include<cmath>
#include<cstdio>
#include
<cstring> #include<iostream> #include<algorithm> using namespace std; const int N=507; const double eps=1e-4; int n,m,ans,cnt,p[N]; struct Node { int cost; double x[N]; bool operator < (const Node x) const { return cost < x.cost; } }a[N]; int
main() { ios::sync_with_stdio(false); cin>>n>>m; for(int i=1; i<=n; ++i) for(int j=1; j<=m; ++j) cin>>a[i].x[j]; for(int i=1; i<=n; ++i) cin>>a[i].cost; sort(a+1,a+n+1); for(int i=1; i<=n; ++i) for(int j=1
; j<=m; ++j) if( fabs(a[i].x[j])>eps ){ if( !p[j] ) { p[j]=i; cnt++; ans+=a[i].cost; break; } else { double t=a[i].x[j]/a[p[j]].x[j]; for(int k=j; k<=m; ++k) a[i].x[k]-=a[p[j]].x[k]*t; } } printf("%d %d\n",cnt,ans); return 0; }

洛谷P3265 [JLOI2015]裝備購買 [線性基]