Petya and Pipes

Time Limit: 1000ms
Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 362E
64-bit integer IO format: %I64d      Java class name: (Any)

 
A little boy Petya dreams of growing up and becoming the Head Berland Plumber. He is thinking of the problems he will have to solve in the future. Unfortunately, Petya is too inexperienced, so you are about to solve one of such problems for Petya, the one he's the most interested in.

The Berland capital has n water tanks numbered from 1 to n. These tanks are connected by unidirectional pipes in some manner. Any pair of water tanks is connected by at most one pipe in each direction. Each pipe has a strictly positive integer width. Width determines the number of liters of water per a unit of time this pipe can transport. The water goes to the city from the main water tank (its number is 1). The water must go through some pipe path and get to the sewer tank with cleaning system (its number is n).

Petya wants to increase the width of some subset of pipes by at most k units in total so that the width of each pipe remains integer. Help him determine the maximum amount of water that can be transmitted per a unit of time from the main tank to the sewer tank after such operation is completed.

 

Input

The first line contains two space-separated integers n and k (2 ≤ n ≤ 50, 0 ≤ k ≤ 1000). Then follow n lines, each line contains n integers separated by single spaces. The i + 1-th row and j-th column contain number cij — the width of the pipe that goes from tank i to tank j (0 ≤ cij ≤ 106, cii = 0). If cij = 0, then there is no pipe from tank i to tank j.

 

Output

Print a single integer — the maximum amount of water that can be transmitted from the main tank to the sewer tank per a unit of time.

 

Sample Input

Input
5 7
0 1 0 2 0
0 0 4 10 0
0 0 0 0 5
0 0 0 0 10
0 0 0 0 0
Output
10
Input
5 10
0 1 0 0 0
0 0 2 0 0
0 0 0 3 0
0 0 0 0 4
100 0 0 0 0
Output
5

Hint

In the first test Petya can increase width of the pipe that goes from the 1st to the 2nd water tank by 7 units.

In the second test Petya can increase width of the pipe that goes from the 1st to the 2nd water tank by 4 units, from the 2nd to the 3rd water tank by 3 units, from the 3rd to the 4th water tank by 2 units and from the 4th to 5th water tank by 1 unit.

 

Source

 
解题:拆边费用流。把原来的边拆成两条,一条流量为原来的,费用为0,另一条费用为1流量为k.
 
然后进行最小费用路径增广,我们每次增广,d[T】存的是从源到汇,单位流量最便宜的一条路径。。。。一旦费用超过k即停止算法。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc {
int to,flow,cost,next;
arc(int x = ,int y = ,int z = ,int nxt = -) {
to = x;
flow = y;
cost = z;
next = nxt;
}
};
arc e[];
int head[maxn],d[maxn],p[maxn],S,T,n,k,tot;
bool in[maxn];
void add(int u,int v,int flow,int cost) {
e[tot] = arc(v,flow,cost,head[u]);
head[u] = tot++;
e[tot] = arc(u,,-cost,head[v]);
head[v] = tot++;
}
bool spfa() {
queue<int>q;
for(int i = ; i <= n; ++i) {
d[i] = INF;
p[i] = -;
in[i] = false;
}
d[S] = ;
q.push(S);
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].flow && d[e[i].to] > d[u] + e[i].cost) {
d[e[i].to] = d[u] + e[i].cost;
p[e[i].to] = i;
q.push(e[i].to);
}
}
}
return p[T] > -;
}
int solve() {
int cost = ,flow = ;
while(spfa()) {
int theMin = INF;
for(int i = p[T]; ~i; i = p[e[i^].to])
theMin = min(theMin,e[i].flow);
if(cost + d[T]*theMin > k) return flow + (k - cost)/d[T];
flow += theMin;
cost += d[T]*theMin;
for(int i = p[T]; ~i; i = p[e[i^].to]) {
e[i].flow -= theMin;
e[i^].flow += theMin;
}
}
return flow;
}
int main() {
while(~scanf("%d %d",&n,&k)) {
memset(head,-,sizeof(head));
S = ;
T = n-;
int tmp;
for(int i = tot = ; i < n; ++i)
for(int j = ; j < n; ++j) {
scanf("%d",&tmp);
if(tmp) {
add(i,j,tmp,);
add(i,j,k,);
}
}
printf("%d\n",solve());
}
return ;
}

