10087. 「一本通 3.4 例 1」Intervals
题意
从0sim 5times 10^4中选出尽量少的整数,使每个区间 [a_i,b_i]内都有至少 c_i个数被选出。
思路
当然食用spfa啦。 设s[k]表示0~k中至少选多少个整数。根据题意可得:
也就是:
那么跑一次最长路就好了。
代码语言:javascript复制#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define E 2000010
#define eps 1e-10
#define ll long long
using namespace std;
inline ll read(){
ll res=0,f=1;char ch=getchar();
while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') res=res*10 ch-'0',ch=getchar();
return res*f;
}
inline void write(ll x){
if(x<0) putchar('-'),x=-x;
if(x<10) putchar(x '0');
else{
write(x/10);
putchar(x '0');
}
}
//queue<ll> q;
//set<ll> s;
//priority_queue<ll> q1;
//priority_queue<ll,vector<ll>,greater<ll> > q2;
//list<ll> l;
//stack<ll> s;
ll n,m;
ll fir[E],nxt[E],son[E],tot;
double w[E];
void add(ll x,ll y,double z){ tot;nxt[tot]=fir[x];son[tot]=y;fir[x]=tot;w[tot]=z;}
double dis[E],flag;
ll vis[E];
queue<int> q;
int tt[E];
ll spfa(ll s){
vis[s]=1;
q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
for(ll i=fir[u];i;i=nxt[i]){
ll to=son[i];
if(dis[u] w[i]>dis[to]){
dis[to]=dis[u] w[i];
tt[to] ;
if(tt[to]>n 1){
puts("-1");
exit(0);
}
if(vis[to]==0){
vis[to]=1;
q.push(to);
}
}
}
vis[u]=0;
}
return 0;
}
ll st[E],Min,Max;
int main(){
n=read();
Max=-1;Min=2e9;
for(int i=1;i<=n;i ){
ll x=read(),y=read(),z=read();
add(x-1,y,z);
Max=max(Max,y);
Min=min(Min,x-1);
}
for(int i=Min;i<=Max;i ){
add(i,i 1,0);
add(i 1,i,-1);
}
for(int i=Min;i<=Max;i ) dis[i]=-2e9;
memset(vis,0,sizeof(vis));
dis[Min]=0;
spfa(Min);
printf("%.0lfn",dis[Max]);
return 0;
}