vijos 1071 01背包+输出路径

描述

过年的时候,大人们最喜欢的活动,就是打牌了。xiaomengxian不会打牌,只好坐在一边看着。

这天,正当一群人打牌打得起劲的时候,突然有人喊道:“这副牌少了几张!”众人一数,果然是少了。于是这副牌的主人得意地说:“这是一幅特制的牌,我知道整副牌每一张的重量。只要我们称一下剩下的牌的总重量,就能知道少了哪些牌了。”大家都觉得这个办法不错,于是称出剩下的牌的总重量,开始计算少了哪些牌。由于数据量比较大,过了不久,大家都算得头晕了。

这时,xiaomengxian大声说:“你们看我的吧!”于是他拿出笔记本电脑,编出了一个程序,很快就把缺少的牌找了出来。

如果是你遇到了这样的情况呢?你能办到同样的事情吗?

格式

输入格式

第一行一个整数TotalW,表示剩下的牌的总重量。

第二行一个整数N(1<N<=100),表示这副牌有多少张。

接下来N行,每行一个整数Wi(1<=Wi<=1000),表示每一张牌的重量。

输出格式

如果无解,则输出“0”;如果有多解,则输出“-1”;否则,按照升序输出丢失的牌的编号,相邻两个数之间用一个空格隔开。

样例1

样例输入1[复制]

270
4
100
110
170
200

样例输出1[复制]

2 4

题意:就是个裸的01背包,加个路径输出。

思路:记录下当前值下的前一个值,顺带记录序号,最后遍历一遍,筛出没出现过的数就好了。

/** @Date    : 2016-11-29-08.14
  * @Author  : Lweleth ([email protected])
  * @Link    : https://github.com/
  * @Version :
  */

#include<bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+2000;

int w, n;
int dp[100010];
int re[100010];
int mp[100010];
int a[110];
int main()
{
    while(cin >> w >> n)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", a + i);
        }

        MMF(dp);
        MMF(re);
        int flag = 0;
        dp[0] = 1;
        for(int i = 1; i <= n; i++)
        {
            for(int j = w; j >= 0; j--)
            {
                if(j >= a[i] && dp[j-a[i]])
                {
                    dp[j] += dp[j - a[i]];
                    if(dp[j] == 1)
                        re[j] = j - a[i], mp[j] = i;
                }
            }
        }
        if(!dp[w])
            printf("0\n");
        else if(dp[w] > 1)
            printf("-1\n");
        else
        {
            int t = w;
            while(t != 0)
            {
                a[mp[t]] = -1;
                t = re[t];
            }
            for(int i = 1; i <= n; i++)
            {
                if(a[i] != -1)
                {
                    if(flag)
                        printf(" ");
                    printf("%d", i);
                    flag = 1;
                }
            }
            printf("\n");
        }

    }
    return 0;
}
时间: 2024-08-26 10:04:29

vijos 1071 01背包+输出路径的相关文章

CD-----UVa624(01背包+输出路径)

  CD  You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most out of

01背包输出路径(模板)

int n,m; int v[MAX],w[MAX]; int dp[MAX]; bool path[MAX][MAX]; int V; void solve() { memset(dp,0,sizeof(dp)); memset(path,false,sizeof(path)); for(int i=0;i<n;i++) { for(int j=V;j>=w[i];j--) if(dp[j-w[i]]+v[i]>dp[j]) { dp[j]=dp[j-w[i]]+v[i]; path[

0-1背包打印路径(递归和非递归版本)

简单的0-1背包打印路径问题,我们可以记录一个p[][]数组来判断,当前物品是否被选中,最后按照记录输出,注意是逆序. #include<stdio.h> #include<string.h> int main() { int a[25],p[25][10005],i,j,n,m,s[10005]; while(scanf("%d%d",&m,&n)!=EOF){ for(i=1;i<=n;i++) scanf("%d"

UVA624(01背包记录路径)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=565 01背包打印路径. #include <cstdio> #include <cstring> using namespace std; const int MAXN=10005; int n,W; int w[30]; int dp[MAXN]; in

UVA 624 CD (01背包 带路径)

题意 输入两个数 len,n 表示长度和个数,接下来输入n个数, 表示每一个的长度, 求这n个数能够组成的不超过len的最大长度,并输出这些数. 分析:01背包,dp数组非0表示可以组成的数,dp数组用来记录路径 #include <iostream> #include <queue> #include <cstdio> #include <cstring> #include <cstdlib> #include <stack> #i

UVA 624 CD (01背包+打印路径 或 dfs+记录路径)

Description You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most o

Uva 642-CD(0-1背包+打印路径)

题目链接:点击打开链接 裸01背包 ,此题中 价值即体积... 打印路径..不多说了 一维的没看懂..上个二维的 #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio>

新年趣事之打牌(01背包+唯一路径)

过年的时候,大人们最喜欢的活动,就是打牌了.蒜头君不会打牌,只好坐在一边看着. 这天,正当一群人打牌打得起劲的时候,突然有人喊道: "这副牌少 了几张!”众人一数,果然是少了.于是这副牌的主人得意地说: "这是一幅特制的牌,我知道整副牌每一张的重量. 只要我们称一下剩下的牌的总重量,就能知道少了哪些牌了.”大家都觉得这个办法不错,于是称出剩下的牌的总重量,开始计算少了哪些牌.于数据量比较大,过了不久,大家都算得头晕了. 这时,蒜头君大声说:“你们看我的吧! ”于是他拿出笔记本电脑,编出

UVA 624CD(01背包输出 + 输出路径)

You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is onCDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. Howto choose tracks from CD to get most out of tape spa