Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2350    Accepted Submission(s): 1241

Problem Description
Yifenfei very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.
Every
time yifenfei should to do is that choose a detour which frome the top
left point to the bottom right point and than back to the top left point
with the maximal values of sum integers that area of Matrix yifenfei
choose. But from the top to the bottom can only choose right and down,
from the bottom to the top can only choose left and up. And yifenfei can
not pass the same area of the Matrix except the start and end.
 
Input
The input contains multiple test cases.
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
 
Output
For each test case output the maximal values yifenfei can get.
 
Sample Input
2
10 3
5 10
3
10 3 3
2 5 3
6 7 10
5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
 
Sample Output
28
46
80
 
Author
yifenfei
 
Source
 
题意:一个人要从(1,1)->(n,n) 然后要从(n,n)->(1,1),每个点最多走一次,每个点都要一个权值,问这样走完之后能够得到的最大权值是多少?
题解:由于每个点只能够走一次,所以我们将除了 (1,1)和(n,n) 之外的点都拆点来限制次数(其实好像(n,n)也可以拆,因为也只会走一次,但是(1,1)是不能够拆的),然后将(i-1)*n+j向(i-1)+j+n*n连一条容量为1,费用为 -graph[i][j] ,然后将每个点都和其右边和下面的点连一条容量为1,费用为 0的边,然后建立超级源点,向 (1,1)连一条容量为2,费用为0的边,建立超级汇点,然后将(n,n)向超级汇点连一条容量为2,费用为0的边,这样就达到了限制次数为2的效果,最后跑一遍最小费用最大流,取反之后加上graph[1][1]和graph[n][n]即最后的结果.
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = ;
const int N = ;
const int M = ;
struct Edge{
int u,v,cap,cost,next;
}edge[M];
int head[N],tot,low[N],pre[N];
int total ;
bool vis[N];
int flag[N][N];
void addEdge(int u,int v,int cap,int cost,int &k){
edge[k].u=u,edge[k].v=v,edge[k].cap = cap,edge[k].cost = cost,edge[k].next = head[u],head[u] = k++;
edge[k].u=v,edge[k].v=u,edge[k].cap = ,edge[k].cost = -cost,edge[k].next = head[v],head[v] = k++;
}
void init(){
memset(head,-,sizeof(head));
tot = ;
}
bool spfa(int s,int t,int n){
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
low[i] = INF;
pre[i] = -;
}
queue<int> q;
low[s] = ;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
if(edge[k].cap>&&low[v]>low[u]+edge[k].cost){
low[v] = low[u] + edge[k].cost;
pre[v] = k; ///v为终点对应的边
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t]==-) return false;
return true;
}
int MCMF(int s,int t,int n){
int mincost = ,minflow,flow=;
while(spfa(s,t,n))
{
minflow=INF+;
for(int i=pre[t];i!=-;i=pre[edge[i].u])
minflow=min(minflow,edge[i].cap);
flow+=minflow;
for(int i=pre[t];i!=-;i=pre[edge[i].u])
{
edge[i].cap-=minflow;
edge[i^].cap+=minflow;
}
mincost+=low[t]*minflow;
}
total=flow;
return mincost;
}
int n;
int graph[][];
int P(int x,int y){
return (x-)*n+y;
}
int main(){
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&graph[i][j]);
}
}
int src = ,des = *n*n+;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(P(i,j)!=&&P(i,j)!=n*n){
addEdge(P(i,j),P(i,j)+n*n,,-graph[i][j],tot);
if(i!=n) addEdge(P(i,j)+n*n,P(i+,j),,,tot);
if(j!=n) addEdge(P(i,j)+n*n,P(i,j)+,,,tot);
}else{
if(P(i,j)==){
addEdge(P(i,j),P(i+,j),,,tot);
addEdge(P(i,j),P(i,j)+,,,tot);
}
}
}
}
addEdge(src,,,,tot);
addEdge(n*n,des,,,tot);
int min_cost = MCMF(src,des,*n*n+);
printf("%d\n",-min_cost+graph[][]+graph[n][n]);
}
}

