图的存储

#include<iostream>
#include<vector>
#define MAX_N 100
using namespace std;
struct edge{
	int to;
	int cost;
};
vector<edge>G[MAX_N];
int main()
{
	int n,m;//n个点,m条边 
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		edge E;
		int s,t,w;
		cin>>s>>t>>w;
		E.to=t;
		E.cost=w;
		G[s].push_back(E);
		
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<G[i].size();j++)
		{
			cout<<i<<" "<<G[i][j].to<<" "<<G[i][j].cost<<endl;
		}
	}
	return 0;
 } 
 /*//测试数据
3 4
0 1 1
0 2 2
1 2 3
0 2 4

*/

我不管,劳资就是会存图了

struct edge{
	int to;
	int cost;
};
vector<edge>G[MAX_N];

猜你喜欢

转载自blog.csdn.net/qq_41660465/article/details/81171449