链式前向星(邻接表)

#include<vector>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#define FORa(i,s,e) for(int i=s;i<=e;i++)
#define FORs(i,s,e) for(int i=s;i>=e;i--)
#define File(name) freopen(name".in","r",stdin),freopen(name".out","w",stdout);


using namespace std;

static char buf[100000],*pa=buf,*pb=buf;
#define gc getchar() //pa==pb&&(pb=(pa=buf)+fread(buf,1,100000,stdin),pa==pb)?EOF:*pa++
inline int read();



const int N=100,M=200;
struct Edge{
    int to,dis,next;
}*v_edge;
int n,m,num_edge,head[N];
void Add_edge(int from,int to,int dis)
{
    v_edge[++num_edge]=((Edge){to,dis,head[from]});
    head[from]=num_edge;
}
int main()
{
    int from,to,dis;
    
    n=read(),m=read();
    v_edge=new Edge[2*m];
    FORa(i,1,m)
        from=read(),to=read(),dis=read(),Add_edge(from,to,dis),Add_edge(to,from,dis);    
    FORa(i,1,n)
    {
        int p=head[i];
        while(p)
        {
            cout<<v_edge[p].to<<" ";
            p=v_edge[p].next;
        }
        cout<<endl;
     } 
    return 0;
}

inline int read()
{
    register int x(0);register int f(1);register char c(gc);
    while(c<'0'||c>'9') f=c=='-'?-1:1,c=gc;
    while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=gc;
    return x*f;
}
/*4 3
1 2 1
2 3 1
3 4 1
*/

 

posted @ 2019-02-14 19:06  SeanOcean  阅读(510)  评论(0编辑  收藏  举报