poj 2987 最大权闭合图

Language:
Default
Firing
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 8744   Accepted: 2631

Description

You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do some firings. You’re now simply too mad to give response to questions like “Don’t you think it is an even more stupid decision to have signed them?”, yet calm enough to consider the potential profit and loss from firing a good portion of them. While getting rid of an employee will save your wage and bonus expenditure on him, termination of a contract before expiration costs you funds for compensation. If you fire an employee, you also fire all his underlings and the underlings of his underlings and those underlings’ underlings’ underlings… An employee may serve in several departments and his (direct or indirect) underlings in one department may be his boss in another department. Is your firing plan ready now?

Input

The input starts with two integers n (0 < n ≤ 5000) and m (0 ≤ m ≤ 60000) on the same line. Next follows n + m lines. The first n lines of these give the net profit/loss from firing the i-th employee individuallybi (|bi| ≤ 107, 1 ≤ i ≤ n). The remaining m lines each contain two integers i and j (1 ≤ ij ≤ n) meaning the i-th employee has the j-th employee as his direct underling.

Output

Output two integers separated by a single space: the minimum number of employees to fire to achieve the maximum profit, and the maximum profit.

Sample Input

5 5
8
-9
-20
12
-10
1 2
2 5
1 4
3 4
4 5

Sample Output

2 2

Hint

As of the situation described by the sample input, firing employees 4 and 5 will produce a net profit of 2, which is maximum.
 
很基础的最大权闭合图
 
 #include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<climits>
#define MAXE 65100*2
#define MAXP 5100
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
using namespace std;
struct Edge
{
long long int s,t,next;
long long f;
} edge[MAXE];
long long int head[MAXP];
long long int cur[MAXP];
long long int pre[MAXP];
long long int stack[MAXE];
long long int dep[MAXP];
long long int ent;
long long int n,m,s,t,cot;
void add(long long int start,long long int last,long long int f)
{
edge[ent].s=start;
edge[ent].t=last;
edge[ent].f=f;
edge[ent].next=head[start];
head[start]=ent++;
edge[ent].s=last;
edge[ent].t=start;
edge[ent].f=;
edge[ent].next=head[last];
head[last]=ent++;
}
bool bfs(long long int S,long long int T)
{
memset(pre,-,sizeof(pre));
pre[S]=;
queue<long long int>q;
q.push(S);
while(!q.empty())
{
long long int temp=q.front();
q.pop();
for(long long int i=head[temp]; i!=-; i=edge[i].next)
{
long long int temp2=edge[i].t;
if(pre[temp2]==-&&edge[i].f)
{
pre[temp2]=pre[temp]+;
q.push(temp2);
}
}
}
return pre[T]!=-;
}
long long int dinic(long long int start,long long int last)
{
long long int flow=,now;
while(bfs(start,last))
{
long long int top=;
memcpy(cur,head,sizeof(head));
long long int u=start;
while()
{
if(u==last)//如果找到终点结束对中间路径进行处理并计算出该流
{
long long int minn=INT_MAX;
for(long long int i=; i<top; i++)
{
if(minn>edge[stack[i]].f)
{
minn=edge[stack[i]].f;
now=i;
}
}
flow+=minn;
for(long long int i=; i<top; i++)
{
edge[stack[i]].f-=minn;
edge[stack[i]^].f+=minn;
}
top=now;
u=edge[stack[top]].s;
}
for(long long int i=cur[u]; i!=-; cur[u]=i=edge[i].next) //找出从u点出发能到的边
if(edge[i].f&&pre[edge[i].t]==pre[u]+)
break;
if(cur[u]==-)//如果从该点未找到可行边,将该点标记并回溯
{
if(top==)break;
pre[u]=-;
u=edge[stack[--top]].s;
}
else//如果找到了继续运行
{
stack[top++]=cur[u];
u=edge[cur[u]].t;
}
}
}
return flow;
}
void dfs(long long int u)
{
cot++;
dep[u]=;
for(long long int i=head[u];i!=-;i=edge[i].next)
{
long long int v=edge[i].t;
if(!dep[v]&&edge[i].f>)
{
dfs(v);
}
}
}
int main()
{
while(~scanf("%lld%lld",&n,&m))
{
s=;
t=n+;
ent=;
long long int cost,u,v;
long long int ans=;
memset(head,-,sizeof(head));
for(int i=; i<=n; i++)
{
scanf("%lld",&cost);
if(cost>)
{
add(s,i,cost);
ans+=cost;
}
else add(i,t,-cost);
}
for(int i=; i<=m; i++)
{
scanf("%lld%lld",&u,&v);
add(u,v,INT_MAX);
}
memset(dep,,sizeof(dep));
long long int flow=dinic(s,t);
cot=;
dfs(s);
printf("%lld ",cot-);
printf("%lld\n",ans-flow);
}
return ;
}
上一篇:计算eks node 中pod数量


下一篇:5T 的盘分区 parted