萌新求助!!!!!!! 56分 BFS
  • 板块P1807 最长路
  • 楼主syLph
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/3/25 18:30
  • 上次更新2023/10/28 05:43:20
查看原帖
萌新求助!!!!!!! 56分 BFS
545026
syLph楼主2022/3/25 18:30

萌新求助DL BFS 4个WA 56分求助!!!!!

手写了个队列,用的邻接矩阵

#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;
}
2022/3/25 18:30
加载中...