数据结构 图

 太懒了 不想写注释。。就这叭

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#define ll long long
#define N 0x3f3f3f3f
using namespace std;
map<string,int>mp;
map<int,string>mpp;
int r[100][100];
int n;
int z;
int head[100];
int vis[100];
struct ac
{
	int u,v,w,next;
}rr[100];
int add(int u,int v,int w)
{
	rr[z].u=u;
	rr[z].v=v;
	rr[z].w=w;
	rr[z].next=head[u];
	head[u]=z++;
	
	rr[z].u=v;
	rr[z].v=u;
	rr[z].w=w;
	rr[z].next=head[v];
	head[v]=z++;
}
int show()
{
	cout<<"**********************************"<<endl;
	cout<<"***************无向图*************"<<endl;
	cout<<"**********************************"<<endl;
	cout<<"********** 1.邻接矩阵构图  *******"<<endl;
	cout<<"********** 2.邻接表构图    *******"<<endl;
	cout<<"*********  3.输出邻接矩阵  *******"<<endl;
	cout<<"*********  4.输出邻接表    *******"<<endl;
	cout<<"*********  5.深度优先遍历  *******"<<endl;
	cout<<"*********  6.广度优先遍历  *******"<<endl;
	cout<<"*********  7.Dijkstra算法  *******"<<endl;
	cout<<"*********  8.退出          *******"<<endl;
	cout<<"**********************************"<<endl;
}
int Dfs(int x)
{
	vis[x]=1;
	cout<<mpp[x]<<" ";
	for(int i=x;i<=n;i++)
	{
		if(!vis[i]&&r[x][i]!=N)
		Dfs(i);
	}
}
int main()
{
	int flag=0;
	show();
	while(1)
	{
	int T;
	cin>>T;
	
	if(T==1)
	{
		cout<<"输入一共有多少个点"<<endl;
		cin>>n;
		int h=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			r[i][j]=N;
		}
		for(int i=1;i<=n;i++)
		{
			string s,ss;
			cin>>s;
			if(!mp[s])
			mp[s]=++h,mpp[h]=s;
		}
		cout<<"输入一共有多少个边"<<endl;
		int m;
		cin>>m;
		cout<<"m对边对应的顶点及其权值"<<endl; 
		for(int i=1;i<=m;i++)
		{
			string s,ss;
			cin>>s>>ss;
			int cost;
			cin>>cost;
			r[mp[ss]][mp[s]]=r[mp[s]][mp[ss]]=cost;
		}
		cout<<"操作成功"<<endl;
	}
	
	if(T==2)
	{
		flag=1;
		z=0;
		memset(head,-1,sizeof(head));
		cout<<"输入一共有多少个点"<<endl;
		cin>>n;
		int h=0;
		for(int i=1;i<=n;i++)
		{
		string s,ss;
		cin>>s;
		if(!mp[s])
		mp[s]=++h,mpp[h]=s;
		}
		int m;
		cout<<"m对边对应的顶点及其权值"<<endl; 
		cin>>m;
		for(int i=0;i<m;i++)
		{
			string u,v;
			int w;
			cin>>u>>v>>w;
			add(mp[u],mp[v],w);
		}
		cout<<endl;
		cout<<"操作成功"<<endl;
	}
	
	if(T==3)
	{
		cout<<"x表示不存在"<<endl; 
		if(n==0)
		{
		cout<<"输入错误"<<endl;
		continue ;
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				if(r[i][j]==N)
				cout<<"x"<<" ";
				else
				cout<<r[i][j]<<" ";
			}
			cout<<endl;
		}
		cout<<endl;
		cout<<"操作成功"<<endl;
	} 
	
	if(T==4)
	{
		if(!flag)
		{
			cout<<"输入错误"<<endl;
			continue ;
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=head[i];j+1;j=rr[j].next)
			{
				cout<<mpp[rr[j].u]<<" "<<mpp[rr[j].v]<<" ";
				cout<<rr[j].w<<endl;
			}
			
		}
		cout<<endl;
		cout<<"操作成功"<<endl;
	}
	
	if(T==5)
	{
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=n;i++)
		{
			if(!vis[i])
			{
				Dfs(i);
			}
		}
		cout<<endl;
		cout<<"操作成功"<<endl;
	}
	
	if(T==6)
	{
		if(n==0)
		{
			cout<<"输入错误"<<endl;
			continue ;
		}
		memset(vis,0,sizeof(vis));
		queue<int>q;
		while(!q.empty()) 
		q.pop();
		for(int i=1;i<=n;i++)
		{
			if(!vis[i])
			{
				vis[i]=1;
				cout<<mpp[i]<<" ";
				q.push(i);
				while(!q.empty())
				{
					int u=q.front();
					q.pop();
					for(int j=u;j<=n;j++)
					{
						if(!vis[j]&&r[u][j]!=N)
						{
							vis[j]=1;
							cout<<mpp[j]<<" ";
							q.push(j);
						}
					}
				}
			}
		}
		cout<<endl;
		cout<<"操作成功"<<endl;
	}
	
	if(T==7)
	{
		int s[100];
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=n;i++)
		{
			s[i]=N;
		}
		s[1]=0;
		for(int i=1;i<=n;i++)
		{
			int minn=N;
			int u=-1;
			for(int j=1;j<=n;j++)
			{
				if(!vis[j]&&minn>s[j])
				{
					minn=s[j];
					u=j;
				}
			}
			vis[u]=1;
			for(int j=1;j<=n;j++)
			{
				if(!vis[j]&&r[u][j]+s[u]<s[j])
				{
					s[j]=r[u][j]+s[u];
				} 
			}
		}
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			ans+=s[i];
		}
		cout<<ans<<endl;
	}
	
	if(T==8)
	{
		break;
	}
	
	}
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值