KUANGBIN_SHORTPATHP (HDU 4725)

Source: Internet
Author: User

A very challenging topic of direct violence to build the words of No doubt O (n^2) will be tle each layer of virtual a point and will let no point of the layer can also connect the past

Refer to the method of Kuangbin chicory two virtual points per layer n+i*2-1 is the entrance n+i*2 is the exit and then the one-way side.

VA once, because MAXN should be twice times bigger than the data. Accidentally ignored as far as MAXM directly to 1e7

#include <iostream>#include<string>#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<map>#include<vector>#include<Set>#include<algorithm>#defineINF 0x3f3f3f3fusing namespacestd;Const intMAXN = 1e6 +Ten;Const intMAXM = 2e7 +Ten; typedef pair<int,int>PII;structcmp{BOOL operator()(ConstPII A,ConstPII b) {        returnA.first >B.first; }};intsize, HEAD[MAXN], POINT[MAXM], NXT[MAXM], VAL[MAXM];intt, N, M, C, DIST[MAXN];voidinit () {size=0; memset (Head,-1,sizeofhead);} InlinevoidAddint  from,intTo,intvalue) {Val[size]=value; Point[size]=to ; Nxt[size]= head[ from]; head[ from] = size++;}voidDij () {priority_queue<pii, Vector<pii>, cmp>Q; memset (Dist,0x3f,sizeofDist); Q.push (Make_pair (0,1)); dist[1] =0;  while(!Q.empty ()) {PII u=Q.top ();        Q.pop (); if(U.first > Dist[u.second])Continue;  for(inti = Head[u.second]; ~i; i =Nxt[i]) {            if(Dist[point[i] > Dist[u.second] +Val[i]) {Dist[point[i]]= Dist[u.second] +Val[i];            Q.push (Make_pair (Dist[point[i]), point[i]); }        }    }}intMain () {scanf ("%d", &t);  for(intKase =1; Kase <= t; kase++) {init (); scanf ("%d%d%d", &n, &m, &c);  for(inti =1; I <= N; i++){            intlayer; scanf ("%d", &layer); Add (n+2*layer-1I0); Add (i, n+2*layer,0); }         for(inti =1; I < n; i++) {Add (n+2*i, n +2* (i+1) -1, c); Add (n+2* (i+1), n +2*i-1, c); }        intu, V, W;  for(inti =1; I <= m; i++) {scanf ("%d%d%d", &u, &v, &W);            Add (U, V, W);        Add (V, u, W);        } dij (); printf ("Case #%d:%d\n", Kase, dist[n] = = INF? -1: Dist[n]); }    return 0;}

KUANGBIN_SHORTPATHP (HDU 4725)

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.