[Luogu P1462] [Bi-point + heap optimization dij]

Topic description

In Azeroth, there are n cities. The numbers are 1,2,3,...,n.

There are m two-way roads between the cities, connecting the two cities, from one city to another city, will be attacked by the alliance, and then lose a certain amount of blood.

Every time you pass through a city, you will be charged a certain toll (including the starting point and the ending point). There are no toll booths on the road.

Suppose 1 is Stormwind, n is Orgrimmar, and his HP is at most b, and his HP is full when he starts.

Crooked mouth does not want to spend a lot of money, he wants to know what is the minimum cost of the most one-time charge in all the cities he passes through, provided he can reach Orgrimmar.

topic analysis

The topic has said so much, in fact, it is to find the minimum value of the maximum value of F[i] encountered during the movement process when the character does not die.. [When you see the minimum value of the maximum value, you should immediately think of two points.. However, konjac is a konjac after all.. I didn’t expect two points.. It’s a brainless heap optimized use of dij.. As a result, TLE became suspicious of life, and only after seeing other people’s solutions did I think of using two points..orz..]

The meaning of the question is very clear in this way that two points must be used. Basically, nine out of ten questions require two points.

So what do we want in blood or money?

Because the request is for a fee, so just two cents of money////

The condition for bisection is to take the current value as the maximum value, not to take a point greater than the bisection value, and then use the heap-optimized dij to determine whether it is connected.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 using namespace std;
 6 const int maxn1=10000;
 7 const int maxn2=50000;
 8 const int maxn3=1000000005;
 9 struct pot{
10     int id;
11     int rest;
12      bool operator <(const struct pot&AA)const{
13         return rest < AA.rest;
14     }
15 };
16 struct edge{
17     int next;
18     int to;
19     int len;
20 }EDGE[maxn2*2+5];
21 int dist[maxn1+5],head[maxn1+5],dist1[maxn1+5];
22 int n,m,b;
23 int edge_cnt=0;
24 priority_queue<struct pot>pq;
25 void add(int x,int y,int z)
26 {
27     EDGE[edge_cnt].to=y;
28     EDGE[edge_cnt].next=head[x];
29     EDGE[edge_cnt].len=z;
30     head[x]=edge_cnt++;
31 }
32 int mmin=-1;
33 bool dij(int x)
34 {
35     memset(dist1,0,sizeof(dist1));
36     while(!pq.empty())
37     {
38         pq.pop();
39     }
40     pq.push((struct pot){1,b});
41     while(!pq.empty())
42     {
43         struct pot aa=pq.top();pq.pop();
44         if(aa.rest<dist1[aa.id])continue;
45         for(int i = head[aa.id]; i != -1 ; i=EDGE[i].next)
46         {
47             int v=EDGE[i].to;
48             if(dist[v]<=x&&aa.rest-EDGE[i].len>0)
49             {
50                 if(v==n){
51                 return true;
52             }
53                 if(dist1[v]<aa.rest-EDGE[i].len)
54                 {
55                     dist1[v]=aa.rest-EDGE[i].len;
56                     pq.push((struct pot){v,aa.rest-EDGE[i].len});
57                 }
58             }
59         }
60     }
61     return false;
62 }
63 int main()
64 {
65     scanf("%d%d%d",&n,&m,&b);
66     int r=0;    
67     for(int i = 1 ; i <= n ; i++)
68     {
69         scanf("%d",&dist[i]);
70         r=max(r,dist[i]);
71     }
72     memset(head,-1,sizeof(head));
73     while(m--)
74     {
75         int q,w,e;
76         scanf("%d%d%d",&q,&w,&e);
77         add(q,w,e);
78         add(w,q,e);
79     }
80     int l=max(dist[1],dist[n]);
81     bool flag=false;
82     while(l<=r)
83     {
84         int mid=(l+r)/2;
85         if(dij(mid)){
86             r=mid-1;
87             flag=true;
88         }
89         else
90         {
91             l=mid+1;
92          } 
93     }
94     if(!flag)
95     printf("AFK\n");
96     else
97     cout << l<< endl;
98     return 0;
99 }
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325263958&siteId=291194637