在个给出的矩阵从,从左上角走到右下角,然后再从右下角走到左上角,两次不能经过想同的点,每个点都有一个价值,问最大的价值是多少。

可以把原来的问题化简成从左上角走两条路到右下角,然后把价值加起来,然是这时候我出发点和目标点的价值加了两次,而原本只计算一次,所以最后要减掉,然后建图

1.超源到(1,1), 容量inf,费用0

2.自身拆点,(1, 1)和(n, m)容量2,其他点容量1,费用就是本身的价值的负值

3.(n, m)到超汇,容量inf,费用0

#include<map>
#include<set>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define first fi
#define second se
#define lowbit(x) (x & (-x)) typedef unsigned long long int ull;
typedef long long int ll;
const double pi = 4.0*atan(1.0);
const int inf = 0x3f3f3f3f;
const int maxn = ;
const int maxm = ;
const int mod = ;
using namespace std; int n, m, tol, T;
struct Node {
int u;
int v;
int w;
int val;
int next;
};
Node node[maxm];
int head[maxn];
int dis[maxn];
int pre[maxn];
int cap[maxn];
bool vis[maxn];
int maps[][]; void init() {
tol = ;
memset(node, , sizeof node);
memset(maps, , sizeof maps);
memset(head, -, sizeof head);
} void addnode(int u, int v, int w, int val) {
node[tol].u = u;
node[tol].v = v;
node[tol].w = w;
node[tol].val = val;
node[tol].next = head[u];
head[u] = tol++;
} bool spfa(int src, int des, int &flow, int &cost) {
memset(vis, , sizeof vis);
memset(dis, inf, sizeof dis);
memset(cap, inf, sizeof cap);
queue<int > q;
dis[src] = ;
cap[src] = inf;
pre[src] = src;
vis[src] = true;
q.push(src);
while(!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for(int i=head[u]; ~i; i=node[i].next) {
int v = node[i].v;
if(node[i].w && dis[v] > dis[u] + node[i].val) {
dis[v] = dis[u] + node[i].val;
pre[v] = i;
cap[v] = min(cap[u], node[i].w);
if(!vis[v]) {
vis[v] = true;
q.push(v);
}
}
}
}
if(dis[des] == inf) return false;
flow += cap[des];
cost += cap[des] * dis[des];
int u = des;
while(u != src) {
node[pre[u]].w -= cap[des];
node[pre[u]^].w += cap[des];
u = node[pre[u]].u;
}
return true;
} int MCMF(int src, int des) {
int flow = ;
int cost = ;
while(spfa(src, des, flow, cost));
return cost;
} int main() {
int cas = ;
scanf("%d", &T);
while(T--) {
init();
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++) for(int j=; j<=m; j++) scanf("%d", &maps[i][j]);
int src = , des = *n*m+;
addnode(src, , inf, );
addnode(, src, , );
addnode(*n*m, des, inf, );
addnode(des, *n*m, , );
int cnt = n * m;
for(int i=; i<=n; i++) {
for(int j=; j<=m; j++) {
if((i== && j==) || (i==n && j == m)) {
addnode((i-)*m+j, (i-)*m+j+cnt, , -maps[i][j]);
addnode((i-)*m+j+cnt, (i-)*m+j, , maps[i][j]);
} else {
addnode((i-)*m+j, (i-)*m+j+cnt, , -maps[i][j]);
addnode((i-)*m+j+cnt, (i-)*m+j, , maps[i][j]);
}
if(i+<=n) {
addnode((i-)*m+j+cnt, i*m+j, , );
addnode(i*m+j, (i-)*m+j+cnt, , );
}
if(j+<=m) {
addnode((i-)*m+j+cnt, (i-)*m+j+, , );
addnode((i-)*m+j+, (i-)*m+j+cnt, , );
}
}
}
int ans = -MCMF(src, des) - maps[][] - maps[n][m];
printf("Case %d: %d\n", cas++, ans);
}
return ;
}

