zoukankan      html  css  js  c++  java
  • poj 3159 Candies (差分约束+spfa)

    spfa 的处理 栈比队列快了很多,前几天vongang 说,当时还不信

    这题是见证了,队列 直接 RE ,栈 500Ms

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    #define inf 999999999
    #define V 30005
    #define E 150005
    int n,m;
    int  nxt[E],pnt[E],cost[E];
    int  vis[V], d[V], head[V];
    
    void init()
    {
        int e = 0;
        memset(head, -1, sizeof(head));
        for (int i = 0; i < m; i++)
        {
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
    
            pnt[e] = v; cost[e] = c;
            nxt[e] = head[u]; head[u] = e++;
        }
    }
    
    void spfa()
    {
        for(int i=0;i<=n;i++)
        d[i]=inf,vis[i]=0;
        vis[1] = 1; d[1] = 0;
    
        int p[E];
        p[0] = 1;int top = 1;
        while (top)
        {
            int u = p[--top];
            vis[u] = false;
            for (int i = head[u]; i != -1; i = nxt[i])
            {
                int v=pnt[i];
                if (d[v] > d[u] + cost[i])
                {
                    d[v] = d[u] + cost[i];
                    if (!vis[ v ])
                    {
                        p[top++] = v;
                        vis[v] = true;
                    }
                }
            }
        }
        printf("%d\n", d[n]);
    }
    
    int main()
    {
        scanf("%d%d", &n, &m);
        init();
        spfa();
        return 0;
    }
    Just a little, maybe change the world
  • 相关阅读:
    Java
    oracle与mysql(2)
    oracle与mysql
    junit中的assert方法总结
    java Future用法和意义一句话击破
    Java序列化中的SerialVersionUid
    Nginx了解
    现如今的CDN网站加速技术,细说CDN
    slf4j日志的使用
    IDEA 快捷键整理
  • 原文地址:https://www.cnblogs.com/skyming/p/2476750.html
Copyright © 2011-2022 走看看