【Dijkstra】【Floyd】-HDU-2544-最水题演示

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544

题目描述:最裸最短路题,拿来做实验。输入节点数n和路线数m,求最短路


dijkstra+邻接矩阵+优先队列版:

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;

const int INF = 0x3f3f3f3f;
typedef pair <int,int> pii;

int cost[120][120],dis[120],vis[120];
int n,m;

void dijkstra()
{
	memset(dis,INF,sizeof(dis));
	memset(vis,0,sizeof(vis));
	priority_queue <pii,vector<pii>,greater<pii> > q;
	
	int from=1,to;
	dis[from]=0;
	q.push(make_pair(dis[from],from));
	while(!q.empty())
	{
		pii t=q.top();q.pop();
		from=t.second;
		if(vis[from])continue;
		vis[from]=1;
		for(to=1;to<=n;to++)
		{
			if(!vis[to]&&dis[from]+cost[from][to]<dis[to]&&cost[from][to]<INF)
			{
				dis[to]=dis[from]+cost[from][to];
				q.push(make_pair(dis[to],to));
			}
		}
	}
}

int main()
{
	freopen("input.txt","r",stdin);
	int from,to,w;
	while(cin>>n>>m,n||m)
	{
		memset(cost,INF,sizeof(cost));
		while(m--)
		{
			cin>>from>>to>>w;
			cost[from][to]=cost[to][from]=min(cost[from][to],w);
		}
		dijkstra();
		cout<<dis[n]<<endl;
	}
	return 0;
}


dijkstra+邻接表+优先队列版:

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 120;

typedef pair <int,int> pii;

struct edge
{
	int end;
	int value;
	edge(int a,int b){end=a;value=b;}
};

int dis[maxn],vis[maxn];
int n,m;
vector<edge> eg[maxn];

int dijkstra(int S,int T)
{
	memset(dis,INF,sizeof(dis));
	memset(vis,0,sizeof(vis));
	priority_queue <pii,vector<pii>,greater<pii> > q;
	
	int from=S,to,i;
	dis[from]=0;
	q.push(make_pair(dis[from],from));

	while(!q.empty())
	{
		pii t=q.top();q.pop();
		from=t.second;
		if(vis[from])continue;
		vis[from]=1;
		for(i=0;i<eg[from].size();i++)
		{
			to=eg[from][i].end;
			if(!vis[to]&&dis[from]+eg[from][i].value<dis[to])
			{
				dis[to]=dis[from]+eg[from][i].value;	
				q.push(make_pair(dis[to],to));
			}
		}
	}
	return dis[T];
}


void add_edge(int from,int to,int w)
{
	int flag=0,i,j;
	for(i=0;i<eg[from].size();i++)
	{
		flag=0;
		if(eg[from][i].end==to)
		{
			eg[from][i].value=min(eg[from][i].value,w);flag=1;break;
		}
	}
	if(!flag)
	{
		eg[from].push_back(edge(to,w));
	}

	for(i=0;i<eg[to].size();i++)
	{
		flag=0;
		if(eg[to][i].end==from)
		{
			eg[to][i].value=min(eg[to][i].value,w);flag=1;break;
		}
	}
	if(!flag)
	{
		eg[to].push_back(edge(from,w));
	}
}

void edge_test()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<eg[i].size();j++)
			cout<<i<<"-->"<<eg[i][j].end<<"need"<<eg[i][j].value<<endl;
	}
	cout<<endl;
}

int main()
{
	freopen("input.txt","r",stdin);
	int i,j,from,to,w;
	while(cin>>n>>m,n||m)
	{
		for(i=0;i<maxn;i++)eg[i].clear();
		while(m--)
		{
			cin>>from>>to>>w;
			add_edge(from,to,w);
		}
		//edge_test();
		int ans=dijkstra(1,n);
		cout<<ans<<endl;
	}
	return 0;
}






Floyd:

#include <iostream>
#include <cstdio>
#include <cstring>

const int INF=0x3f3f3f3f;
using namespace std;

int n,m;
int  d[120][120];

int main()
{
	int from,to,w,i,j,k;
	freopen("input.txt","r",stdin);
	while(cin>>n>>m,n||m)
	{
		memset(d,INF,sizeof(d));
		while(m--)
		{
			cin>>from>>to>>w;
			d[from][to]=d[to][from]=min(d[from][to],w);
		}	
		for(k=1;k<=n;k++)
			for(i=1;i<=n;i++)
				for(j=1;j<=n;j++)
					d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
		cout<<d[1][n]<<endl;
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值