链式前向星

链式前向星可以存图
它存图的方式是:
将任意一个节点的所有临边按输入顺序依次连接起来将任意一个节点的所有临边按输入顺序依次连接起来将任意一个节点的所有临边按输入顺序依次连接起来
然后头节点(数组)存的是最后一个临边的地址然后头节点(数组)存的是最后一个临边的地址然后头节点(数组)存的是最后一个临边的地址

int head[maxn];//head[i]中i是u->v中的u,head[i]存的是这个头节点对应的最后临边的地址
int cnt//cnt是edge[cnt]中edge的地址
struct node{int w;//u->v中的边权int e;//u->v中的vint next;//就是用next让这个头节点下面的全部临边相连
}edge[maxn];
void add(int u,int v,int w){edge[cnt].w=w;edge[cnt].e=v;edge[cnt].next=head[u];//就是这一步让这个头节点下面的全部临边相连head[u]=cnt++;
}
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100501
struct NODE{int w;int e;int next;
}edge[MAXN];
int cnt;
int head[MAXN];
void add(int u,int v,int w){edge[cnt].w=w;edge[cnt].e=v;  edge[cnt].next=head[u];head[u]=cnt++;
}
int main(){memset(head,0,sizeof(head));cnt=1;int n;cin>>n;int a,b,c;while(n--){cin>>a>>b>>c;add(a,b,c);}int start;cin>>start;for(int i=head[start];i!=0;i=edge[i].next)cout<<start<<"->"<<edge[i].e<<" "<<edge[i].w<<endl;return 0;
}

深度理解链式前向星 https://blog.csdn.net/acdreamers/article/details/16902023

spfa

