HDU 3810 magina queue simulation 0-1 backpack

Source: Internet
Author: User

Question:

There are a lot of monsters on each land on some independent land. It takes some time to kill every monster and get a certain amount of money to give the specified money M, find the minimum time required to get m money and select only one land to kill monsters.

Question:

Regardless of the data range, it is easy to think of a 0-1 backpack (dp (I, j) = min (dp (I-1, J), DP [I-1, j-money] + time), I is money), and then find the least time in all land, that is, the answer, but the data range of this question is too large, money M can reach 1e9, so can not simply directly use the array simulation, consider the I monster, to update the I monster, just need to know the two states, that is DP [I-1] [J], and DP [I-1] [J-money], so we only need to save all the valid states of the first I-1 monster, you can update the status of the first monster, so we can consider using two priority queues to maintain the status of the previous round and the status of this round, and we can find the answer. First, sort the two queues in the order of money from large to small, and time from small to large. Update the next status of all Q1 queues each time, and put both statuses into Q2, then select the optimal solution from Q2, copy it to Q1, and then update ans.

Code:

#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;typedef long long LL;typedef vector<int>::iterator Pointer;const int maxn = 60;const int inf = 1e9;vector<int> group[maxn];vector<int> mp[maxn];int n, m, gcnt;struct node{    LL tm, val;    node() {}    node(LL tm, LL val) : tm(tm), val(val) {}    bool operator < (const node& tmp) const    {        return val < tmp.val || (val == tmp.val && tm > tmp.tm);    }}arr[maxn], now, nxt;void init(){    for (int i = 0; i < maxn; ++i) group[i].clear(), mp[i].clear();}bool vis[maxn];void dfs(int u){    group[gcnt].push_back(u);    vis[u] = true;    for (Pointer it = mp[u].begin(); it != mp[u].end(); ++it)    {        int v = *it;        if (!vis[v])        {            dfs(v);        }    }}void divide_group(){    memset(vis, false, sizeof vis);    gcnt = 0;    for (int i = 1; i <= n; i++)    {        if (!vis[i])        {            dfs(i);            gcnt++;        }    }}priority_queue<node> q1, q2;inline void clear_queue(){    while (!q1.empty())    {        q1.pop();    }    while (!q2.empty())    {        q2.pop();    }}LL mint;void zeroOnePack(int team){    for (Pointer it = group[team].begin(); it != group[team].end(); ++it)    {        int v = *it;        while (!q1.empty())        {            now = q1.top(), q1.pop();            q2.push(now);            nxt = node(now.tm+arr[v].tm, now.val+arr[v].val);            if (nxt.val >= m && nxt.tm < mint)            {                mint = nxt.tm; continue;            }            if (nxt.tm >= mint) continue;            q2.push(nxt);        }        LL pre = inf;        while (!q2.empty())        {            node tmp = q2.top(); q2.pop();            if (tmp.tm < pre)            {                pre = tmp.tm;                q1.push(tmp);            }        }    }}LL solve(){    mint = inf;        for (int i = 0; i < gcnt; i++)    {        now = node(0, 0);        clear_queue();        q1.push(now);        zeroOnePack(i);    }        return mint == inf ? -1 : mint;}int main(){//    freopen("/Users/apple/Desktop/in.txt", "r", stdin);        int t, kase = 0; scanf("%d", &t);        while (t--)    {        scanf("%d%d", &n, &m);        init();        for (int i = 1; i <= n; i++)        {            int k; scanf("%lld%lld%d", &arr[i].tm, &arr[i].val, &k);            for (int j = 0; j < k; j++)            {                int v; scanf("%d", &v);                mp[i].push_back(v);            }        }        divide_group();        LL ans = solve();        printf("Case %d: ", ++kase);        if (ans == -1)        {            printf("Poor Magina, you can't save the world all the time!\n");        }        else        {            printf("%lld\n", ans);        }    }                    return 0;}


HDU 3810 magina queue simulation 0-1 backpack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.