POJ1201.Intervals题解--zhengjun
思路
一看就是差分约束,直接建边就好了。
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;typedef long long ll;
struct Precision{int x;Precision(int a){x=a;}};struct SW{int x;SW(int a){x=a;}};struct SF{int x;SF(int a){x=a;}};
#define Set(x,y,z) SW(x)<<SF(y)<<z<<SW(0)
struct IO{
#define Tp template<typename T>
#define _D isdigit(c=gc())
#define _A f|=eb;if(f)_R
#define _R return *this
#define _G p1==p2&&(p2=(p1=B)+fread(B,1,S,stdin),p1==p2)?EOF:*p1
#define _T(x) while(st[++st[0]]=x%10+'0',x/=10)
#define _O operator
static const int S=1<<21;char B[S],*p1,*p2,sf,t;int st[105],H,bs,sw;bool eb,f;char nex(){return _G;}IO(){bs=6;sf=eb=sw=0;}
IO& _O >> (char&c){_A;while(T(c=gc())){_A;}_R;}char gc(){t=(_G++);t=='\r'&&nex()=='\n'&&(gc());t==EOF&&(eb=1);return t;}
IO& _O >> (string&s){_A;s="";char c;while(T(c=gc())){_A;}while(s+=c,!T(c=gc()));_R;}IO& _O << (Precision x){bs=x.x;_R;}
IO& _O >> (char*c){_A;while(T(*c=gc())){_A;}while(!T(*++c=gc()));*c=0;_R;}int P(char c){return c=='\n'||c=='\r'||c==EOF;}
IO& _O >> (double&x){_A;x=0;bool F=0;char c;while(!_D){F^=(c=='-');_A;}while(x=x*10+(c^48),_D&&(P(c),1));if(c^'.')_R;c=gc();
double k=1;while(x+=(c^48)*(k*=0.1),_D);F&&(x=-x);_R;}void pu(int x){while(x-->0)pc(sf);}IO& _O << (SW x){sw=x.x;_R;}
Tp IO& _O >> (T&x){_A;x=0;bool F=0;char c;while(!_D){F^=(c=='-');_A;}while(x=(x<<3)+(x<<1)+(c^48),_D);F&&(x=-x);_R;}
IO& _O << (const string &s){int l=s.length();pu(sw-l);for(int i=0;i<l;i++)pc(s[i]);_R;}IO& _O << (const char c){pc(c);_R;}
IO& _O << (char*c){int l=strlen(c);pu(sw-l);for(int i=0;i<l;i++)pc(c[i]);_R;}void CL(){fwrite(B,1,H,stdout);H=0;}
IO& _O << (const char*c){int l=strlen(c);pu(sw-l);for(int i=0;i<l;i++)pc(c[i]);_R;}IO& _O << (SF x){sf=x.x;_R;}
IO& _O << (double x){x<0&&(st[++st[0]]='-',x=-x);double t=0.5;for(int i=1;i<=bs;i++)t*=0.1;x+=t;ll y=x;_T(y);pu(sw-st[0]-
bool(bs)-bs);while(st[0])pc(st[st[0]--]);x-=ll(x);if(bs)pc('.');for(int i=1;i<=bs;i++)pc(int(x*=10)+'0'),x-=int(x);_R;}
Tp IO& _O << (T x){x<0&&(pc('-'),x=-x);_T(x);pu(max(sw-st[0],0));while(st[0])pc(st[st[0]--]);_R;}_O bool()const{return !f;}
IO& getline(string&s){_A;s="";char c=gc();if(!P(c))while(s+=c,!P(c=gc()));_R;}void pc(const char c){H==S&&(CL(),0);B[H++]=c;}
IO& getline(char*c){_A;*c=gc();if(!P(*c))while(!P(*++c=gc()));*c=0;_R;}int T(char c){return c==' '||P(c);}~IO(){CL();}
}fin,fout;
const int N=5e4+10;struct edges{int to,w,nex;}edge[N<<2];
int n,s,head[N],kk,d[N];void add(int u,int v,int w){edge[++kk]=(edges){v,w,head[u]};head[u]=kk;}
int main(){
while(fin>>n){
memset(head,kk=0,sizeof(head));queue<int>q;for(int a,b,c,i=1;i<=n;i++)fin>>a>>b>>c,add(a,b+1,c);
for(int i=0;i<N-1;i++)add(i,i+1,0),add(i+1,i,-1);q.push(0);memset(d,-1,sizeof(d));d[0]=0;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].to;
if(d[v]<d[u]+edge[i].w){
d[v]=d[u]+edge[i].w;
q.push(v);
}
}
}
fout<<d[N-1]<<'\n';
}
return 0;
}