Problem Solving report HOJ2816 Power Line
Problem Description
While DUT was hosting this necpc Contest, in order to control the expenditure, careful considerations should being given in MA NY respects, such as the layout of the contest venue. When the contest venue was arranged, each computer requires power input. While the layout of computers and power-points is already fixed, some lines has to is purchased to link the computers WI Th power-points. And, Raven finds out that it's cheaper to buy power lines of the same length than to buy lines of different lengths, so H E would buy all the lines with the same length. If Raven wants to save money, would your help him?
Input Details
The first line of the input contains an integer T, which indicates the number of the test cases.
For each test case, there is three positive integers in the first row, N, M, p (0 <= N, M <= 200,0 < p < 100) . It means that there is n computers and M power-points. The power line costs¥p per unit.
In the following n rows, i-th line have m positive integers indicating the distances between the i-th computer and all the Power points. The distance is less than 10000.
In the last row, there is m positive integers, ci (0 < ci < 5,0 <= i < m), showing no.i Power-point can provi De power to CI computers in total.
Output Details
For each case, if the layout of computers and power lines is not reasonable, which means your cannot provide power to ever Y computer, just output-1.
Otherwise, output the lowest total price for power lines.
Sample Input
12 2 11 32 21 1
Sample Output
4
Hint:
You many link the first computer with the first power-points, and link the second computer with the second power-points. This would cost 4 (2*2).
The main topic: There are n computers, M power strip. Now you need to configure the power cable, the price is P yuan/meter. In order to save costs, it is now decided to buy the same length of power cord. How much do you need to spend at least, given the distance each day from the computer to each power strip and the amount each power strip can hold?
Analysis: This problem gives the power cord the same length of condition, not the obvious maximum value of the minimum. So the proper use of two points, and then this allocation problem with the maximum flow solution, is a classic collocation. The operation is as follows, the two largest power cord length mid, and then connect the super source point to all computers, the load is 1, each computer with a distance within its mid-power strip connected to the load of 1; Each power strip is connected to a super sink, and the load is the amount it can hold. Then run the maximum flow to see if >=n, to determine the direction of the two points. If the last low=inf-1, then there is no solution, output-1.
on the code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #include <queue>using namespace std;const int maxn = 810;const int MAXM = 90000;const int INF = 0x3f3f3f3f;s Truct Edge{int from, to, cap, Next, oricap;}; Edge edge[maxm];int head[maxn];int cap[maxn];int dist[maxn][maxn];int level[maxn];int src, des, cnt;void addedge (int fro m, int to, int cap) {Edge[cnt].from = From;edge[cnt].to = To;edge[cnt].cap = Cap;edge[cnt].oricap = Cap;edge[cnt].next = h Ead[from];head[from] = Cnt++;swap (from, to); Edge[cnt].from = from;edge[cnt].to = To;edge[cnt].cap = 0;edge[cnt].oricap = 0;edge[cnt].next = Head[from];head[from] = cnt++;} int BFs () {memset (level,-1, sizeof level);queue<int> Q;while (!q.empty ()) Q.pop (); LEVEL[SRC] = 0;q.push (src); w Hile (!q.empty ()) {int u = q.front (); Q.pop (); for (int i = head[u]; I! = 1; i = edge[i].next) {int v = edge[i].to;if (edg E[i].cap > 0 && level[v] = =-1) {Level[v] = Level[u] + 1;Q.push (v);}} return Level[des]! =-1;} int dfs (int u, int f) {if (U = = des) return f;int tem;for (int i = head[u]; I! =-1; i = edge[i].next) {int v = edge[i].to; if (Edge[i].cap > 0 && level[v] = = Level[u] + 1) {tem = DFS (V, min (f, edge[i].cap)), if (Tem > 0) {edge[i]. Cap-= tem;edge[i ^ 1].cap + = Tem;return tem;}} Level[u] = -1;return 0;} int dinic () {int ans = 0, Tem;while (BFS ()) {while (tem = DFS (src)) > 0) {ans + = tem;}} return ans;} int main () {int kase;cin >> kase;while (kase--) {int n, m, p;cin >> n >> m >> p;src = 0; des = 805; for (int i = 1, i <= N; i++) {for (int j = 1; j <= M; j + +) {cin >> dist[i][j];}} for (int i = 1; I <= m; i++) cin >> Cap[i];int low = 0, high = inf-1;int ans = -1;while (Low <= high) {int mid = (high + low)/2;memset (head,-1, sizeof head), CNT = 0;for (int i = 1; I <= n; i++) {Addedge (src, I, 1);} for (int i = 1, i <= N; i++) {for (int j = 1; j <= M; j + +) {if (Dist[i][j] <= mid) Addedge (I, J + 200, 1);}} for (int i = 1; I <= m; i++) {Addedge (i + A, DES, cap[i]);} if (Dinic () < n) Low = mid + 1;else{ans = Mid;high = Mid-1;}} if (low >= INF-1) cout << 1 << endl;elsecout << ans*p*n << Endl;} return 0;}
swollen do swollen do my project How to end the problem Ah!!!
Problem Solving report HOJ2816 Power Line