1497: [NOI2006]最大获利
新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战。THU集团旗下的CS&T通讯公司在新一代通讯技术血战的前夜,需要做太多的准备工作,仅就站址选择一项,就需要完成前期市场研究、站址勘测、最优化等项目。在前期市场调查和站址勘测之后,公司得到了一共N个可以作为通讯信号中转站的地址,而由于这些地址的地理位置差异,在不同的地方建造通讯中转站需要投入的成本也是不一样的,所幸在前期调查之后这些都是已知数据:建立第i个通讯中转站需要的成本为Pi(1≤i≤N)。另外公司调查得出了所有期望中的用户群,一共M个。关于第i个用户群的信息概括为Ai, Bi和Ci:这些用户会使用中转站Ai和中转站Bi进行通讯,公司可以获益Ci。(1≤i≤M, 1≤Ai, Bi≤N) THU集团的CS&T公司可以有选择的建立一些中转站(投入成本),为一些用户提供服务并获得收益(获益之和)。那么如何选择最终建立的中转站才能让公司的净获利最大呢?(净获利 = 获益之和 - 投入成本之和)
Input
输入文件中第一行有两个正整数N和M 。第二行中有N个整数描述每一个通讯中转站的建立成本,依次为P1, P2, …, PN 。以下M行,第(i + 2)行的三个数Ai, Bi和Ci描述第i个用户群的信息。所有变量的含义可以参见题目描述。
Output
你的程序只要向输出文件输出一个整数,表示公司可以得到的最大净获利。
Sample Input
5 5
1 2 3 4 5
1 2 3
2 3 4
1 3 3
1 4 2
4 5 3
Sample Output
4
HINT
【样例说明】选择建立1、2、3号中转站,则需要投入成本6,获利为10,因此得到最大收益4。【评分方法】本题没有部分分,你的程序的输出只有和我们的答案完全一致才能获得满分,否则不得分。【数据规模和约定】 80%的数据中:N≤200,M≤1 000。 100%的数据中:N≤5 000,M≤50 000,0≤Ci≤100,0≤Pi≤100。
自己解的方程好开森
a = Pi
b = Pj
c = sigma(c[i, j])
d = sigma(c[i, j])
e = c[i, j] / 2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 1000010
using namespace std;
int n, m;
const int inf = 0x7fffffff;
struct Edge{
int to, next, w;
}edge[maxn];
int h[5010], cnt = 1;
void add(int u, int v, int w){
cnt ++;
edge[cnt].to = v;
edge[cnt].next = h[u];
edge[cnt].w = w;
h[u] = cnt;
swap(u, v), w = 0;
cnt ++;
edge[cnt].to = v;
edge[cnt].next = h[u];
edge[cnt].w = w;
h[u] = cnt;
}
queue<int>Q;
int d[5010], S, T;
bool BFS(){
memset(d, -1, sizeof d);
Q.push(S);d[S] = 0;
while(!Q.empty()){
int u = Q.front();Q.pop();
for(int i = h[u]; i; i = edge[i].next){
if(!edge[i].w)continue;
int v = edge[i].to;
if(d[v] == -1){
d[v] = d[u] + 1;
Q.push(v);
}
}
}return d[T] != -1;
}
int DFS(int x, int a){
if(x == T || a == 0)return a;
int used = 0, f;
for(int i = h[x]; i; i = edge[i].next){
int v = edge[i].to;
if(d[v] == d[x] + 1){
f = DFS(v, min(a - used, edge[i].w));
edge[i].w -= f;
edge[i ^ 1].w += f;
used += f;
if(used == a)return used;
}
}
if(used == 0)d[x] = -1;
return used;
}
int Dinic(){
int ans = 0;
while(BFS())
ans += DFS(S, inf);
return ans;
}
int sum[5010];
int main(){
scanf("%d%d", &n, &m);
S = 0, T = n + 1;
int P, total = 0;
for(int i = 1; i <= n; i ++){
scanf("%d", &P);
add(S, i, P * 2);
}
int u, v, d;
for(int i = 1; i <= m; i ++){
scanf("%d%d%d", &u, &v, &d);
add(u, v, d);
add(v, u, d);
total += d;
sum[u] += d;
sum[v] += d;
}
for(int i = 1; i <= n; i ++)
add(i, T, sum[i]);
printf("%d\n", total - Dinic() / 2);
return 0;
}