HDU - 2677 Dota all stars

大数据竞赛!

Dota all stars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1148    Accepted Submission(s): 336


Problem Description
Dota as a popular computer game has serval years. Lots of Dota fans know that the equipment in the game is very important. To get a excellent equipment may be pass many processes. Look the picture, you may see that in order to get the finally one must get the other two first. But this two may be can not get directly.

     

               



       
 
Now give the primary equipments which you can buy it by money directly in the shop.
And the numbers of equipments you already have.
You also know all the formulas how to get a new better equipment by primary equipments.
Last give the numbers of equipments you want, tell us how much extra money you need. 
 

Input
The input contains multiple test cases.
First part a integer n1 expressing the numbers of primary equipments you can buy in the shop.
Next n1 lines, each line form as S V,S is the name of equipment and V meaning you should cost V money to buy one equipments S.
Second part a integer n2 expressing the numbers of kinds equipments you have.
Next n2 lines, each line form as S,M, meaning the numbers of equipment S you have is M.
Third part a integer n3 expressing the numbers of formulas.
Next n3 lines, each line have a character '=', The left is the equipments you should use to get the right equipment. For example A + B + C = D, In order to get a D, you must use A,B,C,D.each one.
Last part a integer n4, expressing n4 kinds of equipments you need.
Than n4 lines, each line form as S,M. Meaning the numbers of equipment S you need is M.

You may sure the total kinds of equipment will not larger than 100. And the lengths of equipment name will less than 50.(a chinese words made up of two character )
 

Output
Output the extra money you need to achieve goal.
 

Sample Input
 
       
4 欢欣之刃 100 敏捷之靴 20 半兽人之斧 100 力量腰带 50 2 散华 1 力量腰带 2 3 欢欣之刃 + 敏捷之靴 = 散华 半兽人之斧 + 力量腰带 = 夜叉 散华 + 夜叉 = 散夜对剑 1 散夜对剑 2 5 鹰角弓 3200 短棍 1100 攻击之爪 950 阔剑 1200 恶魔刀锋 2600 0 3 鹰角弓 + 短棍 = 蝴蝶 攻击之爪 + 阔剑 = 水晶剑 水晶剑 + 恶魔刀锋 = 大炮 2 蝴蝶 1 大炮 1
 

Sample Output
 
       
320 9050
 


思路:

看到这题首先想到了最短路,可惜不能用最短路,因为需要考虑你已经有的物品,不能构成一个稳定的图。接着我又画蛇添足的想到了记忆化搜索,可惜因为同样的原因也行不通。这道题直接dfs就行了。等式部分可以用链表来存储,只不过因为不知道等号左边有多少物品,需要先把等号左边的物品保存下来。

搜索的时候需要考虑已经有的物品。

代码:

#include
#include
#include
#include
#include
#include
#include
using namespace std;
int n1,n2,n3,n4,cnt,ans;
int price[105],bag[105],need[105];
int buf[105];
map mm;
vector vec[105];
int dfs(int obj,int num)
{
    if(bag[obj])
    {
        if(bag[obj]>=num)
        {
            bag[obj]-=num;
            return 0;
        }
        else
        {
            int k=bag[obj];
            bag[obj]=0;
            return dfs(obj,num-k);
        }
    }
    if(price[obj]!=-1) return num*price[obj];
    int cost=0;
    for(int i=0;i> s >> p;
            mm[s]=++cnt;
            price[cnt]=p;
        }
        scanf("%d",&n2);
        for(int i=0;i> s >> p;
            if(!mm[s]) mm[s]=++cnt;
            bag[mm[s]]=p;
        }
        scanf("%d",&n3);
        string u,v,op;
        for(int i=0;i> s >> op;
                if(!mm[s]) mm[s]=++cnt;
                buf[num++]=mm[s];
            }while(op!="=");
            cin>> s;
            if(!mm[s]) mm[s]=++cnt;
            for(int j=0;j> s >> p;
            ans+=dfs(mm[s],p);
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(搜索)