Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 300300 points
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≤102≤N≤10
- 1≤M≤101≤M≤10
- 1≤Q≤501≤Q≤50
- 1≤ai<bi≤N1≤ai<bi≤N ( i=1,2,...,Qi=1,2,...,Q )
- 0≤ci≤M−10≤ci≤M−1 ( i=1,2,...,Qi=1,2,...,Q )
- (ai,bi,ci)≠(aj,bj,cj)(ai,bi,ci)≠(aj,bj,cj) (where i≠ji≠j)
- 1≤di≤1051≤di≤105 ( i=1,2,...,Qi=1,2,...,Q )
Input
Input is given from Standard Input in the following format:
NN MM QQ a1a1 b1b1 c1c1 d1d1 :: aQaQ bQbQ cQcQ dQdQ
Output
Print the maximum possible score of AA.
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
110
When A={1,3,4}A={1,3,4}, its score is 110110. Under these conditions, no sequence has a score greater than 110110, so the answer is 110110.
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
因为M<=10,所以DFS遍历所有情况复杂度O(M!)
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> #define ll long long #define PII pair<int, int> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dec(i,a,b) for(int i=a;i>=b;i--) #define pb push_back #define mk make_pair using namespace std; int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = 3.14159265358979323846; const int mod = 998244353; const int N = 50 + 5; //if(x<0 || x>=r || y<0 || y>=c) inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m % n); } ll lcm(ll m, ll n) { return m * n / gcd(m, n); } ll m, n, q; ll a[N], b[N], c[N], d[N]; vector<bool> vis(11, false); ll ans = 0; void dfs(vector<int> &v) { if (v.size()-1 == n) { ll res = 0; rep(i, 1, q) { if (v[b[i]] - v[a[i]] == c[i]) res += d[i]; } ans = max(ans,res); return; } for (int i = 1; i <= m; i++) { if (v.back()<=i) { v.push_back(i); dfs(v); v.pop_back(); } } } int main() { cin >> n >> m >> q; rep(i, 1, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; } vector<int> v(1,0); dfs(v); cout << ans << endl; return 0; }