手写了个队列,用的邻接矩阵
#include<stdio.h>
#include<stdlib.h>
typedef long long ll;
int maze[1502][1502];
int m,n,ans=-99999999;
int _max(int a,int b){return (a > b) ? a : b ;}
typedef struct{
int l;
ll p;
}pair;
typedef struct {
int size;
pair* data;
}queue;
queue* queue_new(int length){
queue* temp = (queue*)malloc(sizeof(queue));;
temp->data = (pair*)malloc( (length+1) * sizeof(pair));
return temp;
}
void queue_append(queue* aList,pair element){
aList->data[aList->size] = element;
aList->size += 1;
}
pair queue_push(queue* aList){
pair res = aList->data[0];
for (int i = 1 ; i < aList->size ; i ++ ){
aList->data[i-1] = aList->data[i];
}
aList->size -= 1;
return res;
}
void bfs(){
queue*temp = queue_new(500001);
pair new;new.l = 0;new.p = 1;
queue_append(temp,new);
while (temp->size > 0){
pair crt = queue_push(temp);
ll l = crt.l,p = crt.p;
for (int i = p ; i <= m ; i ++ ){
if ( maze[p][i] != 0 && i != p){
pair t;
t.l = l + maze[p][i];
t.p = i;
maze[p][i] = 0;
if ( t.p == m ){
ans = _max(ans,t.l);
}else{
queue_append(temp,t);
}
}
}
}
if ( ans != -99999999){
printf("%d",ans);
}else puts("-1");
}
int main(){
//freopen("testdata.in","r",stdin);
scanf("%d%d",&m,&n);
for (int i = 0 ; i < n ; i ++ ){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
maze[a][b] = _max(maze[a][b],c);
}
bfs();
return 0;
}