为何读入优化会炸?

P1462 通往奥格瑞玛的道路

Timothy @ 2016-11-12 22:32:02

这道题为什么读入优化会炸,去掉后就过了。。。

望诸位大神指点。

#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
int n,m,maxn;
//----------------------------------------------------------------------------------------------------------------------------------------------
inline void read(int &s)
{
    s=0;int f=1;char ch=getchar();
    while (ch<'0' && ch>'9'){if (ch=='-')f=-1;ch=getchar();}
    while (ch>='0' && ch<='9'){s=s*10+ch-'0';ch=getchar();}
    s*=f;

}//这是读入优化 //----------------------------------------------------------------------------------------------------------------------------------------------

int i[1000005],j[1000005],k[1000005],first[1000005],next[1000005];

struct node
{
    int xh;
    long long val;
    friend bool operator <= (node a1,node a2){return a1.val<=a2.val;}
}dui[1000005];
long long dis[1000005];
int len,l=1,r,cos[1000005],ans=-1,fan[1000005],nn;
inline void pop()
{
    fan[dui[len].xh]=1;
    dui[1]=dui[len--];
    int now=1;
    while ((now<<1)<=len)
    {
        int next=now<<1;
        if (next<len && dui[next+1]<=dui[next])next++;
        if (dui[now]<=dui[next])break;
        fan[dui[next].xh]=now;
        fan[dui[now].xh]=next;
        node t=dui[now];
        dui[now]=dui[next];
        dui[next]=t;
        now=next;
    }
}
inline void pop1(int now)
{
    while (now>1)
    {
        int next=now>>1;
        if (dui[now]<=dui[next])
        {
            fan[dui[next].xh]=now;
            fan[dui[now].xh]=next;
            node t=dui[now];
            dui[now]=dui[next];
            dui[next]=t;
        }
        else break;
        now=next;
    }
}
bool dijkstra(int MAX)
{
    if (MAX<cos[1] || MAX<cos[n])return false;
    dui[1].val=0;dui[1].xh=1;fan[1]=1;dis[1]=0;len=1;
    for (int b=2;b<=n;++b)
    {
        if (cos[b]>MAX)
        {
            fan[b]=n+1;
            continue;
        }
        dui[++len].val=1e18;
        dui[len].xh=b;
        fan[b]=len;
        dis[b]=1e18;
    }
    nn=len;
    for (int b=1;b<=nn;++b)
    {
        dis[dui[1].xh]=dui[1].val;
        node y=dui[1];
        fan[y.xh]=n+1;
        pop();
        for (int u=first[y.xh];u;u=next[u])
            if (dis[y.xh]+k[u]<dui[fan[j[u]]].val && fan[j[u]]<=len)
            {
                dui[fan[j[u]]].val=dis[y.xh]+k[u];
                pop1(fan[j[u]]);
            }
    }
    return dis[n]<=maxn;
}
int main ()
{
    read(n);read(m);read(maxn);
    for (int b=1;b<=n;++b)
    {
        scanf ("%d",&cos[b]);
        r=r>cos[b]?r:cos[b];
    }
    for (int b=1;b<=m;++b)
    {
        scanf ("%d%d%d",&i[b],&j[b],&k[b]);
        next[b]=first[i[b]];first[i[b]]=b;
        i[b+m]=j[b];j[b+m]=i[b];k[b+m]=k[b];
        next[b+m]=first[j[b]];first[j[b]]=b+m;
    }
    while (l<=r)
    {
        int mid=(l+r)>>1;
        if (dijkstra(mid)){ans=mid;r=mid-1;}
        else l=mid+1;
    }
    if (ans!=-1)printf ("%d",ans);
    else printf ("AFK");
    return 0;
}

by cxy004 @ 2016-11-12 23:05:52

while (ch<'0' && ch>'9')


by charlie003 @ 2016-11-12 23:24:54

orzczp orzcxy

ch<'0' || ch>'9'


|