zzoce 🧊
一种简单高效的存图方法,由邻接表演变而来,本质上是用链表实现的邻接表
特点:
i^1
i
i+1
12345678910111213141516171819202122232425262728293031323334353637383940
#include <iostream> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <map> #include <algorithm> using namespace std; const int maxn = 1e5+5; struct E{ int to,w,next;//节点,权值,指针 }Edge[maxn<<1]; int tot,Head[maxn]; inline void AddEdge(int u,int v,int w){ Edge[tot].to=v; Edge[tot].w=w; Edge[tot].next=Head[u];//表头插入的方式 Head[u]=tot++; } int main () { memset(Head,-1,sizeof (Head));//初始化链表结尾为1,相当于NULL int n; cin>>n; for (int i=1,u,v,w;i<=n;i++){ cin>>u>>v>>w; AddEdge(u,v,w); AddEdge(v,u,w); } for (int u=1;u<=n;u++){ cout<<u; for (int i=Head[u];~i;i=Edge[i].next){//对i按位取反,~(-1)==0; int v=Edge[i].to,w=Edge[i].w; cout<<"->"<<v<<","<<w; } cout<<endl; } return 0; }