[bzoj]1092: [SCOI2003]蜘蛛难题

来源:互联网 发布:藏宝库源码 编辑:程序博客网 时间:2024/05/18 00:09

按时间去模拟就行了。

把管道建成双向边,然后每次求出可以流到的,如果有一个已经溢出就输出-1。

#include <cstdio>#include <algorithm>using namespace std;const int N=25;struct node{    int x,y,h;}g[N];bool f[N];int p,pre[N*N],L,tot,u,v,w;int x,y,ans;struct edge{    int cot,nxt,to;}e[N*N];inline int find(int x){    for (int i=1;i<=p;i++)    if (g[i].x==x) return i;}inline void add(int x,int y,int z){    e[++tot].to=y;    e[tot].nxt=pre[x];    e[tot].cot=z;    pre[x]=tot;}int main(){    register int i,j;    scanf("%d",&p);    for (i=1;i<=p;i++)    {        scanf("%d %d %d",&g[i].x,&g[i].y,&g[i].h);        g[i].h+=g[i].y;    }    f[1]=1;    scanf("%d",&L);    for (i=1;i<=L;i++)    {        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",&x,&y);    while (1)    {        bool fl=1;        while (fl)        {            fl=0;            for (i=1;i<=p;i++)            if (f[i])            for (int t=pre[i];t;t=e[t].nxt)            if (g[i].h<=e[t].cot&&!f[e[t].to])            f[e[t].to]=fl=1;        }        int m=0;        for (i=1; i<=p; i++)        if (f[i]) m=max(m,g[i].h);          if (f[x] && m==y)        {             printf("%d\n",ans);             return 0;         }          for (i=1;i<=p;i++)        if (f[i]&&g[i].h==g[i].y&&m==g[i].y)        {             puts("-1");            return 0;         }          for (i=1;i<=p;i++)          if (f[i]&&g[i].h==m)        {             g[i].h--;             ans++;         }      }    return 0;}