优先队列优化的dijkstra不是万能的!

今天做天梯赛训练题L2-01

L2-001 紧急救援 (25 分)

作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领你的救援队尽快赶往事发地,同时,一路上召集尽可能多的救援队。

输入格式:

输入第一行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0 ~ (N−1);M是快速道路的条数;S是出发地的城市编号;D是目的地的城市编号。

第二行给出N个正整数,其中第i个数是第i个城市的救援队的数目,数字间以空格分隔。随后的M行中,每行给出一条快速道路的信息,分别是:城市1、城市2、快速道路的长度,中间用空格分开,数字均为整数且不超过500。输入保证救援可行且最优解唯一。

输出格式:

第一行输出最短路径的条数和能够召集的最多的救援队数量。第二行输出从S到D的路径中经过的城市编号。数字间以空格分隔,输出结尾不能有多余空格。

输入样例:

4 5 0 3
20 30 40 10
0 1 1
1 3 2
0 3 3
0 2 2
2 3 2

输出样例

 

这里的数据量很小,自然用不着优先队列,但是今天就是想练一练

于是写了这么一个玩意,只有18分。

 

#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
struct edge
{
	int to;
	int cost;
};
typedef pair<int,int>P;
vector<edge> e[1005];
int ren[1005];
int ren2[1005];
int d[1005];
int pre[1005];
int next2[1005]; 
int n,m,s,dis;
int tiao[1005];
bool visit[1005];
bool operator<(P x,P y)
{
	return x.first>y.first;
}
void dijkstra(int s)
{
	memset(visit,false,sizeof(visit));
	fill(d,d+n,2140000);
	d[s]=0;tiao[s]=1;
	priority_queue<P> que;
	que.push(P(0,s));
	while (!que.empty())
	{
		P p=que.top();que.pop();
		int v=p.second;visit[v]=true;
		if (d[v]<p.first)continue;
		for (int i=0;i<e[v].size();i++)
		{
			edge temp=e[v][i];
			if (d[temp.to]==d[v]+temp.cost)
			{
				tiao[temp.to]+=tiao[v];
				if (ren2[temp.to]<ren2[v]+ren[temp.to])
				{
				    que.push(P(d[temp.to],temp.to));
				    ren2[temp.to]=ren2[v]+ren[temp.to];
				    pre[temp.to]=v;
				}
			}
			if (d[temp.to]>d[v]+temp.cost)
			{
				tiao[temp.to]=tiao[v];
				d[temp.to]=d[v]+temp.cost;
				que.push(P(d[temp.to],temp.to));
				ren2[temp.to]=ren2[v]+ren[temp.to];
				pre[temp.to]=v;
			}
		}
	}
}
int main()
{
	memset(tiao,0,sizeof(tiao));
	scanf("%d%d%d%d",&n,&m,&s,&dis);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&ren[i]);
		ren2[i]=ren[i];
	}
	for (int i=0;i<m;i++)
	{
		edge temp;int start,x1,x2;
		scanf("%d%d%d",&start,&x1,&x2);
		temp.to=x1;temp.cost=x2;
		e[start].push_back(temp);
		temp.to=start;temp.cost=x2;
		e[x1].push_back(temp);
	}
	dijkstra(s);
	//dfs(s,-1,0,0);
	printf("%d %d\n",tiao[dis],ren2[dis]);
	int ttt=dis;
	int j=1;
	next2[0]=ttt;
	while (ttt!=s)
	{	
		ttt=pre[ttt];
		next2[j++]=ttt;
	}
	for (int i=j-1;i>0;i--)
	printf("%d ",next2[i]);
	printf("%d\n",dis);
}

 

 

 

后来老老实实改写成普通的dijkstra,如下:

#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
struct edge
{
	int to;
	int cost;
};
typedef pair<int,int>P;
vector<edge> e[1005];
int ren[1005];
int ren2[1005];
int d[1005];
int pre[1005];
int next2[1005]; 
int n,m,s,dis;
int tiao[1005];
int visit[1005];
bool operator<(P x,P y)
{
	return x.first>y.first;
}
void dijkstra(int s)
{
	fill (d,d+n,2140000000);
	fill (visit,visit+n,0);
	d[s]=0;tiao[s]=1;
	while (true)
	{
		int v=-1;
		for (int u=0;u<n;u++)
		{
			if (!visit[u]&&(v==-1||d[u]<d[v]))
			v=u;
		}
		//printf("%d\n",v);
		if (v==-1)break;
		visit[v]=1;
		for (int u=0;u<e[v].size();u++)
		{
			edge temp=e[v][u];
			if (d[temp.to]==d[v]+temp.cost)
			{
				tiao[temp.to]+=tiao[v];
				if (ren2[temp.to]<ren[temp.to]+ren2[v])
				{
					ren2[temp.to]=ren[temp.to]+ren2[v];
				    pre[temp.to]=v;
				}
			}
			if (d[temp.to]>d[v]+temp.cost)
			{
				d[temp.to]=d[v]+temp.cost;
				ren2[temp.to]=ren[temp.to]+ren2[v];
				pre[temp.to]=v;
				tiao[temp.to]=tiao[v];
			}
		}
	}
}
int main()
{
	memset(tiao,0,sizeof(tiao));
	scanf("%d%d%d%d",&n,&m,&s,&dis);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&ren[i]);
		ren2[i]=ren[i];
	}
	for (int i=0;i<m;i++)
	{
		edge temp;int start,x1,x2;
		scanf("%d%d%d",&start,&x1,&x2);
		temp.to=x1;temp.cost=x2;
		e[start].push_back(temp);
		temp.to=start;temp.cost=x2;
		e[x1].push_back(temp);
	}
	dijkstra(s);
	//dfs(s,-1,0,0);
	printf("%d %d\n",tiao[dis],ren2[dis]);
	int ttt=dis;
	int j=1;
	next2[0]=ttt;
	while (ttt!=s)
	{	
		ttt=pre[ttt];
		next2[j++]=ttt;
	}
	for (int i=j-1;i>0;i--)
	printf("%d ",next2[i]);
	printf("%d\n",dis);
}

得了满分

希望大神告诉我们为什么。我仔细想了想,应该是优先队列会有重复的情况多次出现,之前已经确定的点,可能还是会进入队列里面。而在普通的dijkstra就不会。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值