求助,路径输出有误
查看原帖
求助,路径输出有误
124676
JimmyFlower楼主2020/11/28 11:38

输出格式什么的不用在意啦,主要是路径输出会输出很多重复的。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ri register int
using namespace std;
const int N=201,M=21,P=400;
const int INF=1061109568;
int n,m,ans,pos,tot,cnt=0,jur[M],f[M][2*P+1],a1,b1,t,p[M][2*P+1];
struct node{int x,y;}a[N];
int main()
{
    while(scanf("%d %d",&n,&m),n||m)
    {
        for(ri i=1;i<=n;i++) scanf("%d %d",&a[i].x,&a[i].y);
        memset(f,0xc0,sizeof(f)); f[0][P]=0; 
        for(ri i=1;i<=n;i++)
            for(ri j=m;j>=1;j--)
                for(ri k=max(a[i].x-a[i].y-P,-P);k<=P;k++)
                    if(f[j-1][k-(a[i].x-a[i].y)+P]+a[i].x+a[i].y>f[j][k+P])
                    {
                        f[j][k+P]=f[j-1][k-(a[i].x-a[i].y)+P]+a[i].x+a[i].y;
                        p[j][k+P]=i;
                    }
		ans=pos=-INF;
        for(ri i=0;i<=P;i++)
        {
            if(f[m][i+P]<0&&f[m][-i+P]<0) continue;
            if(f[m][i+P]>0) ans=max(ans,f[m][i+P]),pos=i,b1=i+P;
            if(f[m][-i+P]>ans) ans=f[m][-i+P],pos=-i,b1=-i+P;
            break;
        }
        printf("Jury #%d\n",++cnt);
        printf("Best jury has value %d for prosecution and value %d for defence:\n",(pos+ans)/2,ans-(pos+ans)/2);
        for(tot=0,a1=m;a1&&b1;)
        {
            int pa=p[a1][b1];
            jur[++tot]=pa;
            a1--,b1-=a[pa].x-a[pa].y;
        }
        sort(jur+1,jur+tot+1);
        for(ri i=1;i<=tot;i++) printf(" %d",jur[i]);
        printf("\n\n");
    }
    return 0;
}
2020/11/28 11:38
加载中...