BZOJ1663: [Usaco2006 Open]赶集

来源:互联网 发布:法国房补 知乎 编辑:程序博客网 时间:2024/05/17 04:46

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.

每一年,约翰都会带着他的奶牛们去赶集.集会中一共有N(1≤N≤400)个商店,第i个商店会在特定的时间Pi(0≤Pi≤109)对当时在店里的顾客送出一份精美的礼物.约翰当然得到了这个消息,于是他希望能拿到尽量多的礼物送给他的奶牛们.也就是说,他想尽可能多地在某商店发放礼物的时候,正好呆在店里.
经过一定的调查,约翰弄清楚了从i号商店走到J号商店所需要的时间Ti,J(1≤Ti,J≤1000000).虽然乡间小路奇特的布局使得从i号商店走到j号商店的最短路不一定是直接连接这两个商店的那条,但约翰并不会选择那些会经过其他商店的路线,只是直接走到目标商店等待礼物的送出.此外,Ti,j并不一定等于Tj,i,由于约翰爬山的速度总是很慢.
约翰在时间0时于1号商店开始他的旅途.请你帮他设计一条路线来获得尽可能多的礼物.
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.

    第1行输入一个正整数N.接下来N行每行一个整数Pi.
    接下来N^2行,输入乃,J.先输入T1,1 T1,2 T1,3…T1,N再输入T2,1 T2,2…T2,N依次类推.

Output

  • Line 1: A single integer, containing the maximum number of prizes Farmer John can acquire.

    输出一个整数,即约翰最多能拿到的礼物的个数

Sample Input

4
13 9 19 3
0 10 20 3
4 0 11 2
1 15 0 12
5 5 13 0

Sample Output

3

题目传送门

题目标签上说是最短路,然后。。
我调了n次(n>=5),经过了各种WA,MLE,RE之后
成功TLE!

尼玛!不是说最短路吗
不过天无绝人之路,我看了看,咦?
这好像是很垃圾的dp啊
然后1A水过~~

We found the way when there was no way柳暗花明又一村                                      ——《The Vampire Diaries》

代码如下:

#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;int n;struct node{    int t,id;}a[2100000];bool cmp(node n1,node n2){return n1.t<n2.t;}int d[410][410],f[410];int main(){    scanf("%d",&n);    for(int i=1;i<=n;i++){        scanf("%d",&a[i].t);        a[i].id=i;    }    for(int i=1;i<=n;i++)        for(int j=1;j<=n;j++)            scanf("%d",&d[i][j]);    sort(a+1,a+1+n,cmp);    for(int i=1;i<=n;i++){        if(d[1][a[i].id]<=a[i].t) f[i]=1;        else f[i]=-9999999;        for(int j=1;j<i;j++)if(d[a[j].id][a[i].id]+a[j].t<=a[i].t)f[i]=max(f[i],f[j]+1);    }    int ans=0;    for(int i=1;i<=n;i++)ans=max(ans,f[i]);    printf("%d\n",ans);    return 0;}

by_lmy

阅读全文
0 0
原创粉丝点击