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.
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 , idx ; //n个点, m条边,idx是新结点加入的数据内索引号
//链式前向星
struct Edge {
int to ; //到哪个结点
int value ; //边权
int next ; //同起点的下一条边的编号
} edge [ N < < 1 ] ; //同起点的边的集合 N<<1就是2*N, 一般的题目, 边的数量通常是小于2*N的, 这个看具体的题目要求
int head [ N ] ; //以i为起点的边的集合入口处
//加入一条边, x起点, y终点, value边权
void add_edge ( int x , int y , int value ) {
edge [ idx ] . to = y ; //终点
edge [ idx ] . value = value ; //权值
edge [ idx ] . next = head [ x ] ; //以x为起点上一条边的编号, 也就是与这个边起点相同的上一条边的编号
head [ x ] = idx + + ; //更新以x为起点上一条边的编号
}
/**
* 测试数据
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 ;
//初始化head数组
memset ( head , - 1 , sizeof head ) ;
//m条边
for ( int i = 1 ; i < = m ; i + + ) {
int u , v , l ; //点u到点v有一条权值为l的边
cin > > u > > v > > l ;
//加入到链式前向星
add_edge ( u , v , l ) ;
}
//遍历每个结点
for ( int i = 1 ; i < = n ; i + + ) {
printf ( " 出发点:%d " , i ) ;
for ( int j = head [ i ] ; j ! = - 1 ; j = edge [ j ] . next ) //遍历每个结点的每一条边
printf ( " 目标点:%d,权值:%d; " , edge [ j ] . to , edge [ j ] . value ) ;
puts ( " " ) ;
}
return 0 ;
}