求最短距离及其花费(hdu 3790)

在自己学校OJ上写成vector<vector<node> >v,v.resize(n+1);能过,在hdu上和自己编译器上就RE,改成vector<node>v[1001]才行!???


#include<bits/stdc++.h>
using namespace std;
struct node
{
    int e;
    int w;
    int cost;
};
struct cmp
{
    bool operator()(const node &a,const node &b)
    {
        if(a.w!=b.w)
            return a.w>b.w;
        else
            return a.cost>b.cost;
    }
};
int main()
{
    int n,m,s,t;
    while(scanf("%d%d",&n,&m)==2&&(n||m))
    {
        priority_queue<node,vector<node>,cmp>que;
        vector<node>v[1001];
        int vis[1001]={0};
        node x;
        while(m--)
        {
            int a,b,d,p;
            scanf("%d %d %d %d",&a,&b,&d,&p);
            x.e=b,x.w=d,x.cost=p;
            v[a].push_back(x);
            x.e=a;
            v[b].push_back(x);
        }
        scanf("%d%d",&s,&t);
        x.e=s,x.w=0,x.cost=0;
        que.push(x);
        while(!que.empty())
        {
            x=que.top(),que.pop();
            vis[x.e]=1;
            if(x.e==t)
                break;
            for(int i=0,j=v[x.e].size(); i<j; i++)
            {
                node q;
                q.e=v[x.e][i].e;
                if(vis[q.e])
                    continue;
                q.w=x.w+v[x.e][i].w;
                q.cost=x.cost+v[x.e][i].cost;
                que.push(q);
            }
        }
        printf("%d %d\n",x.w,x.cost);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41061455/article/details/80666169