动态数组实现邻接表建图

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
struct edge//存储边连接的点和改边的权值 
{
	int to,w;
};
vector<edge>map[10001];//二维动态数组实现 
int n,m;
int x,y,z,k;
int main ()
{
	edge e;
	cin>>n>>m;
	for (int i=1;i<=m;i++)
	{
		cin>>x>>y>>z;
		e.to=y;
		e.w=z;
		map[x].push_back(e);
	}
	for (int i=1;i<=n;i++)//从每个点遍历 
		for (vector<edge>::iterator k=map[i].begin();k!=map[i].end();k++)
		{
			edge t=*k;
			cout<<i<<' '<<t.to<<' '<<t.w<<endl;
		}
	return 0;
}


posted @ 2016-11-16 10:41  2000xyy  阅读(174)  评论(0编辑  收藏  举报