1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include <cctype> #include <cstdio> #include <algorithm> #include <iostream> #include <vector> #include <cstring> #include <climits> #include <queue> #include <ctime> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/priority_queue.hpp> using namespace std; const int iol = 1024 * 1024; char iobuf[iol], *ioh, *iot, ioc; inline char read() { if(ioh == iot) { iot = (ioh = iobuf) + fread(iobuf, 1, iol, stdin); if(ioh == iot) return -1; } return *ioh++; } template<class T> inline void read(T& x) { for(ioc = read(); !isdigit(ioc); ioc = read()); x = 0; for(; isdigit(ioc); ioc = read()) x = (x << 1) + (x << 3) + (ioc ^ '0'); } const int MAXN = 5000011; struct Node { int v; long long w; Node() {} Node(int v, long long w) : v(v), w(w) {} inline bool operator < (const Node &n) const { return w > n.w; } }; vector<Node> edge[MAXN]; inline void addEdge(int u, int v, long long w) { edge[u].push_back(Node(v, w)); edge[v].push_back(Node(u, 0)); } int n, m; bool vis[MAXN]; long long dis1[MAXN], disn[MAXN]; const long long INF = LONG_LONG_MAX >> 1; typedef __gnu_pbds::priority_queue<Node> Heap; Heap::point_iterator id[MAXN]; inline void dijkstra(int s, long long *dis) { fill(dis + 1, dis + n + m + 1, INF); memset(vis, 0, sizeof(bool) * (n + m + 1)); memset(id, 0, sizeof(id)); dis[s] = 0; Heap q; id[s] = q.push(Node(s, 0)); while (!q.empty()) { register int now = q.top().v; q.pop(); if (vis[now]) continue; vis[now] = 1; for (int i = 0; i < edge[now].size(); i++) { Node *x = &edge[now][i]; if (x->w + dis[now] < dis[x->v]) { dis[x->v] = x->w + dis[now]; if (id[x->v] != NULL) q.modify(id[x->v], Node(x->v, dis[x->v])); else id[x->v] = q.push(Node(x->v, dis[x->v])); } } } } int main() { #ifndef ONLINE_JUDGE freopen("farm.in", "r", stdin); #endif read(n), read(m); for (int i = 1, w, ni; i <= m; i++) { read(w), read(ni); for (int j = 0, v; j < ni; j++) read(v), addEdge(v, n + i, w); } dijkstra(1, dis1); dijkstra(n, disn); long long tmp = INF; for (int i = 1; i <= n; i++) tmp = min(tmp, max(dis1[i], disn[i])); if (tmp == INF) cout << "impossible", exit(0); cout << tmp << "\n"; for (int i = 1; i <= n; i++) if (max(dis1[i], disn[i]) == tmp) cout << i << " "; return 0; }
|