spfa中链式向前星数组模拟邻接表

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  
  
#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000
#define clean(a,b) memset(a,b,sizeof(a))// 水印 

/*
向前星:
1.把边的起点终点,全职存起来,然后以起点 从小到大(从大到小)排序
2. 记录每个定点在数组中的起始位置和长度

链式向前星;
数组模拟链表实现向前星的功能
 构造:
 1.读入一条边i的信息
 2.边的终点和权值存入edge【i】中
 3.next=headlist【a】,headlist【a】=i。
 构造完成 
 */ 
 struct Edge{
	int t;//边的终点
	int next;//当前下一条边的编号
	int w;//边的权值 
 } edge[maxedge]; 
int headlise[maxedge];
//以每一个点为起点的链表,最终起点就是元素符号 ,
//其中第一个元素为末尾元素,然后依次找上级 
int n,m;
void show_graph()
{
	int i,k;
	for(int i=1;i<=n;++i)
	{
		for(int k=headlist[i];k!=-1;k=edge[k].next)
		{
			cout<<i<<"-->"<<edge[k].t<<"=="<<edge[k].w<<endl;
		}
	}
}
int main()
{
	int i,a,b,c;
	while(cin>>n>>m)
	{
		for(int i=1;i<=n;++i)
		{
			headlist[i]=-1;
		}
		for(int i=1;i<=n;++i)
		{
			cin>>a>>b>>c;
			edge[i].t=b;
			edge[i].w=c;
			edge[i].next=headlist[a];
			//索引 :节点i后一条边编号为headlist【a】; 
			headlist[a]=i;
		}
		show_graph();
	}
	return 0;
}
/*
input:
4 5 
1 4 5
1 2 4
2 3 3
3 1 8
3 4 7

output:
1-->2==4
1-->4==5
2-->3==3
3-->4==7
3-->1==8 
 
*/

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81135358
今日推荐