标签:
Description
Input
Output
Sample Input
3 2 1 2 5 6 2 3 4 5 1 3 0 0
Sample Output
9 11
Dijkstra算法模版题,没有什么好说的;
AC代码:
#include"algorithm" #include"iostream" #include"cstring" #include"cstdlib" #include"string" #include"cstdio" #include"vector" #include"cmath" #include"queue" using namespace std; typedef long long LL; #define memset(x,y) memset(x,y,sizeof(x)) #define memcpy(x,y) memcpy(x,y,sizeof(x)) #define MX 401 const int dij_v=1005; const int dij_edge=100005; template <class T> struct Dijkstra { struct Edge { int v,nxt,cost; T w; } E[dij_edge<<1]; int Head[dij_v],erear,money[dij_v]; T p[dij_v],INF; typedef pair< T ,int > PII; void edge_init() { erear=0; memset(money,0); memset(Head,-1); } void edge_add(int u,int v,T w,int cost) { E[erear].v=v; E[erear].w=w; E[erear].cost=cost; E[erear].nxt=Head[u];; Head[u]=erear++; } void run(int u) { memset(p,0x3f); INF=p[0]; priority_queue<PII ,vector<PII >,greater<PII > >Q; while(!Q.empty()) { Q.pop(); } Q.push(PII(0,u)); p[u]=0; while(!Q.empty()) { PII a=Q.top(); Q.pop(); int u=a.second; if(a.first!=p[u])continue; for(int i=Head[u]; ~i; i=E[i].nxt) { int v=E[i].v; int cost=E[i].cost; T w=E[i].w; if(p[u]+w==p[v]) { money[v]=min(money[v],money[u]+cost); } if(p[u]+w<p[v]) { p[v]=w+p[u]; money[v]=money[u]+cost; Q.push(PII(p[v],v)); } } } } }; Dijkstra<int > dij; int main() { int n,m; while(~scanf("%d%d",&n,&m)) { if(!n&&!m)break; dij.edge_init(); for(int i=1; i<=m; i++) { int u,v,w,l; scanf("%d%d%d%d",&u,&v,&w,&l); dij.edge_add(u,v,w,l); dij.edge_add(v,u,w,l); } int st,ed; scanf("%d%d",&st,&ed); dij.run(st); printf("%d %d\n",dij.p[ed],dij.money[ed]); } return 0; }
ACM: HDU 3790 最短路径问题-Dijkstra算法
标签:
原文地址:http://www.cnblogs.com/HDMaxfun/p/5751311.html