Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

DP思路,测试几组数据都正确,提交就错误,高手帮忙分析下原因吧。

Posted by free_sundy at 2011-02-25 21:13:36 on Problem 1018
# 高手帮忙分析下:

几组数据测试下来,都正确,提交就WA。
不知道错误在哪里了。

# 思路:

R[MAXN][MAXP]做记录。

for (i=1; i<=n; i++)         // 记录第i个组设备
  for (j=1; j<=MAXP; j++)    // 当价格为j时
    for (k=1; k<=N[i]; k++)  // 对i组中的每一个设备计算带宽

# 代码如下: 


/* Peking Acm 1018 */
/* 分组背包问题 */

#include "stdio.h"

#define MAXN 101
#define MAXM 101
#define MAXP 20000

int N[MAXN];
int B[MAXN][MAXM];
int P[MAXN][MAXM];

int R[MAXN][MAXP];


int main()
{
    int t,n,m;
    int i,j,k,x;

    double temp,ans;

    scanf("%d", &t);

    while(t--)
    {
        /* input */
        scanf("%d", &n);

        for (i=1; i<=n; i++)
        {
            scanf("%d", &N[i]);
            for (j=1; j<=N[i]; j++)
                scanf("%d %d", &B[i][j], &P[i][j]);
        }

        /* initialize */
        for (i=0; i<=n; i++)
            for (j=0; j<=MAXP; j++)
                R[i][j] = -1;
        R[0][0] = 10000;


        /* compute */
        for (i=1; i<=n; i++)                /* group number */
            for (j=1; j<=MAXP; j++)         /* prize        */
                for (k=1; k<=N[i]; k++)     /* object in group */
                {
                    x = j-P[i][k];          /* 买下此设备,剩下的钱*/

                    if ( x >= 0  &&                     /* 钱够买此设备 */
                         R[i-1][x] != -1 )              /* 剩下的钱买其他设备的方案已有*/
                    {
                        if ( B[i][k] >= R[i-1][x] )     /* 此设备的带宽大于总带宽*/
                            R[i][j] = R[i-1][x];        /* 则对总带宽无影响*/
                        else if ( B[i][k] > R[i][j] )   /* 此设备的带宽小于总带宽 && 相对于上一个设备这个设备能得到更高的带宽 */
                            R[i][j] = B[i][k];
                    }
                }

        /* print answer */
        ans = 0.0;
        for (i=1; i<MAXP; i++)
        {
            if (R[n][i] != -1)
            {
                temp = (double)R[n][i] / (double)i;
                if (temp > ans)
                    ans = temp;
            }
        }
        printf("%.3lf\n", ans);
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator