「AGC034D」 Manhattan Max Matching

传送门

不知道这个结论啊。。。

(其实就是菜嘛)

首先 \(O(n^2)\) 的建边显然不太行。

曼哈顿距离有这样一个性质,如果将绝对值符号拆掉,曼哈顿距离的值一定是所有情况的最大值。

然后根据这个性质我们可以把点拆成四种 \((\pm x,\pm y)\),然后连边直接跑最大流就完事了。

/*---Author:HenryHuang---*/
/*---Never Settle---*/
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
typedef long long ll;
struct edge{
ll to,nex,w,v;
}e[maxn*20];
int head[maxn],cur[maxn],cnt=1;
int n,m,s,t;
void add(ll a,ll b,ll c,ll d){
e[++cnt]=(edge){b,head[a],c,d};
head[a]=cnt;
}
void addedge(ll a,ll b,ll c,ll d){
add(a,b,c,d),add(b,a,0,-d);
}
ll dis[maxn],vis[maxn];
bool spfa(){
for(int i=s;i<=t;++i) dis[i]=-(1ll<<60),cur[i]=head[i];
queue<int> Q;
dis[s]=0,vis[s]=1,Q.emplace(s);
while(!Q.empty()){
ll u=Q.front();Q.pop();
vis[u]=0;
for(int i=head[u];i;i=e[i].nex){
int v=e[i].to;
if(e[i].w&&dis[v]<dis[u]+e[i].v){
dis[v]=dis[u]+e[i].v;
if(!vis[v]) vis[v]=1,Q.emplace(v);
}
}
}
return dis[t]>-(1ll<<60);
}
ll dfs(int u,ll in){
if(u==t) return in;
ll out=0,tmp;
vis[u]=1;
for(int i=cur[u];i;i=e[i].nex){
cur[u]=i;
int v=e[i].to;
if((!vis[v])&&e[i].w&&dis[v]==dis[u]+e[i].v&&(tmp=dfs(v,min(e[i].w,in)))){
in-=tmp,out+=tmp;
e[i].w-=tmp,e[i^1].w+=tmp;
if(!in) break;
}
}
if(!out) dis[u]=0;
vis[u]=0;
return out;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n;
int p1=2*n+1,p2=2*n+2,p3=2*n+3,p4=2*n+4;
s=0,t=2*n+5;
for(int i=1;i<=n;++i){
int x,y,c;cin>>x>>y>>c;
addedge(s,i,c,0);
addedge(i,p1,c,x+y);
addedge(i,p2,c,x-y);
addedge(i,p3,c,-x+y);
addedge(i,p4,c,-x-y);
}
for(int i=1;i<=n;++i){
int x,y,c;cin>>x>>y>>c;
addedge(i+n,t,c,0);
addedge(p1,i+n,c,-x-y);
addedge(p2,i+n,c,-x+y);
addedge(p3,i+n,c,x-y);
addedge(p4,i+n,c,x+y);
}
ll ans=0;
while(spfa()){
ll tmp=dfs(s,1e9);
ans-=tmp*dis[t];
}
cout<<-ans<<'\n';
return 0;
}

最新文章

  1. [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三
  2. swap的应用两个数的交换
  3. 通过LVS+Keepalived搭建高可用的负载均衡集群系统
  4. AxureRP8实战手册(基础11-20)
  5. Testlink &amp; Redmine组合拳演练
  6. c#使用XSLT将xml文档转换为html文档
  7. Hibernate实体对象继承策略
  8. excel表格数据导入数据库Oracle
  9. virtualbox虚拟机NAT模式下不能连接外网
  10. 使用Spring+MySql实现读写分离(二)spring整合多数据库
  11. 模块——Getopt::Long接收客户命令行参数和Smart::Comments输出获得的命令行参数内容
  12. 【nodejs】让nodejs像后端mvc框架(asp.net mvc )一样处理请求--路由限制及选择篇(2/8)【route】
  13. C# 因IIS回收导致定时器失效的解决方案
  14. C++中的构造函数,拷贝构造函数,赋值函数
  15. openssl在多平台和多语言之间进行RSA加解密注意事项
  16. Jvisualvm监控远程linux下Tomcat
  17. Flex入坑指南
  18. linux下进程的最大线程数、进程最大数、进程打开的文件数
  19. canvas绘制圆环
  20. Win Server 8中的利器:微软在线备份服务

热门文章

  1. IDEA中怎么创建ini文件
  2. webdriver中的等待——主要讲解WebDriverWait()
  3. Go语言流程控制03--goto跳转到任意标签位置
  4. Java中如何将函数名作为参数传递
  5. LATEX如何写多个条件推导式推出一个结论
  6. Stopper的使用
  7. PyTorch数据加载处理
  8. Linux内存技术分析(上)
  9. Pass Infrastructure基础架构(下)
  10. 使用TENSORRT和NVIDIA-DOCKER部署深部神经网络