UVA 301 Transportation (backtracking)

Source: Internet
Author: User



In fact, the retrospective is very simple is the violence + judgment conditions, the conditions of the establishment of continued violence, conditions are not set to return to the next violence, the difficulty is to find the conditions for the establishment,



In my opinion, backtracking is the pruning of violence, to find out the condition of the establishment of the basic solution ...



Ideas:



This question should find out the current bus order and the number of people can get on the train,, I used num Array to mark the number of each station



Post code:




#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int num[10];
struct 
{
	int x,y,nu;
}node[30];
int m,cnt,max,n,sum;
void solve(int t)
{
	for(int i=t; i<m; i++)
	{
		int flag = 0;
		for(int k=node[i].x; k<node[i].y; k++)
		{
			if(num[k]+node[i].nu > sum)
			{
				flag = 1;
				break;
			}
		}
		if(flag == 0)
		{
			for(int k=node[i].x; k<node[i].y; k++)
			{
				num[k]+=node[i].nu;
			}
			cnt += (node[i].y - node[i].x)*node[i].nu;
			if(cnt > max)
				max = cnt;
			solve(i+1);
			for(int k=node[i].x; k<node[i].y; k++)
			{
				num[k]-=node[i].nu;
			}
			cnt -= (node[i].y - node[i].x)*node[i].nu;
		}
	}
	return ;
}
int main()
{
	int i;
	while(scanf("%d%d%d",&sum,&n,&m),n||m||sum)
	{
		max = cnt = 0;
		memset(num,0,sizeof(num));
		memset(node,0,sizeof(node));
		for(i=0; i<m; i++)
		{
			scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].nu);
		}
		solve(0);
		printf("%d\n",max);
	}
}






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



UVA 301 Transportation (backtracking)


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.