ZOJ 3229 Shoot the Bullet [上下界最大流]

时间:2021-11-08 04:19:29

ZOJ 3229 Shoot the Bullet

题意:此生无悔入东方


上下界最大流

spj挂掉了我也不知道对不对,把代码放这里吧以后正常了可能会评测一下

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=2005, M=4e5+5, INF=1e9;
inline ll read(){
char c=getchar();ll x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
} int n, m, extra[N], s, t, tot, c, b, x, l, r;
int a[370][1005];
struct edge{int v, c, f, ne, lower;}e[M];
int cnt=1, h[N];
inline int ins(int u, int v, int c, int b=0) { //printf("ins %d %d %d %d\n",u,v,c,b);
e[++cnt]=(edge){v, c, 0, h[u], b}; h[u]=cnt;
e[++cnt]=(edge){u, 0, 0, h[v], b}; h[v]=cnt;
return cnt-1;
}
int q[N], head, tail, vis[N], d[N], cur[N];
bool bfs(int s, int t) {
memset(vis, 0, sizeof(vis));
head=tail=1;
q[tail++]=s; d[s]=0; vis[s]=1;
while(head!=tail) {
int u=q[head++];
for(int i=h[u];i;i=e[i].ne)
if(!vis[e[i].v] && e[i].c>e[i].f) {
vis[e[i].v]=1; d[e[i].v]=d[u]+1;
q[tail++]=e[i].v;
if(e[i].v == t) return true;
}
}
return false;
}
int dfs(int u, int a, int t) {
if(u==t || a==0) return a;
int flow=0, f;
for(int &i=cur[u];i;i=e[i].ne)
if(d[e[i].v]==d[u]+1 && (f=dfs(e[i].v, min(a, e[i].c-e[i].f), t))>0) {
flow+=f;
e[i].f+=f;
e[i^1].f-=f;
a-=f;
if(a==0) break;
}
if(a) d[u]=-1;
return flow;
}
int dinic(int s, int t) {
int flow=0;
while(bfs(s, t)) {
for(int i=0; i<=tot; i++) cur[i]=h[i];
flow+=dfs(s, INF, t);
}
return flow;
} int main() {
freopen("in","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF) {
s=0; t=n+m+1;
cnt=1; memset(h,0,sizeof(h)); memset(extra, 0, sizeof(extra));
for(int i=1; i<=m; i++) b=read(), ins(n+i, t, INF, b), extra[n+i]-=b, extra[t]+=b;
for(int i=1; i<=n; i++) {
a[i][0]=read();
c=read(); ins(s, i, c, 0);
for(int j=1; j<=a[i][0]; j++)
x=read()+1, l=read(), r=read(), a[i][j]=ins(i, n+x, r-l, l), extra[i]-=l, extra[n+x]+=l;
} int ss=n+m+2, tt=n+m+3, sum=0;
tot=n+m+3;
for(int i=s; i<=t; i++) {
if(extra[i] > 0) ins(ss, i, extra[i]), sum+=extra[i];
if(extra[i] < 0) ins(i, tt, -extra[i]);
}
ins(t, s, INF);
int flow=dinic(ss, tt); //printf("sum %d %d\n",sum,flow); if(flow != sum) puts("-1");
else {
printf("look %d\n",e[cnt-1].f);
//e[cnt-1]=e[cnt]=edge();
e[cnt].f=0;
printf("%d\n", dinic(s, t));
for(int i=1; i<=n; i++)
for(int j=1; j<=a[i][0]; j++) x=a[i][j], printf("%d\n", e[x].f + e[x].lower);
}
puts("");
}
return 0;
}