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