//注释为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; }
hdu 3790 最短路径问题 【双权值最短路(相同最小距离,求最小花费)】
最新推荐文章于 2019-02-04 00:17:22 发布