CodeForces 362E Petya and Pipes
Petya and Pipes
This problem will be judged on CodeForces. Original ID: 362E
64-bit integer IO format: %I64d Java class name: (Any)
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
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
10
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
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
#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 ;
}
CodeForces 362E Petya and Pipes的更多相关文章
- Codeforces 362E Petya and Pipes 费用流建图
题意: 给一个网络中某些边增加容量,增加的总和最大为K,使得最大流最大. 费用流:在某条边增加单位流量的费用. 那么就可以2个点之间建2条边,第一条给定边(u,v,x,0)这条边费用为0 同时另一条边 ...
- codeforces B. Petya and Staircases 解题报告
题目链接:http://codeforces.com/problemset/problem/362/B 题目意思:给出整数n和m,表示有n级楼梯和m级dirty的楼梯,接下来m个数表示对应是哪一个数字 ...
- Codeforces 890C - Petya and Catacombs 模拟
C. Petya and Catacombstime limit per test1 secondmemory limit per test256 megabytesinputstandard inp ...
- CodeForces 832B Petya and Exam
B. Petya and Exam time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforces 112B Petya and Square
B. Petya and Square time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 1282C. Petya and Exam (贪心)
链接:https://codeforces.com/contest/1282/problem/C 题意: 有一个人参加考试,考试只有两种题,一种是简单题,每道题耗时固定为a:另一种是困难题,每道题耗 ...
- CodeForces 362B Petya and Staircases
题意:一个小男孩要上楼梯,他一次可以走1个台阶或2个台阶或3个台阶,但是有一些台阶是脏的,他不想走在脏台阶上.一共有n个台阶和m个脏台阶,他最开始在第1个台阶上,要走到第n个台阶.问小男孩能不能不踩到 ...
- CodeForces 111B - Petya and Divisors 统计..想法题
找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...
- codeforces 1082G - Petya and Graph 最大权闭合子图 网络流
题意: 让你选一些边,选边的前提是端点都被选了,求所有的边集中,边权和-点权和最大的一个. 题解: 对于每个边建一个点,然后就是裸的最大权闭合子图, 结果比赛的时候我的板子太丑,一直T,(不会当前弧优 ...
随机推荐
- robot Framework控制浏览器
向下 向上为负值
- Ajax发送简单请求案例
所谓简单请求,是指不包含任何参数的请求.这种请求通常用于自动刷新的应用,例如证券交易所的实时信息发送.这种请求通常用于公告性质的响应,公告性质的响应无需客户端的任何请求参数,而是由服务器根据业务数据自 ...
- HDU 1575 EASY
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- http格式(graph)
http请求格式 http请求头 字段 http响应 http响应头字段
- oracle实现查询每个部门的员工工资排在前三的员工的基本信息具体举例
--先删除原先存在的表: drop table emp; --创建表emp create table emp ( deptno number, ename varchar2(20), sal numb ...
- How to improve Java's I/O performance( 提升 java i/o 性能)
原文:http://www.javaworld.com/article/2077523/build-ci-sdlc/java-tip-26--how-to-improve-java-s-i-o-per ...
- 并行编程(1) - sum.msic.Unsafe 一
相信看过java源代码的同学.对 sum.msic.Unsafe 这个类并不陌生,特别是在java.util.concurrent包有非常多的使用. sum.msic.Unsafe源代码: ...
- NSKeyedArchiver
如果对象是NSString.NSDictionary.NSArray.NSData.NSNumber等类型,可以直接用NSKeyedArchiver进行归档和恢复 不是所有的对象都可以直接用这种方法进 ...
- myeclipse打开jsp页面慢或者卡死
不知道你们有没有这种经历,反正无论是公司电脑还是自己电脑,myeclipse打开jsp页面卡的不行不行的,又是甚至会出现卡死的现象,几经周折,找到了解决办法,亲测有效 打开window-prefere ...
- Laravel-事件简单使用
Laravel-事件简单使用 标签(空格分隔): php, laravel 注册事件和监听器 生成事件和监听器:php artisan event:generate key => 事件 valu ...