坑
需要输出路径,方法是对每种状态保存当前最优的路径,这题还要求字典序,需要比较
#include<iostream>
#include<cstdio>
#include<set>
#include<string>
#include<string.h>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<algorithm>
#include<sstream>
#include<utility>
#include<cmath>
#include<functional>
#define mt(a) memset(a,0,sizeof (a))
#define fl(a,b,c) fill(a,b,c)
#define SWAP(a,b,t) (t=a,a=b,b=t)
#define inf 1000000000+7
using namespace std;
typedef long long ll;
class mnode
{
public:
int num;
int ans[16];
};
mnode dp[40000];
class node
{
public:
string name;
int dl, d;
};
int main()
{
int T;
cin >> T;
while (T--)
{
mt(dp);
int n;
cin >> n;
node a[16];
for (int i = 0; i<n; i++)
{
cin >> a[i].name >> a[i].dl >> a[i].d;
}
for (int i = 0; i < 40000; i++)
dp[i].num = inf;
dp[0].num = 0;
for (int s = 0; s<(1 << n); s++)
{
for (int i = 0; i<n; i++)
{
if ((s & (1 << i)))
{
int alltime = 0, cot = 0;
for (int j = 0; j < n; j++)
{
if (j == i)continue;
if (s&(1 << j))
{
alltime += a[j].d;
cot++;
}
}
if (dp[s].num > dp[s ^ (1 << i)].num + max(0, alltime + a[i].d - a[i].dl))
{
dp[s].num = dp[s ^ (1 << i)].num + max(0, alltime+a[i].d-a[i].dl);
for (int k = 0; k < cot; k++)
{
dp[s].ans[k] = dp[s ^ (1 << i)].ans[k];
}
dp[s].ans[cot] = i;
}
else if (dp[s].num == dp[s ^ (1 << i)].num + max(0, alltime + a[i].d - a[i].dl))
{
int flag = 0;
for (int k = 0; k < cot; k++)
{
if (a[dp[s].ans[k]].name>a[dp[s ^ (1 << i)].ans[k]].name)
{
for (; k < cot; k++)
dp[s].ans[k] = dp[s ^ (1 << i)].ans[k];
flag = 1;
break;
}
}
if (flag)dp[s].ans[cot] = i;
}
}
}
}
int mixx = inf;
cout << dp[(1 << n) - 1].num << endl;
for (int i = 0; i<n ; i++)
{
cout << a[dp[(1<<n)-1].ans[i]].name << "\n";
}
}
return 0;
}