关于 最短路 的堆优化的迪杰斯科拉 算法

首先 

是 优化的迪杰斯科啦

是 通过 一个叫优先队列 的 东西 可以将原来的 n*n的复杂度 变成 n*2 左右的复杂度

优先队列 是一种数据结构 行为和队列相似 但不是先进先出的队列 ,先当与在排队 ,允许情况的紧急程度进行插队排队

 

 

头文件是 <queue>

先介绍队列

 


定义queue对象的示例代码如下:
queue<int>q1;
queue<double>q2;
queue的基本操作有:
1.入队:如q.push(x):将x元素接到队列的末端;
2.出队:如q.pop() 弹出队列的第一个元素,并不会返回元素的值;
3,访问队首元素:如q.front()
4,访问队尾元素,如q.back();
5,访问队中的元素个数,如q.size();

要用 priority_queue<int >q  来定义

在<queue>头文件中,还定义了一个非常有用的模版类priority_queue(优先队列),优先队列与队列的差别在于优先队列不是按照入队的顺序出队,而是按照队列中元素的优先权顺序出队(默认为大者优先,也可以通过指定算子来指定自己的优先顺序)。
priority_queue模版类有三个模版参数,元素类型,容器类型,比较算子。其中后两个都可以省略,默认容器为vector,默认算子为less,即小的往前排,大的往后排(出队时序列尾的元素出队)。
定义priority_queue对象的示例代码如下:
priority_queue<int >q1;
priority_queue<pair<int,int> >q2;
priority_queue<int,vector<int>,greater<int> >q3;//定义小的先出队
priority_queue的基本操作均与queue相同

但优先队列中 出队列是用的

top()

下面是俩个关于 优先队列 使用的题目和 代码

1

最短路

在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?
 

Input

输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。

Output

对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间

Sample Input

2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0

Sample Output

3
2

 

这个题就是单纯的板子题目 可以通过任意方法通过 这里 只介绍 堆优化的 迪杰斯科拉

#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=0x3f3f3f;
const int maxn=1e6+7;
typedef long long ll;
struct Node{
	int to,next,len;
}node[maxn];
int tot=0;
int head[maxn];
void addeage(int u,int v,int w)
{
	node[tot].to=v;   
	node[tot].next=head[u];  
	node[tot].len=w; 
	head[u]=tot++;
}
int dis[maxn];
int n,m,u,v,w;
struct PII{
	int id;
	int lens;
};
bool operator<(PII a,PII b)
{
	return a.lens>b.lens;
}
void pop(int n)
{
	memset(head,-1,sizeof(head[0])*n+1);
	tot=0;
}
int diji(int n)
{
	memset(dis,inf,sizeof(dis));
	dis[n]=0;
	priority_queue<PII> q;
    q.push(PII{n,0});
    while(!q.empty())
    {
        PII k=q.top(); q.pop();
        if(k.lens!=dis[k.id])	continue;
        for(int i=head[k.id];i!=-1;i=node[i].next)
        {
            int t=node[i].to;
            if(dis[t]>dis[k.id]+node[i].len)
            {
                dis[t]=dis[k.id]+node[i].len;
                q.push(PII{t,dis[t]});
            }
        }
    }
	
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		if(!n&&!m)	break;
		pop(n);
		while(m--)
		{
			scanf("%d%d%d",&u,&v,&w);
			addeage(u,v,w);
			addeage(v,u,w);
		}
		diji(1);
		printf("%d\n",dis[n]);
		}
	return 0; 
}

代码用的是c语言写的 一些函数和队列 用的c++

首先这题目 的双向路 所以在定义时要进行 两次 两方面定义

前面是用链式前向星 输入的相当于邻接 表 防止了 指针 出现混乱和 开molloc的时间问题 不过 需要变通一下

如一个 板子题 一个人的旅行 你就要知道其中的点的最大值都在进行 建表

代码 重点是 这里

struct PII{
	int id;
	int lens;
};
bool operator<(PII a,PII b)
{
	return a.lens>b.lens;
}
void pop(int n)
{
	memset(head,-1,sizeof(head[0])*n+1);
	tot=0;
}
int diji(int n)
{
	memset(dis,inf,sizeof(dis));
	dis[n]=0;
	priority_queue<PII> q;
    q.push(PII{n,0});
    while(!q.empty())
    {
        PII k=q.top(); q.pop();
        if(k.lens!=dis[k.id])	continue;
        for(int i=head[k.id];i!=-1;i=node[i].next)
        {
            int t=node[i].to;
            if(dis[t]>dis[k.id]+node[i].len)
            {
                dis[t]=dis[k.id]+node[i].len;
                q.push(PII{t,dis[t]});
            }
        }
    }
	
}

 

中间是对head初始化 可以不看

这个地方

struct PII{
	int id;
	int lens;
};
bool operator<(PII a,PII b)
{
	return a.lens>b.lens;
}

是进队列时候大的在后面

小的在前面

最后再加上另外一题

Invitation Cards

In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.

The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan.

All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.

Input

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.

Output

For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.

Sample Input

2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50

Sample Output

46
210

 

#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=0x3f3f3f;
const int maxn=1e7+7;
typedef long long ll;
struct Node{
	int to,next,len;
}node[maxn];
int tot=0;
int head[maxn];
void addeage(int u,int v,int w)
{
	node[tot].to=v;   //
	node[tot].next=head[u];  // 
	node[tot].len=w; // 
	head[u]=tot++;
}
int dis[maxn];
bool vis[maxn];
struct PII{
	int id;
	int lens;
};
bool operator<(PII a,PII b)
{
	return a.lens>b.lens;
}
int diji(int n)
{
	memset(dis,inf,sizeof(dis));
	dis[n]=0;
	priority_queue<PII> q;
    q.push(PII{n,0});
    while(!q.empty())
    {
        PII k=q.top(); q.pop();
        if(k.lens!=dis[k.id])	continue;
        for(int i=head[k.id];i!=-1;i=node[i].next)
        {
            int t=node[i].to;
            if(dis[t]>dis[k.id]+node[i].len)
            {
                dis[t]=dis[k.id]+node[i].len;
                q.push(PII{t,dis[t]});
            }
        }
    }
	
}
int n,m,u[1000005],v[1000005],w[1000005],t;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
	scanf("%d%d",&n,&m);
	memset(head,-1,sizeof(head));
	for(int i=0;i<m;i++)
	{
		scanf("%d%d%d",&u[i],&v[i],&w[i]);
		addeage(u[i],v[i],w[i]);
	}
		ll sum1=0,sum2=0;
	diji(1);
    	for(int i=1;i<=n;i++)
    		sum1+=dis[i];
	memset(head,-1,sizeof(head));
    for(int i=0;i<m;i++)
    	addeage(v[i],u[i],w[i]);
    diji(1);
    	for(int i=1;i<=n;i++)
    		sum2+=dis[i];
	printf("%lld\n",sum1+sum2); 
	}
	return 0;
} 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值