原题链接: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. jQuery获取动态添加的元素,live和on的区别

    今天给大家说一下如果用jQuery获取动态添加的元素,通常如果你在网页上利用jQuery添加一个元素,那么用平常的jQuery获取元素的方法无效的获取不到的.可以用以下的方法获取动态元素!假设我们现在 ...

  2. 图解Disruptor框架(一):初识Ringbuffer

    图解Disruptor框架(一):初识Ringbuffer 概述 1. 什么是Disruptor?为什么是Disruptor? Disruptor是一个性能十分强悍的无锁高并发框架.在JUC并发包中, ...

  3. POJ:2586-Y2K Accounting Bug

    Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Description Accounting for Computer Machi ...

  4. Linux下查看USB设备信息

    首先需要将usbfs挂载一下,然后才能查看.$ mount -t usbfs none /proc/bus/usb$ cat  /proc/bus/usb/devices或者在文件(/etc/fsta ...

  5. Linux学习-核心编译的前处理与核心功能选择

    硬件环境检视与核心功能要求 根据自己的需求来确定编译的选项 保持干净原始码: make mrproper 我们还得要处理一下核心原始码底下的残留文件才行!假设我们是第一次 编译, 但是我们不清楚到底下 ...

  6. Educational Codeforces Round 20 B. Distances to Zero

    B. Distances to Zero time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. PHP 格式化显示时间 date()函数

    Y 4位数字年,y为2位数字,如99即1999年 m 数字月份,前面有前导0,如01.n 为无前导0数字月份 F 月份,完整的文本格式,例如 January 或者 March M 三个字母缩写表示的月 ...

  8. [译] Pandas中根据列的值选取多行数据

    # 选取等于某些值的行记录 用 == df.loc[df['column_name'] == some_value] # 选取某列是否是某一类型的数值 用 isin df.loc[df['column ...

  9. 简单使用jstl实现敏感字替换

    package com.ceshi; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; ...

  10. 使用grunt实现自动化单元测试

    闲话不多说~ 使用步骤 1.安装插件 npm install grunt-contrib-qunit --save-dev 2.加载包含 "qunit" 任务的插件 grunt.l ...