'commit'
This commit is contained in:
46
TangDou/LuoGuBook/L18_2_2_2.cpp
Normal file
46
TangDou/LuoGuBook/L18_2_2_2.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int N = 1005; //图的最大点数量
|
||||
struct edge { //记录边的终点,边权的结构体
|
||||
int to; //终点
|
||||
int cost; //边权
|
||||
};
|
||||
int n, m; //表示图中有n个点,m条边
|
||||
vector<edge> p[N]; //使用vector的邻接表
|
||||
|
||||
int v[N][N]; //邻接矩阵
|
||||
|
||||
/**
|
||||
* 测试数据
|
||||
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;
|
||||
//m条边
|
||||
for (int i = 1; i <= m; i++) {
|
||||
int u, v, l; //点u到点v有一条权值为l的边
|
||||
cin >> u >> v >> l;
|
||||
p[u].push_back({v, l});
|
||||
}
|
||||
|
||||
//读入到邻接矩阵
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 0; j < p[i].size(); j++)
|
||||
v[i][p[i][j].to] = p[i][j].cost;
|
||||
|
||||
//按邻接矩阵输出
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++)
|
||||
cout << v[i][j] << " ";
|
||||
cout << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user