【CF】142 Div.1 B. Planes

SPFA.注意状态转移条件,ans的求解需要在bfs中间求解。因为只要到了地点n,则无需等待其他tourist。
还是蛮简单的,注意细节。

  1 /* 229B */
  2 #include <iostream>
  3 #include <string>
  4 #include <map>
  5 #include <queue>
  6 #include <set>
  7 #include <stack>
  8 #include <vector>
  9 #include <deque>
 10 #include <algorithm>
 11 #include <cstdio>
 12 #include <cmath>
 13 #include <ctime>
 14 #include <cstring>
 15 #include <climits>
 16 #include <cctype>
 17 #include <cassert>
 18 #include <functional>
 19 #include <iterator>
 20 #include <iomanip>
 21 using namespace std;
 22 //#pragma comment(linker,"/STACK:102400000,1024000")
 23 
 24 #define sti                set<int>
 25 #define stpii            set<pair<int, int> >
 26 #define mpii            map<int,int>
 27 #define vi                vector<int>
 28 #define pii                pair<int,int>
 29 #define vpii            vector<pair<int,int> >
 30 #define rep(i, a, n)     for (int i=a;i<n;++i)
 31 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 32 #define clr                clear
 33 #define pb                 push_back
 34 #define mp                 make_pair
 35 #define fir                first
 36 #define sec                second
 37 #define all(x)             (x).begin(),(x).end()
 38 #define SZ(x)             ((int)(x).size())
 39 #define lson            l, mid, rt<<1
 40 #define rson            mid+1, r, rt<<1|1
 41 
 42 const int maxn = 1e5+5;
 43 const int INF = 0x3f3f3f3f;
 44 vpii E[maxn];
 45 vpii T[maxn];
 46 int dis[maxn];
 47 bool visit[maxn];
 48 int a[maxn], b[maxn];
 49 
 50 int main() {
 51     ios::sync_with_stdio(false);
 52     #ifndef ONLINE_JUDGE
 53         freopen("data.in", "r", stdin);
 54         freopen("data.out", "w", stdout);
 55     #endif
 56 
 57     int n, m;
 58     int u, v, w;
 59 
 60     scanf("%d %d", &n, &m);
 61     while (m--) {
 62         scanf("%d %d %d", &u, &v, &w);
 63         E[u].pb(mp(v, w));
 64         E[v].pb(mp(u, w));
 65     }
 66     rep(i, 1, n+1) {
 67         scanf("%d", &m);
 68         rep(j, 1, m+1)
 69             scanf("%d", &a[j]);
 70         b[m] = 1;
 71         per(j, 1, m) {
 72             if (a[j]+1 == a[j+1])
 73                 b[j] = b[j+1] + 1;
 74             else
 75                 b[j] = 1;
 76         }
 77         rep(j, 1, m+1)
 78             T[i].pb(mp(a[j], b[j]));
 79     }
 80 
 81     int i, tmp;
 82     int ans = INF;
 83     pii p(0, 0);
 84     vpii::iterator iter;
 85     queue<int> Q;
 86     Q.push(1);
 87     memset(dis, 0x3f, sizeof(dis));
 88     // may be have 0 in T[1]
 89     iter = upper_bound(all(T[1]), p);
 90     if (iter!=T[1].end() && iter->fir==0) {
 91         dis[1] = iter->sec;
 92     } else {
 93         dis[1] = 0;
 94     }
 95     visit[1] = true;
 96 
 97     while (!Q.empty()) {
 98         u = Q.front();
 99         Q.pop();
100         visit[u] = false;
101         for (i=0; i<SZ(E[u]); ++i) {
102             v = E[u][i].fir;
103             w = E[u][i].sec;
104             tmp = dis[u] + w;
105             if (v==n && tmp<ans)
106                 ans = tmp;
107             p.fir = tmp;
108             iter = upper_bound(all(T[v]), p);
109             if (iter!=T[v].end() && iter->fir==tmp) {
110                 tmp += iter->sec;
111             }
112             if (tmp < dis[v]) {
113                 dis[v] = tmp;
114                 if (!visit[v]) {
115                     visit[v] = true;
116                     Q.push(v);
117                 }
118             }
119         }
120     }
121 
122     ans = ans==INF ? -1:ans;
123     printf("%d\n", ans);
124 
125     #ifndef ONLINE_JUDGE
126         printf("time = %d.\n", (int)clock());
127     #endif
128 
129     return 0;
130 }

 

posted on 2015-07-12 12:56  Bombe  阅读(247)  评论(0编辑  收藏  举报

导航