Codeforces 793D Presents in Bankopolis DP
来源:互联网 发布:node会取代php吗 编辑:程序博客网 时间:2024/06/10 02:48
点击打开链接
题意:n个点n<=80,落在[1,n]线段上,有m条带权路径(u,v).
问能否找到长度为k的路径使得不存在已经访问过的x出现在路径的第i条边内min(u,v)<=x<=max(u,v) 如果有输出路径的最小权值?
从a->b a<b a之前的点无法在被访问, a>b a之后的点无法被访问,每增加一条边,能走的范围(区间)都会减小
dp[l][r][cur][k] 起点为cur,下一步的合法点在(l,r)内,长度为k的路径的最小权值 按cur的边合法的转移状态 O(N^4)记忆化即可
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N=82;int dp[N][N][N][N];//dp[l][r][cur][k] 起点为cur,下一步的合法点在(l,r)内,长度为k的路径的最小权值int n,k,m,g[N][N];int DP(int l,int r,int cur,int k){if(dp[l][r][cur][k]!=-1)return dp[l][r][cur][k];if(k==1){dp[l][r][cur][k]=0;return 0;}int res=1e8;for(int i=1;i<=n;i++){if(g[cur][i]){if(i>l&&i<r){int s=l,t=r;if(cur<i)s=cur;elset=cur;res=min(res,g[cur][i]+DP(s,t,i,k-1));}}}dp[l][r][cur][k]=res;return dp[l][r][cur][k];}int main(){while(cin>>n>>k>>m){int u,v,w;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)g[i][j]=1e8;memset(dp,-1,sizeof(dp));for(int i=0;i<m;i++){scanf("%d%d%d",&u,&v,&w);g[u][v]=min(g[u][v],w); //可能有重边 }int ans=1e8;for(int i=1;i<=n;i++)ans=min(ans,DP(0,n+1,i,k));if(ans>=1e8)ans=-1;cout<<ans<<endl;}return 0;}
0 0
- Codeforces 793D Presents in Bankopolis DP
- Codeforces 793D Presents in Bankopolis【Dp+记忆化搜索】
- 树形DP 【codeforces 793D】Presents in Bankopolis
- Codeforces 793 D. Presents in Bankopolis
- 793D Presents in Bankopolis
- Presents in Bankopolis CodeForces
- CF 793D Presents in Bankopolis
- CF-Tinkoff Challenge-Elimination Round-D-Presents in Bankopolis
- codeforces #326 D. Duff in Beach (dp)
- codeforces 161D D. Distance in Tree(树形dp)
- codeforces 161D Distance in Tree(树形dp)
- CodeForces 160D - Distance in Tree 树形DP
- codeforces 161D. Distance in Tree(树dp)
- codeforces 161D - Distance in Tree 树状DP
- codeforces 161D - Distance in Tree(树形dp)
- CodeForces 161D Distance in Tree(树形DP)
- codeforces 161D Distance in Tree (树形DP 经典题)
- codeforces#326-D - Duff in Beach- dp+分块
- webview加载html文件,如何调用原生态的代码的
- eclipse新浪云sae插件安装
- Shell 间接变量问题
- 实施CMMI3有感:CMMI绝对是治疗软件公司特大毒瘤的特效药!
- 155_集合_Collection集合的常用方法详解_03_contains
- Codeforces 793D Presents in Bankopolis DP
- 自定义AlertDialog在android-7.0上距离屏幕边界margin无法消除问题
- EmguCV 一些基本操作
- jq 获取option 选中的各种值
- "解决“就是当做信仰的存在
- 'OSError: .*.so: undefined symbol'
- 线性表—顺序存储结构-顺序表
- hadoop全分布式环境配置-访问slave节点路径不存在问题
- 大数相乘 例 n的阶乘