POJ 1040 典型回溯!
来源:互联网 发布:java中^是什么 编辑:程序博客网 时间:2024/06/02 15:46
代码如下:
station[i]表示当前站点有多少人。
#include<cstdio>
#include<algorithm>
using namespace std;
#define BUFSIZE 25
struct orders{
int from;
int to;
int person;
int value;
}order[BUFSIZE];
int station[BUFSIZE];
int m,n,l,result,best;
int dfs(int ordernum,int remain)
{
if(ordernum == l){
if(result>best)best = result;
return 0;
}
remain -= order[ordernum].value;
for(int i = order[ordernum].from;i < order[ordernum].to;i++)
station[i]+=order[ordernum].person;
bool flag = false;
for(int i= order[ordernum].from;i < order[ordernum].to;i++)
if(station[i]>m)flag=true;
if(!flag){
result += order[ordernum].value;
dfs(ordernum+1,remain);
result -= order[ordernum].value;
}
for(int i = order[ordernum].from;i < order[ordernum].to;i++)
station[i]-=order[ordernum].person;
if(remain+result>best)
dfs(ordernum+1,remain);
return 0;
}
bool mycomp(struct orders a,struct orders b)
{
return a.value>b.value;
}
int main()
{
scanf("%d%d%d",&m,&n,&l);
while(!(m==0&&n==0&&l==0)){
int remain = 0;
for(int i=0;i<l;i++){
scanf("%d%d%d",&order[i].from,&order[i].to,&order[i].person);
order[i].value = (order[i].to - order[i].from)*order[i].person;
remain += order[i].value;
}
fill_n(station,BUFSIZE,0);
sort(order,order+l,mycomp);
best = 0;
result = 0;
dfs(0,remain);
printf("%d/n",best);
scanf("%d%d%d",&m,&n,&l);
}
return 0;
}
- POJ 1040 典型回溯!
- UVA 301 POJ 1040- Transportation DFS 回溯
- poj 2488 回溯法
- POJ 2488 回溯
- poj 2488 深搜+回溯
- poj-2531-暴搜+回溯
- POJ 2488 DFS回溯
- POJ 1315 DFS+回溯
- POJ 2488 DFS+回溯
- poj 1416 dfs 回溯
- poj 3465 回溯+贪心
- poj 3009 DFS +回溯
- poj 2488 dfs+回溯
- poj 1935(搜索+回溯)
- poj 3740 dfs回溯
- POJ 3009 深搜+回溯
- BFS + 回溯路径 POJ
- POJ 2948 - 典型动态规划
- 程序员 每天鼓励自己的80句话
- CRT与Windows
- Linux下的FireBird安装
- C#中转换Office中的COM组件方法
- Flickr被和谐掉了...
- POJ 1040 典型回溯!
- vc6.0快捷键大全
- POJ 1047 没什么难度,注意读懂题。
- vs2005常用快捷键
- 网站实时监控系统的设计与实现
- 求助!关于DES的
- linux 飞鸽
- 信道容量的迭代算法实现
- Linux下firebird无法识别 localhost 的解决