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
  • 相关阅读:
    Vasya and Endless Credits CodeForces
    Dreamoon and Strings CodeForces
    Online Meeting CodeForces
    数塔取数 基础dp
    1001 数组中和等于K的数对 1090 3个数和为0
    1091 线段的重叠
    51nod 最小周长
    走格子 51nod
    1289 大鱼吃小鱼
    POJ 1979 Red and Black
  • 原文地址:https://www.cnblogs.com/skyming/p/2476750.html
Copyright © 2011-2022 走看看