图论粗略笔记
作者:互联网
1 void dfs(int x){ 2 vis[x] = 1; 3 printf("%d\n",x); 4 for(ont i = head[x];i;i = nst[i]) if(!vis[to[i]]) dfs(to[i]); 5 } 6 7 void bfs(int x){ 8 z[++top] = x; 9 for(int i = 1;i<=top;i++){ 10 int now = z[top++]; 11 printf("%d\n",now); 12 for(itn j = head[now];j;j = nxt[j]) 13 if(!vis[to[j]]) vis[to[j]] = 1,z[++top] = to[j]; 14 } 15 } 16 //图的遍历 17 void spfa(){ 18 memeset(dis,0x3f,sizeof(dis)); 19 dis[s] = 0; 20 z[top = 1] = s; 21 for(int j = 1;j<=top;j++){ 22 int now = z[j]; 23 vis[now] = 0; 24 for(int i = head[now];i;i = nxt[i]){ 25 if(dis[to[i]]>dis[now] + w[i]){ 26 dis[to[i]] = dis[now] + w[i]; 27 if(!vis[to[i]]) vis[to[i]] = 1,z[++top] = to[i]; 28 } 29 } 30 } 31 return; 32 } 33 //SPFA单源最短路 每次从队列中取出队首点,更新与其相连的点的dis 34 35 for(int k = 1;k<=n;k++){ 36 for(int i = 1;i<=n;i++){ 37 for(int j = 1;j<=n;j++){ 38 f[i][j] = min(f[i][j],f[i][k]+f[k][j]); 39 } 40 } 41 } 42 //FLOYD 多源最短路 43 44 struct node{ 45 int id,dis; 46 friend bool operator < (node x,node y){ 47 return x.dis>y.dis; 48 } 49 }; 50 priority_queue<node>q; 51 52 memset(dis,0x3f,sizeof(dis)); 53 dis[1] = 0; 54 q.push({0,0}); 55 while(!q.empty()){ 56 now = (q.top()).id; 57 q.pop(); 58 if(vis[now]) continue; 59 vis[now] = 1; 60 for(int i = head[now];i;i = nxt[i]){ 61 if(dis[to[i]]>dis[now] + w[i]){ 62 dis[to[i]] = dis[now] + w[i]; 63 q.push({to[i],dis[to[i]]}); 64 } 65 } 66 } 67 68 //Dijkstra算法
标签:图论,int,top,粗略,笔记,vis,now,head,dis 来源: https://www.cnblogs.com/Eruption/p/15881793.html