题目:hdoj 3376 Matrix Again

题意:给出一个m*n的矩阵,然后从左上角到右下角走两次,每次仅仅能向右或者向下,出了末尾点其它仅仅能走一次,不能交叉,每次走到一个格子拿走这个格子中的数字,求价值最大?

分析:非常明显费用流。開始想的到一种建图方案,可是那样的话流量全为负值的话会成一个环,所以果断换了。

建图方案是:

首先拆点,每一个点拆成两个i 和 ii ,建边,费用为当前格子的值,流量为1,初始点和末尾点为2

然后每一个点向它的右边和下边分别建边,容量为1,费用为0

s 连接 左上角 流量 2 ,费用 0

右下角连接 t 。流量为 2 。费用为 0

PS:这个题目居然卡vector,的模拟自己实现邻接表,否则的话会超内存。

ac代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std; #define PB push_back
#define MP make_pair
#define Del(a,b) memset(a,b,sizeof(a)) typedef vector<int> VI;
typedef long long LL;
const LL inf = 0x3f3f3f3f;
const int N = 750000;
int cost,flow;
struct Node
{
int from,to,cap,flow,cost;
int next;
}e[N<<2];
int head[N],top;
void add_Node(int from,int to,int cap,int cost)
{
e[top] = ((Node){from,to,cap,0,cost,head[from]});
head[from] = top++;
e[top] = ((Node){to,from,0,0,-cost,head[to]});
head[to] = top++;
}
int vis[N],dis[N];
int father[N],pos[N];
bool BellManford(int s,int t,int& flow,int& cost)
{
Del(dis,inf);
Del(vis,0);
queue<int> q;
q.push(s);
vis[s]=1;
father[s]=-1;
dis[s] = 0;
pos[s] = inf;
while(!q.empty())
{
int f = q.front();
q.pop();
vis[f] = 0;
for(int i = head[f];i!=-1 ; i = e[i].next)
{
Node& tmp = e[i];
if(tmp.cap>tmp.flow && dis[tmp.to] > dis[f] + tmp.cost)
{
dis[tmp.to] = dis[f] + tmp.cost;
father[tmp.to] = i;
pos[tmp.to] = min(pos[f],tmp.cap - tmp.flow);
if(vis[tmp.to] == 0)
{
vis[tmp.to]=1;
q.push(tmp.to);
}
}
} }
if(dis[t] == inf)
return false;
flow += pos[t];
cost += dis[t]*pos[t];
for(int u = t; u!=s ; u = e[father[u]].from)
{
e[father[u]].flow += pos[t];
e[father[u]^1].flow -= pos[t];
}
return true;
}
int Mincost(int s,int t)
{
flow = 0, cost = 0;
while(BellManford(s,t,flow,cost));
return cost;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
Del(head,-1);top = 0;
int num = n*n;
int one,x;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
scanf("%d",&x);
if(i==0 && j==0)
one = x;
int tmp = 1;
if(i==0 && j==0 || i==n-1 && j == n-1)
tmp = 2;
add_Node(i*n+j,i*n+j+num,tmp,-x);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if((j+1)<n)
add_Node(i*n+j+num,i*n+j+1,1,0);
if((i+1)<n)
add_Node(i*n+j+num,(i+1)*n+j,1,0);
}
}
int s = 2*num+1 , t = s + 1;
add_Node(s,0,2,0);
add_Node(num+num-1,t,2,0);
int ans = Mincost(s,t);
printf("%d\n",-ans-x-one);
}
return 0;
}

最新文章

  1. ComboBoxEdit设置选项值(单选 多选)
  2. ttttttttttt
  3. bzoj 2599 [IOI2011]Race (点分治)
  4. 多校6 1003 HDU5795 A Simple Nim (sg函数)
  5. 实例源码--Android的ListView控件的总结
  6. [Excel] CsvHelper---C#关于CSV文件的导入和导出以及转化 (转载)
  7. 多版本号并发控制(MVCC)在分布式系统中的应用
  8. 让自己的C++程序(非服务程序)运行为一个windows service
  9. linux日志(常用命令)
  10. 8.21.2 深入finally语句快
  11. bzoj5250 [2018多省省队联测]秘密袭击
  12. BZOJ4025 二分图 线段树分治、带权并查集
  13. 大数据技术 - 通俗理解MapReduce之WordCount(二)
  14. CentOS 7 无法yum安装解决方法
  15. 为什么Firefox在SSH上这么慢?
  16. Linux内核分析作业第七周
  17. Tomcat中catalina run后台运行脚本
  18. Go(02)windows环境搭建和vscode配置
  19. 数据结构(三)串---KMP模式匹配算法
  20. IDEA使用笔记(三)——小齿轮的显示和隐藏(Autoscroll from Source)

热门文章

  1. Vue2+Webpack+ES6 兼容低版本浏览器(IE9)解决方案
  2. 洛谷P1138 第k小整数
  3. React:关于虚拟DOM(Virtual DOM)
  4. IDEA使用操作说明(自己总结)
  5. 【转】Visual Studio單元測試小應用-測執行時間
  6. angular-API
  7. Qt Quick 简单介绍
  8. node--19 moogose demo1
  9. mysql实战45讲读书笔记(二) 一条SQL更新语句是如何执行的 极客时间
  10. spring boot integrated mybatis three ways!--转