可以看出来是有上下界的费用流,我们对于一条边权为z的边u,v,从s向u连一条容量为1的边,从u到v连上容量为
INF的边,让此边可以重复走。对于每个点p,从p到T连上一条流量为p,费用无的边,再从x到1连上一条费用无,
流量无限的边,直接跑网络流就行了。
跑的好慢啊。。。
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int N=305;
const int M=500005,Inf=1<<29;
int x,y;
int k;
int n;
struct edge{
int to,nxt,ro,c,flow;
}e[M];
int head[N],tot;
inline void add(int u,int v,int ro,int c)
{
e[tot].to=v,e[tot].ro=ro,e[tot].c=c,e[tot].nxt=head[u],head[u]=tot,tot++;
e[tot].to=u,e[tot].ro=0,e[tot].c=-c,e[tot].nxt=head[v],head[v]=tot,tot++;
}
int sk[N];
bool vis[N];
int dis[N],prp[N],prd[N],qaq[N];
inline bool spfa(int s,int t)
{
register int i;
for (i=0;i<=n+1;i++) dis[i]=Inf;
vis[s]=1;qaq[s]=Inf;dis[s]=0;
queue<int> q;
q.push(s);
while (!q.empty())
{
int x=q.front();
q.pop(),vis[x]=false;
for (i=head[x];i!=-1;i=e[i].nxt)
{
if (e[i].ro>e[i].flow&&dis[x]+e[i].c<dis[e[i].to])
{
prp[e[i].to]=i;
prd[e[i].to]=x;
dis[e[i].to]=dis[x]+e[i].c;
qaq[e[i].to]=min(qaq[x],e[i].ro-e[i].flow);
if (!vis[e[i].to]) q.push(e[i].to),vis[e[i].to]=1;;
}
}
}
return dis[t]==Inf?0:1;
}
int sum;
inline void MCMF(int s,int t)
{
int flow=0,cost=0;
while (spfa(s,t))
{
for (int i=t;i!=s;i=prd[i])
{
e[prp[i]].flow+=qaq[t];
e[prp[i]^1].flow-=qaq[t];
}
flow+=qaq[t];
cost+=dis[t]*qaq[t];
}
printf("%d",cost);
}
int main()
{
register int i,j;
scanf("%d",&n);
memset(head,-1,sizeof(head));
for (i=1;i<=n;i++)
{
scanf("%d",&k);
if (i!=1) add(i,1,Inf,0);
add(i,n+1,k,0);
for (j=1;j<=k;j++)
{
sum+=y;
scanf("%d %d",&x,&y);
add(i,x,Inf,y);
add(0,x,1,y);
}
}
MCMF(0,n+1);
}