利用STL中的vector實現“有向有權圖”的鄰接表表示

【程序代碼】 

/* 利用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
 


【對應上面測試樣例的有向有權圖】
圖片


【運行截圖】
圖片

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章