[usaco]4.1.3 Fence Rails 多维背包问题,dfsid
2011-09-12 19:32
453 查看
Fence Rails
Burch, Kolstad, and Schrijvers
Farmer John is trying to erect a fence around part of his field. He has decided on the shape of the fence and has even already installed the posts, but he's having a problem with the rails. The local lumber store has dropped off boards of varying lengths; Farmer
John must create as many of the rails he needs from the supplied boards.
Of course, Farmer John can cut the boards, so a 9 foot board can be cut into a 5 foot rail and a 4 foot rail (or three 3 foot rails, etc.). Farmer John has an `ideal saw', so ignore the `kerf' (distance lost during sawing); presume that perfect cuts can
be made.
The lengths required for the rails might or might not include duplicates (e.g., a three foot rail and also another three foot rail might both be required). There is no need to manufacture more rails (or more of any kind of rail) than called for the list
of required rails.
PROGRAM NAME: fence8
INPUT FORMAT
Line 1: N (1 <= N <= 50), the number of boards
Line 2..N+1: N lines, each containing a single integer that represents the length of one supplied board
Line N+2: R (1 <= R <= 1023), the number of rails
Line N+3..N+R+1: R lines, each containing a single integer (1 <= ri <= 128) that represents the length of a single required fence rail
SAMPLE INPUT (file fence8.in)
4
30
40
50
25
10
15
16
17
18
19
20
21
25
24
30
OUTPUT FORMAT
A single integer on a line that is the total number of fence rails that can be cut from the supplied boards. Of course, it might not be possible to cut all the possible rails from the given boards.
SAMPLE OUTPUT (file fence8.out)
7
HINTS (use them carefully!)
Fence Rails: Hint 1
This is a high dimensionality multiple knapsack problem, so we just have to test the cases. Given that the search space has a high out-degree, we will use depth first search with iterative deepening in order to limit the depth of the tree. However, straight
DFSID will be too slow, so some tree-pruning is necessary.
---------------------------------------------------------
多维背包问题
使用dfs遍历,
首先把rails排序,然后,从最大的rail开始遍历。
对于第k个rail,遍历所有的板子,然后遍历地k-1个rail。如果所有的rail都有解法,则输出最开始rail。
usaco称之为DFSID
Burch, Kolstad, and Schrijvers
Farmer John is trying to erect a fence around part of his field. He has decided on the shape of the fence and has even already installed the posts, but he's having a problem with the rails. The local lumber store has dropped off boards of varying lengths; Farmer
John must create as many of the rails he needs from the supplied boards.
Of course, Farmer John can cut the boards, so a 9 foot board can be cut into a 5 foot rail and a 4 foot rail (or three 3 foot rails, etc.). Farmer John has an `ideal saw', so ignore the `kerf' (distance lost during sawing); presume that perfect cuts can
be made.
The lengths required for the rails might or might not include duplicates (e.g., a three foot rail and also another three foot rail might both be required). There is no need to manufacture more rails (or more of any kind of rail) than called for the list
of required rails.
PROGRAM NAME: fence8
INPUT FORMAT
Line 1: N (1 <= N <= 50), the number of boards
Line 2..N+1: N lines, each containing a single integer that represents the length of one supplied board
Line N+2: R (1 <= R <= 1023), the number of rails
Line N+3..N+R+1: R lines, each containing a single integer (1 <= ri <= 128) that represents the length of a single required fence rail
SAMPLE INPUT (file fence8.in)
4
30
40
50
25
10
15
16
17
18
19
20
21
25
24
30
OUTPUT FORMAT
A single integer on a line that is the total number of fence rails that can be cut from the supplied boards. Of course, it might not be possible to cut all the possible rails from the given boards.
SAMPLE OUTPUT (file fence8.out)
7
HINTS (use them carefully!)
Fence Rails: Hint 1
This is a high dimensionality multiple knapsack problem, so we just have to test the cases. Given that the search space has a high out-degree, we will use depth first search with iterative deepening in order to limit the depth of the tree. However, straight
DFSID will be too slow, so some tree-pruning is necessary.
---------------------------------------------------------
多维背包问题
使用dfs遍历,
首先把rails排序,然后,从最大的rail开始遍历。
对于第k个rail,遍历所有的板子,然后遍历地k-1个rail。如果所有的rail都有解法,则输出最开始rail。
usaco称之为DFSID
/* ID:yunleis2 PROG:fence8 LANG:C++ */ #include<iostream> #include<fstream> using namespace std; const int MAXB=50; const int MAXR=1024; int boards[MAXB]; int remains[MAXB]; int rails[MAXR]; int b; int r; int board_sum=0; int result=0; int waste_max=0; int from[MAXR]; fstream fout("fence8.out",ios::out); inline void swap(int * a,int * b){ int t=*a; *a=*b; *b=t; // *a=*a+*b; // *b=*a-*b; // *a=*a-*b; } int partiton(int * a,int p,int r) { int x=a[r]; int i=p-1; for(int j=p;j<r;j++){ if(a[j]<x){ ++i; swap(a+j,a+i); } } swap(a+i+1,a+r); return i+1; } void quicksort(int * a,int p,int r) { if(p<r) { int q=partiton(a,p,r); quicksort(a,p,q-1); quicksort(a,q+1,r); } } void dfs(int k,int waste){ if(k<0){ fout<<result+1<<endl; exit(0); } int i=0; if(k!=result&&rails[k]==rails[k+1]) i=from[k+1]; for(;i<b;i++){ if(remains[i]>=rails[k]){ from[k]=i; remains[i]-=rails[k]; if(remains[i]<rails[0])waste+=remains[i]; if(waste>waste_max){ remains[i]+=rails[k]; waste-=remains[i]; continue; } dfs(k-1,waste); remains[i]+=rails[k]; } } } int main() { fstream fin("fence8.in",ios::in ); fin>>b; for(int i=0;i<b;i++) { fin>>boards[i]; board_sum+=boards[i]; remains[i]=boards[i]; } fin>>r; for(int i=0;i<r;i++) fin>>rails[i]; quicksort(boards,0,b-1); quicksort(rails,0,r-1); int rail_sum=0; for(int i=0;i<r;i++){ rail_sum+=rails[i]; if(rail_sum>board_sum) { rail_sum-=rails[i]; r=i; break; } } for(int i=r-1;i>=0;--i){ result=i; waste_max = board_sum - rail_sum; rail_sum -= rails[i]; dfs(i,0); } fout<<0<<endl; return 0; }
相关文章推荐
- USACO 6.3.1 Fence Rails 01多背包问题(BS+IDDFS)
- USACO Section 4.1 Fence Rails - 用DFSID解多维背包..
- http://poj.org/problem?id=3628 背包问题还有一个地方没想明白 直接DFS()
- ZOJ 3631超大背包问题(DFS || 折半搜索)
- 一中OJ #1193 倒水问题[1] | IDDFS-迭代加深 + 二分猜答案 | 解题报告
- 背包问题以及DFS算法均可解之1014
- 迭代加深、IDDFS解决埃及分数问题
- vijos1426兴奋剂检查(多维费用的背包问题+状态压缩+hash)
- 0-1背包问题(dfs记忆化搜索写法)
- [DFS]FJSDFZOJ 1420 0/1背包问题
- USACO--3.1Score Inflation+完全背包问题
- vijos1426兴奋剂检查(多维费用的背包问题+状态压缩+hash)
- USACO 1.4 Mother's Milk 母亲的牛奶(经典的dfs倒水问题)
- USACO4.1 Fence rails DFSID
- USACO-Section3.1 Contact【动态规划(背包问题)】
- poj 1014 Dividing 背包问题(dfs)(其实我用最笨的方法写的)
- USACO 4.1 Fence rails(迭代加深DFS+二分)
- 旅游背包(多维有界的背包问题)
- dfs 简单背包问题
- USACO5.3 IDDFS_强连通_二维树状数组_斐蜀定理_矩形切割