【程序代码】
/* 利用STL中的vector实现“有向有权图”的邻接表表示 */
#include <bits/stdc++.h>
using namespace std;
const int N=1e5;
struct edge {
int to,cost; //to有向边入点序号,cost有向边权值
};
vector<edge> v[N];
int main() {
int n,m;
cin>>n>>m; //n点数,m边数
int s; //s有向边起始点序号
for(int i=0; i<m; i++) {
edge e;
cin>>s>>e.to>>e.cost;
v[s].push_back(e);
}
for(int i=0; i<n; i++) {
for(int j=0; j<v[i].size(); j++)
//cout<<i<<" "<<v[i][j].to<<" "<<v[i][j].cost<<endl;
cout<<"V"<<i+1<<"->"<<"V"<<v[i][j].to+1<<":"<<v[i][j].cost<<endl;
}
return 0;
}
【有向有权图的测试样例】
5 6
0 1 7
0 3 5
1 2 12
2 3 18
1 4 36
2 4 51
【对应上面测试样例的有向有权图】
【运行截图】