hdu 3790 最短路径问题 【双权值最短路(相同最小距离,求最小花费)】

41 篇文章 0 订阅
2 篇文章 0 订阅

//注释为CIN/COUT版,cin.sync_with_stdio(false)一定要有,不然会超时

#include<iostream>
using namespace std;
const int maxn=1000+10;     //点数
const int maxm=1000000+10;   //边数
const int inf=0x3f3f3f3f;
typedef pair<int,int> elem_t;
inline elem_t operator +(const elem_t a,const elem_t b)
{
    return elem_t(a.first+b.first,a.second+b.second);
}
struct edge_t{
    int from,to;
    elem_t len;
    edge_t *next;
};
#define _cp(a,b) ((a).d<(b).d)
struct heap_t{elem_t d;int v;};
struct heap{
    heap_t h[maxn*10];
    int n,p,c;
    void init(){n=0;}
    void ins(heap_t e){
        for(p=++n;p>1&&_cp(e,h[p>>1]);h[p]=h[p>>1],p>>=1);
        h[p]=e;
    }
    int del(heap_t &e){
        if(!n)return 0;
        for(e=h[p=1],c=2;c<n&&_cp(h[c+=(c<n-1&&_cp(h[c+1],h[c]))],h[n]);h[p]=h[c],p=c,c<<=1);
        h[p]=h[n--];return 1;
    }
};
void dijkstra(int n,edge_t*list[],int s,elem_t*min,int*pre)
{
    heap h;
    edge_t*t;heap_t e;
    int v[maxn],i;
    for(i=0;i<n;i++)
        min[i]=elem_t(inf,inf),v[i]=0,pre[i]=-1;
    h.init();min[e.v=s]=e.d=elem_t(0,0),h.ins(e);
    while (h.del(e)) {
        if(!v[e.v])
            for(v[e.v]=1,t=list[e.v];t;t=t->next)
                if(!v[t->to]&&min[t->from]+t->len<min[t->to])
                    pre[t->to]=t->from,min[e.v=t->to]=e.d=min[t->from]+t->len,h.ins(e);
    }
}

edge_t edgepool[maxm];
int size;
edge_t *list[maxn];
elem_t len;
elem_t dis[maxn];
int pre[maxn];

void insert(edge_t*list[],int from,int to,elem_t len)
{
    edgepool[size].from=from;
    edgepool[size].to=to;
    edgepool[size].len=len;
    edgepool[size].next=list[from];
    list[from]=&edgepool[size++];
}
void outputpath(int pre[],int i)
{
    if(pre[i]==-1)
    {
        cout<<i;return;
    }
    outputpath(pre,pre[i]);
    cout<<"-->"<<i<<endl;
}
int main()
{
    int n,m;
    //cin.sync_with_stdio(false);
    while(scanf("%d%d",&n,&m)!=EOF)//cin>>n>>m)
    {
        if(n==0&&m==0)break;
        int a,b,d,p;
        memset(list,0,sizeof(list));
        size=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d%d",&a,&b,&d,&p);//            cin>>a>>b>>d>>p;
            insert(list,a,b,elem_t(d,p));
            insert(list,b,a,elem_t(d,p));
        }
        int s,e;
        scanf("%d%d",&s,&e);
        //cin>>s>>e;
        dijkstra(n+1,list,s,dis,pre);
        printf("%d %d\n",dis[e].first,dis[e].second);
        //cout<<dis[e].first<<' '<<dis[e].second<<endl;
        //outputpath(pre, n);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值