萌新刚学链式前向星,求教
  • 板块学术版
  • 楼主SDLTF_凌亭风
  • 当前回复6
  • 已保存回复6
  • 发布时间2020/10/6 13:54
  • 上次更新2023/11/5 11:49:59
查看原帖
萌新刚学链式前向星,求教
367343
SDLTF_凌亭风楼主2020/10/6 13:54
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt = 0;
struct {
	int to, w1, w2, next;
}edge[100];
int head[100]; 
void init(){
	memset(head, -1, sizeof(head)); 
}
void add_edge(int u, int v,int w1, int w2){
	edge[cnt].to 	= v;
	edge[cnt].w1 	= w1;
	edge[cnt].w2 	= w2;
	edge[cnt].next 	= head[u];
	head[u]         = cnt ++;
}
int main() {
	cin >> n >> m; 
	int u, v, w1, w2;
	for(int i = 1;i <= m;i ++){
		cin >> u >> v >> w1 >> w2;
		add_edge(u, v, w1, w2);
	}
	for(int i = 1;i <= n;i ++){
		cout << i << endl;
		for(int j = head[i];j != -1;j = edge[j].next){
			cout << i << ' ' << edge[j].to << ' ' <<edge[j].w1 << ' ' << edge[j].w2 << endl;
		} 
		cout << endl;
	} 
}

如图,这是我写的链式前向星,w1w2是题目要用的两个不同的价值
问题在于输入数据后会无限输出,请问哪里有问题啊QAQ
数据在这里:

3 3 
1 3 1 2 
1 2 1 2 
2 3 1 2
2020/10/6 13:54
加载中...