bzoj1877[SDOI2009]晨跑
传送门
最小费用最大流,被我秒了的水题,由于每个点也只能经过一次,所以拆点处理就好了,zkw费用流又被EK踩了。。。
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
inline void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define min(a,b) (a<b?a:b)
#define rg register
int n,m,s,t,sla[100001],pre[100001],dis[100001],nxt[100001],h[50001],v[100001],w[100001],cnt=1,inf=1e9,ans,sum;bool vis[100001];
void add(int x,int y,int z,int u)
{
pre[++cnt]=y,nxt[cnt]=h[x],h[x]=cnt,v[cnt]=z,w[cnt]=u,
pre[++cnt]=x,nxt[cnt]=h[y],h[y]=cnt,v[cnt]=0,w[cnt]=-u;
}
int dfs(int x,int flow)
{
if(x==t){ans+=dis[t]*flow,sum+=flow;return flow;}
vis[x]=1;int f=flow;
for(rg int i=h[x];i;i=nxt[i])
if(!vis[pre[i]]&&v[i])
{
if(!(dis[x]+w[i]-dis[pre[i]]))
{
int y=dfs(pre[i],min(v[i],f));
f-=y,v[i]-=y,v[i^1]+=y;
if(!f)return flow;
}
else sla[pre[i]]=min(sla[pre[i]],dis[x]+w[i]-dis[pre[i]]);
}
return flow-f;
}
int aug()
{
int mn=inf;
for(rg int i=1;i<=2*n;i++)if(!vis[i])mn=min(mn,sla[i]),sla[i]=inf;
if(mn==inf)return 1;
for(rg int i=1;i<=2*n;i++)if(!vis[i])dis[i]+=mn;
return 0;
}
int main()
{
read(n),read(m);s=n+1,t=n;
for(rg int i=1,x,y,z;i<=m;i++)read(x),read(y),read(z),add(x+n,y,1,z);
for(rg int i=1;i<=n;i++)add(i,i+n,1,0);
memset(sla,63,sizeof sla);
while(1)
{
while(1){memset(vis,0,sizeof vis);if(!dfs(s,inf))break;}
if(aug())break;
}
printf("%d %d\n",sum,ans);
}