- 旅行
- 2016-12-28 12:08:49 @
#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 条评论
-
JeffWGL LV 5 @ 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;
}
- 1