水水的。
给几个不等式: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[2000100]; struct Edge{ int to,next,val; }edge[Maxm]; void init() { memset(vi,0,sizeof(vi)); memset(index,-1,sizeof(index)); for(int i=0;i<Maxn;i++) dis[i]=-inf; e=0; } 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=0; dis[u]=0; Que[head++]=u; while(head!=rear) { temp=Que[rear++]; //cout<<temp<<endl; vi[temp]=0; for(i=index[temp];i!=-1;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]=1; } } } return dis[ans]; } int main() { int m,i,j,a,b,c; while(scanf("%d",&n)!=EOF) { init(); int maxn=0; int minn=inf; for(i=1;i<=n;i++) { scanf("%d%d",&a,&b); maxn=max(maxn,b+1); minn=min(minn,a); addedge(a,b+1,2); } for(i=minn;i<=maxn;i++) { addedge(i+1,i,-1); addedge(i,i+1,0); } int ans; ans=spfa(minn,maxn); printf("%d ",ans); } return 0; }