[hdu3790]最短路径问题

时间:2022-08-04 09:42:02

题目链接

题解:加一个cost数组就好

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <climits>
using  namespace std;
#define INF 0x3f3f3f3f
queue <int> q;
int m,n,t,sp,ep,ans;
const int maxn=1005;
int head[maxn],cost[maxn];
bool vis[maxn];
int d[maxn];

struct edge{
    int to,from,next;
    int val,length;
}e[1500005];

void add(int i,int j,int w,int cost)
{
    e[t].from=i;
    e[t].to=j;
    e[t].val=w;
    e[t].length=cost;
    e[t].next=head[i];
    head[i]=t++;
}

void spfa(int s)
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++) 
    {
        d[i]=INF;
        cost[i]=INF;
    }
    d[s]=0;cost[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            if(d[v]>d[u]+e[i].val||(d[v]==d[u]+e[i].val&&cost[v]>cost[u]+e[i].length))
            {
                cost[v]=cost[u]+e[i].length;
                d[v]=d[u]+e[i].val;
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
}

void m_int()
{
    int x,y,z,w;
    while(scanf("%d%d",&n,&m)!=EOF&&n&&m)
    {
        t=1;
        memset(head,-1,sizeof(head));
        while(m--)
        {
            scanf("%d%d%d%d",&x,&y,&z,&w);
            add(x,y,z,w);
            add(y,x,z,w);
        }
        scanf("%d%d",&sp,&ep);
        spfa(sp);
        printf("%d %d\n",d[ep],cost[ep]);
    }
}

int main()
{
    m_int();
    return 0;
}