原题链接:http://codeforces.com/contest/580/problem/D

题意:

给你一些一个有向图,求不超过m步的情况下,能获得的最大权值和是多少,点不能重复走。

题解:

令$dp[u][s]$为在节点u的时候状态是s的最大值。利用spfa的松弛操作来转移。

代码:

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#define MAX_S 1<<19
#define MAX_N 22
using namespace std; typedef long long ll; struct edge {
public:
int to;
ll cost; edge(int t, ll c) : to(t), cost(c) { } edge() { }
}; vector<edge> G[MAX_N];
ll g[MAX_N][MAX_N];
ll a[MAX_N];
int n,m,k; int ones[MAX_S]; ll dp[MAX_N][MAX_S]; struct node {
public:
ll s;
int u; node(ll ss, int uu) : s(ss), u(uu) { } node() { }
}; queue<node> que;
bool inQue[MAX_N][MAX_S]; int main() {
cin.sync_with_stdio(false);
cin >> n >> m >> k;
for (int i = ; i < ( << n); i++) {
int x = i;
while (x)ones[i] += (x & ), x >>= ;
}
for (int i = ; i < n; i++)
cin >> a[i];
for (int i = ; i < k; i++) {
int x, y;
ll c;
cin >> x >> y >> c;
x--, y--;
g[y][x] = c;
}
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
G[i].push_back(edge(j, g[i][j]));
ll ans = ;
for (int i = ; i < n; i++) {
dp[i][ << i] = a[i];
que.push(node( << i, i));
inQue[i][ << i] = ;
}
while (!que.empty()) {
node now = que.front();
que.pop();
ll s = now.s;
int u = now.u;
inQue[u][s] = ;
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i].to;
ll c = G[u][i].cost;
if (s & ( << v))continue;
ll ns = s | ( << v);
if (ones[ns] > m)continue;
if (dp[v][ns] < dp[u][s] + a[v] + c) {
dp[v][ns] = dp[u][s] + a[v] + c;
que.push(node(ns, v));
inQue[v][ns] = ;
}
}
}
for (int i = ; i < n; i++)
for (int s = ; s < ( << n); s++)
if (ones[s] == m)
ans = max(ans, dp[i][s]);
cout << ans << endl;
return ;
}

Codeforces Round #321 (Div. 2) Kefa and Dishes 状压+spfa的更多相关文章

  1. Codeforces Round #222 (Div. 1) C. Captains Mode 状压

    C. Captains Mode 题目连接: http://codeforces.com/contest/377/problem/C Description Kostya is a progamer ...

  2. Codeforces Round #297 (Div. 2) [ 折半 + 三进制状压 + map ]

    传送门 E. Anya and Cubes time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  3. Codeforces Round #321 (Div. 2) Kefa and Company 二分

    原题链接:http://codeforces.com/contest/580/problem/B 题意: 给你一个集合,集合中的每个元素有两个属性,$m_i,s_i$,让你求个子集合,使得集合中的最大 ...

  4. Codeforces Round #321 (Div. 2) Kefa and First Steps 模拟

    原题连接:http://codeforces.com/contest/580/problem/A 题意: 给你一个序列,问你最长不降子串是多长? 题解: 直接模拟就好了 代码: #include< ...

  5. Codeforces Round #321 (Div. 2) Kefa and Park 深搜

    原题链接: 题意: 给你一棵有根树,某些节点的权值是1,其他的是0,问你从根到叶子节点的权值和不超过m的路径有多少条. 题解: 直接dfs一下就好了. 代码: #include<iostream ...

  6. Codeforces Round #302 (Div. 1) C - Remembering Strings 状压dp

    C - Remembering Strings 思路:最关键的一点是字符的个数比串的个数多. 然后就能状压啦. #include<bits/stdc++.h> #define LL lon ...

  7. Codeforces Round #585 (Div. 2) E. Marbles (状压DP)

    题目:https://codeforc.es/contest/1215/problem/E 题意:给你一个序列,你可以交换相邻的两个数,要达到一个要求,所有相同的数都相邻,问你交换次数最少是多少 思路 ...

  8. Codeforces Round #585 (Div. 2) E. Marbles (状压DP),BZOJ大理石(同一道题)题解

    题意 林老师是一位大理石收藏家,他在家里收藏了n块各种颜色的大理石,第i块大理石的颜色为ai.但是林老师觉得这些石头在家里随意摆放太过凌乱,他希望把所有颜色相同的石头放在一起.换句话说,林老师需要对现 ...

  9. Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp

    题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...

随机推荐

  1. TCP的三次握手和四次握手

    三次握手(建立连接) 首先,服务器进程(B)先创建传控制块TCB(用来存储连接信息,如连接表,发送和接收序号等),准备接收客户进程(A)的请求.然后服务器进程处于LISTEN(收听)状态,等待客户的连 ...

  2. 大家好,我是一个JAVA初学者,想在这里记下自己学习过程中的点点滴滴,请多多关照

    大家好,我是一个JAVA初学者,想在这里记下自己学习JAVA的点点滴滴,请多多关照. 以前一直在QQ空间里记录的,但感觉有些麻烦,而且有些东西自己理解的并不完善甚至都不正确,现在开始在这里重新记录,从 ...

  3. mysql查询的语法

    单表查询语法 SELECT DISTINCT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条 ...

  4. Jenkins自动化搭建测试环境(二)

    Fork项目 找到项目 单击Fork 这时,会发送一个邮件到你的git邮箱中,点击链接即可完成fork 这样,这个工程就已经fork到自己的git上了 然后就可以下载这个工程到本机了 这里我们需要使用 ...

  5. angularjs报错问题记录

    1.[$injector:unpr]:没有找到注入的东西    2.$compile:multidir:多指令编译错误.    3.[ng:areq]:重复定义了ng-controller.    4 ...

  6. 软件工程师应该关注的web攻击手段

    1.SQL注入------常见的安全性问题. 解决方案:前端页面需要校验用户的输入数据(限制用户输入的类型.范围.格式.长度),不能只靠后端去校验用户数据.一来可以提高后端处理的效率,二来可以提高后端 ...

  7. 正在创建模型,此时不可使用上下文“的解决办法。 正在创建模型,此时不可使用上下文。如果在 OnModelCreating 方法内使用上下文或如果多个线程同时访问同一上下文实例,可能引发此异常。请注意不

    //默认为: Database.SetInitializer<testContext>(null);//这里报错, 检查原因:catch(Exception ex) 错误提示: 基础连接未 ...

  8. [转]netstat -tnl 列出监听中的连接,查看端口是否开启

    任何网络服务的后台进程都会打开一个端口,用于监听接入的请求. 这些正在监听的套接字也和连接的套接字一样,也能被 netstat 列出来. 参数 tnl, 现在我们可以看到处于监听状态的 TCP 端口和 ...

  9. mq类----1

    MQ.php <?php /** * Created by PhpStorm. * User: brady * Date: 2017/12/6 * Time: 14:42 * * amqp协议操 ...

  10. oracle报错处理

    oracle安装过程报错 报错一:Error in invoking target 'install' of makefile '/u01/app/oracle/product/11.2.0/dbho ...