P1462 road to orgerma, p1462 road to orgerma
Background
On the continent of azelas, there is a magic named "grin". He is the backbone of the tribe.
One day, when he woke up, he found himself in the consortium's main city, stormwind.
After being attacked by a large number of consortium soldiers, he decided to escape to his hometown ogreema.
Description
There are n cities in azelas. 1, 2, 3,..., n.
There are m two-way roads between cities, connecting two cities. from one city to another, the city will be attacked by the Alliance, which will cause a loss of blood.
A certain toll (including the start and end points) will be collected each time you pass through a city ). There is no toll station on the road.
Assume that 1 is stormwind, n is ogreema, and his blood volume is B at most, his blood volume is full at departure.
He doesn't want to spend a lot of money. He wants to know what is the minimum charge for the most one of the cities he passes by when he can reach ogreima.
Input/Output Format
Input Format:
The first row has three positive integers, n, m, and B. It indicates that there are n cities, m roads, and bits, respectively.
Next there are n rows, each row has 1 positive integer, fi. It indicates that the user passes through the city I and needs to pay the fi yuan.
Then there are m rows, with three positive integers in each row: ai, bi, and ci (1 <= ai, bi <= n ). It indicates that there is a highway between city ai and city bi. From city ai to city bi, or from city bi to city ai, the blood volume of ci will be lost.
Output Format:
Only one integer indicates the minimum value of the maximum payment.
If he cannot reach ogrema, output AFK.
Input and Output sample
Input example #1:
4 4 8856102 1 22 4 11 3 43 4 3
Output sample #1:
10
Description
For 60% of data, n ≤ 200, m ≤ 10000, and B ≤ 200
For 100% of data, n ≤ 10000, m ≤ 50000, and B ≤ 1000000000
For 100% of the data, the ci ≤ 1000000000 and fi ≤ 1000000000 may have two sides connected to the same city.
Binary answer + SPFA
Be sure to enable long.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #define lli long long int 7 using namespace std; 8 void read(lli & n) 9 { 10 char c='+';lli x=0; 11 while(c<'0'||c>'9') 12 c=getchar(); 13 while(c>='0'&&c<='9') 14 { 15 x=x*10+(c-48); 16 c=getchar(); 17 } 18 n=x; 19 } 20 const lli MAXN=100001; 21 const lli maxn=0x7fffffff; 22 struct node 23 { 24 lli u,v,w,nxt; 25 }edge[MAXN]; 26 lli head[MAXN]; 27 lli num=1; 28 lli dis[MAXN]; 29 lli vis[MAXN]; 30 lli n,m,maxnblood; 31 lli spend[MAXN]; 32 lli l=0x7fffffff,r=-1; 33 void add_edge(lli x,lli y,lli z) 34 { 35 edge[num].u=x; 36 edge[num].v=y; 37 edge[num].w=z; 38 edge[num].nxt=head[x]; 39 head[x]=num++; 40 } 41 lli SPFA(lli r) 42 { 43 if(r<spend[1]) 44 {return 0;} 45 for(lli i=1;i<=n+1;i++) 46 dis[i]=maxn,vis[i]=0; 47 queue<int>q; 48 q.push(1); 49 vis[1]=1; 50 dis[1]=0; 51 while(q.size()!=0) 52 { 53 lli p=q.front(); 54 q.pop(); 55 vis[p]=0; 56 for(lli i=head[p];i!=-1;i=edge[i].nxt) 57 { 58 lli will=edge[i].v; 59 if((dis[will]>dis[p]+edge[i].w)&&spend[will]<=r) 60 { 61 dis[will]=dis[p]+edge[i].w; 62 if(vis[will]==0) 63 { 64 vis[will]=1; 65 q.push(will); 66 } 67 } 68 } 69 } 70 if(dis[n]<=maxnblood) 71 return 1; 72 else 73 return 0; 74 } 75 int main() 76 { 77 read(n);read(m);read(maxnblood); 78 for(lli i=1;i<=n;i++) 79 head[i]=-1; 80 for(lli i=1;i<=n;i++) 81 { 82 read(spend[i]); 83 l=min(spend[i],l); 84 r=max(spend[i],r); 85 } 86 for(lli i=1;i<=m;i++) 87 { 88 lli x,y,z; 89 read(x);read(y);read(z); 90 add_edge(x,y,z); 91 add_edge(y,x,z); 92 } 93 lli ans=0; 94 while(l<=r) 95 { 96 lli mid=(l+r)/2; 97 if(SPFA(mid)) 98 { 99 ans=mid;100 r=mid-1;101 }102 else l=mid+1;103 }104 //printf("%d",r);105 if(ans&&SPFA(ans))106 printf("%d",ans);107 else 108 printf("AFK");109 return 0;110 }