Time Limit: 2 sec / Memory Limit: 1024 MB
ps:一道dfs,我暂时还不会写,就看了一下。
Given are positive integers NN , MM , QQ , and QQ quadruples of integers ( aiai , bibi , cici , didi ).
Consider a sequence AA satisfying the following conditions:
Let us define a score of this sequence as follows:
Find the maximum possible score of AA .
Input is given from Standard Input in the following format:
N M Q a1 b1 c1 d1 :: aQ bQ cQ dQ
Print the maximum possible score of A .
3 4 3 1 3 3 100 1 2 2 10 2 3 2 10
When A={1,3,4} , its score is 110 . Under these conditions, no sequence has a score greater than 110, so the answer is 110 .
4 6 10 2 4 1 86568 1 4 0 90629 2 3 0 90310 3 4 1 29211 3 4 3 78537 3 4 2 8580 1 2 1 96263 1 4 2 2156 1 2 0 94325 1 4 3 94328
357500
10 10 1 1 10 9 1
1
#include <bits/stdc++.h> using namespace std; #define ll long long const int mod=1e9+7; int N, M, Q, maxn = -9999999; int a[101], b[101], c[101], d[101]; int flag[11], A[11]; void clear() { int res = 0; for(int i=1;i<=Q;i++) { if(A[b[i]]-A[a[i]]==c[i]) res += d[i]; } if(res>maxn) maxn = res; } void dfs(int k) { for(int i=A[k-1];i<=M;i++) //由于A递增 所以可以从前一个A的元素开始枚举 { if(i!=0) { A[k] = i; if(k==N) { clear(); } else dfs(k+1); A[k] = 0; } } } int main() { ios::sync_with_stdio(0); scanf("%d%d%d", &N, &M, &Q); for(int i=1;i<=Q;i++) { scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]); } dfs(1); printf("%d\n", maxn); return 0; }
原文:https://www.cnblogs.com/asunayi/p/12839595.html