bzoj1663: [Usaco2006 Open]赶集
Description
Every year, Farmer John loves to attend the county fair. The fair has N booths (1 <= N <= 400), and each booth i is planning to give away a fabulous prize at a particular time P(i) (0 <= P(i) <= 1,000,000,000) during the day. Farmer John has heard about this and would like to collect as many fabulous prizes as possible to share with the cows. He would like to show up at a maximum possible number of booths at the exact times the prizes are going to be awarded. Farmer John investigated and has determined the time T(i,j) (always in range 1..1,000,000) that it takes him to walk from booth i to booth j. The county fair's unusual layout means that perhaps FJ could travel from booth i to booth j by a faster route if he were to visit intermediate booths along the way. Being a poor map reader, Farmer John never considers taking such routes -- he will only walk from booth i to booth j in the event that he can actually collect a fabulous prize at booth j, and he never visits intermediate booths along the way. Furthermore, T(i,j) might not have the same value as T(j,i) owing to FJ's slow walking up hills. Farmer John starts at booth #1 at time 0. Help him collect as many fabulous prizes as possible.
Input
* Line 1: A single integer, N.
* Lines 2..1+N: Line i+1 contains a single integer, P(i). * Lines 2+N..1+N+N^2: These N^2 lines each contain a single integer T(i,j) for each pair (i,j) of booths. The first N of these lines respectively contain T(1,1), T(1,2), ..., T(1,N). The next N lines contain T(2,1), T(2,2), ..., T(2,N), and so on. Each T(i,j) value is in the range 1...1,000,000 except for the diagonals T(1,1), T(2,2), ..., T(N,N), which have the value zero.
Output
* Line 1: A single integer, containing the maximum number of prizes Farmer John can acquire.
输出一个整数,即约翰最多能拿到的礼物的个数
Sample Input
13 //第一个东西,在第一个地点第13分钟掉下来
9 //第二个东西,在第二个地点第9分钟掉下来
19 //第三个东西,在第三个地点第19分钟掉下来
3 //这是第四个东西.
0 //这下面有4*4行,代表每个点到其它点的要花的时间,自己到自己的时间为0。
10
20
3
4
0
11
2
1
15
0
12
5
5
13
0
Sample Output
Farmer John first walks to booth #4 and arrives at time 3, just in
time to receive the fabulous prize there. He them walks to booth
#2 (always walking directly, never using intermediate booths!) and
arrives at time 8, so after waiting 1 unit of time he receives the
fabulous prize there. Finally, he walks back to booth #1, arrives
at time 13, and collects his third fabulous prize.
HINT
想写最短路然鹅不会.....
但是可以dp
设$f[i]$表示走到点$i$时最多可以拿几个礼物
当$time[j]+d[j][i]<=time[i]$时,就可以转移状态$f[i]=max(f[i],f[j]+1)$
每个点事先按时间排序就好了
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int max(int a,int b){return a>b?a:b;} 7 #define N 405 8 struct data{int t,id;}a[N]; 9 bool cmp(const data &A,const data &B){return A.t<B.t;} 10 int n,d[N][N],ans,f[N]; 11 int main(){ 12 scanf("%d",&n); 13 for(int i=1;i<=n;++i) 14 scanf("%d",&a[i].t),a[i].id=i; 15 for(int i=1;i<=n;++i) 16 for(int j=1;j<=n;++j) 17 scanf("%d",&d[i][j]); 18 sort(a+1,a+n+1,cmp); 19 for(int i=1;i<=n;++i){ 20 if(d[1][a[i].id]<=a[i].t) f[i]=1; 21 for(int j=1;j<i;++j) 22 if(a[j].t+d[a[j].id][a[i].id]<=a[i].t) 23 f[i]=max(f[i],f[j]+1); 24 ans=max(ans,f[i]); 25 }printf("%d",ans); 26 return 0; 27 28 }