萌新求调简单差分约束题

P1645 序列

@[dkqwq](/user/311306) 又改了一下 ```cpp #include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<utility> #define MP make_pair #define inf 0x3f3f3f3f using namespace std; namespace INPUT{ char buf[1<<20],*p1,*p2; #define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++) } using namespace INPUT; template<typename T> inline T read(){ T x=0,p=1; char ch=gc(); while(ch<'0'||ch>'9'){ if(ch=='-') p=-1; ch=gc(); } while(ch>='0'&&ch<='9'){ x=(x<<3)+(x<<1)+(ch^48); ch=gc(); } return x*p; } const int N=1005; vector<pair<int,int>>G[N]; void add(int u,int v,int w){ G[u].push_back(MP(v,w)); } int dis[N]; bool vis[N]; queue<int>q; void spfa(int s){ memset(dis,-0x3f,sizeof(dis)); dis[s]=0; q.push(s); while(!q.empty()){ int u=q.front(); q.pop(),vis[u]=0; for(auto ed:G[u]){ int v=ed.first,w=ed.second; if(dis[v]<dis[u]+w){ dis[v]=dis[u]+w; if(!vis[v]) q.push(v),vis[v]=1; } } } } int n; #define R 1000 int main(){ freopen("data.in","r",stdin); n=read<int>(); for(int i=1;i<=n;i++){ int l=read<int>(); int r=read<int>(); add(l-1,r,read<int>()); } for(int i=1;i<=R;i++) add(i-1,i,0); spfa(0); printf("%d",dis[R]); } ```
by dk_qwq @ 2022-11-24 17:21:35


@[dkqwq](/user/311306) 为什么加了`add(i,i-1,-1)`就对了呢?
by dk_qwq @ 2022-11-24 17:22:43


|