链式前向星(图)

来源:互联网 发布:java定义方法 编辑:程序博客网 时间:2024/06/06 16:47
#include <iostream>#include <cstring>using namespace std;const int M=1005;int head[M];struct node{    int to;    int w;    int next;}Node[M];int main(){     int n,m;     cin>>n>>m;     memset(head,-1,sizeof(head));     for(int i=1;i<=m;i++)     {         int a,b,w;         cin>>a>>b>>w;         Node[i].to=b;         Node[i].w=w;         Node[i].next=head[a];         head[a]=i;     }     for(int i=1;i<=n;i++)     {         for(int k=head[i];k!=-1;k=Node[k].next)         cout<<i<<" "<<Node[k].to<<" "<<Node[k].w<<endl;     }    return 0;}/*8 125 8 296 1 128 3 111 2 43 1 224 3 177 4 256 5 98 7 71 6 93 2 196 7 41 6 91 2 43 2 193 1 224 3 175 8 296 7 46 5 96 1 127 4 258 7 78 3 11*/

0 0
原创粉丝点击