70 lines
1.2 KiB
C++
Raw Normal View History

2022-08-15 10:35:14 +08:00
#include <iostream>
#include <queue>
#include<cstdio>
#include <string.h>
using namespace std;
const int N=5e4+5,N3=N*4,inf=0x3f3f3f3f; //RE N3=N*4<><34>add()<29><>һ<EFBFBD>֣<EFBFBD>
int to[N3],ne[N3],w[N3],h[N],cnt=0,s=N-4,dist[N];
bool in[N];
queue<int>q;
void add(int u,int v,int w1)
{
to[++cnt]=v;
w[cnt]=w1;
ne[cnt]=h[u];
h[u]=cnt;
}
void spfa()
{
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
in[u]=0;//spfa<66>ľ<EFBFBD><C4BE><EFBFBD><E8A3A1>
for(int i=h[u];i;i=ne[i])
{
int v=to[i],w1=w[i];
// printf("%d %d %d\n",u,v,w1);
if(dist[u]+w1<dist[v])
{
dist[v]=dist[u]+w1;
// printf("%d(%d) %d(%d) %d\n",u,dist[u],v,dist[v],w1);
if(!in[v])
{
q.push(v);
in[v]=1;
}
}
}
}
}
int main()
{
int n,u,v,w1,maxn=0;
scanf("%d",&n);
memset(in,0,sizeof(bool)*(n+1));
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&u,&v,&w1);
add(v,u-1,w1*-1);
maxn=maxn>u? maxn:u;
maxn=maxn>v? maxn:v;
}
//n<><6E><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD>maxn<78><6E><EFBFBD><EFBFBD>
for(int i=0;i<=maxn;i++)
{
dist[i]=inf;
}
for(int i=0;i<maxn;i++)
{
add(i,i+1,1);
add(i+1,i,0);
add(s,i,0);
}
add(s,maxn,0);//©<><C2A9>
spfa();
printf("%d",dist[maxn]-dist[0]);
return 0;
}