当前位置:网站首页>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;
}
边栏推荐
- NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices
- Mysql 自增id、uuid与雪花id
- 军品研制过程所需文件-进阶版
- High number_ Chapter 1 space analytic geometry and vector algebra__ Distance from point to plane
- Future applications and technical challenges of backscatter communication
- Configuration of vscode+unity3d
- Documents required for military product development process - advanced version
- 2022/7/16
- Goldfish rhca memoirs: cl210 describes the openstack control plane -- identify the overcloud control platform service + chapter experiment
- [Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (2)
猜你喜欢

火箭大机动运动欧拉角解算的探讨

Win10 install Apache Jena 3.17

Use testeract JS offline recognition picture text record

37. Flex layout

Beego framework realizes file upload + seven cattle cloud storage

Game theory (Depu) and investment (40/100)

LeetCode 2331. Calculate the value of Boolean binary tree (tree traversal)

Environment variable configuration of win10

Four methods of traversing key value in map
![[leetcode weekly replay] 302 weekly 20220717](/img/38/446db9b4755f8b30f9887faede7e95.png)
[leetcode weekly replay] 302 weekly 20220717
随机推荐
Explanation of tree chain dissection idea + acwing 2568 Tree chain dissection (DFS sequence + mountain climbing method + segment tree)
Avi Deployment Guide (2): overview of AVI architecture
一个报错, Uncaught TypeError: ModalFactory is not a constructor
2022/7/14
Win10 start key click no response
Pytorch.nn实现多层感知机
火箭大机动运动欧拉角解算的探讨
Detailed explanation of multiple linear regression
Mysql索引的类型(单列索引、组合索引 btree索引 聚簇索引等)
Pytoch realizes multi-layer perceptron manually
Category imbalance in classification tasks
[leetcode weekly replay] 302 weekly 20220717
String type function transfer problem
SSM使用poi将数据导出到excel
可定义的6G安全架构
8.固定收益投资
ENVI_ Idl: use the inverse distance weight method to select the nearest n points for interpolation (bottom implementation) and output them to GeoTIFF format (the effect is equivalent to the inverse di
Prospect of 6G global convergence network
ue4对动画蓝图的理解
Svn learning