题解:P5045 [SCOI2003] 蜘蛛难题

· · 题解

按时间模拟。

维护当前灌水能灌到的最低位置,然后将水位加 1。高度超过最高高度还不能碰到蜘蛛就输出 -1。

下面是代码:

#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;
}