Ultraviolet A 301 Transportation

Source: Internet
Author: User
#include<cstdio>#include<cstring>using namespace std;int n,m,k,seat[30],accept[30],max;typedef struct ord{    int start,end,number,earn;} ord;ord order[30];void dfs(int stop,int money,int cur){    max=max>money?max:money;    if(stop==m)  return ;    int i,j;    for(i=cur; i<k; i++)    {        if(order[i].start==stop&&!accept[i])        {            int flag=1;            for(j=stop; j<order[i].end; j++)                if(seat[j]<order[i].number)                {                    flag=0;                    break;                }            if(flag)            {                for(j=stop; j<order[i].end; j++)                    seat[j]-=order[i].number;                accept[i]=1;                dfs(stop,money+order[i].earn,i+1);                accept[i]=0;                for(j=stop; j<order[i].end; j++)                    seat[j]+=order[i].number;            }        }        else if(order[i].start>stop)   break;    }    dfs(stop+1,money,cur);}int main(){    //freopen("in.txt","r",stdin);    while(scanf("%d%d%d",&n,&m,&k)!=EOF&&(n||m||k))    {        for(int i=0; i<=m; i++)            seat[i]=n;        for(int i=0; i<k; i++)        {            scanf("%d%d%d",&order[i].start,&order[i].end,&order[i].number);            order[i].earn=(order[i].end-order[i].start)*order[i].number;            accept[i]=0;        }        for(int i=0; i<k; i++)        {            for(int j=i+1; j<k; j++)            {                if(order[i].start>order[j].start)                {                    ord temp;                    temp=order[i];                    order[i]=order[j];                    order[j]=temp;                }            }        }        max=0;        dfs(0,0,0);        printf("%d\n",max);    }    return 0;}

The Code imitates what others write. It mainly involves backtracking. at each stop station, you can choose to let passengers get on the bus or choose not. The global variable seat marks the number of people on the bus at all times, used to determine whether a passenger can get on the bus. In DFS, stop is the current station and cur is the current command. Write this for the time being and try again!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.