POJ 1724 ROADS 最短路 邻接表 + bfs +优先队列

最近比较懒,一种方法A了以后不想再去学习别的方法,诶,需要调整

本题:邻接表 + bfs +优先队列 32MS还行

View Code
#include
#include<string.h>
#include
using namespace std;
#define INF 100000000
#define maxn 103
#define maxm 10003
struct node
{
    int c, l, w;
    node(int cc,int ll,int ww):c(cc),l(ll),w(ww)
    {}
    friend bool operator<(node a,node b)
    {
        return a.l > b.l;
    }
};
int tot,n,m,ww;
int head[maxn],dis[maxn];
struct edge
{
    int v,next,w,l;
}list[maxm];
void add(int s,int t, int l, int w)
{
    list[tot].v = t;
    list[tot].l = l;
    list[tot].w = w;
    list[tot].next = head[s];
    head[s]= tot++;
}
void init()
{
    int i;
    for(i=0;i<=n;i++)
        dis[i]=INF;
    memset(head,-1,sizeof(head));
    tot =0;
}
void bfs()
{
    priority_queueq;
    int i,j;
    q.push(node(1,0,0));
    while(!q.empty())
    {
        node u = q.top();q.pop();
        if(u.c == n)
        {
            printf("%d\n",u.l);return;
        }
        for(i=head[u.c];i!=-1;i=list[i].next)
        {
            node p = u;
            p.w += list[i].w;
            p.l += list[i].l;
            p.c = list[i].v;
            if(p.w <= ww)
                q.push(p);
        }
    }
    printf("%d\n",-1);
}
int main()
{
    int a, b, c, d;
    while(~scanf("%d%d%d",&ww,&n,&m))
    {
        init();
        while(m--)
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            add(a,b,c,d);
        }
        bfs();
    }
    return 0;
}

 

你可能感兴趣的:(POJ 1724 ROADS 最短路 邻接表 + bfs +优先队列)