AtCoder AGC036D Negative Cycle (图论、DP)
题目链接
https://atcoder.jp/contests/agc036/tasks/agc036_d
题解
这都是怎么想出来的啊。。目瞪口呆系列。。
第一步转化至关重要: 一张图中不存在负环意味着什么?
不存在负环就存在最短路,我们可以给每个点分配一个权值\(p_i\)(相当于从\(1\)号到该点的最短路,点从\(1\)开始标号)满足对于任何边\((i,j)\)有\(p_j\ge p_i+w(i,j)\).
然后我们令\(q_i=p_i-p_{i+1}\), 那么由于边权都是\(1\)或者\(-1\)并且存在不能删的\(0\)边, 显然有\(q\)数组的值都是\(0\)或者\(1\).
约束变成了: 对于每条边\((i,j)\ (i>j)\)有\(\sum^{i-1}_{k=i}q_k\le 1\), 对于每条边\((i,j)\ (i<j)\)有\(\sum^{j-1}_{k=i}q_k\ge 1\).
所以问题就被转化成了: 你要给每个\(1\)到\((n-1)\)中的点\(q_i\)分配一个\(0\)或者\(1\)的权值,再删掉所有不满足约束条件的边,使得总代价最小!
天哪,这也太神仙了吧……
然后就是一个很容易的DP了,设\(dp[i][j]\)表示安排好前\(i\)位的\(q\)值,且强行令\(q_i=1\), 上一个为\(1\)的位置是\(j\)
那么考虑枚举\(k\), \(dp[i][j]\)转移到\(dp[k][i]\),同时删去不合法的边
对于\(a>b\)的边\((a,b)\), 要删掉所有满足\(j<b\le i<x<a\)的边
对于\(a<b\)的边\((a,b)\), 要删掉所有满足\(j<i<a<b\le x\)的边
然后这个很容易使用二维前缀和优化,时间复杂度\(O(n^3)\).
啊啊啊人类智慧……
代码
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<iostream>
#define llong long long
using namespace std;
inline int read()
{
int x=0; bool f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(c^'0');
if(f) return x;
return -x;
}
const int N = 500;
llong a[N+3][N+3];
llong s[2][N+3][N+3];
llong dp[N+3][N+3];
int n;
void update(llong &x,llong y) {x = x<y?x:y;}
llong getsum(int typ,int lx,int rx,int ly,int ry)
{
return s[typ][rx][ry]-s[typ][lx-1][ry]-s[typ][rx][ly-1]+s[typ][lx-1][ly-1];
}
int main()
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(j==i) continue;
scanf("%lld",&a[i][j]);
}
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(i<j) {s[0][i][j] = a[i][j];}
s[0][i][j] += s[0][i][j-1];
}
for(int j=1; j<=n; j++) s[0][i][j] += s[0][i-1][j];
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(i>j) {s[1][i][j] = a[i][j];}
s[1][i][j] += s[1][i][j-1];
}
for(int j=1; j<=n; j++) s[1][i][j] += s[1][i-1][j];
}
memset(dp,42,sizeof(dp));
dp[0][0] = 0ll;
for(int i=0; i<=n; i++)
{
for(int j=0; j<max(i,1); j++)
{
for(int k=i+1; k<=n; k++)
{
llong tmp = dp[i][j]+getsum(1,k+1,n,j+1,i)+getsum(0,i+1,k,i+1,k);
update(dp[k][i],tmp);
}
}
}
llong ans = dp[n][1];
for(int i=1; i<=n; i++) update(ans,dp[n][i]);
printf("%lld\n",ans);
return 0;
}
AtCoder AGC036D Negative Cycle (图论、DP)的更多相关文章
- @atcoder - AGC036D@ Negative Cycle
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 N 个点的有向带权图,从 0 编号到 N - 1.一开 ...
- Atcoder Grand Contest 036 D - Negative Cycle
Atcoder Grand Contest 036 D - Negative Cycle 解题思路 在某些情况下,给一张图加或删一些边要使图合法的题目要考虑到最短路的差分约束系统.这一题看似和最短路没 ...
- AtCoder Grand Contest 036D - Negative Cycle
神仙题?反正我是完全想不到哇QAQ 这场AGC真的很难咧\(\times 10086\) \(\bf Description\) 一张 \(n\) 个点的图,\(i\) 到 \(i+1\) 有连边. ...
- 图论+dp poj 1112 Team Them Up!
题目链接: http://poj.org/problem?id=1112 题目大意: 有编号为1~n的n个人,给出每个人认识的人的编号,注意A认识B,B不一定认识A,让你将所有的人分成两组,要求每组的 ...
- atcoder B - Frog 2 (DP)
B - Frog 2 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement There a ...
- atcoder A - Frog 1(DP)
A - Frog 1 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement There a ...
- 洛谷P3953 逛公园 [noip2017] 图论+dp
正解:图论(最短路)+dp(记忆化搜索) 解题报告: 这题真的是个好东西! 做了这题我才发现我的dij一直是错的...但是我以前用dij做的题居然都A了?什么玄学事件啊...我哭了TT 不过其实感觉还 ...
- Atcoder E - RGB Sequence(dp)
题目链接:http://arc074.contest.atcoder.jp/tasks/arc074_c 题意:一共有3种颜色,红色,绿色,蓝色.给出m个要求l,r,x表示在区间[l,r]内要有x种不 ...
- AtCoder AGC019E Shuffle and Swap (DP、FFT、多项式求逆、多项式快速幂)
题目链接 https://atcoder.jp/contests/agc019/tasks/agc019_e 题解 tourist的神仙E题啊做不来做不来--这题我好像想歪了啊= =-- 首先我们可以 ...
随机推荐
- 2019中山纪念中学夏令营-Day4[JZOJ]
Begin (题目的排序方式:难易程度) 什么是对拍: 对拍是一种在写完程序后,验证自己程序是不是正解的比较方便的方法. 实现过程: 对同一道题,再打一个暴力程序,然后用一些大数据等跑暴力程序来进行验 ...
- GitHub从小白到熟悉<五>
GitHub 主页
- jQuery导出word文档
DDoc.js function DDoc() { this.data = []; this.relationData = []; this.listCount = 0; this.counter = ...
- (转)Cvte提前批
1. 加密解密了解么?几种算法,讲一下你了解的(链接) 算法选择:对称加密AES,非对称加密: ECC,消息摘要: MD5,数字签名:DSA 常见加密算法 1.DES(Data Encryption ...
- python 高级函数
高级函数 map 格式:map(func, lt) 说明:接受两个参数,一个函数和一个可迭代对象,返回一个生成器,将func依次作用于lt 示例: l = [1,2,3,4,5]def double ...
- 本地远程调试Linux 部署的web 项目
1.在linux tomcat 下面的 bin 目录下增加一个远程调试的命令: declare -x CATALINA_OPTS="-server -Xdebug -Xnoagent -Dj ...
- Lubuntu 16.04 64位兼容32位程序
第一步:确认自己系统的架构 dpkg --print-architecture输出:amd64 结果为 amd64 表示系统是64位的 第二步:确认打开了多架构支持功能 dpkg --print-f ...
- D-query SPOJ - DQUERY (莫队算法裸题)
Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...
- War of the Corporations CodeForces - 625B (greed)
A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue the ...
- label smooth
图像分类的一个trick,推导可参考这位博主https://leimao.github.io/blog/Label-Smoothing/ 知乎上的讨论https://www.zhihu.com/que ...