前向星
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4+10;
int n,m;
int head[maxn];//存储起点为Vi的第一条边的位置
struct NODE
{
int from;
int to;
int w;
};
NODE edge[maxn];
bool cmp(NODE a,NODE b)
{
if(a.from==b.from&&a.to==b.to) return a.w<b.w;
if(a.from==b.from) return a.to<b.to;
return a.from<b.from;
}
int main()
{
cin>>n>>m;
for(int i=0;i<m;i++) {
cin>>edge[i].from>>edge[i].to>>edge[i].w;
}
sort(edge,edge+m,cmp);
for(int i=0;i<maxn;i++){
head[i]=-1;
}
head[edge[0].from]=0;
for(int i=1;i<m;i++){
if(edge[i].from!=edge[i-1].from) head[edge[i].from]=i;
//确定起点为Vi的第一条变的位置
}
for(int i=1;i<=n;i++){
for(int k=head[i];edge[k].from==i&&k<m;k++){
cout<<edge[k].from<<' '<<edge[k].to<<' '<<edge[k].w<<endl;
}
}
return 0;
}