水水的。
给几个不等式:dis[b]-dis[a]>=2; 0<=dis[i+1]-dis[i]<=1;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 1000000000
#define Maxn 10110
#define Maxm 160000
using namespace std;
int index[Maxn],dis[Maxn],vi[Maxn],e,n,Que[];
struct Edge{
int to,next,val;
}edge[Maxm];
void init()
{
memset(vi,,sizeof(vi));
memset(index,-,sizeof(index));
for(int i=;i<Maxn;i++)
dis[i]=-inf;
e=;
}
void addedge(int from,int to,int val)
{
edge[e].to=to;
edge[e].val=val;
edge[e].next=index[from];
index[from]=e++;
}
int spfa(int u,int ans)
{
int i,j,temp,now,head,rear;
head=rear=;
dis[u]=;
Que[head++]=u;
while(head!=rear)
{
temp=Que[rear++];
//cout<<temp<<endl;
vi[temp]=;
for(i=index[temp];i!=-;i=edge[i].next)
{
now=edge[i].to;
if(dis[temp]+edge[i].val>dis[now])
{
dis[now]=dis[temp]+edge[i].val;
if(!vi[now])
Que[head++]=now;
vi[now]=;
}
}
}
return dis[ans];
}
int main()
{
int m,i,j,a,b,c;
while(scanf("%d",&n)!=EOF)
{
init();
int maxn=;
int minn=inf;
for(i=;i<=n;i++)
{
scanf("%d%d",&a,&b);
maxn=max(maxn,b+);
minn=min(minn,a);
addedge(a,b+,);
}
for(i=minn;i<=maxn;i++)
{
addedge(i+,i,-);
addedge(i,i+,);
}
int ans;
ans=spfa(minn,maxn);
printf("%d\n",ans);
}
return ;
}