Matrix

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

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
 
Recommend
yifenfei
//费用流模板题,因为题目中的表述就是一个网络流模型,拆点建图,没点只经过一次,起点终点容量为2,
//其他点容量为1,要求最大费用费用为负权值,套模板。起点和终点经历了两次,最后要减去。
/****************最小费用最大流模板,白书363页*******************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int maxn=30*30*2+5,inf=0x7fffffff;//本体拆点,数组多开两倍
struct Edge
{
int from,to,cap,flow,cost;
Edge(int u,int v,int c,int f,int cs):from(u),to(v),cap(c),flow(f),cost(cs){}
};
struct MCMF
{
int n,m,s,t;
vector<Edge>edges;
vector<int>g[maxn];
int inq[maxn],d[maxn],p[maxn],a[maxn];
void init(int n)
{
this->n=n;
for(int i=0;i<n;i++) g[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap,int cost)
{
edges.push_back((Edge){from,to,cap,0,cost});
edges.push_back((Edge){to,from,0,0,-cost});
m=edges.size();
g[from].push_back(m-2);
g[to].push_back(m-1);
}
bool BellmanFord(int s,int t,int &flow,int &cost)
{
for(int i=0;i<n;i++) d[i]=inf;
memset(inq,0,sizeof(inq));
d[s]=0;inq[s]=1;p[s]=0;a[s]=inf;
queue<int>q;
q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
inq[u]=0;
for(int i=0;i<(int)g[u].size();i++){
Edge &e=edges[g[u][i]];
if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
d[e.to]=d[u]+e.cost;
p[e.to]=g[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to]) {q.push(e.to);inq[e.to]=1;}
}
}
}
if(d[t]==inf) return false;
flow+=a[t];
cost+=d[t]*a[t];
int u=t;
while(u!=s){
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
u=edges[p[u]].from;
}
return true;
}
int Mincost(int s,int t)
{
int flow=0,cost=0;
while(BellmanFord(s,t,flow,cost));
return cost;//返回最小费用,flow存最大流
}
}MC;
/**********************************************************************************/
int main()
{
int n,mp[35][35];
while(scanf("%d",&n)==1){
int s=0,t=n*n*2+1;
MC.init(n*n*2+2);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&mp[i][j]);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
int id=(i-1)*n+j;
if(id==1){
MC.AddEdge(id,id+n*n,2,-mp[i][j]);
MC.AddEdge(s,id,2,0);
}
else if(id==n*n){
MC.AddEdge(id,id+n*n,2,-mp[i][j]);
MC.AddEdge(id+n*n,t,2,0);
}
else MC.AddEdge(id,id+n*n,1,-mp[i][j]);
if(i<n){
int nid=id+n;
MC.AddEdge(id+n*n,nid,1,0);
}
if(j<n){
int nid=id+1;
MC.AddEdge(id+n*n,nid,1,0);
}
}
}
int ans=-(MC.Mincost(0,n*n*2+1)+mp[1][1]+mp[n][n]);
printf("%d\n",ans);
}
return 0;
}

  

Matrix Again

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 4255    Accepted Submission(s): 1233

Problem Description
Starvae 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 starvae should to do is that choose a detour which from 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 starvae 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 starvae can not pass the same area of the Matrix except the start and end..
Do you know why call this problem as “Matrix Again”? AS it is like the problem 2686 of HDU.
 
Input
The input contains multiple test cases.
Each case first line given the integer n (2<=n<=600) 
Then n lines, each line include n positive integers. (<100)
 
Output
For each test case output the maximal values starvae 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
Starvae
 
Source
//和HDU2686一样,只是数据变大了,上一个模板会超内存,这个板不会。
/***********************最小费用最大流模板2*************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=610*610*2+2;
const int maxm=4*maxn;//!边数要够
const int inf=0x7fffffff;
struct Edge
{
int to,next,cap,flow,cost;
}edges[maxm];
int head[maxn],tol,pre[maxn],dis[maxn];
bool vis[maxn];
int N;
void init(int n)
{
N=n;
tol=0;
memset(head,-1,sizeof(head));
}
void AddEdge(int u,int v,int cap,int cost)
{
edges[tol].to=v;
edges[tol].cap=cap;
edges[tol].cost=cost;
edges[tol].flow=0;
edges[tol].next=head[u];
head[u]=tol++;
edges[tol].to=u;
edges[tol].cap=0;
edges[tol].cost=-cost;
edges[tol].flow=0;
edges[tol].next=head[v];
head[v]=tol++;
}
bool spfa(int s,int t)
{
queue<int>q;
for(int i=0;i<=N;i++){
dis[i]=inf;
vis[i]=0;
pre[i]=-1;
}
dis[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edges[i].next){
int v=edges[i].to;
if(edges[i].cap>edges[i].flow&&dis[v]>dis[u]+edges[i].cost){
dis[v]=dis[u]+edges[i].cost;
pre[v]=i;
if(!vis[v]) {vis[v]=1;q.push(v);}
}
}
}
if(pre[t]==-1) return 0;
return 1;
}
int MinCostFlow(int s,int t)
{
int flow=0,cost=0;
while(spfa(s,t)){
int Min=inf;
for(int i=pre[t];i!=-1;i=pre[edges[i^1].to])
Min=min(Min,edges[i].cap-edges[i].flow);
for(int i=pre[t];i!=-1;i=pre[edges[i^1].to]){
edges[i].flow+=Min;
edges[i^1].flow-=Min;
cost+=edges[i].cost*Min;
}
flow+=Min;
}
return cost;//返回最小费用,flow存最大流
}
/*********************************************************************/
int main()
{
int n,mp[605][605];
while(scanf("%d",&n)==1){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++) scanf("%d",&mp[i][j]);
int s=0,t=n*n*2+1;
init(n*n*2+2);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
int id=(i-1)*n+j;
if(id==1){
AddEdge(id,id+n*n,2,-mp[i][j]);
AddEdge(s,id,2,0);
}
else if(id==n*n){
AddEdge(id,id+n*n,2,-mp[i][j]);
AddEdge(id+n*n,t,2,0);
}
else AddEdge(id,id+n*n,1,-mp[i][j]);
if(i<n) AddEdge(id+n*n,id+n,1,0);
if(j<n) AddEdge(id+n*n,id+1,1,0);
}
}
int ans=-(MinCostFlow(s,t)+mp[1][1]+mp[n][n]);
printf("%d\n",ans);
}
return 0;
}

  

