70 lines
1.2 KiB
C++
70 lines
1.2 KiB
C++
#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多add()了一轮!
|
||
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的精髓!!
|
||
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不是点数,maxn才是
|
||
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);//漏了
|
||
spfa();
|
||
printf("%d",dist[maxn]-dist[0]);
|
||
return 0;
|
||
}
|