Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked in congestion. In order to convince people avoid shortest routes, and hence the crowded roads, to reach destination, the city authority has made a new plan. Each junction of the city is marked with a positive integer (≤ 20) denoting the busyness of the junction. Whenever someone goes from one junction (the source junction) to another (the destination junction), the city authority gets the amount (busyness of destination - busyness of source)^3 (that means the cube of the difference) from the traveler. The authority has appointed you to find out the minimum total amount that can be earned when someone intelligent goes from a certain junction (the zero point) to several others.
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case contains a blank line and an integer n (1 < n ≤ 200) denoting the number of junctions. The next line contains n integers denoting the busyness of the junctions from 1 to n respectively. The next line contains an integer m, the number of roads in the city. Each of the next m lines (one for each road) contains two junction-numbers (source, destination) that the corresponding road connects (all roads are unidirectional). The next line contains the integer q, the number of queries. The next q lines each contain a destination junction-number. There can be at most one direct road from a junction to another junction.
For each case, print the case number in a single line. Then print q lines, one for each query, each containing the minimum total earning when one travels from junction 1 (the zero point) to the given junction. However, for the queries that gives total earning less than 3, or if the destination is not reachable from the zero point, then print a ‘?’.
Sample Input
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
Sample Output
Case 1:
3
4
Case 2:
?
在一个堵车的城市中, 每个路口都有一个拥挤程度b[i], 从u驶向v得到一个数量, 值为(b[v]-b[u])^3, 给出Q组查询, 每组给出一个数, 求1到这个数的点的最小数量, 如果不可达输出 ?
注意两点:
就能想到这道题肯定要存在判负环的情况, 也就是目标点不可达(图不联通)和目标点在负环上都要输出?
因为图是有向的, 所以负环有可能被绕过去不走
首先使用SPFA, 在判到负环的情况下, 做一次Dfs即可把负环上的点标记一下即可(同一连通分量), 这也是数据量如此小的原因
unidirectional意思是有向的, unoriented才是无向的
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const int inf = 1<<30;
const LL maxn = 210;
int N, M, b[maxn];
struct Edge{
int to, w;
Edge(int tt, int ww){to = tt, w = ww;}
Edge(){}
};
vector<Edge> es[maxn];
void addEdge(int u, int v, int w){
es[u].push_back(Edge(v, w));
}
int d[maxn], cnt[maxn];
bool used[maxn];
bool is[maxn]; //是否是负环上的点
void Dfs(int u){
is[u] = true;
for(int i = 0; i < es[u].size(); ++i)
if(!is[es[u][i].to])
Dfs(es[u][i].to);
}
void SPFA(int s){
queue<int> q;
fill(d, d+maxn, inf);
ms(used, 0); ms(cnt, 0); ms(is, 0);
d[s] = 0, cnt[s] = 1, used[s] = true;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
used[u] = false;
for(int i = 0; i < es[u].size(); ++i){
int v = es[u][i].to, w = es[u][i].w;
if(!is[v] && d[u]+w < d[v]){
d[v] = d[u]+w;
if(!used[v]){
used[v] = true;
q.push(v);
if(++cnt[v] > N)
Dfs(v); //当存在负环时Dfs标记连通分量里s所有点
}
}
}
}
}
int main()
{
int T, u, v, Q, e;
cin >> T;
for(int t = 1; t <= T; ++t){
ms(es, 0);
cin >> N;
for(int i = 1; i <= N; ++i)
cin >> b[i];
cin >> M;
while(M--){
cin >> u >> v;
addEdge(u, v, (b[v]-b[u])*(b[v]-b[u])*(b[v]-b[u]));
}
SPFA(1);
cin >> Q;
cout << "Case " << t << ":\n";
while(Q--){
cin >> e;
if(d[e]<3 || d[e]==inf || is[e]) cout << "?" << endl;
else cout << d[e] << endl;
}
}
return 0;
}
/*
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
*/