HDU2686-Matrix & HDU3376-Matrix Again(费用流)
比较简单的题了。
只需从左上角到右下角找两条路就可以了。
因为每个点只能走一次,所以拆点,限制流量为1。
因为求的是最大值,所以权值取反求最小值。
因为第一个点和最后一个点经过两次,只算一次,最后要减去。
ps:数组还是开大点好。。。。不知道什么时候就SB了。。。
注意汇点可能不是最后一个点(模板的问题。。
注意初始化的范围。。。。
matrix again只是数据大了,算法不用改。。。无聊。。。。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <bitset>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define pk(x) printf("%d\n", x)
using namespace std;
#define PI acos(-1.0)
#define EPS 1E-6
#define clr(x,c) memset(x,c,sizeof(x))
//#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll; const int MAXV = ;
const int INF = <<; struct Edge { int to, cap, cost, rev; };
vector<Edge> G[MAXV];
int dist[MAXV], prv[MAXV], pre[MAXV], in[MAXV];
queue<int> que; void addedge(int from, int to, int cap, int cost) {
G[from].push_back((Edge){to, cap, cost, G[to].size()});
G[to].push_back((Edge){from, , -cost, G[from].size()-});
} int min_cost_max_flow(int s, int t) { //, int f) {
int res = ;
int f = ;
while () { //f > 0) {
for (int i = ; i <= t; ++i) dist[i] = INF, in[i] = ;
dist[s] = ;
while (!que.empty()) que.pop();
in[s] = ;
que.push(s); while (!que.empty()) {
int u = que.front(); que.pop(); in[u] = ;
for (int i = ; i < G[u].size(); ++i) {
Edge &e = G[u][i];
if (e.cap > && dist[e.to] > dist[u] + e.cost) {
dist[e.to] = dist[u] + e.cost; prv[e.to] = u;
pre[e.to] = i;
if (in[e.to] == ) {
in[e.to] = ;
que.push(e.to);
}
}
}
} if (dist[t] == INF) break; //return -1; int d = INF; // d = f;
for (int v = t; v != s; v = prv[v]) {
d = min(d, G[prv[v]][pre[v]].cap);
}
f += d;
res += d * dist[t];
for (int v = t; v != s; v = prv[v]) {
Edge &e = G[prv[v]][pre[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
//return f;
} int n;
int getid1(int x, int y) { return x*n+y+; }
int getid2(int x, int y) { return x*n+y+n*n+; }
int a[][];
int main()
{
while (~scanf("%d", &n)) {
for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
scanf("%d", &a[i][j]);
}
}
for (int i = ; i <= *n*n; ++i) G[i].clear();
for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
if (i+j == ) addedge(getid1(i,j), getid2(i,j), , -a[i][j]);
else if (i+j == n* - ) addedge(getid1(i,j), getid2(i, j), , -a[i][j]);
else addedge(getid1(i, j), getid2(i, j), , -a[i][j]);
if (i+<n) addedge(getid2(i, j), getid1(i+, j), , );
if (j+<n) addedge(getid2(i, j), getid1(i, j+), , );
}
}
printf("%d\n", -min_cost_max_flow(getid1(,), getid2(n-,n-)) - a[][] - a[n-][n-]);
}
return ;
}
hdu2686
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <bitset>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define pk(x) printf("%d\n", x)
using namespace std;
#define PI acos(-1.0)
#define EPS 1E-6
#define clr(x,c) memset(x,c,sizeof(x))
//#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll; const int MAXV = **+;
const int INF = <<; struct Edge { int to, cap, cost, rev; };
vector<Edge> G[MAXV];
int dist[MAXV], prv[MAXV], pre[MAXV], in[MAXV];
queue<int> que; void addedge(int from, int to, int cap, int cost) {
G[from].push_back((Edge){to, cap, cost, G[to].size()});
G[to].push_back((Edge){from, , -cost, G[from].size()-});
} int min_cost_max_flow(int s, int t) { //, int f) {
int res = ;
int f = ;
while () { //f > 0) {
for (int i = ; i <= t; ++i) dist[i] = INF, in[i] = ;
dist[s] = ;
while (!que.empty()) que.pop();
in[s] = ;
que.push(s); while (!que.empty()) {
int u = que.front(); que.pop(); in[u] = ;
for (int i = ; i < G[u].size(); ++i) {
Edge &e = G[u][i];
if (e.cap > && dist[e.to] > dist[u] + e.cost) {
dist[e.to] = dist[u] + e.cost; prv[e.to] = u;
pre[e.to] = i;
if (in[e.to] == ) {
in[e.to] = ;
que.push(e.to);
}
}
}
} if (dist[t] == INF) break; //return -1; int d = INF; // d = f;
for (int v = t; v != s; v = prv[v]) {
d = min(d, G[prv[v]][pre[v]].cap);
}
f += d;
res += d * dist[t];
for (int v = t; v != s; v = prv[v]) {
Edge &e = G[prv[v]][pre[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
//return f;
} inline int Scan()
{
char ch = getchar();
int data = ;
while (ch < '' || ch > '') ch = getchar();
do {
data = data* + ch-'';
ch = getchar();
} while (ch >= '' && ch <= '');
return data;
} int n;
int getid1(int x, int y) { return x*n+y+; }
int getid2(int x, int y) { return x*n+y+n*n+; }
int a[][];
int main()
{
while (~scanf("%d", &n)) {
int maxn = *n*n;
for (int i = ; i <= maxn; ++i) G[i].clear();
for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
a[i][j] = Scan();
if (i+j == || i+j == n*-) addedge(getid1(i,j), getid2(i,j), , -a[i][j]);
else addedge(getid1(i, j), getid2(i, j), , -a[i][j]);
if (i+<n) addedge(getid2(i, j), getid1(i+, j), , );
if (j+<n) addedge(getid2(i, j), getid1(i, j+), , );
}
}
printf("%d\n", -min_cost_max_flow(getid1(,), getid2(n-,n-)) - a[][] - a[n-][n-]);
}
return ;
}
hdu3376
HDU2686-Matrix & HDU3376-Matrix Again(费用流)的更多相关文章
- HDU 2686 Matrix 3376 Matrix Again(费用流)
HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...
- POJ3422 Kaka's Matrix Travels 【最大费用最大流】
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8006 Accepted: ...
- POJ 3422 Kaka's Matrix Travels(费用流)
POJ 3422 Kaka's Matrix Travels 题目链接 题意:一个矩阵.从左上角往右下角走k趟,每次走过数字就变成0,而且获得这个数字,要求走完之后,所获得数字之和最大 思路:有点类似 ...
- hdoj 3376,2686 Matrix Again 【最小费用最大流】
题目:hdoj 3376 Matrix Again 题意:给出一个m*n的矩阵,然后从左上角到右下角走两次,每次仅仅能向右或者向下,出了末尾点其它仅仅能走一次,不能交叉,每次走到一个格子拿走这个格子中 ...
- POJ 2135 Farm Tour && HDU 2686 Matrix && HDU 3376 Matrix Again 费用流求来回最短路
累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...
- POJ 3422 Kaka's Matrix Travels (K取方格数:最大费用流)
题意 给出一个n*n大小的矩阵,要求从左上角走到右下角,每次只能向下走或者向右走并取数,某位置取过数之后就只为数值0,现在求解从左上角到右下角走K次的最大值. 思路 经典的费用流模型:K取方格数. 构 ...
- poj 3422 Kaka's Matrix Travels 费用流
题目链接 给一个n*n的矩阵, 从左上角出发, 走到右下角, 然后在返回左上角,这样算两次. 一共重复k次, 每个格子有值, 问能够取得的最大值是多少, 一个格子的值只能取一次, 取完后变为0. 费用 ...
- HDU 3376 费用流 Matrix Again
题意: 给出一个n × n的矩阵,每个格子中有一个数字代表权值,找出从左上角出发到右下角的两条不相交的路径(起点和终点除外),使得两条路径权值之和最大. 分析: 如果n比较小的话是可以DP的,但是现在 ...
- HDU2686 费用流 模板
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
随机推荐
- pthread_create用法
linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread. #include <pthread.h> int pthread_create(pth ...
- hdu 2897 邂逅明下 博弈论
这个分区间: 1<=n%(p+q)<=p 必败 其他必胜! 代码如下: #include<iostream> #include<stdio.h> #include& ...
- jmeter 测试java协议经验总结
对java协议的良好支持,是jmeter比loadrunner优秀的地方,但是坑也不少,本文将相关点都整理下来备忘 一. 依赖的jar包 使用IDE开发jemter java协议脚本时,需要导入以下几 ...
- Subset leetcode java
题目: Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset mus ...
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- (转)Decision Tree
Decision Tree:Analysis 大家有没有玩过猜猜看(Twenty Questions)的游戏?我在心里想一件物体,你可以用一些问题来确定我心里想的这个物体:如是不是植物?是否会飞?能游 ...
- 关于C#控制台传递参数和接收参数
前言: 写了这么久程序,今天才知道的一个基础知识点,就是程序入口 static void Main(string[] args) 里的args参数是什么意思 ?惭愧... 需求: 点击一个button ...
- date 获取昨天日期
使用date -d 选项: date +"%Y%m%d" -d "+n days" 今天的后n天日期 date +" ...
- xUTils框架的学习(二)
这章讲的是框架的DbUtils模块的学习 三 xUtils框架的DButils模块 最开始接触这个框架就是从数据库模块开始的.当时的需求是需要记录用户的登录数据,保存在本地以便进行离线登录.首先想到的 ...
- Hibernate4.x之映射关系--双向1-n
双向1-n与双向n-1是完全相同的两种情形 双向1-n需要在1的一端可以访问n的一端,反之亦然. 域模型:从Order到Customer的多对一双向关联需要在Order类中定义一个Customer属性 ...