图论 邻接表实现 动态数组实现

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct edge
{
      int to,cost;
};
vector<edge> G[10000];
int main()
{
      int v,e;
      while(cin>>v>>e)
      {
            int i,j;
            for(i=0;i<e;i++)
           {
                      int st;
                      edge es;
                      cin>>st>>es.to>>es.cost;
                      G[st].push_back(es);
           }
           for(i=0;i<v;i++)
          {
                for(j=0;j<G[i].size();j++)
                {
                               cout<<G[i][j].to<<" ";
                }
                cout<<endl;
           }
           for(i=0;i<v;i++)
          {
                      for(j=0;j<G[i].size();j++)
                     {
                            cout<<G[i][j].cost<<" ";
                     }
                      cout<<endl;
            }
      }
      return 0;
}

posted @ 2016-07-22 09:29  TWhh  阅读(191)  评论(0编辑  收藏  举报