邻接表存储带权的无向图
内存限制: 128 MiB 时间限制: 1000 ms 标准输入输出 题目类型: 传统 评测方式: 文本比较
题目描述
给出一个无向带权图,顶点数为 n,边数为 m。
输入格式
第一行两个整数 n,m,
接下来有 m 行,每行 3 个整数 u,v, w,表示点 u 到点 v 有一条边,边权为 w。
输出格式
第 i 行输出第 i 点的所有邻接点,按照点 i 到该点的边权由小到大输出,如果边权相等,则按照点的编号有小到大输出。
样例
样例输入
复制4 5
1 2 3
1 3 1
1 4 1
2 3 5
2 4 3
样例输出
复制3 4 2
1 4 3
1 2
1 2
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, z;
vector<pair<int, int> > a[1005];
int main() {
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; i++) {
scanf("%d %d %d", &x, &y, &z);
a[x].push_back({z, y});
a[y].push_back({z, x});
}
for(int i = 1; i <= n; i++) {
sort(a[i].begin(), a[i].end());
for(auto j : a[i]) {
printf("%d ", j.second);
}
printf("\n");
}
return 0;
}