/ Vijos / 讨论 / 旅行 /

求指教。。为什么第五个点和第八个点过不去。。。谢谢!

#include<stdio.h>
#include<stdlib.h>
#include<mem.h>
struct group
{
int str;
int dst;
int num;
};
group team[18];
int n,m,v;
int profit[18],maxprofit;
int c,pt,ps;
bool aboard[18];

int cpr(const void *e1, const void *e2)
{
return ((group *)e1)->str - ((group *)e2)->str;
}

void check()
{
for (int i=ps;i<m;i++)
if (aboard[i])
{
pt += (team[i].dst-team[i].str)*team[i].num;
if (pt>profit[ps]) profit[ps] = pt;
}
return;
}

void DFS(int k)
{
int c0,p0;
int i,j;
c0 = c;
p0 = pt;

for (i=k;i<m;i++)
{
j = ps;
while (j<i)
{
if (aboard[j]&&team[j].dst<=team[i].str)
{
pt += (team[j].dst-team[j].str)*team[j].num;
aboard[j] = false;
c -= team[j].num;
}
j++;
}
if (c+team[i].num<=v)
{
aboard[i] = true;
c += team[i].num;
if (i==m-1) check();
else DFS(i+1);
}
else if (i==m-1) check();
}

for (i=k;i<m;i++) aboard[i] = false;
c = c0;
pt = p0;
return;
}

int main()
{
scanf("%d %d %d",&n,&m,&v);
for (int i=0;i<m;i++)
scanf("%d %d %d",&team[i].str,&team[i].dst,&team[i].num);
memset(aboard,false,sizeof(aboard));
memset(profit,0,sizeof(profit));
qsort(team,m,sizeof(group),cpr);

for (int i=0;i<m;i++)
{
ps = i;
if (team[i].num>v) continue;
pt = 0;
aboard[i] = true;
c = team[i].num;
if (i==m-1) check();
else DFS(i+1);
}

maxprofit = 0;
for (int i=0;i<m;i++)
if (profit[i]>maxprofit) maxprofit = profit[i];
printf("%d\n",maxprofit);
return 0;
}

1 条评论

  • @ 2016-12-28 12:12:19

    更新一下代码。。。
    #include<stdio.h>
    #include<stdlib.h>
    #include<mem.h>
    struct group
    {
    int str;
    int dst;
    int num;
    };
    group team[18];
    int n,m,v;
    int profit[18],maxprofit;
    int c,pt,ps;
    bool aboard[18];

    int cpr(const void *e1, const void *e2)
    {
    return ((group *)e1)->str - ((group *)e2)->str;
    }

    void check()
    {
    for (int i=ps;i<m;i++)
    if (aboard[i])
    {
    pt += (team[i].dst-team[i].str)*team[i].num;
    if (pt>profit[ps]) profit[ps] = pt;
    }
    return;
    }

    void DFS(int k)
    {
    int c0,p0;
    int i,j;
    c0 = c;
    p0 = pt;

    for (i=k;i<m;i++)
    {
    j = ps;
    while (j<i)
    {
    if (aboard[j]&&team[j].dst<=team[i].str)
    {
    pt += (team[j].dst-team[j].str)*team[j].num;
    aboard[j] = false;
    c -= team[j].num;
    }
    j++;
    }
    if (c+team[i].num<=v)
    {
    aboard[i] = true;
    c += team[i].num;
    if (i==m-1) check();
    else DFS(i+1);
    }
    else if (i==m-1) check();
    }

    for (i=k;i<m;i++) aboard[i] = false;
    c = c0;
    pt = p0;
    return;
    }

    int main()
    {
    scanf("%d %d %d",&n,&m,&v);
    for (int i=0;i<m;i++)
    scanf("%d %d %d",&team[i].str,&team[i].dst,&team[i].num);
    memset(aboard,false,sizeof(aboard));
    memset(profit,0,sizeof(profit));
    qsort(team,m,sizeof(group),cpr);

    for (int i=0;i<m;i++)
    {
    ps = i;
    if (team[i].num>v) continue;
    pt = 0;
    aboard[i] = true;
    c = team[i].num;
    if (i==m-1) check();
    else DFS(i+1);
    }

    maxprofit = 0;
    for (int i=0;i<m;i++)
    if (profit[i]>maxprofit) maxprofit = profit[i];
    printf("%d\n",maxprofit);
    return 0;
    }

    • @ 2016-12-28 12:13:07

      好吧。。没有什么区别。。*显示不出来

  • 1

信息

ID
1341
难度
6
分类
模拟 点击显示
标签
(无)
递交数
972
已通过
268
通过率
28%
被复制
4
上传者