zoukankan      html  css  js  c++  java
  • Evanyou Blog 彩带

    2019/11/15 更新日志

    发现我的Dijstra优先队列模板有点问题,修改了,并在多处删繁就简,增添详细注释。


    昨天: 图论-概念与记录图的方法

    以上是昨天的Blog,有需要者请先阅读完以上再阅读今天的Blog。


    分割线


    第二天

    引子:昨天我们简单讲了讲图的概念与记录图的方法,那么大家有一定的底子了,我们就开始初步接触图论算法了!

    我们只讲Dijkstra和Floyd,因为其实在比赛中会这两个算法就很好了。

    今天我们要讲的是:最短路径问题

    Top1:最短路的概念

    相信大家都知道有一款Made in China的导航软件——百度导航。那么他们是怎么为我们导航的?就是使用了今天我们要学的问题 最短路径

    说不定你学了之后就可以做一个导航的 是不是有点小激动?

    (重点:最短路问题就是一个点到另一个最短的路径!)


    最短路专业术语:

    中转点: 一个点到另一个点不一定是有直接道路连接的,可能会经过一些别的点,我们就叫那些点叫做 中转点

    松弛: 比如现在从 (I) 点到 (J) 点的边权为 (X) ,而现在有一个点 (K)(K)(I) 的边权为 (Y)(K)(J) 的边权为 (Z)。如果 (Y) + (Z) < (X) ,也就是 ((I) 点到 (J) 点的路径边权) 比 ((K)(J) 的边权) 加上 ((K)(I) 的边权) 还要大,那么显而易见, (I)(J) 的直接路径 (X) 可以由中转点 (K) 降到 (Y + Z),使得 (I)(J) 的最短路径更优。


    Top2:Floyd算法

    现在大家都知道最短路是什么了,那么从简单到复杂,我们先来看看新手必懂的算法。

    Floyd简单粗暴,就是枚举三个点,一个起点,一个终点,一个中转点。看 起点到中转点的路径 加上 中转点到终点的路径 是不是小于 目前起点到终点的路径 即可(就是不断松弛)。

    显而易见,Floyd算法很好懂,就是时间复杂度高了点—— (N^3) 的复杂度。而且,Floyd是多源最短路,询问时只需调用dis就行了。

    所以, 当 N 大于1000时,慎用!

    代码就很简答啦(蒟蒻用邻接矩阵写的):

    //如果为无向图,dis就会对称,Floyd的j就只要到i,且dis[i][j]dis[j][i] 要一起更新 
    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 1000 + 10;
    int n,m;
    int x,y,z;
    int dis[MAXN][MAXN];
    void Floyd(){
    	for(int k = 1;k <= n; k++)
    		for(int i = 1;i <= n; i++)
    			for(int j = 1;j <= n/*i*/; j++)
    				if(dis[i][k] + dis[k][j] < dis[i][j])dis[i][j] = dis[i][k] + dis[k][j];
    	return;
    }
    int main(){
    	cin>>n>>m;
    	for(int i = 1;i <= n; i++)dis[i][i] = 0;
    	for(int i = 1;i <= n; i++)
    		for(int j = 1;j <= n; j++){
    			if(i != j)dis[i][j] = 1e9;
    		}
    	for(int i = 1;i <= m; i++){
    		cin>>x>>y>>z;
    		dis[x][y] = z;
    	}
    	Floyd();
    	for(int i = 1;i <= n; i++){
    		for(int j = 1;j <= n; j++){
    			cout<<dis[i][j]<<" ";
    		}
    		cout<<endl;
    	}
    	return 0;
    }
    

    Top3:Dijkstra算法

    Dijkstra与Floyd相反,是单元最短路,即只能求出一个点到其他所有点的最短路。

    Dijkstra属于贪心的思想,正解:

    首先定义两个种类——黑点和白点,黑点就是在目前算完最短路径的点,白点反之。

    每次在白点中找一个离目前任意一个黑点最近的,加入黑点,更新白点到原点的最短路即可。

    代码如下:注意:这里的dis不再是邻接矩阵,是单源最短路径。tot才是邻接矩阵!

    邻接矩阵,蒟蒻是用洛谷P1828 香甜的黄油 Sweet Butter 作为例题写的模板,体谅一下

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 100 + 10;
    struct Node{
    	int x,y;
    }f[MAXN];
    int n,m,a,b,s,t;
    bool black[MAXN];
    double dis[MAXN];
    double tot[MAXN][MAXN];
    double calc(int i,int j){
    	return sqrt((f[i].x - f[j].x) * (f[i].x - f[j].x) + (f[i].y - f[j].y) * (f[i].y - f[j].y));
    }
    double Dijkstra(int start,int end){
    	for(int i = 1;i <= n; i++){
    		dis[i] = tot[start][i];
    	}
    	dis[start] = 0;
    	black[start] = true;
    	for(int i = 1;i < n; i++){
    		double M = 2e9;
    		int u = start;
    		for(int j = 1;j <= n; j++){
    			if(dis[j] < M && !black[j]){
    				M = dis[j];
    				u = j;
    			}
    		}
    		if(u == start)continue;
    		//此处的判断与前面的u = start对应,若该图存在一个单独的点这里就要加上
    		//否则可以u = 0,这个判断删掉 
    		black[u] = true;
    		for(int j = 1;j <= n; j++){
    			if(black[j])continue;
    			if(dis[u] + tot[u][j] < dis[j]){
    				dis[j] = dis[u] + tot[u][j];
    			}
    		}
    	}
    	return dis[end];
    } 
    int main(){
    	scanf("%d",&n);
    	for(int i = 1;i <= n; i++)
    		for(int j = 1;j <= n; j++){
    			tot[i][j] = i == j ? 0 : 1e9;
    		}
    	for(int i = 1;i <= n; i++){
    		scanf("%d%d",&f[i].x,&f[i].y);
    	}
    	scanf("%d",&m);
    	for(int i = 1;i <= m; i++){
    		scanf("%d%d",&a,&b);
    		tot[a][b] = calc(a,b);
    		tot[b][a] = tot[a][b];
    	}
    	scanf("%d%d",&s,&t);
    	printf("%.2f",Dijkstra(s,t));
    	return 0;
    }
    
    

    所以,Dijkstra的时间复杂度是 (N^2)

    怎么优化呢?很简单——在寻找离黑点最近的白点时,使用优先队列即可。

    但是这里要注意的是,我们朴素的使用单调队列维护,在洛谷 P4779 【模板】单源最短路径 中会TLE。

    为什么呢?

    我们的优先队列不想 set 等 STL ,没有自动去重的功能。所以当队列中有多个相同的元素时,Dijkstra的效率会大大减少。

    所以 ,我们需要一个bool型的数组,不难想到,该数组用来记录 每个元素当前在队列中是否存在。

    细节又来了。 我们bool型数组的定义是 每个元素当前在队列中是否存在。 那么我们每次在优先队列弹出队首进行操作时,我们需要 将队首的bool标记取消。

    一个元素可以多次单独出现在队列,但是不能一次多个出现在队列。

    虽然多次拓展到队首,但是队首到源点的最短路径 可能 更新了。所以我们不妨再次从队首再次进行拓展,更新周围点的答案。 这就是我们为什么要将队首的bool标记取消。

    优先队列Dijkstra(机房大佬说我这是SPFA,无解):

    #include<bits/stdc++.h>
    #include<cctype>
    #pragma GCC optimize(2)
    
    #define in(a) a = read()
    #define out(a) write(a),printf(" ")
    #define outn(a) write(a),putchar('
    ')
    
    #define ll long long
    #define rg register
    #define New int
    
    using namespace std;
    
    namespace IO_Optimization{
    
    	inline New read()
    	{
    	    New X = 0,w = 0;
    		char ch = 0;
    
    		while(!isdigit(ch))
    		{
    			w |= ch == '-';
    			ch=getchar();
    		}
    	    while(isdigit(ch))
    		{
    			X = (X << 3) + (X << 1) + (ch ^ 48);
    			ch = getchar();
    		}
    	    return w ? -X : X;
    	}
    
    	inline void write(New x)
    	{
    	     if(x < 0) putchar('-'),x = -x;
    	     if(x > 9) write(x/10);
    	     putchar(x % 10 + '0');
    	}
    
    	#undef New
    }
    using namespace IO_Optimization;
    
    const int MAXN = 1000000 + 2;
    
    int n,m,s,x,y,z,len,p;
    int dis[MAXN],nxt,val;
    struct Node
    {
    	int num,dist;
    	inline bool operator <(const Node &nnxt)const{
    		return dist > nnxt.dist;
    	}
    };
    vector<Node> nei[MAXN];
    bool vis[MAXN];
    
    inline void Dijkstra(int start)
    {
    	memset(dis,0x3f3f3f3f,sizeof(dis));
    	memset(vis,false,sizeof(vis));
    	priority_queue<Node>q;
    	Node cur = {start,0};
    
    	q.push(cur);
    	dis[start] = 0;
    	vis[start] = true;
    
    	while(!q.empty())
    	{
    		cur = q.top();
    		q.pop();
    		p = cur.num;
    		vis[p] = false;
    		len = nei[p].size();
    
    		for(rg int i = 0;i < len; ++i)
    		{
    			nxt = nei[p][i].num;
    			val = nei[p][i].dist;
    
    			if(dis[nxt] > dis[p] + val)
    			{
    				dis[nxt] = dis[p] + val;
    				if(!vis[nxt])
    				{
    					Node tmp = {nxt,dis[nxt]};
    					q.push(tmp);
    					vis[nxt] = true;
    				}
    			}
    		}
    	}
    	return;
    }
    
    int main()
    {
    	in(n),in(m),in(s);
    	for(rg int i = 1;i <= m; ++i)
    	{
    		in(x),in(y),in(z);
    		nei[x].push_back((Node){y,z});
    	}
    
    	Dijkstra(s);
    	
    	for(rg int i = 1;i <= n; ++i)
    		out(dis[i] == 0x3f3f3f3f ? 2147483647 : dis[i]);
    
    	return 0;
    }
    
    

    顺便带一下SPFA的算法模板和用动态数组记录的Dijkstra(这里不做详解了,有需要的人可以复制看一下)

    SPFA:

    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<cstring> 
    using namespace std;
    int n, p, c, cow[801], a, b, d, cnt = 0, sum = 0, ans = 2147483647;
    int dis[10000], w[10000], next[10000], to[10000], first[10000] = {0};
    bool exist[10000] = {false};
    queue<int> q;
    
    void addEdge(int u, int v, int weight)
    {
    	cnt++; //边的编号 
    	to[cnt] = v; //第cnt条边指向点v 
    	w[cnt] = weight; //第cnt条边的权值 
    	next[cnt] = first[u]; // 第cnt条边指向连接点u的第一条边 
    	first[u] = cnt; //将连接点u的第一条边更新为第cnt条边
    	return; 
    } 
    
    void spfa(int start)
    {
    	memset(exist, false, sizeof(exist)); //一开始所有点在队列外 
    	memset(dis, 0x7f, sizeof(dis)); //将所有点到起始点的距离置为极大值 
    	dis[start] = 0; 
    	q.push(start); //起始点入队列 
    	exist[start] = true; 
    	while(!q.empty())
    	{
    		int head = q.front(); //取队列的第一个点 
    		q.pop();
    		exist[head] = false;
    		for(int e = first[head]; e != 0; e = next[e]) //循环head连接的每一条边 
    		{
    			//松弛操作 
    			if(dis[head] + w[e] < dis[to[e]])
    			{
    				dis[to[e]] = dis[head] + w[e];
    				if(exist[to[e]] == false)
    				{
    					q.push(to[e]); //将被更新的点入队列 
    					exist[to[e]] = true;
    				}
    			}
    		}
    	}
    	return;
    }
    
    int main()
    {
    	cin >> n >> p >> c;
    	for(int i=1; i <= n; i++) //输入每头牛所在的位置 
    	{
    		cin >> cow[i];
    	}
    	for(int e=1; e <= c; e++) //输入每一条边 
    	{
    		cin >> a >> b >> d;
    		addEdge(a, b, d);
    		addEdge(b, a, d);
    	}
    	for(int i=1; i <= p; i++) //注意是循环牧场
    	{
    		spfa(i);
    		sum = 0;
    		for(int j=1; j <= n; j++)
    		{
    			sum = sum + dis[cow[j]];
    		}
    		ans = min(ans, sum);
    	}
    	cout << ans;
    	return 0;
    }
    
    
    

    好了,第二天就到这里,是不是都听懂了呢狗屁?欢迎在下方留言!

  • 相关阅读:
    51nod 1174 区间最大值(RMQ and 线段树)
    Round #447(Div 2)
    51nod 2006 飞行员匹配
    75.Java异常处理机制throws
    74.Java异常处理机制
    emmm
    数据库关系代数
    汇编实验二 2进制转16进制
    汇编实验一 显示字符串
    JustOj 1386: 众数的数量
  • 原文地址:https://www.cnblogs.com/CJYBlog/p/short-path.html
Copyright © 2011-2022 走看看