zoj 3812 We Need Medicine (dp 位优化 巧妙记录路径)

We Need Medicine

Time Limit: 10 Seconds      Memory Limit: 65536 KB      Special Judge

A terrible disease broke out! The disease was caused by a new type of virus, which will lead to lethal lymphoedema symptom. For convenience, it was namedLL virus.

After several weeks of research, the scientists found the LL virus highly lethal and infectious. But more importantly, it has a long incubation period. Many victims were unaware of being infected until everything was too late. To prevent from the apocalypse, we need medicine!

Fortunately, after another several weeks of research, the scientists have finished the analysis of the LL virus. You need write a program to help them to produce the medicine.

The scientists provide you N kinds of chemical substances. For each substance, you can either use it exactWi milligrams in a medicine, or not use it. Each selected substance will addTi points of therapeutic effect value (TEV) to the medicine.

The LL virus has Q different variants. For each variant, you need design a medicine whose total weight equals toMi milligrams and total TEV equals to Si points. Since the LL virus is spreading rapidly, you should start to solve this problem as soon as possible!

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers N (1 <= N <= 400) andQ (1 <= Q <= 400).

For the next N lines, each line contains two integers Wi (1 <=Wi <= 50) and Ti (1 <= Ti <= 200000).

Then followed by Q lines, each line contains two integers Mi (1 <=Mi <= 50) and Si (1 <= Si <= 200000).

Output

For each test case, output Q lines. For the i-th line, output the indexes (1-based) of chemical substances in the i-th medicine, separated by a space. If there are multiple solutions, output any one. If there is no solution, output "No solution!" instead.

Sample Input
1
3 3
2 10
1 12
1 5
3 15
4 27
3 17
Sample Output

2 1
No solution!


题意:
给你一些材料来配置药品,每种材料有一定的重量和药效,有q个询问,问重量为m,药效为s的药能否配出来。

思路:
一看就觉得是二维费用的背包,但是数据量太大,接受不了,所以要针对问题找另外的解决办法,考虑到重量都在50之内,想到状压。
dp[i][j]表示处理完前i个物品,药效为j时能达到的重量的状态,第一维可以优化。
有点麻烦的是记录路径,因为400*200000的数组开不下,所以得找额外的办法,因为一条路径最多只有50的长度,所以可以用pre[j][k]记录到达药效为j时重量为k时放的是哪一个物品,根据第i个物品递推时会增加的一些状态来记录。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 401
#define MAXN 200005
#define INF 0x3f3f3f3f
#define mod 1000000007
#define pi acos(-1.0)
#define eps 1e-6
typedef unsigned long long ull;
using namespace std;

int n,m,u,v,ma;
int w[maxn],t[maxn];
short pre[200001][51];
ull dp[200001];

void solve()
{
    int i,j;
    memset(pre,0,sizeof(pre));
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    ma=200000;
    ull tmp,k,tot=(1ULL<<51)-1;
    for(i=1;i<=n;i++)
    {
        for(j=ma;j>=t[i];j--)
        {
            if(dp[j-t[i]]==0) continue ;
            tmp=dp[j];
            dp[j]|=(dp[j-t[i]]<<w[i])&tot;
            for(k=tmp^dp[j];k;k=(k-1)&k)  // 新增加的状态 一位一位取出
            {
                pre[j][__builtin_ctzll(k)]=i;  // builtin_ctzll-末尾有几个0
            }
        }
    }
}
int main()
{
    int i,j,test,tu;
    scanf("%d",&test);
    while(test--)
    {
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&w[i],&t[i]);
        }
        solve();
        while(m--)
        {
            scanf("%d%d",&u,&v);
            if(dp[v]&(1ULL<<u))
            {
                printf("%d",pre[v][u]);
                tu=u;
                u-=w[pre[v][u]]; v-=t[pre[v][tu]];
                while(u)
                {
                    printf(" %d",pre[v][u]);
                    tu=u;
                    u-=w[pre[v][u]]; v-=t[pre[v][tu]];
                }
                puts("");
            }
            else printf("No solution!\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这道题是一个典型的搜索问题,可以使用深度优先搜索(DFS)或广度优先搜索(BFS)来解决。以下是使用DFS的代码实现: ```c++ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 20; const int MAXM = 20; int n, m, sx, sy, ex, ey; char maze[MAXN][MAXM]; // 迷宫 int vis[MAXN][MAXM]; // 标记数组 int dx[] = {0, 0, 1, -1}; // 方向数组 int dy[] = {1, -1, 0, 0}; void dfs(int x, int y) { if (x == ex && y == ey) { // 到达终点 printf("(%d,%d)", x, y); return; } for (int i = 0; i < 4; i++) { // 依次尝试四个方向 int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && maze[nx][ny] != '#' && !vis[nx][ny]) { vis[nx][ny] = 1; // 标记已访问 printf("(%d,%d)->", x, y); dfs(nx, ny); return; } } } int main() { while (scanf("%d%d", &n, &m) == 2) { memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { scanf("%s", maze[i]); for (int j = 0; j < m; j++) { if (maze[i][j] == 'S') { sx = i; sy = j; } else if (maze[i][j] == 'T') { ex = i; ey = j; } } } vis[sx][sy] = 1; dfs(sx, sy); printf("\n"); } return 0; } ``` 代码实现中,使用了一个标记数组 `vis` 来标记每个位置是否已经被访问过,避免走重复的路线。使用DFS的时候,每次从当前位置依次尝试四个方向,如果某个方向可以走,则标记该位置已经被访问过,并输出当前位置的坐标,然后递归进入下一个位置。如果当前位置是终点,则直接输出并返回。 在输出路径的时候,由于是递归调用,所以输出的路径是反向的,需要将其反转过来,即从终点往起点遍历输出。 需要注意的是,题目中要求输出的路径是 `(x1,y1)->(x2,y2)->...->(xn,yn)` 的形式,每个坐标之间用 `->` 连接。所以在输出的时候需要特别处理第一个坐标和最后一个坐标的格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值