上面代码可以AC,但是忘记标记入队点了。。。。。。^_^..

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc {
int to,flow,cost,next;
arc(int x = ,int y = ,int z = ,int nxt = -) {
to = x;
flow = y;
cost = z;
next = nxt;
}
};
arc e[];
int head[maxn],d[maxn],p[maxn],S,T,n,k,tot;
bool in[maxn];
void add(int u,int v,int flow,int cost) {
e[tot] = arc(v,flow,cost,head[u]);
head[u] = tot++;
e[tot] = arc(u,,-cost,head[v]);
head[v] = tot++;
}
bool spfa() {
queue<int>q;
for(int i = ; i <= n; ++i) {
d[i] = INF;
p[i] = -;
in[i] = false;
}
d[S] = ;
q.push(S);
while(!q.empty()) {
int u = q.front();
q.pop();
in[u] = false;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].flow && d[e[i].to] > d[u] + e[i].cost) {
d[e[i].to] = d[u] + e[i].cost;
p[e[i].to] = i;
if(!in[e[i].to]){
in[e[i].to] = true;
q.push(e[i].to);
}
}
}
}
return p[T] > -;
}
int solve() {
int cost = ,flow = ;
while(spfa()) {
int theMin = INF;
for(int i = p[T]; ~i; i = p[e[i^].to])
theMin = min(theMin,e[i].flow);
if(cost + d[T]*theMin > k) return flow + (k - cost)/d[T];
flow += theMin;
cost += d[T]*theMin;
for(int i = p[T]; ~i; i = p[e[i^].to]) {
e[i].flow -= theMin;
e[i^].flow += theMin;
}
}
return flow;
}
int main() {
while(~scanf("%d %d",&n,&k)) {
memset(head,-,sizeof(head));
S = ;
T = n-;
int tmp;
for(int i = tot = ; i < n; ++i)
for(int j = ; j < n; ++j) {
scanf("%d",&tmp);
if(tmp) {
add(i,j,tmp,);
add(i,j,k,);
}
}
printf("%d\n",solve());
}
return ;
}

最新文章

  1. java操作Redis
  2. .net IO异步和Producer/Consumer队列实现一分钟n次http请求
  3. [设计模式] javascript 之 桥接模式
  4. Jmeter—1 安装
  5. MySQL 数据库设计 笔记与总结(1)需求分析
  6. 调用css时,用link 和 @import url 有什么区别
  7. 环信_EaseUI 使用指南
  8. Ubuntu修改语言环境为英文
  9. 使用Unity在MVC上实现动态注入
  10. PHP5中PDO的入门教程
  11. java内存模型1
  12. 进程与进程描写叙述符(task_struct)
  13. 【Luogu1373】小a和uim之大逃离(动态规划)
  14. iOS开发 runtime实现原理以及实际开发中的应用
  15. FreeMaker入门介绍
  16. mysql外键使用
  17. Codeforces 609F Frogs and mosquitoes 线段树
  18. 手工搭建web项目
  19. pycharm pro版本激活
  20. 可用于nodejs的SuperAgent(ajax API)

热门文章

  1. Android自己定义RatingBar
  2. HDFS 文件格式——SequenceFile RCFile
  3. WebRTC开源项目一览之二
  4. 33.Qt模型与视图
  5. 下压栈(LIFO)详解
  6. 比较两个时间的大小 举例:CompareDate(&quot;12:00&quot;,&quot;11:15&quot;)
  7. Route学习笔记
  8. 使用Latex写book类型文本的体会
  9. javascript中缓存
  10. 彻底解决降级安装失败无法彻底卸载应用bug