bzoj3876 支线剧情
其实也是2014江苏的题
上下界的费用流很裸吧
其实也可以利用图的特殊性把他转化成普通的费用流
比较一下时间,普通费用流有时还是很明显的
//普通费用流 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int Maxn=305, Maxm=200005, INF=1e9; int node[Maxm],next[Maxm],flow[Maxm],cost[Maxm],q[Maxm]; int a[Maxn],g[Maxn][Maxn],way[Maxn],path[Maxn],d[Maxn],de[Maxn]; int n,K,i,j,k,tot,ans,minx,x,y,l,r,N,b,t; bool v[Maxn]; void add(int x,int y,int z1,int z2){ node[++tot]=y; next[tot]=a[x]; a[x]=tot; flow[tot]=z1; cost[tot]=z2; node[++tot]=x; next[tot]=a[y]; a[y]=tot; flow[tot]=0; cost[tot]=-z2; } void floyed(){ for (k=1;k<=n;k++) for (i=1;i<=n;i++) if (i!=k && g[i][k]<g[0][0]) for (j=1;j<=n;j++) if (k!=j && i!=j && g[i][j]>g[i][k]+g[k][j]) g[i][j]=g[i][k]+g[k][j]; } bool spfa(){ memset(d,127/2,sizeof(d)); d[1] = 0; v[1] = 1; for (q[l=r=1]=1;l<=r;v[q[l++]]=0) for (i=a[q[l]];i;i=next[i]) if (flow[i]>0 && d[node[i]]>d[q[l]]+cost[i]){ d[node[i]] = d[q[l]]+cost[i]; path[node[i]] = q[l]; way[node[i]] = i; if (v[node[i]]==0) v[ q[++r]=node[i] ]=1; } if (d[N]>=d[0]) return 0; minx = INF; for (i=N;i>1;i=path[i]) minx = min(minx,flow[way[i]]); ans += d[N]*minx; for (i=N;i>1;i=path[i]) flow[way[i]]-=minx, flow[way[i]^1]+=minx; return 1; } int main(){ freopen("story.in","r",stdin); freopen("story.out","w",stdout); scanf("%d",&n); memset(g,127/2,sizeof(g)); for (i=1;i<=n;i++) g[i][i]=0; for (i=1;i<=n;i++){ scanf("%d",&K); while (K--){ scanf("%d%d",&b,&t); ans += t; g[i][b] = min(g[i][b],t); de[i]++; de[b]--; } } floyed(); tot = 1; N = n+2; for (i=2;i<=n;i++) if (de[i]>0) add(i+1,N,de[i],0); else add(1,i+1,-de[i],0); add(1,2,INF,0); for (i=1;i<=n;i++) for (j=1;j<=n;j++) if (i!=j && g[i][j]<g[0][0]) add(i+1,j+1,INF,g[i][j]); while (spfa()); printf("%d\n",ans); return 0; }
//上下界费用流 #include <vector> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define pb push_back const int Maxn=305, Maxm=20000, INF=1e9; int node[Maxm],next[Maxm],flow[Maxm],cost[Maxm],q[Maxm]; int a[Maxn],d[Maxn],path[Maxn],way[Maxn],K[Maxn],de[Maxn]; int n,N,l,r,i,j,tot,sum,fl,cnt,ans,L,R,Mid,minx,x,y; bool v[Maxn]; vector <int> b[Maxn],t[Maxn]; void add(int x,int y,int z1,int z2){ node[++tot]=y; next[tot]=a[x]; a[x]=tot; flow[tot]=z1; cost[tot]=z2; node[++tot]=x; next[tot]=a[y]; a[y]=tot; flow[tot]=0; cost[tot]=-z2; } bool spfa(){ memset(d,10,sizeof(d)); d[1] = 0; v[1] = 1; for (q[l=r=1]=1;l<=r;v[q[l++]]=0) for (i=a[q[l]];i;i=next[i]) if (flow[i]>0 && d[node[i]]>d[q[l]]+cost[i]){ d[node[i]] = d[q[l]]+cost[i]; path[node[i]] = q[l]; way[node[i]] = i; if (v[node[i]]==0) v[ q[++r]=node[i] ]=1; } if (d[N]>=d[0]) return 0; minx = INF; for (i=N;i>1;i=path[i]) minx = min(minx,flow[way[i]]); cnt += d[N]*minx; fl+=minx; for (i=N;i>1;i=path[i]) flow[way[i]]-=minx, flow[way[i]^1]+=minx; return 1; } bool Judge(){ memset(a,0,sizeof(a)); memset(de,0,sizeof(de)); de[1]=Mid; de[n+1]=-Mid; for (i=1,tot=1;i<=n;i++){ for (j=0;j<K[i];j++){ add(i+1,b[i][j]+1,INF,t[i][j]); de[b[i][j]]++; de[i]--; } add(i+1,n+2,INF,0); } N = n+3; int SUM = 0; for (i=1;i<=n+1;i++) if (de[i]>0) SUM+=de[i], add(1,i+1,de[i],0); else add(i+1,N,-de[i],0); fl = 0; cnt = 0; while (spfa()); if (fl==SUM && cnt<ans) ans=cnt; return (fl==SUM); } int main(){ freopen("story.in","r",stdin); freopen("story.out","w",stdout); scanf("%d",&n); for (i=1;i<=n;i++){ scanf("%d",&K[i]); for (j=0;j<K[i];j++){ scanf("%d%d",&x,&y); b[i].pb(x); t[i].pb(y); sum += t[i][j]; } } L = 1; R = 5000; ans=INF; while (L<=R){ Mid = (L+R)>>1; if (Judge()) R = Mid-1; else L = Mid+1; } ans += sum; printf("%d\n",ans); return 0; }