先附上测试数据
8 12
5 8 29
6 1 12
8 3 11
1 2 4
3 1 22
4 3 17
7 4 25
6 5 9
8 7 7
1 6 9
3 2 19
6 7 4
8个点12条边
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define xx 10005
int t,pre[xx];//边标号,表头;
struct node
{
int st,nd,ln,next;//起点,终点,长度,下一条边;
}edge[xx];
void addedge(int st,int nd,int ln)
{
edge[t].st=st;//第t号边的起点;
edge[t].nd=nd;//第t号边的终点;
edge[t].ln=ln;//第t号边的长度;
edge[t].next=pre[st];//下一条边;
pre[st]=t++;//表头就等于边号++
}
int main()
{
int n,m,i;
int st,nd,ln;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
t=1;
memset(pre,-1,sizeof(pre));
/* 初始化边标号为1,pre为-1,表示没有边*/
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&st,&nd,&ln);
if(st!=nd)//除去自环
{
addedge(st,nd,ln);//加边
// addedge(nd,st,ln);
}
}
for(i=1;i<t;i++)
{
printf("edge[%d].st=%d,edge[%d].nd=%d,edge[%d].ln=%d,edge[%d].next=%d,pre[%d]=%d\n",i,edge[i].st,i,edge[i].nd,i,edge[i].ln,i,edge[i].next,edge[i].st,pre[edge[i].st]);
}
}
return 0;
}