hdu 3376开大一点就OK

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = ;
const int N = ;
const int M = ;
struct Edge{
int u,v,cap,cost,next;
}edge[M];
int head[N],tot,low[N],pre[N];
int total ;
bool vis[N];
void addEdge(int u,int v,int cap,int cost,int &k){
edge[k].u=u,edge[k].v=v,edge[k].cap = cap,edge[k].cost = cost,edge[k].next = head[u],head[u] = k++;
edge[k].u=v,edge[k].v=u,edge[k].cap = ,edge[k].cost = -cost,edge[k].next = head[v],head[v] = k++;
}
void init(){
memset(head,-,sizeof(head));
tot = ;
}
bool spfa(int s,int t,int n){
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
low[i] = INF;
pre[i] = -;
}
queue<int> q;
low[s] = ;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
if(edge[k].cap>&&low[v]>low[u]+edge[k].cost){
low[v] = low[u] + edge[k].cost;
pre[v] = k; ///v为终点对应的边
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t]==-) return false;
return true;
}
int MCMF(int s,int t,int n){
int mincost = ,minflow,flow=;
while(spfa(s,t,n))
{
minflow=INF+;
for(int i=pre[t];i!=-;i=pre[edge[i].u])
minflow=min(minflow,edge[i].cap);
flow+=minflow;
for(int i=pre[t];i!=-;i=pre[edge[i].u])
{
edge[i].cap-=minflow;
edge[i^].cap+=minflow;
}
mincost+=low[t]*minflow;
}
total=flow;
return mincost;
}
int n;
int graph[][];
int P(int x,int y){
return (x-)*n+y;
}
int main(){
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&graph[i][j]);
}
}
int src = ,des = *n*n+;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(P(i,j)!=&&P(i,j)!=n*n){
addEdge(P(i,j),P(i,j)+n*n,,-graph[i][j],tot);
if(i!=n) addEdge(P(i,j)+n*n,P(i+,j),,,tot);
if(j!=n) addEdge(P(i,j)+n*n,P(i,j)+,,,tot);
}else{
if(P(i,j)==){
addEdge(P(i,j),P(i+,j),,,tot);
addEdge(P(i,j),P(i,j)+,,,tot);
}
}
}
}
addEdge(src,,,,tot);
addEdge(n*n,des,,,tot);
int min_cost = MCMF(src,des,*n*n+);
printf("%d\n",-min_cost+graph[][]+graph[n][n]);
}
}

hdu 2686&&hdu 3376(拆点+构图+最小费用最大流)的更多相关文章

  1. hdu 4411 2012杭州赛区网络赛 最小费用最大流 ***

    题意: 有 n+1 个城市编号 0..n,有 m 条无向边,在 0 城市有个警察总部,最多可以派出 k 个逮捕队伍,在1..n 每个城市有一个犯罪团伙,          每个逮捕队伍在每个城市可以选 ...

  2. HDU 6118 度度熊的交易计划(最小费用最大流)

    Problem Description度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生产能力的区别,第i个 ...

  3. HDU 3435 A new Graph Game(最小费用最大流)&amp;HDU 3488

    A new Graph Game Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. hdu 3488(KM算法||最小费用最大流)

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  5. hdu-3376-Matrix Again(最小费用最大流)

    题意: 给一个矩形,从左上角走到右下角,并返回左上角(一个单元格只能走一次,左上角和右下角两个点除外) 并且从左上到右下只能往右和下两个方向.从右下返回左上只能走上和左两个方向! 分析: 拆点,最小费 ...

  6. hdu 2686 Matrix 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...

  7. hdu 4494 Teamwork 最小费用最大流

    Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 ...

  8. hdu 1533 Going Home 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...

  9. HDU 5988.Coding Contest 最小费用最大流

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

随机推荐

  1. async的基本用法

    1. async函数的基本形式 //函数声明 async function foo() {} //函数表达式 const foo = async function () {}; //对象的方法 let ...

  2. Semphore信号量的使用

    前言:在多线程环境的同步中,我们为了让每个线程具有同步的作用,经常采用synchronize.reetrantlock等同步手段进行上锁,以便在同一时间只能有一个线程具有访问变量和读写变量的权力.然而 ...

  3. linux查找文件目录及mysql卸载

    我们要卸载 mysql但是不知道其安装在哪里了,可以用  where +关键词 的方式查找,如上图 输入 whereis mysql 后,下面显示出了4个包含mysql的位置. ..... 查看安装m ...

  4. AtCoder Grand Contest 031 B - Reversi(DP)

    B - Reversi 题目链接:https://atcoder.jp/contests/agc031/tasks/agc031_b 题意: 给出n个数,然后现在你可以对一段区间修改成相同的值,前提是 ...

  5. 【题解】Crash的数字表格 BZOJ 2154 莫比乌斯反演

    题目传送门 http://www.lydsy.com/JudgeOnline/problem.php?id=2154 人生中第一道自己做出来的莫比乌斯反演 人生中第一篇用LaTeX写数学公式的博客 大 ...

  6. HDU3579 线性同余方程(模板 余数不一定互质)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  7. jsoup select 选择器

    转载自:http://blog.csdn.net/zhejingyuan/article/details/11801027 方法 利用方法:Element.select(String selector ...

  8. Beautiful Soup的一些中文资料

    如果你着急用的话,可以看下这个简略版的,非常简单: 转自  人世间http://rsj217.diandian.com/post/2012-11-01/40041235132 当然,强烈推荐你看一下的 ...

  9. UVA 1262 Password

    https://vjudge.net/problem/UVA-1262 字典序第k小 注意两点: 1. k-- 2.去重 #include<cstring> #include<cst ...

  10. MyBatis注解Annotation介绍及Demo

     MyBatis注解Annotation介绍及Demo 2014-04-21 17:09:55 标签:Mybatis Annotation 注解 ResultMap SqlBuilder 原创作品,允 ...