PKU 3422 网络流
来源:互联网 发布:js的tostring方法 编辑:程序博客网 时间:2024/06/05 16:17
Description
On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka moves one rook from the left-upper grid to the right-bottom one, taking care that the rook moves only to the right or down. Kaka adds the number to SUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximum SUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximum SUM he can obtain after his Kth travel. Note the SUM is accumulative during the K travels.
Input
The first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤ K ≤ 10) described above. The following N lines represents the matrix. You can assume the numbers in the matrix are no more than 1000.
Output
The maximum SUM Kaka can obtain after his Kth travel.
Sample Input
3 21 2 30 2 11 4 2
Sample Output
15
Source
#include<queue>
#include<memory>
using namespace std;
#define inf 0x7ffffff
const int N=6000;
struct FUCK
{
int to,flow,cost;
int next;
}node[20000];
int dia[60*60];
int n,tot,k,en;
int pre[N];
int dis[N];
bool flag[N];
int cnt[N];
int road[N];
int s,t;
int SPFA()
{
queue<int>Q;
int now,next;
pre[0] = 0;
for(int i=1;i<=t;i++)
dis[i] = inf;
dis[0] = 0;
memset(flag,false,sizeof(flag));
flag[s] = true;
Q.push(s);
while(!Q.empty())
{
now = Q.front();
Q.pop();
flag[now] = false;
next=cnt[now];
while(next!=-1)
{
if(node[next].flow>0&& dis[node[next].to] > dis[now] + node[next].cost)
{
dis[node[next].to] = dis[now] + node[next].cost;
pre[node[next].to] = now;
road[node[next].to]=next;
if(!flag[node[next].to])
{
Q.push(node[next].to);
flag[node[next].to] = true;
}
}
next=node[next].next;
}
}
if(dis[t] != inf)
return 1;
else
return 0;
}
int mincostflow()
{
int last,min,ans=0;
while(SPFA())
{
min=inf;
last=t;
while(last!=s)
{
if(node[road[last]].flow<min)
min=node[road[last]].flow;
last=pre[last];
}
ans+=min*dis[t];
last=t;
while(last!=s)
{
node[road[last]].flow-=min;
node[road[last]^1].flow+=min;
last=pre[last];
}
}
return ans;
}
{
node[en].flow=flow;
node[en].cost=cost;
node[en].to=to;
node[en].next=cnt[from];
cnt[from]=en++;
}
{
addedge(from,to,cost,flow);
addedge(to,from,-cost,0);
}
int main()
{
int i;
while(scanf("%d%d",&n,&k)!=EOF)
{
tot=n*n;
for(i=1;i<=tot;i++)
scanf("%d",&dia[i]);
en=0;
memset(cnt,-1,sizeof(cnt));
s=0;
t=tot+tot+1;
for(i=1;i<=tot;i++)
{
add(i,i+tot,-dia[i],1);
add(i,i+tot,0,inf);
if(i%n!=0)
add(i+tot,i+1,0,inf);
if(i+n<=tot)
add(i+tot,i+n,0,inf);
}
add(s,1,0,k);
add(tot+tot,t,0,k);
printf("%d/n",-mincostflow());
}
return 0;
}
- PKU 3422 网络流
- PKU 1087 网络最大流
- 【PKU 3281】Dining(构图+网络流)
- 【PKU 1149】PIGS(构图+网络流)
- 网络流。。。。经典模型。。。HDU2732 && PKU 2711
- PKU-1273 Drainage Ditches (网络最大流)
- pku 3422 最大费用流
- 网络流 HDU2732 && PKU 2711 最大流 SAP
- pku 1087 A Plug for UNIX 网络流 解题报告
- PKU 2289(网络最大流(SAP) + 二分枚举)
- pku 3189 网络流|二分图多重匹配+枚举区间
- PKU 3422 &&PKU 2135 &&ZJU 3308
- pku 1149 最大流
- 【转】poj pku图论、网络流入门题总结、汇总
- poj pku图论、网络流入门题总结、汇总
- poj pku图论、网络流入门题总结、汇总
- BP网络 - 用2001PKU MCM B题基因检测
- poj pku图论、网络流入门题总结、汇总
- GridFS Specification --MongoDB
- [JS] 简单map实现 (普通和 jquery 版本)
- 什么是RSS?RSS及其发展历程
- Uboot通过网口下载内核时出现的问题处理
- 如何将 joomla 的banner广告替换为Google广告
- PKU 3422 网络流
- Oracle Approvals Management-----Actions
- volatile
- WINDOWS无法引导启动
- ATT和EQ作用
- Win32Forth初学者指导-前言
- 2D游戏编程笔记-1
- Qt应用程序部署—X11平台
- 关于socket