Integer Intervals POJ - 1716_查分约束_

Code:

#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int N=500000+3;
const int INF=-233333333+2;
int head[N],to[N<<1],nex[N<<1],val[N<<1],d[N],inq[N];
queue<int>Q;
int cnt,s,t,n;
void add_edge(int u,int v,int c)
{
	nex[++cnt]=head[u],head[u]=cnt;
	to[cnt]=v,val[cnt]=c;
}
int spfa()
{
	for(int i=1;i<=n;++i)d[i]=INF;
	d[s]=0;Q.push(s);inq[s]=1;
    while(!Q.empty())
    {
    	int u=Q.front();Q.pop();
    	inq[u]=0;
    	for(int v=head[u];v;v=nex[v])
    		if(d[u]+val[v]>d[to[v]])
    		{
    			d[to[v]]=d[u]+val[v];
    			if(inq[to[v]]==0)
    			{
    				Q.push(to[v]);
    				inq[to[v]]=1;
    			}
    		}
    }
    return d[t];
}
int main()
{
	int MIN=N,MAX=0;
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		int a,b;
		scanf("%d%d",&a,&b);
		MIN=min(MIN,a); MAX=max(MAX,b);
		add_edge(a,b+1,2);
	}
	for(int i=MIN;i<=MAX;++i)
	{
		add_edge(i+1,i,-1);
		add_edge(i,i+1,0);
	}
	s=MIN,t=MAX+1;
	printf("%d",spfa());
	return 0;
}
posted @ 2018-09-22 17:48  EM-LGH  阅读(146)  评论(0编辑  收藏  举报