图的静态前向星表示中的转置问题

#include<iostream>
#include<vector>


using namespace std;


const int maxn = 1000 + 10;
int head[maxn],head2[maxn];
struct EdgeNode {
int to;
int w;
int next;
};


EdgeNode Edge2[maxn],Edge[maxn];
int m, n;


int main() {
freopen("input.txt", "r", stdin);
cin >> n >> m;
memset(head, -1, sizeof(head));
memset(head2, -1, sizeof(head2));
for (int i = 0; i < m; i++) {
int t1, t2, w;
cin >> t1 >> t2 >> w;
Edge[i].to = t2;
Edge[i].w = w;
Edge[i].next = head[t1];
head[t1] = i;
}


int cnt = 0;
for (int i = 1; i <= n; i++) {
for (int j = head[i]; j != -1; j = Edge[j].next) {
cout << i << "  " << Edge[j].to << " " << Edge[j].w << endl;
Edge2[cnt].to = i;
Edge2[cnt].w = Edge[j].w;
Edge2[cnt].next = head2[i];
head2[Edge[j].to] = cnt++;
}
}
cout << endl;
for (int i = 1; i <= n; i++)
for (int j = head2[i]; j != -1; j = Edge2[j].next)
{
cout << i << " " << Edge2[j].to << " " << Edge2[j].w << endl;
}






return 0;
}

猜你喜欢

转载自blog.csdn.net/PAN_Andy/article/details/59624849