解题报告 之 HOJ2816 Power Line
来源:互联网 发布:mysql 主备热切换 编辑:程序博客网 时间:2024/04/30 02:01
解题报告 之 HOJ2816 Power Line
Problem Description
While DUT is hosting this NECPC Contest, in order to control the expenditure, careful considerations should be given in many respects, such as the layout of the contest venue. When the contest venue is arranged, each computer requires power input. While the layout of computers and power-points are already fixed, some lines have to be purchased to link the computers with power-points. And, Raven finds out that it is cheaper to buy power lines of the same length than to buy lines of different lengths, so he would buy all the lines with the same length. If Raven wants to save money, would you help him?
Input Details
The first line of the input contains an integer T, which indicates the number of test cases.
For each test case, there are three positive integers in the first row, n, m, p (0 <= n, m <= 200,0 < p < 100). It means that there are n computers and m power-points. The power line costs ¥p per unit.
In the following n rows, i-th line has 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 are m positive integers, ci (0 < ci < 5,0 <= i < m), showing No.i power-point can provide power to ci computers in total.
Output Details
For each case, if the layout of computers and power lines are not reasonable, which means you cannot provide power to every 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 will cost 4(2*2).
#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;struct 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 from, int to, int cap ){edge[cnt].from = from;edge[cnt].to = to;edge[cnt].cap = cap;edge[cnt].oricap = cap;edge[cnt].next = head[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 );while (!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 (edge[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, INF )) > 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 + 200, 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;}
- 解题报告 之 HOJ2816 Power Line
- Power Network之网络流解题报告
- 解题报告 之 POJ1459 Power Network
- Fast Power 解题报告
- POJ 4045 Power Station 解题报告
- LeetCode-Power of Two-解题报告
- [leetcode] 231. Power of Two 解题报告
- [leetcode] 326. Power of Three 解题报告
- 326. Power of Three LeetCode解题报告
- KMP POJ 2406 Power Strings解题报告
- [leetcode] 342. Power of Four 解题报告
- 【LeetCode】Power of Three 解题报告
- 【LeetCode】Power of Two 解题报告
- 【LeetCode】Power of Four 解题报告
- 关路灯——power 解题报告
- leetcode 342. Power of Four 解题报告
- LeetCode 326. Power of Three 解题报告
- LeetCode 231. Power of Two 解题报告
- Android 解决魅族手机豌豆荚能连上,Eclipse不能识别的问题
- Java JDBC
- iOS控件无法显示的缘由总结
- 利用vncserver图形化界面访问局域网主机
- Android 如何在Eclipse中查看Android API源码 及 support包源码
- 解题报告 之 HOJ2816 Power Line
- javascript数组、对象操作总结
- Hadoop 2.7.0 新版本发布
- 我的公开性日记
- PHP_Note.1
- 黑马程序员-Java数组学习笔记
- 快速幂取模
- Android4.4编译环境的搭建和编译
- UITableViewCell的动态计算