hdu2544(spfa,个人早期代码)

<!-- lang: cpp -->
#include<iostream>
using namespace std;
struct list
{
    int num;
    list *next;
    list(){next=NULL;}
};
list *h,*e,*p;
int w[101][101],d[101],n,m;
void sou(int u)
{
    int i;
    for(i=1;i<=n;i++)
    {
        if(d[u]+w[u][i]<d[i])
        {
            d[i]=d[u]+w[u][i];
            p->next=new list;
            p=p->next;
            p->num=i;
        }
    }
    e=h->next;
    delete h;
    h=e;
    if(h==NULL)
        return ;
    sou(h->num);
}
int main()
{
    int i,j,a,b,c;
    while(cin>>n>>m)
    {
        if(n==0&&m==0)
            break;
        for(i=0;i<=n;i++)
            for(j=0;j<=n;j++)
                w[i][j]=0xfffffff;
        for(i=0;i<m;i++)
        {
            cin>>a>>b>>c;
            if(c<w[a][b])
                w[a][b]=w[b][a]=c;
        }
        d[1]=0;
        for(i=2;i<=n;i++)
            d[i]=0xfffffff;
        h=new list;
        h->num=1;
        p=h;
        sou(1);
        cout<<d[n]<<endl;
    }
    return 0;
}

你可能感兴趣的:(hdu2544(spfa,个人早期代码))