原题链接: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 ;
}

最新文章

  1. javascript创建跟随鼠标好玩的东西
  2. Lua中的weak表——weak table
  3. SQL-Server2008 数据库发布订阅
  4. 第三周总结PSP日志文件
  5. 短信接口调用以及ajax发送短信接口实现以及前端样式
  6. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
  7. unix环境高级编程-读书笔记与习题解答-第一篇
  8. 说一说&amp;&amp;符
  9. 【转】c#实现字符串倒序的n种写法
  10. 远程控制编写之屏幕传输 MFC实现 屏幕截图 发送bmp数据 显示bmp图像
  11. 免费下载获取Odoo中文实施 应用 指南 手册
  12. Scanner输入数字时个位十位百位千位单独取出。
  13. return和throw某些特性相似
  14. List、Set、数据结构、Collections
  15. 按奇偶排序数组 II
  16. 学习笔记之TensorFlow
  17. Leetcode刷题记录:编码并解码短网址
  18. tomcat7部署多个web应用不同编码,端口
  19. KVM虚拟机的创建、管理与迁移
  20. 你竟然在公钥中下毒!——如何在RSA公钥中添加后门

热门文章

  1. python数据类型之字符串(str)和其常用方法
  2. $(MAKE) , make命令
  3. action属性和data属性组合事例
  4. Python 前端 js基础
  5. SpringMVC对于跨域访问的支持
  6. list 类
  7. 大数相减 C语言
  8. 【转】Unicode utf8等编码类型的原理
  9. nyoj 题目2 括号配对问题
  10. Nginx+PHPSTORM+Xdebug 配置