HDU 3790 shortest path problem

                                    shortest path problem

Give you n points, m undirected edges, each edge has a length d and a cost p, give you the starting point s and the ending point t, and ask to output the shortest distance from the starting point to the end point and its cost, if the shortest distance has multiple routes, The output costs the least.
Input input n, m, the number of points is 1~n, and then there are m lines, each line has 4 numbers a, b, d, p, indicating that there is an edge between a and b, and its length is d, and the cost is p. The last line is two numbers s, t; starting point s, end point. The input ends when n and m are 0.  
(1<n<=1000, 0<m<100000, s != t) The Output output line has two numbers, the shortest distance and its cost. Sample Input
3 2
1 2 5 6
2 3 4 5
1 3
0 0
Sample Output
9 11
The idea is probably that we need to find the shortest path, and if there are multiple shortest paths, choose the least expensive one.
It is not much different from the template dijsktra, but I have used pair wa several times for the priority queue, and I have made mistakes in the pairing. I don't know what's wrong (cry and rewrite it in structure
code show as below:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#include<list>
using namespace std;
const int INF= 1e17;
struct node
{
    int v,l,f;
    node(int nt,int vb,int gg):v(nt),l(vb),f(gg) {};
};
vector<node> p[100005];
int dis[100005],pay[100005];
bool vis[10005];
struct PQ
{
    int u,w;
    PQ(int uv,int wv):u(uv),w(wv) {};
    bool operator < (const PQ &gg) const
    {
        return w >gg.w;
    }
};
void init(int n)
{
    int i;
    for(i=0; i<=n; i++)
    {
        dis[i]=INF;
        pay[i]=INF;
        vis[i]=false;
    }
}
void dijsktra(int start,int n)
{
    priority_queue<PQ>q;
    init(n);
    dis[start]=0;
    pay[start]=0;
    q.push(PQ(start,0));
    while(!q.empty())
    {
        PQ temp = q.top();
        q.pop();
        int x=temp.u;
        if(vis[x])
            continue ;
        vis[x]=true;
        for(int i=0; i<p[x].size(); i++)
        {
            int y=p[x][i].v;
            if(vis[y]) continue ;
            int d=p[x][i].l;
            int py=p[x][i].f;
            if(dis[y]>dis[x]+d)
            {
                dis[y]=dis[x]+d;
                pay[y]=pay[x]+py;
                q.push(PQ(y,dis[y]));
            }
            else if(dis[y]==(dis[x]+d))
                pay[y]=min(pay[y],pay[x]+py);
        }
    }
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)&&n+m)
    {
        int i,s,d,k,pp;
        for(i=1; i<=m; i++)
        {
            scanf("%d%d%d%d",&s,&d,&k,&pp);
            p[s].push_back(node(d,k,pp));
            p[d].push_back(node(s,k,pp));
        }
        int qd,zd;
        scanf("%d%d",&qd,&zd);
        dijsktra(qd,n);
        printf("%d %d\n",dis[zd],pay[zd]);
        for(int i=0; i<=n; i++)
            p[i].clear();
    }
}
/*5 5
3 2 4 5
1 4 3 2
5 3 3 1
5 2 1 1
4 3 2 2
2 1*/

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326303995&siteId=291194637