1497: [NOI2006]最大获利
Time Limit: 5 Sec
Memory Limit: 64 MB
Submit: 4121
Solved: 2010
[ Submit][ Status][ Discuss]
Description
新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战。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。
Source
[ Submit][ Status][ Discuss]
前两天刚学的。。把最大权闭合子图问题转换成最小割模型
每条边都看成一个点,从源点向它连一条容量为获利的边
每个中转站看做一个点,从它向汇点连一条容量为成本的边
对于题目中的每条边,从它向两个端点各连一条容量为INF的边
跑一遍最大流求出最小割
边化作的点,如果源点向它连的边属于最小割集,说明我们为了更大获利舍去它
中转站化作的点,如果它向汇点连的边属于最小割集,说明我们为了赚钱建它
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 5050;
const int INF = ~0U>>1;
struct E{
int to,cap,flow;
E(int _to = 0,int _cap = 0,int _flow = 0) {
to = _to; cap = _cap; flow = _flow;
}
}edgs[maxn*200];
int n,m,S,T,ans,cnt,cur[maxn*12],L[maxn*12];
vector v[maxn*12];
queue q;
void Add(int from,int to,int cap)
{
edgs[cnt] = E(to,cap,0); v[from].push_back(cnt++);
edgs[cnt] = E(from,0,0); v[to].push_back(cnt++);
}
int getint()
{
int ret = 0;
char ch = getchar();
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
bool BFS()
{
for (int i = S; i <= T; i++) L[i] = 0;
L[S] = 1; q.push(S);
while (!q.empty()) {
int k = q.front(); q.pop();
for (int i = 0; i < v[k].size(); i++) {
E e = edgs[v[k][i]];
if (e.cap == e.flow) continue;
if (L[e.to]) continue;
L[e.to] = L[k] + 1;
q.push(e.to);
}
}
return L[T];
}
int Dicnic(int x,int a)
{
if (x == T) return a;
int flow = 0;
for (int &i = cur[x]; i < v[x].size(); i++) {
E &e = edgs[v[x][i]];
if (e.cap == e.flow) continue;
if (L[e.to] != L[x] + 1) continue;
int f = Dicnic(e.to,min(a,e.cap - e.flow));
if (f) {
flow += f;
e.flow += f;
edgs[v[x][i]^1].flow -= f;
a -= f;
if (!a) return flow;
}
}
if (!flow) L[x] = -1;
return flow;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
n = getint(); m = getint();
S = 0; T = n+m+1;
for (int i = 1; i <= n; i++) {
int x; x = getint();
Add(i,T,x);
}
for (int i = 1; i <= m; i++) {
int x,y,z;
x = getint(); y = getint(); z = getint();
Add(n+i,x,INF); Add(n+i,y,INF);
Add(S,n+i,z); ans += z;
}
int Maxflow = 0;
while (BFS()) {
for (int i = S; i <= T; i++) cur[i] = 0;
Maxflow += Dicnic(S,INF);
}
cout << ans - Maxflow;
return 0;
}
v数组开小贡献一发WA...