Leetcode-5047 Minimum Score Triangulation of Polygon(多边形三角剖分的最低得分)
1 const int maxn = 10000; 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 typedef long long ll; 4 5 6 class Solution 7 { 8 public: 9 int t[60][60]; 10 int s[60][60]; 11 int weight[60]; 12 int N; 13 int get_weight(const int a, const int b, const int c) 14 { 15 return weight[a] * weight[b] * weight[c]; 16 } 17 int minScoreTriangulation(vector<int>& A) 18 { 19 int N = A.size(); 20 _for(i,0,A.size()) 21 weight[i] = A[i]; 22 int i,r,k,j; 23 int min; 24 25 for (i = 1; i < N; i++) 26 { 27 t[i][i] = 0; 28 } 29 30 for (r = 2; r < N; r++) 31 { 32 for (i = 1; i < N-r+1; i++) 33 { 34 j = i + r -1; 35 min = INT_MAX; 36 for (k = i; k < j; k++) 37 { 38 t[i][j] = t[i][k] + t[k+1][j] + get_weight(i-1,k,j); 39 if (t[i][j] < min) 40 { 41 min = t[i][j]; 42 s[i][j] = k; 43 } 44 } 45 t[i][j] = min; 46 } 47 } 48 return t[1][N-1]; 49 } 50 };
多边形三角剖分,DP走起来