直接按时间模拟,先把能流到的求出来,然后把最低的都添加1流量。(不知道为什么以前一直没有人做?)
AC代码如下:
#include<iostream> #include<cstdio> #include<cstring> #define N 205 using namespace std; int n,m,gol,tal,tot,fst[N],pnt[N],len[N],nxt[N],x[N],y[N],h[N]; bool bo[N]; int find(int t){ int i; for (i=1; i<=n; i++) if (x[i]==t) return i; } void add(int x,int y,int z){ pnt[++tot]=y; len[tot]=z; nxt[tot]=fst[x]; fst[x]=tot; } int main(){ scanf("%d",&n); int i; for (i=1; i<=n; i++){ scanf("%d%d%d",&x[i],&y[i],&h[i]); h[i]+=y[i]; } bo[1]=1; scanf("%d",&m); for (i=1; i<=m; i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); w=find(u+w); u=find(u-1); add(u,w,v); add(w,u,v); } scanf("%d%d",&gol,&tal); int ans=0; while (1){ bool flag=1; while (flag){ flag=0; int p; for (i=1; i<=n; i++) if (bo[i]) for (p=fst[i]; p; p=nxt[p]) if (h[i]<=len[p] && !bo[pnt[p]]) bo[pnt[p]]=flag=1; } m=0; for (i=1; i<=n; i++) if (bo[i]) m=max(m,h[i]); if (bo[gol] && m==tal){ printf("%d\n",ans); return 0; } for (i=1; i<=n; i++) if (bo[i] && h[i]==y[i] && m==y[i]){ puts("-1"); return 0; } for (i=1; i<=n; i++) if (bo[i] && h[i]==m){ h[i]--; ans++; } } return 0; }
by lych
2016.3.4