给出代码供以后参考学习

多线程DP的更多相关文章

  1. Matrix(多线程dp)

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  2. HDU 2686 Matrix 多线程dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 思路:多线程dp,参考51Nod 1084:http://www.51nod.com/onlin ...

  3. codevs1169, 51nod1084(多线程dp)

    先说下codevs1169吧, 题目链接: http://codevs.cn/problem/1169/ 题意: 中文题诶~ 思路: 多线程 dp 用 dp[i][j][k][l] 存储一个人在 (i ...

  4. 51Nod 1084 矩阵取数问题 V2 —— 最小费用最大流 or 多线程DP

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1084 1084 矩阵取数问题 V2  基准时间限制:2 秒 空 ...

  5. 8786:方格取数 (多线程dp)

    [题目描述] 设有N*N的方格图(N<=10),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字0.某人从图的左上角的A 点出发,可以向下行走,也可以向右走,直到到达右下角的B点.在走 ...

  6. TYVJ 1011 NOIP 2008&&NOIP 2000 传纸条&&方格取数 Label:多线程dp

    做题记录:2016-08-15 15:47:07 背景 NOIP2008复赛提高组第三题 描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行 ...

  7. (多线程dp)Matrix (hdu 2686)

    http://acm.hdu.edu.cn/showproblem.php?pid=2686     Problem Description Yifenfei very like play a num ...

  8. NOIP 2008 传纸条 NOIP 2000 方块取数 多线程DP

    思路都是一样,建立一个四维dp然后跑一发就完了 当然,也可以像我这么帅的人,降成三维再傻傻的跑一发啦啦啦~ #include<iostream> #include<stdio.h&g ...

  9. 51nod 1503 猪和回文(多线程DP)

    虚拟两个点,一个从左上角开始走,一个从右下角开始走,定义dp[i][j][k]表示走了i步后,第一个点横向走了j步,第二个点横向走了k步后形成的回文方法种数. 转移方程显然可得,然后滚动数组搞一搞. ...

  10. 51nod 1503 多线程dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1503 1503 猪和回文 题目来源: CodeForces 基准时间限制 ...

随机推荐

  1. 如何把MyEclipse中的web项目导入到Eclipse中运行

    有时我们需要将MyEclipse中的项目导入到Eclipse中运行,如果不注意到一些细节,会造成无法运行的后果.下面就说说具体操作: 如何导入到Eclipse就不在重述了,导入后出现如下错误: 与上面 ...

  2. 【原】Coursera—Andrew Ng机器学习—编程作业 Programming Exercise 4—反向传播神经网络

    课程笔记 Coursera—Andrew Ng机器学习—课程笔记 Lecture 9_Neural Networks learning 作业说明 Exercise 4,Week 5,实现反向传播 ba ...

  3. popup non topmost

    public class PopupNonTopmost : Popup { public static DependencyProperty TopmostProperty = Window.Top ...

  4. Ubuntu 15.04 开机无法进入图形界面,自动进入emergency mode解决方法

    原因:关机的时候没有正常退出,直接强制关机了... 解决: 就是在那个GIVE root password for maintenance (or type control-D to continue ...

  5. Python常见字符串处理操作

    Python中字符串处理的方法已经超过37种了,下面是一些常用的字符串处理的方法,以后慢慢添加. >>> s = 'Django is cool' #创建一个字符串 >> ...

  6. Redis搭建(六):Redis持久化配置

    一.介绍 Redis的持久化有2种方式: Rdb快照 Aof日志 1. Rdb快照的配置选项 save 900 1 // 900内,有1条写入,则产生快照 save 300 1000 // 如果300 ...

  7. Android开发实战之拥有Material Design风格的折叠布局

    关于折叠布局,也许你并不陌生,最新版的陌陌,或者一些其他的社交APP都有一个折叠布局.折叠布局,让我们的APP更加具有交互性,同时也更加美观,先来展示一下效果图: 这是我个人做的一个APP主界面,可以 ...

  8. kubernetes基础环境配置

    一.基础环境配置 环境详情 主机名(FQDN) IP地址(NAT) 描述 linux-node1.example.com eth0:192.168.56.11 1VCPU.2G内存.一块硬盘s da5 ...

  9. JAVA数据结构实现原理

    HashTable 线程安全, 内部函数被synchronized修饰,对象级的锁 HashMap 非线程安全, 需要tradeoff 空间和查找时间, 空间利用率低时,冲突少,查询效率高,反之空间利 ...

  10. 19-字符切割函数c++模板

    https://www.cnblogs.com/stonebloom-yu/p/6542756.html #include <cstring> #include <cstdio> ...