首页 > 代码库 > 洛谷OJ 1280 尼克的任务 线性DP

洛谷OJ 1280 尼克的任务 线性DP

https://www.luogu.org/problem/show?pid=1280

 

 1 #include <bits/stdc++.h> 
 2 using namespace std;
 3 typedef long long ll;
 4 typedef pair<int,int> ii;
 5 const int N=2e5+20;
 6 const ll inf=2e15;
 7 //ìaòa:??3?k,k<=1e4??1¤×÷????[s,t],?3ê±?ìóDè???&&???D,?ò±?D?????ò???è???,1¤×÷ê±???a[1,N] ?ê×?′óμ????Dê±???
 8 //???Dê±??×?3¤,?ó×?D??|?μê±???′?é,f[i]=′óμúi???aê?1¤×÷μ?×?D?ê±??
 9 //f[i]=f[i+1] iê±?ì??è???
10 //f[i]=min(f[i],f[a[j].end]+a[j].long) j:iê±?ì?aê?μ?è???
11 //h[i] ±£′?iê±?ì?aê?μ?è???,á′ê?′?′¢
12 
13 struct node{
14     int s,t,dur;
15     int next;
16 }a[N];
17 int f[N],h[N],cnt;
18 void add(int x,int y)
19 {
20     cnt++;
21     a[cnt].dur=y;
22     a[cnt].next=h[x];
23     h[x]=cnt;
24 }
25 int main()
26 {
27     int n,k,x,y;
28     while(cin>>n>>k)
29     {
30         cnt=0;
31         memset(h,0,sizeof(h));
32         for(int i=1;i<=k;i++)
33         {
34             scanf("%d%d",&x,&y);
35             add(x,y);
36         }
37         for(int i=n;i>=1;i--)
38         {
39             if(h[i]==0)//iê±?ì?Tè??? 
40             {
41                 f[i]=f[i+1];
42                 continue;
43             }
44             int res=n+1;
45             for(int j=h[i];j;j=a[j].next)
46                 res=min(res,f[a[j].dur+i]+a[j].dur);
47             f[i]=res;
48         }
49         cout<<n-f[1]<<endl;
50     }
51     return 0;
52 } 

 

洛谷OJ 1280 尼克的任务 线性DP