(简单) 搜索 HOJ 1044 Transportation
2014-02-08 09:51
405 查看
Transportation
My Tags | (Edit) |
---|
Source : ACM ICPC Central European Regional 1995 | |||
Time limit : 5 sec | Memory limit : 32 M |
Input
The input is divided into blocks. The first line in each block contains three integers: passenger capacity n[/i][/b] of the train (n > 0), the number of the city B[/i][/b] station, and the number of ticket orders from all stations. The next lines contain the ticket orders. Each ticket order consists of three integers: starting station, destination station, number of passengers. In one block there can be a maximum of 22 orders. The number of the city B[/i][/b] station will be at most 7. The block where all three numbers in the first line are equal to zero denotes the end of the input.Output
The output file consists of lines corresponding to the blocks of the input file except the terminating block. Each such line contains the biggest possible total earning.Example
Input file:[/b]10 3 4 0 2 1 1 3 5 1 2 7 2 3 10 10 5 4 3 5 10 2 4 9 0 2 5 2 5 8 0 0 0Output file:[/b]
19 34
题意:一辆汽车沿途会经过一些车站,他可以选择让一组人全部上车或者不让他们上车,求到终点后,最多能赚多少钱 (每位乘客的需要给的费用为途径车站包括下车,不包括上车的站的站的数量)思路: 就是搜索嘛,因为最多有22组人,所以可用二进制来表示有没有选改组,在深搜的时候,要检查当前站有没有人下车,剪枝的条件是如果当前所得的钱加上所有剩下最多能得到的钱都不能比目前的ans大的话,就没有必要再递归下去了
代码:#include<iostream>#include<cstdio>#include<string.h>#include<algorithm>#include<string>#include<queue>using namespace std;#define MAX 1000+10#define MOD 100000000const int inf = 0x7fffffff;
int V,m,n;struct Station{ int start; int target; int person; int earn;}station[25];
bool cmp(const Station& s1,const Station& s2){ if (s1.start==s2.start) return s1.target<s2.target; return s1.start<s2.start;}
int ans;void dfs(int cur,int choose,int state,int cap,int earn,int rest){ if (earn+rest<=ans) return; if (cur>=n) { if (ans<earn) ans = earn; return; } for (int i = 0 ; i < choose ; ++i) if (state&(1<<i) && station[i].target==cur) { cap += station[i].person; state -= (1<<i); } if (choose < m && cur==station[choose].start) { if (cap-station[choose].person>=0) dfs(cur,choose+1,state+(1<<choose),cap-station[choose].person,earn+station[choose].earn,rest-station[choose].earn); dfs(cur,choose+1,state,cap,earn,rest-station[choose].earn); } else if (cur<station[choose].start || choose>=m) dfs(cur+1,choose,state,cap,earn,rest);}
int main(){ while (scanf("%d%d%d",&V,&n,&m)==3) { if (V+n+m==0) return 0; int sum = 0; for (int i = 0 ; i < m ; ++i) { scanf("%d%d%d",&station[i].start,&station[i].target,&station[i].person); station[i].earn = (station[i].target-station[i].start)*station[i].person; sum += station[i].earn; } sort(station,station+m,cmp); ans = 0; dfs(0,0,0,V,0,sum); printf("%d\n",ans); }}
相关文章推荐
- HOJ 1440 Knight Moves -------简单搜索 BFS 求l两点之间最小的到达步数
- (简单) 搜索 HOJ 1097 Robot
- (简单) 搜索 HOJ 1105 Egyptian Multiplication
- (简单) 搜索 最短路 HOJ 1121 Erdos Numbers
- poj1111 hoj 1639 Image Perimeters 简单搜索
- (简单)搜索 HOJ 1022 Spacecraft Malfunction
- hdu 4158 hoj 2581 Go 简单搜索
- (简单)搜索 HOJ 1030 Labyrinth
- (简单) 搜索 HOJ 1048 Cipher
- (简单) 搜索 HOJ 1066 Substrings
- (简单)搜索 HOJ 1070 Word
- (简单)搜索 HOJ 1075 The Castle
- Go hdu 4158 hoj 简单搜索
- (简单) 搜索 HOJ 1085 Finding Rectangles
- (简单)搜索 HOJ 1692 Choose Your Own Adventure
- poj 3126 Prime Path([kuangbin带你飞]专题一 简单搜索)
- 几句话描述简单算法——排序与搜索
- 最值得一看的几条简单的谷歌 Google 搜索技巧,瞬间提升你的网络搜索能力!
- HOJ-10513 Allocation Scheme[简单DFS]
- [kuangbin带你飞]专题1 简单搜索 I - Fire Game FZU - 2150