【题目链接】: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的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 764B】Timofey and cubes

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. Extjs grid 某列点击弹窗

    { header : "单号", tooltip : '单号', dataIndex : 'transportCode', width : 130, sortable : true ...

  2. Codeforces Round #332 (Div. 2)C. Day at the Beach 树状数组

    C. Day at the Beach   One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortuna ...

  3. TF101出现“DMClient已停止”处理办法

    设定->应用程式->全部->DMClient强制停止 然后 清除数据 然后 重开机 测试通过.

  4. oc31--new实现

    // // main.m // new方法实现原理 #import <Foundation/Foundation.h> #import "Person.h" int m ...

  5. mysql中DATETIME类型与TIMESTAMP的区别

    1.DATETIME的日期范围是1001--9999年,TIMESTAMP的时间范围是1970--2038年. 2.DATETIME存储时间与时区无关,TIMESTAMP存储时间与时区有关,显示的值也 ...

  6. js开发规范,在php上也适用

    本篇主要介绍JS的命名规范.注释规范以及框架开发的一些问题. 目录 1. 命名规范:介绍变量.函数.常量.构造函数.类的成员等等的命名规范 2. 注释规范:介绍单行注释.多行注释以及函数注释 3. 框 ...

  7. iOS 点击事件传递及响应

    1.iOS中的事件 iOS中的事件可以分为3大类型: 触摸事件 加速计事件 远程控制事件这里我们只讨论iOS中的触摸事件. 1.1响应者对象(UIResponder) 在iOS中不是任何对象都能处理事 ...

  8. B - Chat room

    Problem description Vasya has recently learned to type and log on to the Internet. He immediately en ...

  9. Java中last_insert_id的使用

    last_insert_id的作用是:在当前表中的主键是自增时,插入一条新记录的同时使用last_insert_id可以获取当前的新记录的主键id. 下面是一个例子: import java.sql. ...

  10. jboss-as-7.1.1.Final配置Jndi数据源(以mysql为例)

    1.获取mysql驱动,可以从mysql官方网站下载: http://dev.mysql.com/downloads/connector/j/ 2.进入jboss-as-7安装目录下的modules目 ...