题目链接:http://acm.zzuli.edu.cn/problem.php?id=2617
这个题本来想着靠排序过的,结果思来想去死活过不了只好用全排列暴力过了,关于全排列算法请自行百度,这里不再赘述(其实我也是现学的)。
#include<set> #include<map> #include<stack> #include<queue> #include<cmath> #include<cstdio> #include<cctype> #include<string> #include<vector> #include<climits> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) #define mst(a) memset(a, 0, sizeof(a)) #define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n") using namespace std; typedef long long ll; typedef pair<int, int> P; const double eps = 1e-7; const int INF = 0x3f3f3f3f; const ll ll_INF = 233333333333333; const int maxn = 1e4+10; struct test { ll a, b; }num[10], temp[10]; ll res[maxn]; int kase; void solve(int start, int n) { if (start == n) { ll sum = 0; //_test; for (int i = 0; i<n; ++i) { //求结果 //printf("%lld %lld\n", num[i].a, num[i].b); sum += (num[i].b * sum + num[i].a); } res[kase++] = sum; } for (int i = start; i<n; ++i) { //求全排列 swap(num[start], num[i]); //交换 solve(start+1, n); swap(num[i], num[start]); //复位 } } int main(void) { int t; scanf("%d", &t); while(t--) { kase = 0; fill(res, res+maxn, ll_INF); //将储存答案的数组元素全部初始化为一个足够大的值,方便下面直接调用函数求最小值 int n; scanf("%d", &n); for (int i = 0; i<n; ++i) scanf("%lld%lld", &num[i].a, &num[i].b); solve(0, n); //计算出所有排列的可能结果 printf("%lld\n", *min_element(res, res+maxn)); } return 0; }