2 条题解

  • 0
    @ 2025-10-8 17:00:57

    by hansang:

    #include<bits/stdc++.h>
    using namespace std;
    const int N=22;
    typedef long long LL;
    struct node{LL p, a;}; vector<node> G[N]; 
    LL s[N][N], f[(1<<N)];
    bool cmp(node n1, node n2) {return n1.p<n2.p;}
    int main(){
    	int n, m; scanf("%d%d", &n, &m);
    	for(int i=1; i<=m; i++){
    		int k; LL p, a;
    		scanf("%d%lld%lld", &k, &p, &a);
    		G[k].push_back({p, a});
    	}
    	for(int i=1; i<=n; i++) sort(G[i].begin(), G[i].end(), cmp);
    	for(int i=1; i<=n; i++) for(int j=1; j<=n; j++)
    		scanf("%lld", &s[i][j]);
    		
    	memset(f, 0, sizeof(f));
    	for(int i=0; i<(1<<n); i++){
    		int t=0;
    		for(int j=1; j<=n; j++) if(i&(1<<(j-1))) t++;
    		for(int j=1; j<=n; j++) if(i&(1<<(j-1))){
    			f[i]=max(f[i], f[i^(1<<(j-1))]+s[j][t]);
    		}
    		for(auto j: G[t]) if(f[i]>=j.p){
    			f[i]+=j.a;
    		}
    	} 
    	printf("%lld\n", f[(1<<n)-1]);
    	return 0;
    }
    
    • 0
      @ 2025-10-8 17:00:47

      by hansang:

      #include<bits/stdc++.h>
      using namespace std;
      const int N=22;
      typedef long long LL;
      struct node{LL p, a;}; vector<node> G[N]; 
      LL s[N][N], f[(1<<N)];
      bool cmp(node n1, node n2) {return n1.p<n2.p;}
      int main(){
      	int n, m; scanf("%d%d", &n, &m);
      	for(int i=1; i<=m; i++){
      		int k; LL p, a;
      		scanf("%d%lld%lld", &k, &p, &a);
      		G[k].push_back({p, a});
      	}
      	for(int i=1; i<=n; i++) sort(G[i].begin(), G[i].end(), cmp);
      	for(int i=1; i<=n; i++) for(int j=1; j<=n; j++)
      		scanf("%lld", &s[i][j]);
      		
      	memset(f, 0, sizeof(f));
      	for(int i=0; i<(1<<n); i++){
      		int t=0;
      		for(int j=1; j<=n; j++) if(i&(1<<(j-1))) t++;
      		for(int j=1; j<=n; j++) if(i&(1<<(j-1))){
      			f[i]=max(f[i], f[i^(1<<(j-1))]+s[j][t]);
      		}
      		for(auto j: G[t]) if(f[i]>=j.p){
      			f[i]+=j.a;
      		}
      	} 
      	printf("%lld\n", f[(1<<n)-1]);
      	return 0;
      }
      • 1

      USACO(135)动态规划(位向量型)2:全能奶牛P4877 [USACO14FEB] Cow Decathlon G

      信息

      ID
      2338
      时间
      1000ms
      内存
      128MiB
      难度
      9
      标签
      递交数
      10
      已通过
      4
      上传者