HDU 3790 Shortest Path problemTime
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d & %i64u
Description
Give you n points, M without the edge, each side has a length D and spend P, give you the beginning of the end of T, the minimum distance from the output starting point to the end point and the cost, if the shortest distance there are multiple routes, the output is the least cost.
Input
Enter N,m, the number of the point is 1~n, then the M line, 4 number per line a,b,d,p, indicating A and B have an edge, and its length is D, the cost is P. The last line is two number s,t; start s, end point. N and m are 0 o'clock input end.
(1<n<=1000, 0<m<100000, s! = t)
Output
The output line has two numbers, the shortest distance and the cost.
Sample Input
3 21 2 5 62 3 4 51 30 0
Sample Output
9 11
Dijkstra algorithm template problem, there is nothing to say;
AC Code:
#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 MX 401const int dij_v=1005;const int dij_edge=100005 #define MEMCPY (x, y) memcpy (x,y,sizeof); 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];p riority_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));}}}};D Ijkstra<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);d Ij.edge_add (u,v,w,l); Dij.edge_add (v,u,w,l);} int st,ed;scanf ("%d%d", &st,&ed);d Ij.run (ST);p rintf ("%d%d\n", dij.p[ed],dij.money[ed]);} return 0;}
ACM:HDU 3790 Shortest path problem-dijkstra algorithm