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