hdu 1074 Doing Homework 状态DP+dfs

简介:

从没写过状态dp,只是据说这是而已……

其实就是记录个当前点下可以达到的最大值,只有最大值更新后才向下级更新,过程就和spfa求最短路一样……

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define INF 1E9
using namespace std;
struct node
{
    char s[101];
    int C,D,E;
};
node a[16];
int Min[70000];
int last[70000];
int ok;
int n,t;
void dfs(int time,int now,int ans)
{
    if(now>=n)return;
    for(int i=0;i<n;i++)
    {
        int m=1<<i;
        if(ok&m)continue;
        ok|=m;
        t=time+a[i].E;
        if(t<0)t=0;
        if(ans+t<Min[ok])//记忆最大状态
        {
            Min[ok]=ans+t;
            last[ok]=ok&(~m);
            dfs(time+a[i].C,now+1,ans+t);//继续搜索
        }
        ok&=~m;
    }
    return;
}
void out(int t)//反向打印
{
    if(t==0)return;
    int tt=last[t],i;
    out(tt);
    t^=tt;
    for(i=0;i<n;i++)
    {
        if(t&1)break;
        t>>=1;
    }
    puts(a[i].s);
}

int main()
{
    int T,i;
    scanf("%d",&T);
    while(T--)
    {
        ok=0;
        memset(Min,127,sizeof(Min));
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%s%d%d",a[i].s,&a[i].D,&a[i].C);
            a[i].E=a[i].C-a[i].D;
        }
        dfs(0,0,0);
        t=(1<<n)-1;
        printf("%d\n",Min[t]);
        out(t);
    }

}

目录
相关文章
|
定位技术 ice
POJ-3009,Curling 2.0(DFS)
POJ-3009,Curling 2.0(DFS)
洛谷P1331-海战(简单的DFS)
洛谷P1331-海战(简单的DFS)
|
机器学习/深度学习
POJ-1321,棋盘问题(DFS)
POJ-1321,棋盘问题(DFS)
poj 1562 dfs
http://poj.org/problem?id=1562 #include using namespace std; int n=0,m=0,sum=0; bool aa[105][105]; int dir[8][2]={-1,0, 1,0, ...
684 0
POJ 1979 DFS
题目链接:http://poj.org/problem?id=1979 #include #include using namespace std; int n=0,h=0,sum=0; char aa[21][21]; void DFS(int p,int q) { if(aa[p][q]=='.
746 0
|
网络架构
leetcode DFS
Summary DFS problems have two kinds: One to get the number of all solutions.
908 0
【HDU 4771 Stealing Harry Potter&#39;s Precious】BFS+状压
2013杭州区域赛现场赛二水。。。 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间。 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点,搜索下一个最近的宝藏,直至找到全部k个宝藏。
1033 0