萌新看了一下午OI-Wiki没看懂,就去b站一个视频看到的,但一直输出1 2 5死循环
code:
#include <cstdio>
#include <iostream>
#define int long long
#define INF 9223372036854775800LL
using namespace std;
const int maxn = 1e5 + 10;
struct edge {
int to, w, nxt;
edge() { nxt = -1; }
} Graph[maxn];
int tot, head[maxn];
void addEdge(int u, int to, int w) {
Graph[tot].to = to;
Graph[tot].w = w;
Graph[tot].nxt = head[u];
head[u] = tot++;
}
signed main() {
int n, m, flg; //有向/无向
cin >> n >> m >> flg;
for (int i = 1; i <= m; i++) {
int x, y, v;
cin >> x >> y >> v;
if (flg) {
addEdge(x, y, v);
} else {
addEdge(x, y, v);
addEdge(y, x, v);
}
}
cout << "input ok";
for (int u = 1; u <= n; u++)
for (int i = head[u]; ~i; i = Graph[i].nxt) {
int v = Graph[i].to;
int w = Graph[i].w;
cout << u << " " << v << " " << w << endl;
}
return 0;
}
一直输出1 2 5
还有就是这题说的先按第一个数升序排列,再以链式前向星中的顺序输出即可。要怎么搞啊/kel