#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> PII;
#define pb emplace_back
//#define int ll
#define all(a) a.begin(),a.end()
#define x first
#define y second
#define ps push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
void solve();
const int N = 1e3 + 10;
signed main() {
IOS;
solve();
return 0;
}
ll mp[N][N];
bool vis[N];
ll n,m;//n个节点,m条边
void dfs(ll u)
{
vis[u] = true;
for(int i = 1; i <= n; ++ i)
{
if(mp[u][i]) cout << u << ' ' << i << " " << mp[u][i] << endl;//输出所有存在的边
if(vis[i]) continue;//防止成环死循环
dfs(i);
}
}
void solve() {
cin >> n >> m;
ll a,b,c;
for(int i = 1; i <= m; ++ i)
{
cin >> a >> b >> c;
mp[a][b] = c;
}
dfs(1);
}
建图方法1:邻接矩阵和访问方法
于 2024-07-27 23:40:05 首次发布