最大费用最大流

咋写?取个相反数就可以了……

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int n, a[605][605], ss, tt, hea[750005], cnt, minCost, dis[750005], pre[750005];
queue<int> d;
bool vis[750005];
struct Edge{
int too, nxt, val, cst;
}edge[2200005];
const int oo=0x3f3f3f3f;
const int dx[]={0, 1, 0};
const int dy[]={0, 0, 1};
int f(int i, int j){
return (i-1)*n+j;
}
void init(){
minCost = cnt = 0;
memset(hea, -1, sizeof(hea));
}
void add_edge(int fro, int too, int val, int cst){
edge[cnt].nxt = hea[fro];
edge[cnt].too = too;
edge[cnt].val = val;
edge[cnt].cst = cst;
hea[fro] = cnt++;
}
void addEdge(int fro, int too, int val, int cst){
add_edge(fro, too, val, cst);
add_edge(too, fro, 0, -cst);
}
bool spfa(){
memset(dis, 0x3f, sizeof(dis));
memset(pre, -1, sizeof(pre));
d.push(ss);
vis[ss] = true;
dis[ss] = 0;
while(!d.empty()){
int x=d.front();
d.pop();
vis[x] = false;
for(int i=hea[x]; i!=-1; i=edge[i].nxt){
int t=edge[i].too;
if(dis[t]>dis[x]+edge[i].cst && edge[i].val>0){
dis[t] = dis[x] + edge[i].cst;
pre[t] = i;
if(!vis[t]){
vis[t] = true;
d.push(t);
}
}
}
}
return dis[tt]!=oo;
}
void mcmf(){
while(spfa()){
int tmp=0x3f3f3f3f;
for(int i=pre[tt]; i!=-1; i=pre[edge[i^1].too])
tmp = min(tmp, edge[i].val);
for(int i=pre[tt]; i!=-1; i=pre[edge[i^1].too]){
edge[i].val -= tmp;
edge[i^1].val += tmp;
minCost += tmp * edge[i].cst;
}
}
}
int main(){
while(scanf("%d", &n)!=EOF){
init();
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
scanf("%d", &a[i][j]);
ss = 0; tt = 2 * n * n + 1;
addEdge(ss, 1, 2, 0);
addEdge(1, 1+n*n, 1, 0);
addEdge(f(n,n), f(n,n)+n*n, 1, 0);
addEdge(f(n,n)+n*n, tt, 2, 0);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++){
addEdge(f(i,j), f(i,j)+n*n, 1, -a[i][j]);
for(int k=1; k<=2; k++){
int kx=i+dx[k];
int ky=j+dy[k];
if(kx>n || ky>n) continue;
addEdge(f(i,j)+n*n, f(kx,ky), oo, 0);
}
}
mcmf();
printf("%d\n", -minCost);
}
return 0;
}

hdu3376 Matrix Again的更多相关文章

  1. luogu2770 航空路线问题

    前置技能:HDU3376 Matrix Again 所以看到这个题,我们也会想着用最大费用最大流解决,因为从起点飞到终点再飞回来,就等于从起点飞两次到终点且这两次飞行除了起点终点之外没有访问超过一次的 ...

  2. HDU 2686 Matrix 3376 Matrix Again(费用流)

    HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...

  3. angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation

    今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:

  4. Pramp mock interview (4th practice): Matrix Spiral Print

    March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...

  5. Atitit Data Matrix dm码的原理与特点

    Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...

  6. Android笔记——Matrix

    转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...

  7. 通过Matrix进行二维图形仿射变换

    Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...

  8. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  9. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

随机推荐

  1. SPRING-BOOT系列之SpringBoot的诞生及其和微服务的关系

    转载自 : https://www.cnblogs.com/ityouknow/p/9034377.html 微服务架构 微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法 ...

  2. Python 3.6.5安装过程中小错误zipimport.ZipImportError: can't decompress data; zlib not available

    执行 :yum install -y zlib*之后,就好了.该安装错误是在CentOS7.4中遇到的.

  3. C# 修改DataTable 列的 DataType

    /// <summary> ///当DataTable中有值时,是不允许修改列的DataType /// 修改数据表DataTable某一列的数据类型和记录值 /// </summa ...

  4. 1. UI Tests简介

    (1) User Interface Testing UI Testing库主要提供了与App中的UI元素进行查找和交互的能力,这使得我们可以通过验证UI元素的状态来测试App是否正常运行.     ...

  5. 自定义Toast的显示位置和显示内容

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  6. ubuntu下安装apcu扩展

    apcu前身是apc,apc分为系统缓存和用户缓存 1.系统缓存是指PHP执行时增加缓存,减少PHP文件的反复检查和编译,从而达到系统加速的目的. 2.用户缓存是指,PHP代码中将数据写入缓存,是用户 ...

  7. 关于 propertychange 兼容性问题

    on 事件 $('body').on('property input','.class',function(){ alert(123); });

  8. 再遇BGP

    第一次遇到BGP,是在大学的课堂上,现在再次看到它,有种深深的无奈,我只记得它的名字,忘记了它的样子. 那么什么是BGP呢? 翻译过来就是边界网关协议,一个用来网络数据进行选路的路由协议,使用TCP协 ...

  9. 技术抄录_Java高级架构师教程

    1.B2C商城项目实战     2.高性能架构专题     3.架构筑基与开源框架解析专题     4.团队协作开发专题     5.微服务架构专题     6.设计模式     附上[架构资料]   ...

  10. Object.assign(o1, o2, o3) 对象 复制 合拼

    Object 对象方法学习之(1)—— 使用 Object.assign 复制对象.合并对象 合并对象 var o1 = {a: 1}; var o2 = {b: 2}; var o3 = {c: 3 ...