Atcoder Grand Contest 036 D - Negative Cycle

解题思路

在某些情况下,给一张图加或删一些边要使图合法的题目要考虑到最短路的差分约束系统。这一题看似和最短路没什么关系,但有一个不那么经典的推论,对于一个点 \(u\) 不在负环上的一个充要条件是
\[ \forall_{\text{Edge }v\rightarrow u} dis(S,v)+weight(v, u)\geq dis(S,u) \]
其中 \(S\) 是图中任意与 \(u\) 联通的一点。

随便新建一个源点 \(S\),我们令 \(p_i=dis(S,i)\) ,仅考虑原图的链可以得到 \(p_i \geq p_{i+1}\) 。对于任意两点 \(x,y\ (x<y)\) ,新加的边 \((x, y), (y, x)\) 需分别满足 \(p_x-1\geq p_y,p_y+1\geq p_x\) 。这里看似推不下去了然而巧妙差分后能获得非常显然的结论,令 \(q_i=p_i-p_{i+1}\) ,移项可得
\[ \sum_{i=x}^{y-1} q_i \geq 1,\sum_{i=x}^{y-1}q_i \leq 1 \]
然后我们可以证明出,\(q_i \in \{0,1\}\),这里比较容易,如果 \(q_i <0\) 原链的差分约束条件就不满足,如果 \(q_i > 0\) 则点 \(i+1\) 存在额外的 \(-1\) 入边 \((v,i+1),v< i\),此时 \(v\) 到 \(i\) 最坏情况可以走一段 \(0\) 链更新,所以 \(q_i\) 最多只能为 \(1\) 。

然后我们就可以考虑 \(q_i\) 的每一位取 \(0\) 还是取 \(1\) ,然后删掉不合法的边,这个过程是可以 \(\text{DP}\) 解决的,对于不满足 \(\sum q_i \leq 1\) 的情况,在其跨过第二个 \(1\) 的时候统计掉,对于 \(\sum q_i \geq 1\) 的情况,对于每一段连续的 \(0\) 统计即可,那么就可以令 \(dp[i][j]\) 为当前考虑到前 \(i\) 位且 \(i\) 选 \(1\),上一个 \(1\) 在 \(j\) 的答案,转移使用前缀和优化即可。

code

/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){int ch = 0, f = 0; x = 0;for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;if(f) x = -x;
}
const int N = 505;
#define int ll
int A[N][N], B[N][N], C[N][N], D[N][N], dp[N][N], n;
signed main(){read(n);for(int i = 1; i <= n; i++){for(int j = 1; j < i; j++) read(A[j][i]);for(int j = i + 1; j <= n; j++) read(B[i][j]);}for(int i = 0; i <= n + 1; i++)for(int j = i; j <= n + 1; j++){if(i) C[i][j] += C[i-1][j];for(int k = j; k <= n + 1; k++) C[i][j] += A[i][k];}for(int i = n + 1; i >= 0; i--)for(int j = i; j <= n + 1; j++){D[i][j] += D[i+1][j];for(int k = i; k <= j; k++) D[i][j] += B[i][k];}memset(dp, 0x3f, sizeof(dp));dp[0][0] = 0;for(int i = 1; i <= n + 1; i++)for(int j = 0; j < i; j++){for(int k = 0; k <= j; k++) dp[i][j] = min(dp[i][j], dp[j][k] + C[j][i+1] - C[k][i+1] + D[j+1][i]);}int ans = inf;for(int i = 0; i <= n; i++)ans = min(ans, dp[n+1][i]);cout << ans << endl;return 0;
}

转载于:https://www.cnblogs.com/mangoyang/p/11240437.html

