You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
python/TangDou/JianTu/LianShiQianXiangXing_Zero.cpp

49 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}