File size: 2,118 Bytes
c4b0eef |
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <climits>
#include <cstring>
using namespace std;
#define MAX 105
#define oo INT_MAX
int n, m, q, t = 1;
int parent[MAX], sz[MAX];
int temp[MAX][MAX];
class comparator
{
public:
bool operator()(pair <pair <int, int>, int> &x, pair <pair <int, int>, int> &y)
{
return x.second > y.second;
}
};
priority_queue <pair <pair<int, int>, int>, vector <pair <pair <int, int>, int> >, comparator> pq;
int findP(int v)
{
if (v == parent[v]) return parent[v];
else return parent[v] = findP(parent[v]);
}
bool merge(pair<int, int> a)
{
int x = a.first;
int y = a.second;
if (findP(x) == findP(y)) return true;
else
{
x = findP(x);
y = findP(y);
if (sz[x] < sz[y]) swap(x, y);
parent[y] = x;
sz[x] += sz[y];
return false;
}
}
void mst()
{
while (!pq.empty())
{
pair <pair <int, int>, int> head = pq.top();
if (!merge(head.first))
{
temp[head.first.first][head.first.second] = head.second;
temp[head.first.second][head.first.first] = head.second;
}
pq.pop();
}
}
void initialize(int n)
{
for (int i = 0; i < n; i++)
{
parent[i] = i;
sz[i] = 1;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
temp[i][j] = oo;
}
}
}
int main()
{
while (cin >> n >> m >> q, n != 0 || m != 0 || q != 0)
{
initialize(n);
for (int i = 0; i < m; i++)
{
int x, y, w;
cin >> x >> y >> w;
x--; y--;
pq.push(make_pair(make_pair(x, y), w));
}
mst();
for (int k = 0; k < n; k++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
temp[i][j] = min(temp[i][j], max(temp[i][k], temp[k][j]));
}
}
}
if (t > 1) cout << endl;
cout << "Case #" << t++ << endl;
for (int i = 0; i < q; i++)
{
int x, y;
cin >> x >> y;
x--; y--;
if (temp[x][y] != oo) cout << temp[x][y] << endl;
else cout << "no path" << endl;
}
}
} |