• Dijkstra 链式前向星存图 模板


    Dijkstra 链式前向星存图

    Luogu P4779

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    using namespace std;
    typedef long long int ll;
    const int maxn = 10000005;
    
    ll n, m, s, tot, x;
    ll dis[maxn], head[maxn];
    bool vis[maxn];
    priority_queue <pair <ll, ll> > q;
    
    struct node
    {
    	ll nxt, to, w;
    }t[maxn];
    
    void add (const int u,const int v,const int w)
    {
    	t[++tot].to = v;
    	t[tot].w = w;
    	t[tot].nxt = head[u];
    	head[u] = tot;
    }
    
    void dijkstra()
    {
    	memset (dis, 0x3f3f3f3f, sizeof (dis));  	//初始边无限大
    	memset (vis, 0, sizeof (vis));		//结点初始均为访问 
    	dis[s] = 0;	//起点到自己距离为0 
    	q.push (make_pair (0, s));	//起点进队 
    	while (q.size() != 0)
    	{
    		x = q.top().second;
    		q.pop();	//初始结点入队  
    		if (vis[x])  
    			continue;	//如果走过,直接跳过 
    		vis[x] = 1;	//标记已访问 
    		for (ll i = head[x]; i!=-1; i = t[i].nxt)
    		{
    			ll y = t[i].to, z = t[i].w;
    			if (dis[y] > dis[x] + z)
    			{
    				dis[y] = dis[x] + z;	//更新起点到y最短路 
    				q.push (make_pair (-dis[y], y));	//d[y]相反数入队,转小根队
    			}
    		}
    	}
    }
    
    int main()
    {
    	int t;
    	scanf ("%lld %lld %lld %d", &n, &m, &s, &t);
    	for(int i=1;i<=n;++i)
    	head[i]=-1;
    	for (int i = 1; i <= m; i++)
    	{
    		ll a, b, c;
    		scanf ("%lld %lld %lld", &a, &b, &c);
    		add (a, b, c);
    		add (b, a, c);
    	}
    	dijkstra();
    //	for (int i = 1; i <= n; i++)
    //	{
    //		printf ("%lld
    ", dis[i]);
    //	}
    	printf("%lld",dis[t]);
    	return 0;
    }
    
  • 相关阅读:
    1.数据结构《Pytorch神经网络高效入门教程》Deeplizard
    plt.figure()的使用,plt.plot(),plt.subplot(),plt.subplots()和图中图
    可变长参数
    np.linspace,numpy中的linspace()
    python和numpy中sum()函数的异同
    maven install 错误
    spring boot启动后执行方法
    java 定时任务多线程处理
    java 生成txt文件
    java 方法超时
  • 原文地址:https://www.cnblogs.com/EdisonBa/p/13667780.html
Copyright © 2020-2023  润新知