【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 ...
随机推荐
- 王立平--java se的简单项目创建以及具体解释
创建项目的简单步骤: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTUyNw==/font/5a6L5L2T/fontsize/400/ ...
- USACO Section 2.1 Ordered Fractions
/* ID: lucien23 PROG: frac1 LANG: C++ */ #include <iostream> #include <fstream> #include ...
- 【Linux】Linux下配置apache
一.获取软件: http://httpd.apache.org/ httpd-2.4.10.tar.gz 二.安装步骤: 解压源文件: 1) tar zvxf httpd-2.4.10.tar. ...
- luogu3119 草鉴定
题目大意 给出一个有向图,问将图中的哪一个边翻转,会使节点1所在的强连通分量内的节点数最多.输出这个节点数. 题解 让我们看看暴力怎么做,即枚举每一条边,将其翻转,然后求节点1所在强连通分量节点数,然 ...
- How to do IF NOT EXISTS in SQLite
http://stackoverflow.com/questions/531035/how-to-do-if-not-exists-in-sqlite How about this? INSERT O ...
- C# openfiledialog对文本框的操作//C#中OpenFileDialog的使用
在WebForm中提供了FileUpload控件来供我们选择本地文件,只要我们将该控件拖到页面上了,就已经有了选择本地文件的功能了.而在WinForm中,并没有为我们提供集成该功能的控件,但为我们提供 ...
- 胜利大逃亡(续)(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1429 #include <stdio.h> #include <queue> #incl ...
- myeclipse2014 破解步骤
1.打开破解文件夹Myeclipse 2014 patch,运行run.bat文件 2.在破解界面中,usercode随便输入, systemid 在右边的SystemId按钮处,点击. 将自动生成一 ...
- NGinx 负载均衡作用
1.负载均衡介绍: 负载均衡是由多台服务器以对称的方式组成一个服务器集合,每台服务器都具有等价的地位,都可以单独对外提供服务而无须其他服务器的辅助.其工作模式为将外部发送来的请求均匀分配到对称结构中的 ...
- .htaccess的基本用法与介绍
●自定义错误页 .htaccess的一个应用是自定义错误页面,这将使你可以拥有自己的.个性化的错误页面(例如找不到文件时),而不是你的服务商提供的错误页或没有任何页面.这会让你的网站在出错的时候看上去 ...