【有向有权图实例】
【算法代码】
/* 链式前向星存图
val[idx] 表示第 idx 条边的权值。
e[idx] 表示第 idx 条边的终点。
ne[idx] 表示与第 idx 条边同起点的最近一次被添加的边的编号。
h[a] 表示以结点 a 为起点的最近一次被添加的边的编号。这个表述是在使用 ne[idx]=h[a] 时,也即使用 h[a]=idx++ 更新 h[a] 之前而言的。要特别注意这个语境。
*/#include
using namespace std;const int maxn=1005;
const int maxm=10005;
int e[maxm],ne[maxm],h[maxn],val[maxm],idx;
// Undirected edges are special directed edges, open double
// int e[maxm<<1],ne[maxm<<1],h[maxn],val[maxm<<1],idx;void add(int a,int b,int w) {val[idx]&#61;w,e[idx]&#61;b,ne[idx]&#61;h[a],h[a]&#61;idx&#43;&#43;;
}int main() {int n,m;cin>>n>>m;memset(h,-1,sizeof(h));while(m--) {int a,b,c;scanf("%d%d%d",&a,&b,&c);add(a,b,c);}for(int i&#61;1; i<&#61;n; i&#43;&#43;) {for(int j&#61;h[i]; j!&#61;-1; j&#61;ne[j]) {cout<<"V"<"<<"V"<}/*
in:
5 6
1 2 7
1 4 5
2 3 12
3 4 18
2 5 36
3 5 51out:
V1->V4: 5
V1->V2: 7
V2->V5: 36
V2->V3: 12
V3->V5: 51
V3->V4: 18
*/
【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/101233779
https://blog.csdn.net/hnjzsyjyj/article/details/101233485
https://blog.csdn.net/hnjzsyjyj/article/details/101233249
https://blog.csdn.net/weixin_46211066/article/details/121361769