- /*
- // 链式前向星维护的是一个边集数组
- const int N = 1e6+5;// 边的数目
- int head[N];// 某个点相邻的第一条边的编号
- int cnt;// 边的数目
- struct Edge{
- int to;// 这条边到达的终点
- int dis;// 这条边的权值
- int next;// 这条边指向的下一条边的编号 (可以自己想想, 并不是上一条, 采用的是类似于前插法)
- }edge[N];
- void addEdge(int u,int v,int w){//u->v 的一个权值为 w 的边
- cnt++;
- edge[cnt].to = v;
- edge[cnt].dis = w;
- edge[cnt].next = head[u];
- head[u] = cnt;
- }
- // 遍历
- int u = 1;
- for(int i=head[u]; i; i=edge[i].next){
- cout <<u << "->" <<edge[i].to << " " << edge[i].dis << endl;
- }
- */
- #include<bits/stdc++.h>
- using namespace std;
- const int N = 1e5+5;
- int head[N],cnt;
- struct Edge{
- int to,dis,next;
- }e[N];
- void addEdge(int u,int v,int w){
- cnt++;
- e[cnt].to = v;
- e[cnt].dis = w;
- e[cnt].next = head[u];
- head[u] = cnt;
- }
- int main(){
- int n,m;
- cin>> n>> m;
- for(int i=1; i<=m; i++){
- int u,v,w;
- cin>> u>> v>> w;
- addEdge(u,v,w);
- }
- int u = 1;
- for(int i=head[u];i;i=e[i].next){
- cout <<u << "->" << e[i].to << " " << e[i].dis << endl;
- }
- return 0;
- }
- /*
- 5 6
- 1 2 3
- 2 3 5
- 3 4 6
- 1 3 8
- 4 1 9
- 1 5 6
- */
来源: http://www.bubuko.com/infodetail-3479624.html