zoukankan      html  css  js  c++  java
  • 链式前向星(邻接表)

    #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
    */
  • 相关阅读:
    pytorch 文本输入处理
    理解 on-policy 和 off-policy
    Monte Carlo与TD算法
    Monte Calro Tree Search (MCTS)
    强化学习概览
    linux 服务器 keras 深度学习环境搭建
    sed和awk学习整理
    linux shell编程进阶学习(转)
    gdb调试:
    MySQL C API 访问 MySQL 示例
  • 原文地址:https://www.cnblogs.com/SeanOcean/p/10376515.html
Copyright © 2011-2022 走看看