Baker Vai LightOJ - 1071 (MCMF)的更多相关文章

  1. Baker Vai LightOJ - 1071

    题意:类似传纸条 方法: 把他要求的操作(一个人来回),转化为两个人同时走,除了开始和结束位置只能走不同路,得到的分数和的最大值即可. 一开始想到要定义的状态,是两个人的x(行)和y(列)坐标.这样时 ...

  2. Intervals POJ - 3680 (MCMF)

    给你一些区间,每个区间都有些价值.取一个区间就能获得对应的价值,并且一个点不能覆盖超过k次,问你最大的价值是多少. 我们可以把这些区间放到一维的轴上去,然后我们可以把它看成一个需要从左到右的过程,然后 ...

  3. P3381 【模板】最小费用最大流(MCMF)

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入格式 第一行包含四个正整数N ...

  4. LightOJ - 1265 (概率)

    题意: 1.两只老虎相遇 就互相残杀 2.老虎与鹿相遇 鹿死 3.老虎与人相遇 人死 4.人与鹿相遇     鹿死 5.鹿与鹿相遇     无果 求人活的概率 解析:如果老虎为0  则人活得概率为1 ...

  5. Trailing Zeroes (III) LightOJ - 1138(二分)

    You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...

  6. bzoj 3171 [Tjoi2013]循环格(MCMF)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3171 [题意] 给定一个方向矩阵,要求改变最少的格子,使得任意一个点都在一个环中. [ ...

  7. bzoj 1834 [ZJOI2010]network 网络扩容(MCMF)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1834 [题意] 给定一个有向图,每条边有容量C,扩容费用W,问最大流和使容量增加K的最 ...

  8. Modular Production Line (MCMF)

    Modular Production Line \[ Time Limit: 1000ms\quad Memory Limit: 65536kB \] 题意 给出 \(N\) 种零件,现在你可以用连续 ...

  9. lightoj 1020 (博弈)

    思路:很简单的博弈,找出每个人先拿的必胜态进行状态转移即可. #include<cstdio> #include<string> #include<cstring> ...

随机推荐

  1. RESTful架构详解

    什么是REST REST全称是Representational State Transfer,中文意思是表述性状态转移,它首次出现在2000年Roy Fielding的博士论文中.Roy Fieldi ...

  2. Java遍历HashMap并修改(remove)(转载)

    遍历HashMap的方法有多种,比如通过获取map的keySet, entrySet, iterator之后,都可以实现遍历,然而如果在遍历过程中对map进行读取之外的操作则需要注意使用的遍历方式和操 ...

  3. JSON Support in PostgreSQL and Entity Framework

    JSON 和JSONB的区别(What's difference between JSON and JSONB data type in PosgresSQL?) When should be use ...

  4. JSP从入门到精通

    1. jsp开发环境配置 在windows下配置jsp的开发环境: 假设已经安装好了jdk,下面来配置tomcat 去http://tomcat.apache.org 下载tomcat windows ...

  5. String 常见的十种方法!

    public class ZiFuChuan { public static void main(String[] args) { ZiFuChuanFangFa f=new ZiFuChuanFan ...

  6. python绝对路径和相对路径

    转自https://blog.csdn.net/databatman/article/details/49453953 下面的路径介绍针对windows,其他平台的暂时不是很了解. 在编写的py文件中 ...

  7. Vue插件plugins的基本操作

    前面的话 本文将详细介绍Vue插件plugins的基本操作 开发插件 插件通常会为 Vue 添加全局功能.插件的范围没有限制——一般有下面几种: 1.添加全局方法或者属性,如: vue-custom- ...

  8. How to write to an event log by using Visual C#

    using System; using System.Diagnostics; namespace WriteToAnEventLog_csharp { /// Summary description ...

  9. oracle的用户账号密码设置

    1. 可以用sqlplus system/你输入的密码 可以用sqlplus /nolog 可以用sqlplus /as sysdba2. @你scott.sql的路径3. 修改你的账号 alter ...

  10. CodeCraft-19 and Codeforces Round #537 Div. 2

    D:即有不超过52种物品,求容量为n/2的有序01背包方案数.容易想到设f[i][j]为前i种物品已用容量为j的方案数,有f[i][j]=f[i-1][j-a[i]]*C(n/2-j+a[i],a[i ...