Featured Training - [kuangbin take you to fly] the shortest practice

Shortest practice

 

0. Til the Cows Come Home  POJ - 2387  

Perfect template title

 1 //#include<Windows.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<queue>
 7 using namespace std;
 8 const int MAX_V = 10005;
 9 const int MAX_E = 20010;
10 const int inf = 0x3f3f3f3f;
11 
12 struct ENode
13 {
14     int to;
15     int Next;
16     int w;
17 };
18 ENode Edegs[MAX_E];
19 int Head[MAX_V];
20 int Dis[MAX_V];
21 int tnt;
22 void Add_ENode(int u, int v, int w)
23 {
24     ++tnt;
25     Edegs[tnt].to = v;
26     Edegs[tnt].w = w;
27     Edegs[tnt].Next = Head[u];
28     Head[u] = tnt;
29     ++tnt;
30     Edegs[tnt].to = u;
31     Edegs[tnt].w = w;
32     Edegs[tnt].Next = Head[v];
33     Head[v] = tnt;
34 }
35 struct cmpx
36 {
37     bool operator () (int &a, int &b) const
38     {
39         return Dis[a] - Dis[b] > 0;
40     }
41 };
42 
43 void Dijkstra(int x)
44 {
45     priority_queue<int, vector<int>, cmpx> q;
46     memset(Dis, inf, sizeof(Dis));
47     Dis[x] = 0;
48     q.push(x);
49     while (!q.empty())
50     {
51         int u = q.top();
52         q.pop();
53         for (int k = Head[u]; k != -1; k= Edegs[k].Next)
54         {
55             int v = Edegs[k].to;
56             if (Dis[v] > Dis[u] + Edegs[k].w)
57             {
58                 Dis[v] = Dis[u] + Edegs[k].w;
59                 q.push(v);
60             }
61         }
62     }
63 }
64 
65 int main()
66 {
67     int t, n;
68     cin >> t >> n;
69     tnt = -1;
70     int a, b, w;
71     memset(Head, -1, sizeof(Head));
72     for (int i = 0; i < t; i++)
73     {
74         cin >> a >> b >> w;
75         Add_ENode(a, b, w);
76     }
77     Dijkstra(1);
78     cout << Dis[n] << endl;
79 //    system("pause");
80     return 0;
81 }
View Code

 

Frogger first  POJ - 2253  

Frog and stones. There are two frogs in the pond and n stone, there is a certain distance between the stones, now a (short legs) wants to find another frog frog yuehui; n given the coordinates of stone, No. 1 M where the main stone frog, II as the target of stone, ask it at all possible paths need a single jump shortest distance how much?

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<queue>
 6 #include<cmath>
 7 using namespace std;
 8 const int MAX_V= 210;
 9 const double inf= 99999999999999999.0;
10 typedef pair<double, double> _pair;
11 _pair rock[MAX_V];
12 double get_dis(_pair a, _pair b)
13 {
14     return sqrt(((a.first- b.first)* (a.first- b.first) )+ ((a.second- b.second)* (a.second- b.second) ) );
15 }
16 double Dis[MAX_V];
17 struct cmpx
18 {
19     bool operator() (int &a, int &b) const
20     {
21         return Dis[a]- Dis[b]> 0; 
22     }
23 };
24 int Front[MAX_V];
25 void Dijkstra(int n)
26 {
27     priority_queue<int, vector<int>, cmpx> q;
28     fill(Dis, Dis+ n+ 1, inf);
29     //for(int i= 1; i<= n; i ++) printf("%f\n", Dis[2]);
30     Dis[1]= 0;
31     Front[1]= -1;
32     q.push(1);
33     while (! q.empty() )
34     {
35         int u= q.top();
36         q.pop();
37         for (int i= 2; i<= n; i ++)
38         {
39             if (i== u) continue;
40             double detmp= get_dis(rock[u], rock[i]);
41             //printf("%f---%f---%f\n", Dis[u], detmp, Dis[i]);
42             if (Dis[i]> Dis[u]&& Dis[i]> detmp)
43             {
44                 Dis[i]= max(Dis[u], detmp);
45                 Front[i]= u;
46                 q.push(i);
47             }
48             //printf("%f\n", Dis[i]);
49         }
50     }
51 }
52 int main()
53 {
54     int n;
55     int t= 0;
56     while (cin >> n)
57     {
58         ++ t;
59         if (n== 0) break;
60         for (int i= 1; i<= n; i ++)
61         {
62             cin >> rock[i].first >> rock[i].second;
63         }
64         //for(int i= 2; i<= n; i ++) printf("%f\n", get_dis(rock[1], rock[i]));
65         Dijkstra(n);
66         printf("Scenario #%d\n",t);
67         printf("Frog Distance = %.3f\n\n", Dis[2]);
68         double ans= -1.0;
69         /*for (int c= n; c!= 1; c= Front[c])
70         {
71             double cnp= get_dis(rock[c], rock[Front[c]]);
72             ans= max(ans, cnp);
73         }
74         printf("Frog Distance = %.3f\n\n", ans);*/
75     }
76     return 0;
77 }
View Code

 

