36分求助
查看原帖
36分求助
494992
underline__jian楼主2022/1/23 21:46
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N=1e5+15,inf=0x3f3f3f3f;
struct node{
	int head,nxt,to,w;
}e[N];
int n,m,dis[N],tot;
bool vis[N];
queue<int>q;
inline void add(int x,int y,int z){
	e[++tot].to=y;
	e[tot].w=z;
	e[tot].nxt=e[x].head;
	e[x].head=tot;
}
inline void spfa(){
    q.push(0);
    while(!q.empty()){
        int u=q.front();
		vis[u]=false;
		q.pop();
        for(int i=e[u].head;i;i=e[i].nxt)
            if(dis[e[i].to]<dis[u]+e[i].w){
                dis[e[i].to]=dis[u]+e[i].w;
                if(!vis[e[i].to]){
                	vis[e[i].to]=true;
					q.push(e[i].to);
				}	
            }
    }
    printf("%d",dis[n]);
}
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
    	x=x*10+ch-'0';
		ch=getchar();
    }
    return x*f;
}
int main(){
    n=read(),m=read();
    for(int i=1;i<=m;i++){
        int x=read(),y=read(),z=read();
        add(x-1,y,z);
    }
    for(int i=0;i<=n;i++){
        if(i!=0){
        	add(i,i+1,0);
			dis[i]=-inf;	
		}
        if(i!=n) add(i+1,i,-1);
    }
    spfa();
    return 0;
}
2022/1/23 21:46
加载中...