Valley 1462 Road leading to Orgrimmar
Address: http://www.luogu.org/problem/show?pid=1462
Topic background
On the continent of Azeroth, there is a magical warlock named Wry mouth, who is the backbone of the tribe. One day he woke up and found himself in the main city of the union. After being attacked by many Allied soldiers, he decided to flee to his hometown Orgrimmar
Title Description
In Azeroth, there are N cities. Numbers are,..., N.
There are two-way road between cities, connecting two cities, from one city to another city, will be attacked by the alliance, and then lose a certain amount of blood.
Without passing through a city, a certain toll (including the start and end points) will be charged. There are no toll stations on the road.
Suppose 1 is Storm city, N is Orgrimmar, and his blood volume is at most B, and his blood volume is full when he departs.
Wry mouth Oh, I don't want to spend a lot of money, he wants to know the minimum value of the maximum amount of fees that can be charged in all the cities he passes through to Orgrimmar.
Input/output format
Input format:
The first line is 3 positive integers, n,m,b. respectively, there are N cities, M highway, wry mouth oh, the amount of blood is b.
Next there are n lines, 1 positive integers per line, fi. The expression passes the city I, need to pay fi yuan.
Then there are m lines, 3 positive integers per line, Ai,bi,ci (1<=ai,bi<=n). Indicates that there is a highway between city AI and city bi, which loses CI's blood volume if it is from city Ai to city Bi, or from urban bi to urban AI.
Output format:
Only an integer that represents the minimum value of wry mouth.
If he is unable to reach Orgrimmar, output AFK.
Input/Output sample
Input Sample # #:
4 4 8
8
5
6
10
2 1 2
2 4 1
1 3 4
3 4 3
Sample # # of output:
10
Description
For 60% of data, meet n≤200,m≤10000,b≤200
For 100% of data, meet n≤10000,m≤50000,b≤1000000000
For 100% of the data, the ci≤1000000000,fi≤1000000000 is met, and there may be two edges connected to the same city.
Ideas
Binary method + Shortest circuit determination.
Two minutes after the maximum cost of the city UPC, then judged. Decision: For each of the cost of more than the UPC city marked as unreachable, the shortest path, judging the most short-circuit and the relationship between the amount of blood can be. If a city is unreachable, you can set the INQ to 1 before the SPFA algorithm starts.
Small optimization: See a lot of students about the time around 1000ms, mostly because it is blind two points. In fact, only need to the C value from small to large order, the C value of two points can be.
Code
1#include <cstdio>2#include <queue>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 8typedefLong LongLL;9 Const intMAXN =10000+Ten, maxm=50000+Ten;Ten ConstLL inf=1e15; One A structedge{ - intV,w,next; -}e[2*MAXM]; the intEN,FRONT[MAXN]; - - intn,m; LL B; - intC[MAXN]; + - +InlinevoidAddedge (intUintVintW) { Aen++; E[en].v=v; E[en].w=w; E[en].next=front[u]; front[u]=en; at } - -queue<int>Q; - intINQ[MAXN]; LL D[MAXN]; - BOOLCanintUPC) - { inmemset (INQ,0,sizeof(INQ)); - for(intI=1; i<=n;i++){ tod[i]=INF; + if(C[I]>UPC) inq[i]=1; - } the * if(c[1]>UPC | | C[N]>UPC)return false; $d[1]=0; inq[1]=1; Q.push (1);Panax Notoginseng while(!Q.empty ()) { - intU=q.front (); Q.pop (); inq[u]=0; the for(inti=front[u];i>=0; i=E[i].next) { + intv=e[i].v,w=E[I].W; A if(d[v]>d[u]+W) { thed[v]=d[u]+W; + if(!Inq[v]) { -inq[v]=1; $ Q.push (v); $ } - } - } the } - returnd[n]<=B;Wuyi } the - intMain () { Wumemset (front,-1,sizeof(front)); -scanf"%d%d%lld",&n,&m,&B); About for(intI=1; i<=n;i++) scanf ("%d",&c[i]); $ intu,v,w; - for(intI=1; i<=m;i++) { -scanf"%d%d%d",&u,&v,&W); - Addedge (u,v,w); A Addedge (v,u,w); + } the - intTMPC[MAXN]; $memcpy (&tmpc,&c,sizeof(C)); theSort (tmpc+1, tmpc+n+1); the the intL=1, r=n+1, M; the while(l<R) { -m=l+ (r-l)/2; in if(Can (Tmpc[m])) R=M; the Elsel=m+1; the } About if(L>r | | (L==r &&!can (Tmpc[l]))) printf"afk\n"); the Elseprintf"%d\n", Tmpc[l]); the return 0; the}
Valley 1462 Road leading to Orgrimmar