【codeforces 793D】Presents in Bankopolis
【题目链接】:http://codeforces.com/contest/793/problem/D
【题意】
给你n个点,
这n个点
从左到右1..n依序排;
然后给你m条有向边;
然后让你从中选出k个点.
这k个点形成的一条路径;
且在路径中,一个被访问过的点不会经过两次或以上;
比如从1->3(1和3被访问过)
然后再从3->4(1,3,4都被访问过)
再从4->2(这时会又经过3号节点,而3号节点之前被访问过,所以不合法)
这就不是合法的;
【题解】
每次走完之后都有一个接下来能走的区间了;
比如当前走的区间为
[a,b]
然后你从b走到了a,b中的某个位置u(显然是往左走了);
则对应了两个状态
[a,u]和[u,b]
这里[a,u]只能是往左走的,而[u,b]只能是往右走的;
->区间DP;
设f[i][j][k][0..1]
表示走了i条边,然后当前能够走的区间为j,k;
然后
0表示当前起点在i,然后向右走;
1表示当前起点在j,然后向左走;
f[i][j][k][0]=min(f[i−1][j][u][1],f[i−1][u][k][0])+cost[j][u];
f[i][j][k][1]=min(f[i−1][j][u][1],f[i−1][u][k][0])+cost[j][u];
imax=k-1
然后对于k=1的情况直接输出0;
否则在做DP的过程中一边更新答案就好了
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,0,-1,0,-1,-1,1,1};
const int dy[9] = {0,0,-1,0,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 100;
const int INF = 0x3f3f3f3f;
int n,k,m,f[N][N][N][2],ans=INF;
vector <pii> G[N];
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
cin >> n >> k;
cin >> m;
if (k==1) return cout << 0 << endl,0;
rep1(i,1,m)
{
int x,y,z;
cin >> x >> y >> z;
G[x].pb(mp(y,z));
}
rep1(i,1,k-1)
{
rep2(l,n,0)
rep1(r,l+1,n+1)
{
f[i][l][r][0] = INF;
for (pii temp:G[l])
{
int y = temp.fi,cost = temp.se;
if (l<y && y < r)
{
f[i][l][r][0] = min(f[i][l][r][0],f[i-1][y][r][0]+cost);
f[i][l][r][0] = min(f[i][l][r][0],f[i-1][l][y][1]+cost);
}
}
f[i][l][r][1] = INF;
for (pii temp:G[r])
{
int y = temp.fi,cost = temp.se;
if (l<y && y < r)
{
f[i][l][r][1] = min(f[i][l][r][1],f[i-1][l][y][1]+cost);
f[i][l][r][1] = min(f[i][l][r][1],f[i-1][y][r][0]+cost);
}
}
if (i==k-1)
{
ans = min(ans,f[i][l][r][1]);
ans = min(ans,f[i][l][r][0]);
}
}
}
if (ans>=INF)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
【codeforces 793D】Presents in Bankopolis的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 764B】Timofey and cubes
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- C++对象内存布局 (二)
在上一篇文章中讨论了C++单一一般继承的对象内存布局http://www.cnblogs.com/uangyy/p/4621561.html 接下来继续讨论第二种情况: 2.单一的虚拟继承:有成员变量 ...
- golomb哥伦布编码——本质上就是通过0来区分商和余数
哥伦布编码是一个针对整数的变长编码方式,详细介绍可以看维基百科.这里简单介绍下: 哥伦布编码使用指定的整数 M 把输入的整数分成两部分:商数 q.余数 r. 商数当做一元编码,而余数放在后面做为可缩短 ...
- Codeforces--630C--Lucky Numbers(快速幂)
C - Lucky Numbers Crawling in process... Crawling failed Time Limit:500MS Memory Limit:65536K ...
- 3.2 手机中的数据库——SQLite
http://www.sqlite.org/download.html 截至我安装SQLite数据库为止的时间,最新的版本可以下载sqlite-dll-win64-x64-3200000.zip和sq ...
- mysql如何查询最新插入的数据
在MySQL中,使用auto_increment类型的id字段作为表的主键,并用它作为其他表的外键,形成“主从表结构”,这是数据库设计中常见的用法.但是在具体生成id的时候,我们的操作顺序一般是:先在 ...
- Appium - 命令行参数
1.cmd端口输入,appium -help参考帮助信息 2.Appium - 命令行参数 参数 默认 描述 举个例子 --shell 空值 进入REPL模式 --ipa 空值 (仅限IOS)ab ...
- C# 设置百分比保留2位数
double a=50; double b=100; 没有小数部分Label1.Text = (a / (a+b)).ToString("0%") 两位小数Label2 ...
- Mysql数据库系列
详情点击 MySQL基础 Mysql表操作 Mysql插入 更新 删除 查询操作 Mysql创建用户和授权 基本的Mysql语句 Mysql库的操作 Mysql表的操作 Mysql数据类型(一) My ...
- php解析 html类库 simple_html_dom
如果从字符串加载html文档,需要先从网络上下载.建议使用cURL来抓取html文档并加载DOM中. 查找html元素 可以使用find函数来查找html文档中的元素.返回的结果是一个包含了对象的数组 ...
- itext 生成doc文档 小结(自己备忘)
1.引入maven <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</ ...