HDU 2809(状压DP->多种限制条件的TSP)
2017-11-08 11:56
316 查看
God of War
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
At 184~280 A.D ,there were many kingdoms in China. Three strongest among them are "Wei", "Shu", "Wu". People call this period as "Three Kingdoms".
HH is a super "Three Kingdoms" fan, because at this period there were many heroes and exciting stories. Among the heroes HH worships LvBu most.
LvBu is the God of War and is also intelligent, but his ambition is too big while enemies are too powerful .Many monarchs wanted to kill him.
At 198 A.D ,CaoCao fought with LvBu at Xuzhou.Though Lvbu is the God of War ,CaoCao had so many generals: Xuchu,DianWei XiahouChun……Facing so many heroes ,could LvBu beat all of them?
Given the LvBu's ATI, DEF, HP, and enemies’ ATI, DEF,HP, experience (if LvBu killed one of his enemies, he can get that experience ,and if his experience got more than or equal to 100*level,he would level-up and become stronger) and the In_ATI,In_DEF,In_HP(indicating
when LvBu levels up,his ability will increase this point).
Each turn LvBu will choose an enemy to fight. Please help LvBu find a way to beat all of enemies and survive with the max HP.
Here’s a fight between LvBu and A:
If LvBu attack A, A will lose Max(1,LvBu's ATI- A's DEF) hp;
If A survived, he will give LvBu Max(1,A'ATI- LvBu'DEF) injury.
If LvBu is still alive, repeat it untill someone is dead(hp <= 0).
LvBu's initial level is 1 and experience is 0,and he can level up many times.
Input
The input contains at most 20 test cases.
For each case , the first line contains six intergers ,indicating LvBu's ATI,DEF,HP and In_ATI,In_DEF,In_HP.
The next line gives an interger N(0<N<=20),indicating the number of the enemies .
Then N lines followed, every line contains the name(the length of each name is no more than 20),ATI,DEF,HP, experience(1<experience<=100).
Output
If LvBu is dead output "Poor LvBu,his period was gone."
Or output the maximum HP left.
Sample Input
Sample Output
题目大意:奥特曼很牛逼,要单挑n只怪兽。怪兽和奥特曼一样都有hp、攻击力、防御力,奥特曼有一个经验值属性,通过打怪兽获得经验值超过100就升级,升级时hp加一些,攻击力加一些,防御力加一些,回不到满血状态,奥特曼每次都要和怪兽血拼,奥特曼先打,怪兽后打,直到一方倒下为止。问奥特曼能不能打倒所有的怪兽从而拯救地球?可以的话输出剩下的最大hp。
解题思路:题目看起来很雷人,实际上就是普通的TSP,只是每次状态转移略显麻烦些。为什么可以转换为TSP呢?因为每只怪兽都只要打一次,而且打完若干只怪兽后剩下的经验值、攻击力、防御力都是一样的,只有hp会随顺序改变,那么用一个数组dp[i]表示i状态时的最多hp,每次更新都去计算i状态下的各项属性。我把hp,at,def,ex都封装进一个结构体,意思是一样的,状态转移见代码。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
const int N = (1<<20)+10;
int n;
struct hero
{
int ati,def,hp,ex, lev;
}dp
;
struct enemy
{
int ati,def,hp,ex;
}e
;
void init()
{
for(int i=1;i<(1<<n);i++)
dp[i].ati=dp[i].def=dp[i].ex=dp[i].hp=0;
return ;
}
char str[25];
int main()
{
int ati,def,hp;
while(scanf("%d %d %d %d %d %d",&dp[0].ati, &dp[0].def,&dp[0].hp, &ati, &def, &hp)!=EOF)
{
scanf("%d",&n);
init();
for(int i=0;i<n;i++)
scanf("%s %d %d %d %d",str,&e[i].ati,&e[i].def,&e[i].hp,&e[i].ex);
dp[0].ex=0, dp[0].lev=1;
for(int i=0;i<(1<<n);i++)
{
if(dp[i].hp<=0)
continue;
for(int j=0;j<n;j++)
{
if(i&(1<<j))
continue;
hero tmp=dp[i];
int att1=max(1,tmp.ati-e[j].def);
int att2=max(1,e[j].ati-tmp.def);
int time=e[j].hp/att1+(e[j].hp%att1==0?-1:0);
tmp.hp-=(att2*time);
if(tmp.hp<=0)
continue;
int state=i|(1<<j);
tmp.ex+=e[j].ex;
if(tmp.ex>=tmp.lev*100)
tmp.ati+=ati,tmp.def+=def,tmp.hp+=hp,tmp.lev++;
if(dp[state].hp<tmp.hp)
{
dp[state]=tmp;
}
}
}
if(dp[(1<<n)-1].hp<=0)
printf("Poor LvBu,his period was gone.\n");
else
printf("%d\n",dp[(1<<n)-1].hp);
}
return 0;
}
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
At 184~280 A.D ,there were many kingdoms in China. Three strongest among them are "Wei", "Shu", "Wu". People call this period as "Three Kingdoms".
HH is a super "Three Kingdoms" fan, because at this period there were many heroes and exciting stories. Among the heroes HH worships LvBu most.
LvBu is the God of War and is also intelligent, but his ambition is too big while enemies are too powerful .Many monarchs wanted to kill him.
At 198 A.D ,CaoCao fought with LvBu at Xuzhou.Though Lvbu is the God of War ,CaoCao had so many generals: Xuchu,DianWei XiahouChun……Facing so many heroes ,could LvBu beat all of them?
Given the LvBu's ATI, DEF, HP, and enemies’ ATI, DEF,HP, experience (if LvBu killed one of his enemies, he can get that experience ,and if his experience got more than or equal to 100*level,he would level-up and become stronger) and the In_ATI,In_DEF,In_HP(indicating
when LvBu levels up,his ability will increase this point).
Each turn LvBu will choose an enemy to fight. Please help LvBu find a way to beat all of enemies and survive with the max HP.
Here’s a fight between LvBu and A:
If LvBu attack A, A will lose Max(1,LvBu's ATI- A's DEF) hp;
If A survived, he will give LvBu Max(1,A'ATI- LvBu'DEF) injury.
If LvBu is still alive, repeat it untill someone is dead(hp <= 0).
LvBu's initial level is 1 and experience is 0,and he can level up many times.
Input
The input contains at most 20 test cases.
For each case , the first line contains six intergers ,indicating LvBu's ATI,DEF,HP and In_ATI,In_DEF,In_HP.
The next line gives an interger N(0<N<=20),indicating the number of the enemies .
Then N lines followed, every line contains the name(the length of each name is no more than 20),ATI,DEF,HP, experience(1<experience<=100).
Output
If LvBu is dead output "Poor LvBu,his period was gone."
Or output the maximum HP left.
Sample Input
100 80 100 5 5 5 2 ZhangFei 95 75 100 100 XuChu 90 90 100 90 100 75 100 5 5 5 1 GuanYu 95 85 100 100
Sample Output
30 Poor LvBu,his period was gone.
题目大意:奥特曼很牛逼,要单挑n只怪兽。怪兽和奥特曼一样都有hp、攻击力、防御力,奥特曼有一个经验值属性,通过打怪兽获得经验值超过100就升级,升级时hp加一些,攻击力加一些,防御力加一些,回不到满血状态,奥特曼每次都要和怪兽血拼,奥特曼先打,怪兽后打,直到一方倒下为止。问奥特曼能不能打倒所有的怪兽从而拯救地球?可以的话输出剩下的最大hp。
解题思路:题目看起来很雷人,实际上就是普通的TSP,只是每次状态转移略显麻烦些。为什么可以转换为TSP呢?因为每只怪兽都只要打一次,而且打完若干只怪兽后剩下的经验值、攻击力、防御力都是一样的,只有hp会随顺序改变,那么用一个数组dp[i]表示i状态时的最多hp,每次更新都去计算i状态下的各项属性。我把hp,at,def,ex都封装进一个结构体,意思是一样的,状态转移见代码。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
const int N = (1<<20)+10;
int n;
struct hero
{
int ati,def,hp,ex, lev;
}dp
;
struct enemy
{
int ati,def,hp,ex;
}e
;
void init()
{
for(int i=1;i<(1<<n);i++)
dp[i].ati=dp[i].def=dp[i].ex=dp[i].hp=0;
return ;
}
char str[25];
int main()
{
int ati,def,hp;
while(scanf("%d %d %d %d %d %d",&dp[0].ati, &dp[0].def,&dp[0].hp, &ati, &def, &hp)!=EOF)
{
scanf("%d",&n);
init();
for(int i=0;i<n;i++)
scanf("%s %d %d %d %d",str,&e[i].ati,&e[i].def,&e[i].hp,&e[i].ex);
dp[0].ex=0, dp[0].lev=1;
for(int i=0;i<(1<<n);i++)
{
if(dp[i].hp<=0)
continue;
for(int j=0;j<n;j++)
{
if(i&(1<<j))
continue;
hero tmp=dp[i];
int att1=max(1,tmp.ati-e[j].def);
int att2=max(1,e[j].ati-tmp.def);
int time=e[j].hp/att1+(e[j].hp%att1==0?-1:0);
tmp.hp-=(att2*time);
if(tmp.hp<=0)
continue;
int state=i|(1<<j);
tmp.ex+=e[j].ex;
if(tmp.ex>=tmp.lev*100)
tmp.ati+=ati,tmp.def+=def,tmp.hp+=hp,tmp.lev++;
if(dp[state].hp<tmp.hp)
{
dp[state]=tmp;
}
}
}
if(dp[(1<<n)-1].hp<=0)
printf("Poor LvBu,his period was gone.\n");
else
printf("%d\n",dp[(1<<n)-1].hp);
}
return 0;
}
相关文章推荐
- POJ 2288 Islands and Bridges(状压DP->多种限制条件的TSP)
- HDU 3001 Travelling(状压DP->TSP+多进制)
- hdu 3790 最短路径问题(两个限制条件的最短路)
- hdu 2375 Help Bob(状压DP->优惠卷的搭配问题)
- hdu——4341(有限制条件的dp)
- zhx and contest (HDU_5188) 01背包+条件限制
- hdu 1728 逃离迷宫(BFS 有 限制条件,陷阱比较多)
- hdu 4460 Friend Chains (限制条件的spfa)
- Singleton<T>单例模式,不用Lock的实现版本,限制条件是:被单例的类 必须有一个无参构造函数
- HDU 4539 郑厂长系列故事――排兵布阵(状压DP+滚动数组->求可放棋子最大数量)
- hdu 3001 Travelling 状压dp TSP变形
- HDU 5188 zhx and contest(带限制条件的 01背包)
- hdu 4268 Pieces(状压DP->回文字符串的最少删除次数)@
- hdu 4529 郑厂长系列故事――N骑士问题(状压DP+滚动数组->求给定棋子的所有方法数)
- [笔记] TSP问题状压DP求解 HDU-5067
- HDU 2159 FATE(完全背包:二维限制条件)
- 【K-D树 在限制条件下求最近欧几里德距离】HDU - 5992 Finding Hotels
- HDU 5188 zhx and contest(带限制条件的 01背包)
- HDU(2058)条件限制
- HDU 3311 Hie with the Pie(状压dp+TSP问题)