一、题目描述
2000 ms / 32768 KB
样例输入:
2
5
6 7 8 9 10
6
1 2
2 3
3 4
1 5
5 4
4 5
2
4
5
2
10 10
1
1 2
1
2
样例输出:
Case 1:
3
4
Case 2:
?
二、算法分析说明与代码编写指导
三、AC 代码
#include<cstdio>
#include<algorithm>
#include<bitset>
#pragma warning(disable:4996)
using namespace std;
struct edge { unsigned u, v; int d; };
const int amax = 1 << 30;
unsigned t, n, m, q, d; int b[201], a[201]; edge e[40000]; bool updated, ok; bitset<201> v;
inline int cb(const int& x) { return x * x * x; }
inline void bellman_ford() {
fill(a + 2, a + n + 1, amax); a[1] = 0; ok = false; v.reset();
for (unsigned i = 1; i < n; ++i) {
updated = false;
for (unsigned j = 0; j < m; ++j) {
if (a[e[j].u] != amax && a[e[j].v] > a[e[j].u] + e[j].d) {
a[e[j].v] = a[e[j].u] + e[j].d; updated = true;
}
}
if (updated == false) { ok = true; break; }
}
if (ok == false) {
for (unsigned j = 0; j < m; ++j) {
if (a[e[j].u] != amax && a[e[j].v] > a[e[j].u] + e[j].d) {
a[e[j].v] = a[e[j].u] + e[j].d; v[e[j].u] = v[e[j].v] = true;
}
}
}
}
int main() {
scanf("%u", &t);
for (unsigned i = 1; i <= t; ++i) {
scanf("%u", &n); for (unsigned i = 1; i <= n; ++i)scanf("%d", &b[i]);
scanf("%u", &m);
for (unsigned i = 0; i < m; ++i) {
scanf("%u%u", &e[i].u, &e[i].v); e[i].d = cb(b[e[i].v] - b[e[i].u]);
}
bellman_ford();
printf("Case %u:\n", i); scanf("%u", &q); ++q;
while (--q) {
scanf("%u", &d);
if (a[d] >= 3 && a[d] < amax && v[d] == false)printf("%d\n", a[d]);
else puts("?");
}
}
return 0;
}