hdu3376 Matrix Again
最大费用最大流
咋写?取个相反数就可以了……
#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的更多相关文章
- luogu2770 航空路线问题
前置技能:HDU3376 Matrix Again 所以看到这个题,我们也会想着用最大费用最大流解决,因为从起点飞到终点再飞回来,就等于从起点飞两次到终点且这两次飞行除了起点终点之外没有访问超过一次的 ...
- HDU 2686 Matrix 3376 Matrix Again(费用流)
HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- 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 ...
- Atitit Data Matrix dm码的原理与特点
Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...
- Android笔记——Matrix
转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...
- 通过Matrix进行二维图形仿射变换
Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
- [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 ...
- [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 ...
随机推荐
- python之处理json
import json# json串就是字符串dic={ 'car':{'color':'red','price':100,'count':50}, 'iphone':{'color':'骚粉色',' ...
- bzoj1854 [Scoi2010]游戏【构图 并查集】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1854 没想到怎么做真是不应该,看到每个武器都有两个属性,应该要想到连边构图的!太不应该了! ...
- Codeforces Round #402 (Div. 2) D
Description Little Nastya has a hobby, she likes to remove some letters from word, to obtain another ...
- 制作ubuntu启动U盘:Windows,Mac osx ,Ubuntu
1.How to create a bootable USB stick on Windows https://www.ubuntu.com/download/desktop/create-a-usb ...
- 在sz
在大城市,sz, 每天骑单车去公交车站. 每天用高德地图 坐快线巴士 车上下班要3个小时. 用guomei 的回收管家 回收 旧空调. 我在kfc 看书 在班车上睡觉/眯眼 在办公室睡觉,看书,工作 ...
- c语言读取一个文件夹下的全部文件(jpg / png 文件)
#include <cstdio> #include <cstring> #include <unistd.h> #include<dirent.h> ...
- [转]合理使用ArrayMap代替HashMap
合理使用ArrayMap代替HashMap 2016年07月08日 15:34:44 阅读数:5938 转载请标注: 披萨大叔的博客 http://blog.csdn.net/qq_27258799/ ...
- [转]java注解与APT技术
下面是一个简单的自定义注解的栗子: package annotation; import java.lang.annotation.Documented; import java.lang.annot ...
- 飞秋软件的OA消息接口服务器
由于单位使用了飞秋,同时也使用了OA,但OA的消息系统没有飞秋方便,所以大多数人还是在用飞秋沟通.但审批等流程又在OA上,所以做了个消息接口服务器,提取OA消息自动发送到飞秋上,大大方便了工作. 正好 ...
- Python 访问字典(dictionary)中元素
访问python字典中元素的几种方式 一:通过“键值对”(key-value)访问: print(dict[key]) dict = {1: 1, 2: 'aa', 'D': 'ee', 'Ty': ...