原题链接: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. 汇编语言 Part 1——简介、基本语法、内存分段与内存地址

    简介 什么是汇编语言? 汇编语言是一种低级的编程语言,在程序的语句和体系结构的机器代码指令之间有很强的对应关系. 每种汇编语言都特定于特定的计算机体系结构,但需要解释或编译.汇编语言也可以称为符号机器 ...

  2. python爬取+使用网易卡搭作品数量api

    第一步,当然是打开浏览器~ 然后打开卡搭~ 看着熟悉的界面,是不是有点不知所措? 这就对了,咱找点事情干干. 随便找个倒霉蛋,比如这位:"混世大王",打开他的主页! 按下f12(我 ...

  3. 安装lwqq

    $ git clone https://github.com/xiehuc/pidgin-lwqq.git $ cd pidgin-lwqq/ $ sudo apt-get install cmake ...

  4. HDU 5396 区间DP 数学 Expression

    题意:有n个数字,n-1个运算符,每个运算符的顺序可以任意,因此一共有 (n - 1)! 种运算顺序,得到 (n - 1)! 个运算结果,然后求这些运算结果之和 MOD 1e9+7. 分析: 类比最优 ...

  5. spring boot + activeMq 邮件服务

    引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>s ...

  6. LRESULT CALLBACK WndProc 窗口程序的 重点

    LRESULT CALLBACK WndProc Windows程序所作的一切,都是回应发送给窗口消息处理程序的消息.这是概念上的主要难点之一,在开始写作Windows程序之前,必须先搞清楚. 窗口消 ...

  7. monkey测试工具与常用的linux命令

    Monkey测试工具 说明:monkey是一个安卓自带的命令行工具,可以模拟用户向应用发起一定的伪随机事件.主要用于对app进行稳定性测试与压力测试. 实现:首先需要安装一个ADB工具,安装完之后,需 ...

  8. python中用exit退出程序

    在python中运行一段代码,如果在某处已经完成整次任务,可以用exit退出整个运行.并且还可以在exit()的括号里加入自己退出程序打印说明.不过注意在py3中要加单引号或双引号哦!

  9. linux下 export只能设定临时变量

    今天在调用ABBYY API的时候,需要传递APPID和APPPASSWD给系统环境才能够执行相应的python调用代码. 设置之后,因为写代码自己关掉了terminal,后面直接运行报错,访问权限不 ...

  10. 山东理工大学第七届ACM校赛-G 飞花的传送门

    G - 飞花的传送门 飞花壕最近手头比较宽裕,所以想买两个传送门来代步(夏天太热,实在是懒得走路).平面上有N个传送门,飞花壕想要挑两个距离最远的传送门带回家(距离为欧几里得距离,即两点之间直线距离) ...