当前位置:网站首页>AT5147-[AGC036D]Negative Cycle【dp,模型转换】
AT5147-[AGC036D]Negative Cycle【dp,模型转换】
2022-07-17 13:56:00 【QuantAsk】
正题
题目链接:https://www.luogu.com.cn/problem/AT5147
题目大意
有 n n n个点的一张图,其中 i → i + 1 ( i < n ) i\rightarrow i+1(i< n) i→i+1(i<n)有一条边权值为 0 0 0。
对于其他 i , j ( i ≠ j ) i,j(i\neq j) i,j(i=j)存在一条边 i → j i\rightarrow j i→j,若 i < j i<j i<j那么权值为 − 1 -1 −1,否则为 1 1 1。
删除 i → j ( i ≠ j ) i\rightarrow j(i\neq j) i→j(i=j)的代价为 a i , j a_{i,j} ai,j,要求代价最小的情况下使得图中不存在负环。
1 ≤ n ≤ 500 1\leq n\leq 500 1≤n≤500
解题思路
这个容易负环让人一头雾水不知道怎么维护,我们知道差分约束有解的条件就是没有负环,所以我们可以考虑转成差分约束模型。
那么对于不能删除的边 i → i + 1 i\rightarrow i+1 i→i+1就有限制 x i ≥ x i + 1 x_i\geq x_{i+1} xi≥xi+1,也就是整个序列单调不升。
然后形如 i → j ( i < j ) i\rightarrow j(i<j) i→j(i<j)可以视为 x i − 1 ≥ x j x_i-1\geq x_j xi−1≥xj。
形如 i ← j ( i < j ) i\leftarrow j(i<j) i←j(i<j)可以视为 x i ≤ x j + 1 x_i\leq x_j+1 xi≤xj+1。
也就是 x i − x j ≤ 1 x_i-x_j\leq 1 xi−xj≤1和 x i − x j ≥ 1 x_i-x_j\geq 1 xi−xj≥1的限制,我们考虑维护差分数组(反过来的) y i = x i − 1 − x i y_i=x_{i-1}-x_{i} yi=xi−1−xi,那么条件就是区间和不能大于/小于 1 1 1,显然的那么 y i y_i yi就只有可能是 0 / 1 0/1 0/1。
然后仔细观察这个限制会发现其实只有相邻的 1 1 1会有用,我们考虑 d p dp dp来处理,设 f i , j , k f_{i,j,k} fi,j,k表示做到第 i i i个,上一个 1 1 1在 j j j处,再上一个 1 1 1在 k k k处。
前缀和一下 a a a数组优化转移即可。
时间复杂度: O ( n 3 ) O(n^3) O(n3)
code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const ll N=510;
ll n,a[N][N],s[N][N],t[N][N],f[N][N],ans;
signed main()
{
scanf("%lld",&n);
for(ll i=1;i<=n;i++)
for(ll j=1;j<=n;j++){
if(i==j)continue;
scanf("%lld",&a[i][j]);
}
for(ll i=1;i<=n;i++)
for(ll j=1;j<=i;j++)
s[i][j]=s[i][j-1]+a[j][i],
t[i][j]=t[i][j-1]+a[i][j];
memset(f,0x3f,sizeof(f));
ans=f[0][0];f[1][1]=0;
for(ll i=2;i<=n;i++){
for(ll j=1;j<i;j++)
for(ll k=1;k<=j-(j!=1);k++)
f[i][j]=min(f[i][j],f[j][k]);
for(ll j=1;j<=i;j++)
for(ll k=1;k<=j-(j!=1);k++)
f[j][k]+=s[i][i]-s[i][j-1],f[j][k]+=t[i][k-1];
}
for(ll i=1;i<=n;i++)
for(ll j=1;j<=n;j++)
ans=min(ans,f[i][j]);
printf("%lld\n",ans);
return 0;
}
边栏推荐
- Introduction to the universal theme system of SAP appgyver
- How to build dashboard and knowledge base in double chain note taking software? Take the embedded widget library notionpet as an example
- Second classification learning is extended to multi classification learning
- 军品研制过程所需文件-进阶版
- Mysql索引的类型(单列索引、组合索引 btree索引 聚簇索引等)
- Google Earth engine app (GEE) - set up a nighttime lighting timing analysis app in China
- Documents required for military product development process - advanced version
- 6G smart endogenous: technical challenges, architecture and key features
- 反向散射通信的未来应用与技术挑战
- leetcode-08
猜你喜欢

论文笔记:Mind the Gap An Experimental Evaluation of Imputation ofMissing Values Techniques in TimeSeries

Avi Deployment Guide (2): overview of AVI architecture

antd 下拉多选传值到后台做查询操作

Ppde Q2 welcome | welcome 22 AI developers to join the propeller developer technical expert program!

Unity3d 模型中心点的转换(源代码)

Summary of port mirroring methods with VDS or NSX under vSphere

UE4 understanding of animation blueprint

Evaluation method of machine learning model

OpenCV编程:OpenCV3.X训练自己的分类器

E-commerce sales data analysis and prediction (date data statistics, daily statistics, monthly statistics)
随机推荐
PowerCLI 脚本性能优化
(一)了解MySQL
每日刷题记录 (二十六)
Opencv programming: opencv3 X trains its own classifier
String type function transfer problem
Satellite network capacity improvement method based on network coding
Explanation of tree chain dissection idea + acwing 2568 Tree chain dissection (DFS sequence + mountain climbing method + segment tree)
Unity dropdown (editable, inputable) drop-down selection box with Text Association
LeetCode 2319. Judge whether the matrix is an X matrix
8.固定收益投资
数据库锁的介绍与InnoDB共享,排他锁
Modify the default path of jupyter see this article!
Pytorch与权重衰减(L2范数)
Connected graph (union search set)
Win10的环境变量配置
Mysql优化系列之limit查询
Avi Deployment Guide (2): overview of AVI architecture
Win10安装Apache Jena 3.17
nodeJS中promise对象对结果简便输出办法(建议使用异步终极方案 async+await)
Definable 6G security architecture