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;
}

Atcoder Grand Contest 036 D - Negative Cycle的更多相关文章

  1. AtCoder Grand Contest 036 A-C

    目录 \(\bf A - Triangle\) \(\bf B - Do\ Not\ Duplicate\) \(\bf C - GP 2\) \(\bf D - Negative \ Cycle\) ...

  2. AtCoder Grand Contest 036

    Preface 这篇已经鸽了好久的说,AGC037都打完了才回来补所以题目可能都记不大清楚了,如有错误请指正 这场感觉难度远高于上一场,从D开始就不会了,E没写(看了题解都不会写),F就是抄曲明姐姐的 ...

  3. AtCoder Grand Contest 036 简要题解

    从这里开始 比赛目录 Problem A Triangle 考虑把三角形移到和坐标轴相交,即 然后能够用坐标比较简单地计算面积,简单构造一下就行了. Code #include <bits/st ...

  4. AtCoder Grand Contest 036题解

    传送门 爆炸的比较厉害--果然还是菜啊-- \(A\) 我们强制一个点为\((0,0)\),那么设剩下两个点分别为\((a,b),(c,d)\),根据叉积可以计算出面积为\(ad-bc=S\),那么令 ...

  5. AtCoder Grand Contest 002

    AtCoder Grand Contest 002 A - Range Product 翻译 告诉你\(a,b\),求\(\prod_{i=a}^b i\)是正数还是负数还是零. 题解 什么鬼玩意. ...

  6. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  7. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

  8. AtCoder Grand Contest 031 简要题解

    AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...

  9. AtCoder Grand Contest 010

    AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...

随机推荐

  1. GoCN每日新闻(2019-10-23)

    GoCN每日新闻(2019-10-23) GoCN每日新闻(2019-10-23) 1. 从0开始,用Go实现Lexer和Parser https://mp.weixin.qq.com/s/cFGJX ...

  2. Ajax 的一些概念 解析

    什么是Ajax Ajax基本概念 Ajax(Asynchronous JavaScript and XML):翻译成中文就是异步的JavaScript和XML. 从功能上来看是一种在无需重新加载整个网 ...

  3. Truncate使用注意事项

    1.TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系统和事务日志 ...

  4. Stringbuilde方法的用法以及其作用

    Stringbuilde的方法有以下几种(常用的):(java中的语法) 在程序开发过程中,我们常常碰到字符串连接的情况,方便和直接的方式是通过"+"符号来实现,但是这种方式达到目 ...

  5. Fiddler导出JMX文件配置

    (1)安装fiddler jmeter(免安装) 注意事项!fiddler版本必须在v4.6.2以上(插件支持的是4.6版本), jmeter版本最好在v3.0以上,版本太低容易导致导出不成功 这里我 ...

  6. vue中使用百度地图vue-baidu-map

    安装 npm install vue-baidu-map --save 全局注册 全局注册将一次性引入百度地图组件库的所有组件.需在入口文件main.js中引入vue-baidu-map import ...

  7. 为什么使用css3和div布局?

    1,代码精简(没有本身自带的一些属性,容易设置样式)2,解决了table表格的嵌套问题3,速度问题(页面代码减少,增加了编写代码的速度)4 ,对排名的影响,基于xhtml标准的div+css布局会更快 ...

  8. Xamarin图表开发基础教程(11)OxyPlot框架支持的图表类型

    Xamarin图表开发基础教程(11)OxyPlot框架支持的图表类型 OxyPlot组件中支持7种类型的条型图表,分别为普通条形图.线型条形图.矩形条形图.差值图.龙卷风图.普通柱形图和柱形误差图, ...

  9. 数据分析入门——pandas之数据合并

    主要分为:级联:pd.concat.pd.append 合并:pd.merge 一.numpy级联的回顾 详细参考numpy章节 https://www.cnblogs.com/jiangbei/p/ ...

  10. [LeetCode]577. Employee Bonus 员工奖金

    Select all employee's name and bonus whose bonus is < 1000. Table:Employee +-------+--------+---- ...