题目链接:

pid=2686">http://acm.hdu.edu.cn/showproblem.php?pid=2686

POJ3422一样

删掉K把汇点与源点的容量改为2(由于有两个方向的选择)就可以

#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];
int pre[maxn],dis[maxn],head[maxn],cnt;
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].next=head[u];
head[u]=cnt++; edge[cnt].u=v;
edge[cnt].v=u;
edge[cnt].w=-c;
edge[cnt].cap=0;
edge[cnt].next=head[v];
head[v]=cnt++;
}
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;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
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;
memset(head,-1,sizeof(head));
} int ma;
int main()
{
int s,t,k;
while(~scanf("%d",&n))
{
initt();
s = 0;
t=2*n*n+1;
add(s,1,0,2);
int num = n*n;
FOR(i,1,n+1)
{
FOR(j,1,n+1)
{
scanf("%d",&ma);
add((i-1)*n+j,(i-1)*n+j+num,ma,1);
add((i-1)*n+j,(i-1)*n+j+num,0,1);//本点与拆点连线,费用0。容量为无穷 if(i<=n-1)//向下建图
{
add((i-1)*n+j+num,i*n+j,0,1);
}
if(j<=n-1)//向右建图
{
add((i-1)*n+j+num,(i-1)*n+j+1,0,1);
}
}
}
add(t-1,t,0,2); int ans = MinCostMaxFlow(s,t);
printf("%d\n",ans);
}
return 0;
}

HDU 2686 Matrix(最大费用最大流+拆点)的更多相关文章

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

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

  2. HDU 2686 Matrix(最大费用流)

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

  3. HDU 2686 Matrix 3376 Matrix Again(费用流)

    HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...

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

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

  5. hdu 2686 Matrix && hdu 3367 Matrix Again (最大费用最大流)

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

  6. BZOJ-1070 修车 最小费用最大流+拆点+略坑建图

    1070: [SCOI2007]修车 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 3624 Solved: 1452 [Submit][Status] ...

  7. UVA-11613 Acme Corporation (最大费用最大流+拆点)

    题目大意:有一种商品X,其每每单位存放一个月的代价I固定.并且已知其每月的最大生产量.生产每单位的的代价.最大销售量和销售单价,还已知每个月生产的X能最多能存放的时间(以月为单位).问只考虑前m个月, ...

  8. UVa 1658 - Admiral(最小费用最大流 + 拆点)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. Going Home HDU - 1533(最大费用最小流)

    On a grid map there are n little men and n houses. In each unit time, every little man can move one ...

随机推荐

  1. D - Constructing Roads

    D - Constructing Roads 思路:并查集板子 #include<cstdio> #include<cstring> #include<iostream& ...

  2. 开启WIFI

    C:\Windows\system32>netsh wlan set hostednetwork mode=allow ssid=wuyechun-wifi k ey= 承载网络模式已设置为允许 ...

  3. spring容器启动过程理解

    一.一切从手动启动IoC容器开始 ClassPathResource resource = new ClassPathResource("bean.xml"); DefaultLi ...

  4. 《Python 源码阅读》之 类型Type

    py一切皆对象: 那么Type也是个对象.对象类型叫PyTypeObject demo >>> a = 1 >>> a 1 >>> type(a) ...

  5. [HTML 5] Atomic Relevant Busy

    Together 'aria-live', we can use 'aria-atomic', 'aria-relevant' and 'aria-busy' to give more informa ...

  6. 可编程数据平面将OpenFlow扩展至电信级应用(二)

    可编程数据平面将OpenFlow扩展至电信级应用(二) 案例:基于WinPath网络处理器的电信极OpenFlow (CG-OF)client实现 作者:Liviu Pinchas, Tao Lang ...

  7. UVA 1541 - To Bet or Not To Bet 记忆化DP概率

    Alexander Charles McMillan loves to gamble, and during his last trip to the casino he ran across a n ...

  8. 杂项:ExtJS

    ylbtech-杂项:ExtJS extjs是一种软件.自动生成行号,支持checkbox全选,动态选择显示哪些列,支持本地以及远程分页,可以对单元格按照自己的想法进行渲染,这些也算可以想到的功能. ...

  9. jQuery进度条设置

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="con ...

  10. POJ 3020 Hungary

    一道建图题-- // by SiriusRen #include <cstdio> #include <cstring> using namespace std; #defin ...