【SPFA】P1462 通往奥格瑞玛的道路

  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<iostream>
  4 using namespace std;
  5 #define INF 0x3f3f3f3f
  6 using namespace std;
  7 
  8 int n, m, k;
  9 int front = 0, behind = 1, d;
 10 int f[10010], b[10010];
 11 
 12 int queue[10010];
 13 int maxc = -0x3f3f3f3f;
 14 long long int dis[10010]; 
 15 bool vis[10010];
 16 int cnt;
 17 
 18 struct node 
 19 {
 20     int next;
 21     int n;
 22     int w;
 23 }a[100010];
 24 
 25 void add(int next, int n, int w)
 26 {
 27     a[++cnt].next = f[next];
 28     a[cnt].n = n;
 29     a[cnt].w = w;
 30     f[next] = cnt;
 31 }
 32 
 33 bool spfa(int num)
 34 {
 35     if (b[1] > num) return false;
 36     for (int i = 1; i <= n; i++)
 37     {
 38         dis[i] = INF;
 39     }
 40     front = 0;
 41     queue[1] = 1; 
 42     behind = 1;
 43     vis[1] = true;
 44     dis[1] = 0;
 45     while (front != behind)
 46 
 47     {
 48         front = front % 10001 + 1;
 49         int t1 = queue[front]; 
 50         vis[t1] = true;
 51         int t2 = f[t1];
 52         while (t2)
 53         {
 54             if (dis[t1] + a[t2].w < dis[a[t2].n])
 55 
 56             {
 57                 dis[a[t2].n] = dis[t1] + a[t2].w;
 58                 if (!vis[a[t2].n] && b[a[t2].n] <= num)
 59                 {
 60                     behind = behind % 10001 + 1;
 61                     queue[behind] = a[t2].n;
 62                     vis[a[t2].n] = true;
 63                 }
 64             }
 65             t2 = a[t2].next;
 66         }
 67         vis[t1] = false;
 68     }
 69     if (dis[n] < k) return true;
 70     return false;
 71 }
 72 
 73 int main()
 74 {
 75     cin >> n >> m >> k;
 76     for (int i = 1; i <= n; i++)
 77     {
 78         cin >> b[i];
 79 
 80         maxc = max(b[i], maxc);
 81     }
 82     for (int i = 1; i <= m; i++)
 83     {
 84         int a, b, c;
 85         cin >> a >> b >> c;
 86         if (a == b) continue;
 87         add(a, b, c);
 88         add(b, a, c);
 89     }
 90     if (!spfa(INF)) 
 91     {
 92         cout << "AFK";
 93         return 0; 
 94     }
 95     int left = max(b[1], b[n]);
 96 
 97     int right = maxc;
 98     while (left < right)
 99     {
100         int mid = (left + right) >> 1;
101         if (spfa(mid)) right = mid;
102         else left = mid + 1;
103     }
104     cout << left;
105 }
View Code

 

posted on 2019-10-09 13:32  thjkhdf12  阅读(116)  评论(0)    收藏  举报