dijkstra50求助
  • 板块P2009 跑步
  • 楼主Lips
  • 当前回复7
  • 已保存回复7
  • 发布时间2020/5/3 09:53
  • 上次更新2023/11/7 03:18:28
查看原帖
dijkstra50求助
342090
Lips楼主2020/5/3 09:53
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<iostream>
using namespace std;
const int MAXN=30;
map<char,int>MP;
int n,m,d[MAXN];
struct edge
{
	int to,cost;
	edge(int to,int cost):to(to),cost(cost) {}
};
vector<edge>G[MAXN];
typedef pair<int,int>P;
void init()
{
	for(register int i='A',j=1;j<=26;i++,j++) MP[i]=j;
}
void dijkstra(int s)
{
	priority_queue<P,vector<P>,greater<P> >q;
	for(register int i=1;i<=n;i++) d[i]=1e9;
	d[s]=0;
	q.push(make_pair(0,s));
	while(!q.empty())
	{
		P p=q.top();
		q.pop();
		int v=p.second;
		if(d[v]<p.first) continue;
		for(register int i=0;i<G[v].size();i++)
		{
			edge e=G[v][i];
			if(d[e.to]>d[v]+e.cost)
			{
				d[e.to]=d[v]+e.cost;
				q.push(make_pair(d[e.to],e.to));
			}
		}
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	init();
	for(register int u=1;u<=n;u++)
	{
		int w,v=u+1;
		scanf("%d",&w);
		if(v>n) v=1;
		G[u].push_back(edge(v,w));
		G[v].push_back(edge(u,w));
	}
	while(m--)
	{
		char a,b;
		int w;
		cin>>a>>b>>w;
		int u=MP[a],v=MP[b];
		G[u].push_back(edge(v,w));
		G[v].push_back(edge(u,w));
	}
	char u,v;
	cin>>u>>v;
	dijkstra(MP[u]);
	printf("%d\n",d[MP[v]]);
	return 0;
}
2020/5/3 09:53
加载中...