poj 3159 差分约束

思路:班长的糖果要比snoopy的多。并且要用手写堆栈,且堆栈的大小要开到20000000.

#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<cmath>

#define inf 1000000000

#define Maxn 30110

#define Maxm 160000

using namespace std;

int index[Maxn],dis[Maxn],vi[Maxn],e,n,Que[20000100];

struct Edge{

    int to,next,val;

}edge[Maxm];

void init()

{

    memset(vi,0,sizeof(vi));

    memset(index,-1,sizeof(index));

    for(int i=0;i<Maxn;i++)

        dis[i]=inf;

    e=0;

}

void addedge(int from,int to,int val)

{

    edge[e].to=to;

    edge[e].val=val;

    edge[e].next=index[from];

    index[from]=e++;

}

int spfa(int u)

{

    int i,j,temp,now,head;

    head=0;

    dis[u]=0;

    Que[head++]=u;

    while(head)

    {

        temp=Que[--head];

        vi[temp]=0;

        for(i=index[temp];i!=-1;i=edge[i].next)

        {

            now=edge[i].to;

            if(dis[temp]+edge[i].val<dis[now])

            {

                dis[now]=dis[temp]+edge[i].val;

                if(!vi[now])

                    Que[head++]=now;

                vi[now]=1;

            }

        }

    }

    return dis[n];

}

int main()

{

    int m,i,j,a,b,c;

    scanf("%d%d",&n,&m);

    init();

    for(i=1;i<=m;i++)

    {

        scanf("%d%d%d",&a,&b,&c);

        addedge(a,b,c);

    }

    int ans;

    ans=spfa(1);

    printf("%d\n",ans);

    return 0;

}

 

你可能感兴趣的:(差分约束)