标签:math 思维 its 三元组 ons using line www. pre
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 1e18
inline ll read()
{
ll out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
fh=-1,jp=getchar();
while (jp>='0'&&jp<='9')
out=out*10+jp-'0',jp=getchar();
return out*fh;
}
const int MAXN=1e6+10;
int cnt=-1,head[MAXN],nx[MAXN],to[MAXN];
ll flow[MAXN];
void addedge(int u,int v,ll Flow)
{
++cnt;
to[cnt]=v;
nx[cnt]=head[u];
flow[cnt]=Flow;
head[u]=cnt;
}
void ins(int u,int v,ll Flow)
{
addedge(u,v,Flow);
addedge(v,u,0);
}
int tot=0;
int cur[MAXN],dep[MAXN];
ll maxflow=0;
bool bfs(int S,int T)
{
for(int i=1;i<=tot;++i)
dep[i]=-1;
for(int i=1;i<=tot;++i)
cur[i]=head[i];
dep[S]=0;
queue<int> q;
q.push(S);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=nx[i])
{
int v=to[i];
if(flow[i] && dep[v]==-1)
{
dep[v]=dep[u]+1;
q.push(v);
}
}
}
if(dep[T]==-1)
return false;
return true;
}
ll dfs(int u,int t,ll limit)
{
if(!limit || u==t)
return limit;
ll Flow=0,f;
for(int i=cur[u];i!=-1;i=nx[i])
{
cur[u]=i;
int v=to[i];
if(dep[v]==dep[u]+1 && (f=dfs(v,t,min(limit,flow[i]))))
{
Flow+=f;
limit-=f;
flow[i]-=f;
flow[i^1]+=f;
if(!limit)
break;
}
}
return Flow;
}
void Dinic(int S,int T)
{
while(bfs(S,T))
maxflow+=dfs(S,T,inf);
}
int n,m,k;
int field[MAXN];
int main()
{
freopen("work.in","r",stdin);
freopen("work.out","w",stdout);
memset(head,-1,sizeof head);
int S=++tot;
int T=++tot;
n=read(),m=read(),k=read();
ll ans=0;
for(int i=1;i<=n;++i)
{
field[i]=++tot;
ll a=read();
ins(S,field[i],a);
ans+=a;
}
for(int i=1;i<=n;++i)
{
ll b=read();
ins(field[i],T,b);
ans+=b;
}
while(m--)
{
int i=read(),j=read();
ll w=read();
ins(field[i],field[j],w);
ins(field[j],field[i],w);
}
while(k--)
{
int t=read(),a=read();
ll b=read();
ans+=b;
int np=++tot;
if(a==0)
{
ins(S,np,b);
for(int i=1;i<=t;++i)
{
int x=read();
ins(np,field[x],inf);
}
}
else
{
ins(np,T,b);
for(int i=1;i<=t;++i)
{
int x=read();
ins(field[x],np,inf);
}
}
}
Dinic(S,T);
ans-=maxflow;
cout<<ans<<endl;
return 0;
}
标签:math 思维 its 三元组 ons using line www. pre
原文地址:https://www.cnblogs.com/jklover/p/10659332.html