"Shortest circuit + minimum cost" HDU 3790 minimum path problem

Source: Internet
Author: User

SOURCE:HDU 3790 Shortest Path problem

http://acm.hdu.edu.cn/showproblem.php?pid=3790


Problem 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.

Test Instructions

Unlike the shortest path, it tells you the cost of each route, asking the minimum cost of the output in the case of the same minimum road length.

Example

Sample Input

3 2
1 2 5 6
2 3 4 5
1 3
0 0

Sample Output

9 11

Ideas

Minimum cost under shortest circuit conditions (conditional!) ), add a variable, change the heap priority, and pay attention to the initialization and the addition of edge conditions!
Shortest circuit: Dijkstra algorithm + heap optimization O (NLOGN)

Reference Code
/*dijkstra algorithm + heap optimization O (eloge) */#include <bits/stdc++.h>#define CLR (k,v) memset (k,v,sizeof (k) )#define INF 0x3f3f3f3fusing namespace STD;Const int_max = ++Ten;intn,m,u,v,w,val,st,ed;structqnode{intV,c,val; Qnode (int_v =0,int_c =0,int_val=0): V (_v), C (_c), Val (_val) {}BOOL operator< (ConstQnode &r)Const{//Define Priority level    if(c = = r.c)returnVal > r.val;returnC > r.c; }};structedge{intV,cost,val; Edge (int_v =0,int_cost =0,int_val =0): V (_v), Cost (_cost), Val (_val) {}}; vector<Edge>E[_max];BOOLVis[_max];intDist[_max],value[_max];//vlaue is the minimum cost in the shortest pathnumber of//points starting from 1voidDijkstra (intNintStart) {clr (Vis,0); for(inti =1; I <= N;  + + i) dist[i] = INF; priority_queue<qnode>pq; while(!pq.empty ()) Pq.pop (); Dist[start] =0; Value[start] =0;//Initialize to add! Pq.push (Qnode (Start,0,0)); Qnode tmp; while(!pq.empty ())    {tmp = Pq.top (); Pq.pop ();intu = TMP.V;if(Vis[u])Continue; Vis[u] =true; for(inti =0; I < e[u].size (); + + i) {intv = e[tmp.v][i].v;intCost = E[u][i].cost;intval = e[u][i].val;if(!vis[v] && dist[v] >= Dist[u] + cost) {//Note equals = =DIST[V] = Dist[u] + cost;            VALUE[V] = Value[u] + val;        Pq.push (Qnode (v,dist[v],value[v)); }    }  }}voidAddedge (intUintVintWintc) {E[u].push_back (Edge (v,w,c)); E[v].push_back (Edge (u,w,c));//No-map note push two times!!! }intMain () {#ifndef Online_judgeFreopen ("Input.txt","R", stdin);#endif //Online_judge   while(scanf("%d%d", &n,&m) = =2&& (n | | m)) { for(inti =0; I <= N; + + i) e[i].clear ();//Initialize     for(inti =0; I < m; + + i) {scanf("%d%d%d%d", &u,&v,&w,&val);//adjacency table storage, heavy side not to be considered! Addedge (U,v,w,val);//numbering starting from 1}scanf("%d%d", &st,&ed); Dijkstra (N,ST);//start to end    printf("%d%d\n", dist[ed],value[ed]); }return 0;}
    • Bold Ctrl + B
    • Italic Body Ctrl + I
    • ReferenceCtrl + Q
    • Insert LinkCtrl + L
    • Inserting codeCtrl + K
    • Insert PictureCtrl + G
    • Promote titleCtrl + H
    • Ordered listCtrl + O
    • Unordered listCtrl + U
    • LineCtrl + R
    • RevokeCtrl + Z
    • RedoCtrl + Y

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Shortest circuit + minimum cost" HDU 3790 minimum path problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.