2. Heavy Transportation POJ - 1797  

City there are N junction, M street, every street has a maximum weight limit; now we want to drive from Junction 1 to No. N junction, the maximum allowed weight of vehicles is how much?

 

Travel 3. ( at The 2019 ACM-ICPC Programming Contest The China Shannxi Provincial  M) / ** garlic passenger count reproducibility match: Click here * /

Galaxy there are n planets, numbered from 1 to N. There is connected between the M planet tunnels, each tunnel has a length. You have a spacecraft, the spacecraft has two properties: the number of transmissions and transmission distance d e. Spacecraft only through passage which is shorter than or equal to the transmission distance; if the transmission frequency is exhausted, the spacecraft can no longer use. Spacecraft having a level, LV0 spacecraft d and e are equal to 0, you can give your spacecraft upgrade every time it takes to upgrade the consumption point c, you lift spacecraft dx and ex point properties. Now, you tell n, m, m channels of information, as well as when to upgrade your spacecraft c, dx, ex.

Q: Can you find the smallest cost from 1 to N do?

A: originally recorded the shortest distance from this point Dis [] into recording arrived at this point of the lowest level required for aircraft Dis_Level of [], so the rest is a common Dijkstra.

 1 #include<algorithm>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 using namespace std;
 6 const int MAX_V= 100010;
 7 const int MAX_E= 400010;
 8 const int inf= 0x3f3f3f3f;
 9 
10 struct ENode
11 {
12     int to;
13     int w;
14     int Next;
15 };
16 ENode edegs[MAX_E];
17 int Head[MAX_V], tnt;
18 void Add_ENode(int a, int b, int w)
19 {
20     edegs[++ tnt].to= b;
21     edegs[tnt].w= w;
22     edegs[tnt].Next= Head[a];
23     Head[a]= tnt;
24     edegs[++ tnt].to= a;
25     edegs[tnt].w= w;
26     edegs[tnt].Next= Head[b];
27     Head[b]= tnt;
28 }
29  
30  int Dis_Level [max_v];   // to each point, a minimum level required for the spacecraft; 
31 is  int Deep [max_v];   // depth of each point of bfs; 
32  struct CMPX
 33 is  {
 34 is      BOOL  operator () ( int A &, int & B) const 
35      {
 36          return Dis_Level [A] - Dis_Level [B]> 0 ;
 37 [      }
 38 is  };
 39  void the Dijkstra ( int X, int _dis, int _cost)
 40  {
41 is      / * X as a starting point, _dis each upgrade is to enhance the transmission distance, _cost upgrading is elevated number of transmission; * / 
42 is      Memset (Dis_Level, INF, the sizeof (Dis_Level));
 43 is      Memset (Deep, INF, the sizeof (Deep ));
 44 is      the priority_queue < int , Vector < int >, CMPX> Q;
 45      Dis_Level [X] = 0 ; // aircraft starting level of 0; 
46 is      deep [X] = 0 ;   // starting depth is 0; 
47      q.push (X);
 48      the while (! q.empty ())
 49      {
 50          int U =q.top ();
 51 is          q.pop ();
 52 is          for ( int K = Head [U]; K = -! . 1 ; K = edegs [K] .NEXT)
 53 is          {
 54 is              int V = edegs [K]. to;
 55              int lev_tmp = Dis_Level [U];
 56 is              the while (* _dis lev_tmp <edegs [K] .W || * _cost lev_tmp <Deep [U] + . 1 )
 57 is              {
 58                  / * If the current level of the aircraft can not cross this tunnel, or the transfer count has been exhausted, the aircraft once the upgrade; * / 
59                  lev_tmp ++ ;
 60              }
 61 is              IF (lev_tmp <Dis_Level [v])
 62 is              {
 63 is                  / * If at this time a small aircraft the aircraft grade level before reaching the point v, is updated Dis_Level [v] * / 
64                  Dis_Level [v] = lev_tmp;
 65                  Deep [v] = Deep [ U] + . 1 ; // depth should be + 1'd; 
66                  q.push (V); // queued; 
67              }
 68          }
 69      }
 70  }
 71 is  void INTO ()
 72  {
 73 is      Memset (Head, - . 1 , the sizeof (Head));
 74      TNT = -1;
75 }
76 
77 int main()
78 {
79     int n, m;
80     int c, d, e;
81     int a, b ,w;
82     while (~ scanf("%d %d", &n, &m))
83     {
84         scanf("%d %d %d", &c, &d, &e);
85         into();
86         for (int i= 0;i< m;i ++)
87         {
88             scanf("%d %d %d", &a, &b, &w);
89             Add_ENode(a, b, w);
90         }
91         Dijkstra(1, d, e);
92         if (Dis_Level[n]== inf) printf("-1\n");
93         else printf("%lld\n", (long long)Dis_Level[n]* c);
94     }
95     return 0;
96 }
View Code

 

Guess you like

Origin www.cnblogs.com/Amaris-diana/p/10778340.html