您的位置:首页 > 大数据 > 人工智能

HDU 3917 Road constructions 2011 Multi-University Training Contest 8 - Host by HUST 最大权闭包

2011-08-06 16:49 549 查看
/*
最大权闭包的题目,关键在怎么建图用最大流来求
每个工程队的税收为正权,连源点
每个工程队的施工总和为负权 C[i],连汇点,值为-C[i];
有联系的工程队之间连有向边,边权为inf
答案为所以税收总和-最大流
*/
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <string>

using namespace std;
const int maxn=6000;
const int inf=2000000000;

//最大流模版
//************************************************************************
struct edge
{
int v, next;
int val;
} net[ 50010 ];

int level[maxn], Qu[maxn], out[maxn],next[maxn];
class Dinic {
public:
int end;
Dinic() {
end = 0;
memset( next, -1, sizeof(next) );
}
inline void insert( int x, int y, int c) {
net[end].v = y, net[end].val = c,
net[end].next = next[x],
next[x] = end ++;
net[end].v = x, net[end].val = 0,
net[end].next = next[y],
next[y] = end ++;
}
bool BFS( int S, int E ) {
memset( level, -1, sizeof(level) );
int low = 0, high = 1;
Qu[0] = S, level[S] = 0;
for( ; low < high; ) {
int x = Qu[low];
for( int i = next[x]; i != -1; i = net[i].next ) {
if( net[i].val == 0 ) continue;
int y = net[i].v;
if( level[y] == -1 ) {
level[y] = level[x] + 1;
Qu[ high ++] = y;
}
}
low ++;
}
return level[E] != -1;
}

int MaxFlow( int S, int E ){
int maxflow = 0;
for( ; BFS(S, E) ; ) {
memcpy( out, next, sizeof(out) );
int now = -1;
for( ;; ) {
if( now < 0 ) {
int cur = out[S];
for(; cur != -1 ; cur = net[cur].next )
if( net[cur].val && out[net[cur].v] != -1 && level[net[cur].v] == 1 )
break;
if( cur >= 0 ) Qu[ ++now ] = cur, out[S] = net[cur].next;
else break;
}
int u = net[ Qu[now] ].v;
if( u == E ) {
int flow = inf;
int index = -1;
for( int i = 0; i <= now; i ++ ) {
if( flow > net[ Qu[i] ].val )
flow = net[ Qu[i] ].val, index = i;
}
maxflow += flow;
for( int i = 0; i <= now; i ++ )
net[Qu[i]].val -= flow, net[Qu[i]^1].val += flow;
for( int i = 0; i <= now; i ++ ) {
if( net[ Qu[i] ].val == 0 ) {
now = index - 1;
break;
}
}
}
else{
int cur = out[u];
for(; cur != -1; cur = net[cur].next )
if (net[cur].val && out[net[cur].v] != -1 && level[u] + 1 == level[net[cur].v])
break;
if( cur != -1 )
Qu[++ now] = cur, out[u] = net[cur].next;
else out[u] = -1, now --;
}
}
}
return maxflow;
}
};
//************************************************************************

int n,m,tmp,q;
int C[5001];
struct E
{
int a,b,c;
}ee[3003];
int main()
{
while(scanf("%d%d",&n,&m),n+m)
{
Dinic my;
int sum=0,sum1=0;
for(int i=1;i<=m;i++)
{
scanf("%d",&tmp);
my.insert(0,i,tmp);//0为汇点
sum1+=tmp;
C[i]=0;
}
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
scanf("%d%d%d%d",&ee[i].a,&ee[i].b,&ee[i].c,&tmp);
C[ee[i].c]+=tmp;
}
for(int i=1;i<=q;i++)
{
for(int j=1;j<=q;j++)if(i!=j)
{
if(ee[i].c!=ee[j].c&&ee[i].b==ee[j].a)
my.insert(ee[i].c,ee[j].c,inf);
}
}
for(int i=1;i<=m;i++)
{
my.insert(i,m+1,C[i]);
}
int ans=sum1-my.MaxFlow(0,m+1);
if(ans<0)ans=0;
printf("%d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