思路:
这题考的是最大闭权图。只要知道怎么求最大闭权图就知道怎么做。但好像有点卡模版,要高效的模版才行。
#include <iostream>
#include <stdio.h>
#include <string.h>
#define Maxn 6010
#define Maxm 200000
#define LL __int64
#define Abs(a) (a)>0?(a):(-a)
using namespace std;
struct Edge{
int from,to,next;
LL val;
}edge[Maxm];
LL value[Maxn],inf=;
int index[Maxn],work[Maxn],dis[Maxn],q[Maxn],e,vi[Maxn];
inline void addedge(int from,int to,LL val)//有向边
{
edge[e].from=from;
edge[e].to=to;
edge[e].val=val;
edge[e].next=index[from];
index[from]=e++;
edge[e].from=to;
edge[e].to=from;
edge[e].val=;
edge[e].next=index[to];
index[to]=e++;
}
void init()
{
e=;
memset(index,-,sizeof(index));
memset(vi,,sizeof(vi));
inf=;
}
void add(int u,int v,LL c)
{
edge[e].to=v;edge[e].val=c;edge[e].next=index[u];index[u]=e++;
edge[e].to=u;edge[e].val=;edge[e].next=index[v];index[v]=e++;
}
void DFS(int u)
{
vi[u]=;
int i,v;
for(i=index[u];i!=-;i=edge[i].next)
if(edge[i].val&&!vi[edge[i].to])
DFS(edge[i].to);
}
int bfs(int S,int T)
{
int rear=;
memset(dis,-,sizeof(dis));
dis[S]=;q[rear++]=S;
for(int i=;i<rear;i++)
{
for(int j=index[q[i]];j!=-;j=edge[j].next)
{
if(edge[j].val&&dis[edge[j].to]==-)
{
dis[edge[j].to]=dis[q[i]]+;
q[rear++]=edge[j].to;
if(edge[j].to==T) return ;
}
}
}
return ;
}
LL dfs(int cur,LL a,int T)
{
if(cur==T) return a;
for(int &i=work[cur];i!=-;i=edge[i].next)
{
if(edge[i].val&&dis[edge[i].to]==dis[cur]+)
{
int t=dfs(edge[i].to,min(a,edge[i].val),T);
if(t)
{
edge[i].val-=t;
edge[i^].val+=t;
return t;
}
}
}
return ;
}
LL Dinic(int S,int T)
{
LL ans=;
while(bfs(S,T))
{
memcpy(work,index,sizeof(index));
while(int t=dfs(S,inf,T)) ans+=t;
}
return ans;
} int main()
{
int n,m,i,a,b;
LL sum=;
scanf("%d%d",&n,&m);
init();
for(i=;i<=n;i++)
{
scanf("%I64d",value+i);
if(value[i]>)
{
sum+=value[i];
addedge(,i,value[i]);
}
else
addedge(i,n+,-value[i]);
inf+=Abs(value[i]);
}
inf++;
for(i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
addedge(a,b,inf);
}
LL ans=Dinic(,n+);//起始点和结束点
//DFS(0);//寻找S集合
int num=;
for(i=;i<=n;i++)
if(dis[i]>=)
num++;
printf("%d %I64d\n",num,sum-ans);
return ;
}