Files
python/TangDou/JianTu/LianShiQianXiangXing_Zero.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

49 lines
1.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 1010; //点数最大值
int n, m; //n个点m条边
//idx是新结点加入的数据内索引号
//h[N]表示有N条单链表的头,e[M]代表每个节点的值,ne[M]代表每个节点的下一个节点号
int h[N], e[N << 1], ne[N << 1], w[N << 1], idx;
//链式前向星
void add(int a, int b, int l) {
e[idx] = b, ne[idx] = h[a], w[idx] = l, h[a] = idx++;
}
/**
* 测试数据
4 6
2 1 1
1 3 2
4 1 4
2 4 6
4 2 3
3 4 5
*/
int main() {
cin >> n >> m;
//初始化为-1,每个头节点写成-1
memset(h, -1, sizeof h);
//m条边
for (int i = 1; i <= m; i++) {
int u, v, l; //点u到点v有一条权值为l的边
cin >> u >> v >> l;
//加入到链式前向星
add(u, v, l);
}
//遍历每个结点
for (int i = 1; i <= n; i++) {
printf("出发点:%d ", i);
for (int j = h[i]; j != -1; j = ne[j])
printf(" 目标点:%d,权值:%d;", e[j], w[j]);
puts("");
}
return 0;
}