NC24770. [USACO 2010 Ope G]Water Slides
描述
By way of example, consider a small park that has 3 pools (pool id's shown in brackets) and four mini slides; K has the value 1 (fun values shown outside of brackets): [1] / \ 5 -> / \ <- 9 / \ [2]---3---[3] \__5__/ She alway starts at pool 1 and ends and pool 3. If she had her way, she'd ride direct from pool 1 to pool 2 and then on the higher-fun mini slide (with fun value 5) to slide 3 for a total fun value of 5+5=10. But, if she loses control at pool 1, she might slide directly from pool 1 to pool 3 for total fun 9. If she loses control at pool 2, she could reduce her total fun to just 5+3 = 8. Bessie wants to find the most fun she can have so she strives to choose 1->3 for a total fun of 9. If she loses control at pool 1 and ends up on mini slide 1->2, she knows she will not lose control at pool 2 and will end up with fun 10. Thus, she knows her minimum fun will always be at least 9.
输入描述
* Line 1: Three space separated integers: V, E, and K
* Lines 2..E + 1: Line i+1 contains three space separated integers: and
输出描述
* Line 1: A single line with a single integer that is the minimum fun that Bessie can guarantee she can have.
示例1
输入:
3 4 1 2 3 5 1 2 5 1 3 9 2 3 3
输出:
9
C++14(g++5.4) 解法, 执行用时: 141ms, 内存消耗: 8664K, 提交时间: 2019-09-28 11:19:12
#include<iostream> #include<cstdio> #include<cstring> using namespace std; long long cnt,s[150001][3],o[50005],t[50005][11],m,n,k; void jia(int a,int b,int c){ cnt++; s[cnt][0]=b; s[cnt][1]=c; s[cnt][2]=o[a]; o[a]=cnt;//邻接链表 } long long dfs(long long num,long long k){ if(t[num][k]!=0)//记忆化搜索 return t[num][k]; long long a=o[num]; while(a!=-1){ long long v=0; v+=dfs(s[a][0],k)+s[a][1]; if(v>t[num][k]) t[num][k]=v; a=s[a][2]; } if(k>0){ a=o[num]; while(a!=-1){ long long v=dfs(s[a][0],k-1)+s[a][1]; if(v<t[num][k]) t[num][k]=v; a=s[a][2]; } } return t[num][k]; } int main(){ int i,j,k,a,b,c; cin>>n>>m>>k; memset(o,-1,sizeof(o)); for(i=1;i<=m;i++){ scanf("%d%d%d",&a,&b,&c); jia(a,b,c); } dfs(1,k); cout<<t[1][k]; }
C++11(clang++ 3.9) 解法, 执行用时: 139ms, 内存消耗: 7232K, 提交时间: 2020-02-26 20:09:06
#include<bits/stdc++.h> using namespace std; #define ll long long const int N=50005; const int M=150005; struct edge { int to,next; ll w; }a[M]; int n,m,K,u,v,head[N],cnt; ll w,dp[11][N]; ll dfs(int k,int u) { if(dp[k][u]) return dp[k][u]; for(int e=head[u];e;e=a[e].next) dp[k][u]=max(dp[k][u],dfs(k,a[e].to)+a[e].w); if(k) for(int e=head[u];e;e=a[e].next) dp[k][u]=min(dp[k][u],dfs(k-1,a[e].to)+a[e].w); return dp[k][u]; } int main() { scanf("%d%d%d",&n,&m,&K); for(int i=1;i<=m;i++) scanf("%d%d%lld",&u,&v,&w),a[++cnt]=(edge){v,head[u],w},head[u]=cnt; return printf("%lld\n",dfs(K,1)),0; }