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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <vector> #define pair_int std::pair<int, int> char ch_buffer; bool signum; inline void readInt(int &l) { l = 0; do ch_buffer = getchar(); while ((ch_buffer < '0' || ch_buffer > '9') && ch_buffer != '0' && ch_buffer != '-'); if (ch_buffer == '-') ch_buffer = getchar(), signum = true; while (ch_buffer <= '9' && ch_buffer >= '0') l = (l << 3) + (l << 1) + ch_buffer - '0', ch_buffer = getchar(); if (signum) l = -l, signum = false; } using namespace std; #define DIJKSTRA_MAX 10010
struct node { int v, w; node() {} node(int v0, int w0) : v(v0), w(w0) {} bool operator<(const node &b) const { return w < b.w; } }; bool vis[DIJKSTRA_MAX]; int dis[DIJKSTRA_MAX];
vector<node> son[DIJKSTRA_MAX]; inline int dijkstra(int s, int t) { priority_queue<pair_int, vector<pair_int>, greater<pair_int> > q; memset(dis, 127, sizeof(dis)); memset(vis, false, sizeof(vis)); dis[s] = 0; q.push(make_pair(dis[s], s)); while (!q.empty()) { int now = q.top().second; q.pop(); if (vis[now]) continue; vis[now] = true; for (int i = 0; i < son[now].size(); i++) { node x = son[now][i]; if (dis[now] + x.w < dis[x.v]) { dis[x.v] = dis[now] + x.w; q.push(make_pair(dis[x.v], x.v)); } } } return dis[t]; } inline void insert(int a, int b, int w) { son[a].push_back(node(b, w)); } inline void insert_multi(int a, int b, int w) { son[a].push_back(node(b, w)); son[b].push_back(node(a, w)); } int n, m; int p, t; struct tot { int pos; int val; tot() : val(0) {} inline bool operator<(const tot &_t) const { if (val != _t.val) return val < _t.val; return pos < _t.pos; } }; tot total[105]; int min_pos; bool found[105]; #define NOT_FOUND 2139062143 int main(int argc, char const *argv[]) { ios::sync_with_stdio(false); cin.tie(NULL); memset(found, true, sizeof(found)); readInt(n); for (int i = 1; i <= n; i++) { readInt(m); for (int j = 0; j < m; j++) readInt(p), readInt(t), insert(i, p, t); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i != j) { total[i].pos = i; int tmp = dijkstra(i, j); if (tmp != NOT_FOUND) total[i].val = max(total[i].val, tmp); else found[i] = false, total[i].val = NOT_FOUND; } } } for (int i = 1; i <= n; i++) if (found[i]) goto can; goto notfound; can : { min_pos = min_element(total + 1, total + n + 1)->pos; cout << min_pos << " " << total[min_pos].val; exit(0); } notfound: cout << "disjoint"; return 0; }
|