STL列表链式前向星

#include<list>
#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 gc getchar()//pa==pb&&(pb=(pa=buf)+fread(buf,1,10000,stdin),pa==pb)?EOF:*pa++
#define File(name) freopen(name".in","r",stdin),freopen(name".out","w",stdout);

using namespace std;
static char buf[10000],*pa=buf,*pb=buf;
inline int read();

const int N=100;
int n,m;
struct Edge{
    int to,dis;
    bool operator <(const Edge &a) const  //运算符重载,快排需要用,并且符号要与sort排序比较方式的符号一致
    {
        return to<a.to;
    }
};
list<Edge> l[N];
list<Edge>::iterator it;
int main()
{
    int from,to,dis;
    n=read(),m=read();
    FORa(i,1,m)
        from=read(),to=read(),dis=read(),l[from].push_back((Edge){to,dis});//直接存入结构体方法 strcut pp=(struct){a,b,c}
    FORa(i,1,n) l[i].sort();
    FORa(i,1,n)
    {
        for(it=l[i].begin();it!=l[i].end();it++)
            cout<<(it->to);//<<" "<<it->dis<<" ";
        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;
}
/*6 5
1 2 1
1 3 1
2 4 1
2 5 1
2 6 1*/

 

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