我理解spfa是在图上跑的可回头的bfs我理解spfa是在图上跑的可回头的bfs我理解spfa是在图上跑的可回头的bfs

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<math.h>
#include<vector>
#include<iostream>
#define INF 0x3f3f3f3f
#define ll long long
#define N 100000+10
using namespace std;
int n,m;
int x,y,z;
struct node
{int y,z;
};
vector<node> mp[1000];
int spfa(int b,int e)
{bool color[1000];int d[1000];memset(color,0,sizeof(color));memset(d,INF,sizeof(d));d[b]=0;queue<int>q;q.push(b);color[b]=1;while(!q.empty()){int st=q.front();q.pop();color[st]=0;//这里就是和bfs的唯一区别,bfs没有这里,所以color表示的就是这个点有没有进过,进过就不用进了//spfa里color表示的是队列里有没有st,要是有的话就不用进了for(int i=0;i<mp[st].size();i++){if(d[st]+mp[st][i].z<d[mp[st][i].y]){d[mp[st][i].y]=d[st]+mp[st][i].z;if(!color[mp[st][i].y]){q.push(mp[st][i].y);color[mp[st][i].y]=1;}}}}return d[e];
}
int main()
{scanf("%d%d",&m,&n);for(int i=1;i<=m;i++){scanf("%d%d%d",&x,&y,&z);mp[x].push_back((node){y,z});mp[y].push_back((node){x,z});}cout<<spfa(1,n)<<endl;
}

SPFA详解 https://blog.csdn.net/hlg1995/article/details/70242296

spfa(链式前向星)
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<math.h>
#include<vector>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f
#define maxn 10010
struct node{int w;int e;int next;
}edge[maxn];
int cnt,t,n;
int head[maxn];
void init(){memset(head,0,sizeof head);cnt=1;
}
void add(int u,int v,int w){edge[cnt].w=w;edge[cnt].e=v;edge[cnt].next=head[u];head[u]=cnt++;
}
int spfa(){queue<int> q;bool color[maxn];int d[maxn];memset(d,INF,sizeof d);memset(color,true,sizeof color);q.push(1);d[1]=0;color[1]=false;while(!q.empty()){int st=q.front();q.pop();color[st]=true;for(int i=head[st];i!=0;i=edge[i].next){if(d[st]+edge[i].w<d[edge[i].e]){d[edge[i].e]=d[st]+edge[i].w;if(color[edge[i].e]){q.push(edge[i].e);color[edge[i].e]=false;}}}}return d[n];
}
int main(){while(~scanf("%d %d", &t, &n)){init();int u,v,w;for(int i=0;i<t;i++){scanf("%d %d %d", &u, &v, &w);add(u,v,w);add(v,u,w);}printf("%d\n", spfa());}return 0;
}
dijkstra

我理解dijkstra实际上是BFS+贪心我理解dijkstra实际上是BFS+贪心我理解dijkstra实际上是BFS+贪心

#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#define INF 0x3f3f3f
#define maxn 1005
using namespace std;
int n;
vector< pair<int,int> >mp[maxn];
int dijkstra(int b,int e){priority_queue< pair<int,int> > p;//有限队列存最小节点(默认优先级较大)int d[maxn];//用于记录起点s到v的最短路memset(d,INF,sizeof(d));d[b]=0;p.push(make_pair(0,b));//起点while(!p.empty()){pair<int,int> f = p.top();p.pop();int u=f.second;if(d[u] < f.first*(-1)) continue;for(int j=0; j<mp[u].size(); j++){int v=mp[u][j].first;if(d[v]>d[u]+mp[u][j].second){d[v]=d[u]+mp[u][j].second;p.push(make_pair(d[v]*(-1),v));//  priority_queue(默认优先级较大)所以要*-1;}}}return d[e];
}
int main(){cin>>n;int k,c,u,v;for(int i=0;i<n;i++){cin>>u>>k;for(int j=0;j<k;j++){cin>>v>>c;mp[u].push_back(make_pair(v,c));}}printf("%d\n", dijkstra(1,n));return 0;
}

最短路径问题—Dijkstra算法详解 https://blog.csdn.net/qq_35644234/article/details/60870719

dijkstra(链式前向星)
#include <iostream>
#include <algorithm>
#include <cstring>
#include<cstdio>
#include <queue>
#define INF 0x3f3f3f
#define maxn 10010
using namespace std;
struct Time{int w, e;bool operator < (const Time& t)const{return w > t.w;}
};
struct node{int w;int e;int next;
}edge[maxn];
int cnt,t,n;
int head[maxn];
void init(){memset(head,0,sizeof head);cnt=1;
}
void add(int u,int v,int w){edge[cnt].w=w;edge[cnt].e=v;edge[cnt].next=head[u];head[u]=cnt++;
}
int dijkstra(){priority_queue<Time> q;int d[maxn];memset(d,INF,sizeof d);d[1]=0;q.push(Time{0,1});while(!q.empty()){Time st=q.top();q.pop();if(d[st.e]<st.w) continue;for(int i=head[st.e];i!=0;i=edge[i].next){if(d[st.e]+edge[i].w<d[edge[i].e]){d[edge[i].e]=d[st.e]+edge[i].w;q.push(Time{d[edge[i].e],edge[i].e});}}}return d[n];
}
int main(){while(~scanf("%d %d", &t, &n)){init();int u,v,w;for(int i=0;i<t;i++){scanf("%d %d %d", &u, &v, &w);add(u,v,w);add(v,u,w);}printf("%d\n", dijkstra());}return 0;
}

spfa(链式前向星)+dijkstra(链式前向星)相关推荐

  1. 前向星和链式前向星(详解+模板)

    前向星和链式前向星 参考博客:深度理解链式前向星 什么是前向星 前向星是一种特殊的边集数组,我们把边集数组中的每一条边按照起点从小到大排序,如果起点相同就按照终点从小到大排序,并记录下以某个点为起点的 ...

  2. 前链财经带你明辨区块链技术的是与非!

    对于普通人来说,区块链技术太陌生,唯一知道的就是曾经火极一时的比特币,以及街头大妈们争相购买的各种"币",但是小编相信绝大部分人对于区块链是个什么东东,表示十分懵逼. 那么问题来了 ...

  3. 【C】二叉树--顺序结构(详解堆的实现,topK排序、堆排)、和链式结构(链式结构的遍历、链式结构常见递归操作以及练习题)

    本章我们将引入树的概念并详细介绍二叉树.我们会在介绍顺序二叉树基础上,进一步介绍堆以及堆的实现,并以此为依据详解topK排序.堆排等问题:然后我们会介绍链式二叉树的实现以及各种操作.最后,我们也会给出 ...

  4. 怎么看区块链正规项目与否,区块链投资前你应知道这三点

     怎么看区块链正规项目与否,区块链投资前你应知道这三点 区块链变得异常火爆,只要跟区块链沾边的,身价都会翻倍.网上对于区块链的消息与言论也是各有不同,无论是褒义是贬义,在接触区块链投资前,都应该知道这 ...

  5. 外链式样式表_CSS外链式与内联式的区别是什么

    区别:CSS外链式是将css代码单独写一个以".css"为扩展名的文件中,然后使用link标签链接到html中.CSS内联式是直接使用style属性将css代码写在HTML标签中. ...

  6. 区块链“补短板”:牵住预付式消费风险防控的牛鼻子

    "我们从自2017年开始购买的课程,到目前总共分3次购买音乐.舞蹈.钢琴3个课程共269节课,有效期到2021年.截止目前剩余课程107节,价值两万余元.可是去年年中,商家跟我们说企业破产了 ...

  7. 区块链教程(四):搭建私链、web3.js基础

    注:本教程为技术教程,不谈论且不涉及炒作任何数字货币 区块连教程(一):前置知识-linux补充 区块链教程(二):基础概念介绍 区块链教程(三):Solidity编程基础 区块链教程(四):搭建私链 ...

  8. 海外区块链投融资持续火热 | 产业区块链发展周报

    摘要 产业动态: 四川:拟在乐山建立以区块链算法为支撑的计算产业基地 北京市海淀区:支持建设区块链算力平台 北京市朝阳区金融办.国资委监管指导组建北京商务中心区信链科技有限公司 安永与政府及行业代表合 ...

  9. 研报 | 区块链新基建:物联网+区块链如何打造差异化竞争优势?

    感谢分布式资本提供研究支持,以及摩联科技等代表性企业的交流分享. 基于区块链的物联网市场前景:万物互联时代,数据价值越发重要,物联网+区块链的融合创新将成为新的行业趋势.当前物联网模组厂商都在快速铺量 ...

最新文章

  1. 【力扣网练习题】整数反转
  2. 如何将风险应用加入白名单_将微信服务器、API接口的IP列表加入宝塔防火墙IP白名单...
  3. code first基础
  4. [Java基础]Map集合的遍历
  5. Level up - single parent navigation
  6. linux下的shell运算(加、减、乘、除)
  7. 2003退休去世领了2年退休金没回本就死了能退吗?
  8. log4j2配置文件
  9. html canvas php,关于HTML canvas的总结
  10. 【人脸识别】基于matlab GUI BP神经网络人脸识别(含识别率)【含Matlab源码 891期】
  11. SSM SpringBoot vue高校实训管理系统
  12. Navicat for MySQ中文破解版(无需激活码)
  13. 第十二章:互联网-webbrowser:显示Web页面-使用特定浏览器
  14. 【转】告诉你外语学习的真实方法及误区分析(精编版)-part 3
  15. 如何高效设计游戏——游戏策划的自我修养与心得
  16. 标准误和标准差及CV值
  17. qq相册回收站复原显示服务器繁忙,qq回收站里恢复的照片在哪看 qq照片回收站还原照片路径地址...
  18. 经济形势这么差为什么要创业——Why to Start a Startup in a Bad Economy
  19. 基于pwntools和seccomp-tools的awd pwn通防小工具
  20. springboot导入后Spring包飘红问题解决

热门文章

  1. 基于ssm的家政平台
  2. 华为od机试题 找到好朋友 java解法
  3. 技术帖| 全NDI ®和NDI |HX,让技术工作更为简单的NDI协议
  4. 力扣105 先序和中序遍历数组构建二叉树
  5. radius服务器未响应,中国科学院地球环境研究所安全软件电子竞价成交公告
  6. Stm32F103R6之SPI
  7. 强制刷新网页 html,网页强制刷新快捷键是哪个?
  8. bugku_misc_三色绘恋
  9. 缺点 霍夫圆_霍夫线变换,霍夫圆变换
  10. 【网络安全学习实践】经典红绿球问题