165-C - Many Requirements
Time Limit: 2 sec / Memory Limit: 1024 MB
ps:一道dfs,我暂时还不会写,就看了一下。
Problem Statement
Given are positive integers NN , MM , QQ , and QQ quadruples of integers ( aiai , bibi , cici , didi ).
Consider a sequence AA satisfying the following conditions:
- AA is a sequence of NN positive integers.
- 1≤A1≤A2≤⋯≤AN≤M1≤A1≤A2≤⋯≤AN≤M .
Let us define a score of this sequence as follows:
- The score is the sum of didi over all indices ii such that Abi−Aai=ciAbi−Aai=ci . (If there is no such ii , the score is 00 .)
Find the maximum possible score of AA .
Constraints
- All values in input are integers.
- 2≤N≤10
- 1≤M≤10
- 1≤Q≤50
- 1≤ai<bi≤N ( i=1,2,...,Q )
- 0≤ci≤M−1 ( i=1,2,...,Q)
- (ai,bi,ci)≠(aj,bj,cj) (where i≠j )
- 1≤di≤105 ( i=1,2,...,Q )
Input
Input is given from Standard Input in the following format:
N M Q a1 b1 c1 d1 :: aQ bQ cQ dQ
Output
Print the maximum possible score of A .
Sample Input 1 Copy
Copy
3 4 3 1 3 3 100 1 2 2 10 2 3 2 10
Sample Output 1 Copy
Copy
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 .
Sample Input 2 Copy
Copy
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
Sample Output 2 Copy
Copy
357500
Sample Input 3 Copy
Copy
10 10 1 1 10 9 1
Sample Output 3 Copy
Copy
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; }
浙公网安备 33010602011771号