CodeForces 362E Petya and Pipes
2014-10-21 23:15
197 查看
Petya and Pipes
Time Limit: 1000msMemory 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
Input5 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
Codeforces Round #212 (Div. 2)解题:拆边费用流。把原来的边拆成两条,一条流量为原来的,费用为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 = 110; struct arc { int to,flow,cost,next; arc(int x = 0,int y = 0,int z = 0,int nxt = -1) { to = x; flow = y; cost = z; next = nxt; } }; arc e[10000]; 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,0,-cost,head[v]); head[v] = tot++; } bool spfa() { queue<int>q; for(int i = 0; i <= n; ++i) { d[i] = INF; p[i] = -1; in[i] = false; } d[S] = 0; 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] > -1; } int solve() { int cost = 0,flow = 0; while(spfa()) { int theMin = INF; for(int i = p[T]; ~i; i = p[e[i^1].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^1].to]) { e[i].flow -= theMin; e[i^1].flow += theMin; } } return flow; } int main() { while(~scanf("%d %d",&n,&k)) { memset(head,-1,sizeof(head)); S = 0; T = n-1; int tmp; for(int i = tot = 0; i < n; ++i) for(int j = 0; j < n; ++j) { scanf("%d",&tmp); if(tmp) { add(i,j,tmp,0); add(i,j,k,1); } } printf("%d\n",solve()); } return 0; }
View Code
相关文章推荐
- codeforces 294B Shaass and Bookshelf
- codeforces 543A Writing Code(dp降维优化,完全背包思想)
- CodeForces-626A-Robot Sequence
- Codeforces 600 E. Lomsat gelral (dfs启发式合并map)
- CodeForces 791C Bear and Different Names
- CodeForces 852 H. Bob and stages
- codeforces 2A Winner
- codeforces 235 C Cyclical Quest (后缀自动机 SAM)
- Codeforces 276D. Little Girl and Maximum XOR
- codeforces 351A A. Jeff and Rounding(★)
- Codeforces 678E Another Sith Tournament(状压dp)
- CodeForces - 711D Directed Roads(dfs判环)
- Codeforces 850 A. Five Dimensional Points(暴力/抽屉定理)
- CodeForces 899C Dividing the numbers
- codeforces 181.div2 300B - Coach 并查集
- CodeForces 534D Handshakes 【STL】+【贪心】
- CodeForces 608B Hamming Distance Sum
- codeforces 189D AlgoRace
- Problem-263A-Codeforces- Beautiful Matrix
- Codeforces186C