bfs最后几个数据TLE,我怀疑是重边问题,但加入重边判定后答案就错了
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int MAXV=1000010;
const int mod=100003;
typedef struct ArcNode{
int index;
struct ArcNode *next;
}ArcNode;
typedef struct VNode{
int data;
ArcNode *first;
}VNode,AdjList[MAXV];
typedef struct ALGraph{
AdjList vertices;
int vexnum,arcnum;
}ALGraph;
int n,m,res=0,path[MAXV]={0};
vector<int>dis(MAXV,MAXV);
// bool vis[MAXV]={false};
void bfs(ALGraph *g){
queue<int> q;
path[1]=1;
dis[1]=0;
q.push(1);
while(!q.empty()){
int u=q.front();
q.pop();
for(ArcNode *w=g->vertices[u].first;w!=NULL;w=w->next){
if(dis[u]+1<dis[w->index]){
dis[w->index]=dis[u]+1;
path[w->index]=1;
}
else if(dis[u]+1==dis[w->index]) path[w->index]=(path[w->index]+1)%mod;
else continue;
q.push(w->index);
}
}
}
int main(){
int x,y;
cin>>n>>m;
ALGraph *g=(ALGraph*)malloc(sizeof(ALGraph));
g->vexnum=n;
g->arcnum=m;
for(int i=1;i<=n;i++){
g->vertices[i].first=NULL;
}
for(int i=1;i<=m;i++){
cin>>x>>y;
ArcNode *p=(ArcNode*)malloc(sizeof(ArcNode));
p->index=y;
p->next=g->vertices[x].first;
g->vertices[x].first=p;
ArcNode *q=(ArcNode*)malloc(sizeof(ArcNode));
q->index=x;
q->next=g->vertices[y].first;
g->vertices[y].first=q;
}
bfs(g);
for(int i=1;i<=n;i++){
cout<<path[i]<<endl;
}
return 0;
}