Atcoder Grand Contest 036 D - Negative Cycle相关推荐

  1. 【每日亿题#12】AtCoder Grand Contest 021 (A ~ F)全部题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 文章目录 AtCoder Grand Contest 021 题解 A. Digit Sum 2 B. ...

  2. AtCoder Grand Contest 008: Contiguous Repainting(思维)

    Contiguous Repainting 时间限制: 2 Sec  内存限制: 256 MB 提交: 69  解决: 22 [提交][状态][讨论版][命题人:admin] 题目描述 There a ...

  3. AtCoder Grand Contest 017

    AtCoder Grand Contest 017 A - Biscuits 有\(n\)个数,问有多少个集合的数的和模\(2\)余\(P\). 随便\(dp\)一下就好了. #include< ...

  4. AtCoder题解——AtCoder Grand Contest 048——A - atcoder < S

    题目相关 题目链接 AtCoder Grand Contest 048 A 题,https://atcoder.jp/contests/agc048/tasks/agc048_a. Problem S ...

  5. AtCoder题解 —— AtCoder Grand Contest 050 —— B - Three Coins —— 动态规划

    题目相关 题目链接 AtCoder Grand Contest 050 B 题,https://atcoder.jp/contests/agc050/tasks/agc050_b. Problem S ...

  6. Atcoder Grand Contest 010 B - Boxes 差分

    B - Boxes 题目连接: http://agc010.contest.atcoder.jp/tasks/agc010_b Description There are N boxes arrang ...

  7. AtCoder Grand Contest 010 D - Decrementing

    题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_d 题目大意: 有\(n\)个数\(A_i\),它们的\(gcd\)是1,A.B两人轮流操作, ...

  8. Atcoder Grand Contest 012 B - Splatter Painting解题报告

    题目:http://agc012.contest.atcoder.jp/tasks/agc012_b 有一个n点m边的图,(不一定联通) 还有q个操作:每次将一个点v及其周围距离<=d的点涂成颜 ...

  9. [atcoder]AtCoder Grand Contest 027题解

    [题目链接] https://agc027.contest.atcoder.jp/ A [题解] 题意: 是把xxx个糖果分给nnn个人,一个人如果恰好分到aia_{i}ai​个糖果就会高兴.求最多使 ...

  10. UPC个人训练赛第十五场(AtCoder Grand Contest 031)

    传送门: [1]:AtCoder [2]:UPC比赛场 [3]:UPC补题场 参考资料 [1]:https://www.cnblogs.com/QLU-ACM/p/11191644.html B.Re ...

最新文章

  1. html中设置文本框长度,Html的文本框怎样限制录入文本框的字节长度
  2. apache的es的原理_Elasticsearch的原理简介
  3. 凤凰涅槃:从 iBatis 到 MyBatis
  4. Flying框架思路与感想
  5. windows 安装 python3
  6. matlab cell转数组_MATLAB批量修改文件名
  7. 怎么打公式_我们总结了一条抖音爆款公式
  8. 基于httpd的mod_deflate模块
  9. office另存为pdf的加载项_Word怎样转换成PDF
  10. 机器人到底会不会有情感?
  11. 涨跌停计算器_股票涨跌停计算器
  12. mysql 建模工具 mac_MySQL Workbench for Mac 6.0 下载 - Mac上优秀的数据库建模工具 | 玩转苹果...
  13. 令牌环 典型例题分析解答
  14. Typescript基础知识--学习笔记
  15. Session Fixation session固定攻击
  16. 微信企业号开发(1)--基础入门
  17. C++ 读取txt文件中数据并存入数组中
  18. Excel快速核对两张表格
  19. 汽车领域多语种迁移学习挑战赛-Coggle 30 Days of ML
  20. jmeter 3版本到5版本踩坑之路

热门文章

  1. 拓端tecdat|R语言对股票风险“溃疡指数”( Ulcer Index)曲面图可视化
  2. 拓端tecdat|R语言使用灰色关联分析(Grey Relation Analysis,GRA)中国经济社会发展指标
  3. 拓端tecdat|Python之LDA主题模型算法应用
  4. (5)数据结构-栈顺序存储
  5. JAVA发送邮件案例
  6. nginx基础配置,转发所有
  7. python实现寻找最长回文子序列
  8. Visual Studio 2017 警告C4819解决方案
  9. envi图像裁剪_【ENVI基础】如何进行水体提取?
  10. python开发怎么成长_Python开发者四大进阶攻略,菜鸟的成神之路