# HDU 2686 Matrix(最大费用最大流+拆点)

POJ3422一样

```#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
const int maxn = 20000;
const int maxm = 800000;
const int inf = 1e8;
const int INF = 0x3f3f3f3f;
#define MIN INT_MIN
#define MAX 1e6
#define LL long long
#define init(a) memset(a,0,sizeof(a))
#define FOR(i,a,b) for(int i = a;i<b;i++)
#define max(a,b) (a>b)?(a):(b)
#define min(a,b) (a>b)?(b):(a)
using namespace std;
struct node
{
int u,v,w,cap,next;
} edge[maxm];
bool vis[maxn];
int n;
void add(int u,int v,int c,int cap)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=c;
edge[cnt].cap=cap;

edge[cnt].u=v;
edge[cnt].v=u;
edge[cnt].w=-c;
edge[cnt].cap=0;
}
int spfa(int s,int t)
{
queue<int>q;
while(q.empty()==false) q.pop();
q.push(s);
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
FOR(i,s,t+1)
dis[i] = -1;//求最长路dis数组初始化为-1

dis[s]=0;
vis[s] = 1;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u] = 0;
{
if(edge[i].cap && dis[edge[i].v] < (dis[u]+edge[i].w))//求最长路
{
dis[edge[i].v] = dis[u]+edge[i].w;
pre[edge[i].v] = i;
if(!vis[edge[i].v])
{
vis[edge[i].v]=1;
q.push(edge[i].v);
}
}
}

}
if(dis[t] != -1)//------------------忘改了。。
return 1;
else
return 0;
}
int MinCostMaxFlow(int s,int t)
{
int flow=0,cost=0;
while(spfa(s,t))
{
int df = inf;
for(int i = pre[t]; i!=-1; i=pre[edge[i].u])
{
if(edge[i].cap<df)
df = edge[i].cap;
}
flow += df;
for(int i=pre[t]; i!=-1; i=pre[edge[i].u])
{
edge[i].cap -= df;
edge[i^1].cap += df;
}
//printf("df = %d\n",df);
cost += dis[t] * df;
}
return cost;
}
void initt()
{
cnt=0;
}

int ma;
int main()
{
int s,t,k;
while(~scanf("%d",&n))
{
initt();
s = 0;
t=2*n*n+1;
int num = n*n;
FOR(i,1,n+1)
{
FOR(j,1,n+1)
{
scanf("%d",&ma);

if(i<=n-1)//向下建图
{
}
if(j<=n-1)//向右建图
{
}
}
}

int ans =  MinCostMaxFlow(s,t);
printf("%d\n",ans);
}
return 0;
}
```

HDU 2686 Matrix(最大费用最大流+拆点),布布扣,bubuko.com

## HDU 2686 Matrix(最大费用流)

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1890    Accepted Submission(s): 1005 Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer numbe

## HDU 2686 Matrix 3376 Matrix Again（费用流）

HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,只是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 思路:拆点,建图,然后跑费用流即可,不过HDU3376这题,极限情况是300W条边,然后卡时间过了2333 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #i

## hdu 2686 Matrix &amp;&amp; hdu 3367 Matrix Again (最大费用最大流)

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1394    Accepted Submission(s): 758 Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer number