import java.util.Scanner;
import java.util.Stack;
import javax.jws.soap.SOAPBinding.Use;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while (sc.hasNext())
{
int cnm = sc.nextInt();
while (cnm-- > 0)
{
int g=sc.nextInt();
int nb=1<<g;
int max =Integer.MAX_VALUE;
node node[]=new node[g];for (int i = 0; i < node.length; i++)node[i]=new node(sc.next(), sc.nextInt(), sc.nextInt());
find dp[]=new find[nb];
for (int i = 0; i <nb; i++)dp[i]=new find(0, 0, 0, 0);
for (int i =1; i <nb; i++)
{
dp[i].score=max;
for (int j =g-1;j>=0; j--)
{
int place=1<<j;
if((place & i )!=0) {
int before=i-place;
int change_score=dp[before].time+node[j].usetime-node[j].lasttime;
if (change_score<0)change_score=0;
int find_max=change_score+dp[before].score;
if (find_max<dp[i].score)
{
dp[i].score=find_max;
dp[i].father=before;
dp[i].book=j;
dp[i].time=dp[before].time+node[j].usetime;
}
}
}
}
//
System.out.println(dp[nb-1].score);
Stack<Integer> out =new Stack<Integer>();
int zz=nb-1;
while(zz!=0) {
out.add(dp[zz].book);
zz=dp[zz].father;
}
while(!out.isEmpty()) {
System.out.println(node[out.pop()].name);
}
}
}
}
}
class node {String name;int lasttime,usetime;public node(String name,int lasttime ,int usetime) {
this.name=name;this.lasttime=lasttime;this.usetime=usetime;
}}
class find{int father,book,score,time;public find(int father,int book,int score,int time) {
this.father=father;this.book=book;this.score=score;this.time=time;
}}
hdu1074
最新推荐文章于 2022-11-24 20:47:30 发布