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;
}
posted @ 2012-04-30 10:06  skyming  阅读(393)  评论(0编辑  收藏  举报