题意: A->B区间 至少有两个值,询问1-》10000最少有几个值
思路:
裸差分约束,注意a从0起,所以点都增一
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
using namespace std;
const int maxn=10005;
typedef long long ll;
vector<pair<int ,ll > > vec[maxn];
int vis[maxn];
int info[maxn];
ll dis[maxn];
const ll inf=0x3f3f3f3f3f3f3f3f;
int n,m;
int spfa(int st)
{
memset(vis,0,sizeof(vis));
for(int i=0;i<=st;i++)
{
dis[i]=-inf ;
}
vis[0]=1,dis[0]=0 ;
queue<int>q;
q.push(0);
int u;
while(!q.empty())
{
u=q.front();
q.pop ();
vis[u]=0;
for(int i=0; i<vec[u].size(); i++)
{
int v=vec[u][i].first;
ll w=vec[u][i].second;
if( dis[v] < w+dis[u] )
{
dis[v]=w+dis[u];
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
return 1;
}
int main()
{
while(~scanf("%d",&n) )
{
int t=0;
for(int i=0; i<maxn; i++)
vec[i].clear();
int u,v;
ll w;
///造边
for(int i=1;i<=n;i++)
{
scanf("%d%d",&u,&v);
vec[u].push_back(make_pair(v+1,2));
t=max(t,v+1);
}
for(int i=0;i<=t;i++)
{
vec[i].push_back(make_pair(i+1,0));
vec[i+1].push_back(make_pair(i,-1));
}
spfa(t) ;
// for(int i=0;i<=10;i++)
// printf("%d %lld\n",i,dis[i]);
printf("%lld\n",dis[t]);
}
return 0;
}