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
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.
Output
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:
?
大致题意 : 有T组数据, 每组数据有n个点和m条边, 每个点有点权 ai, 对m条边,其端点u,v由u到v的边的边权v的点权-u的点权的三次方, 即(a[v]-a[u])^3, 随即给出Q个询问对每个询问有一个点P, 问从1到P, 是否权值不小于3
思路 : 按题目条件用邻接表建图(因为后续需要用dfs跑一遍, vector建图也可), 用SPFA求1-P的最短路, 同时判断负环, 然后用dfs对负环上的点打上标记, 表示不能到该点
AC代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <iomanip>
using namespace std;
stringstream ss;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 220, M = 1e5+10;
const int INF = 0x3f3f3f3f;
int n,m;
int d[N], v[N], cnt[N];
int idx, h[N], e[M], ne[M], w[M];
bool st[N], vis[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
// 遍历负环进行标记
void dfs(int u)
{
vis[u] = 1;
for(int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if(!vis[j]) dfs(j);
}
}
void spfa()
{
memset(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
for(int i = 1; i<=n; i++) d[i] = INF;
queue<int> q;
q.push(1);
st[1] = 1;
d[1] = 0;
while(q.size())
{
int t = q.front();
q.pop();
st[t] = 0;
for(int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if(d[j] > d[t] + w[i])
{
d[j] = d[t] + w[i];
cnt[j] = cnt[t] + 1;
if(cnt[j] >= n)
{
// 找到负环进行标记
dfs(j);
return;
}
if(!st[j])
{
q.push(j);
st[j] = 1;
}
}
}
}
}
int main()
{
int T;
scanf("%d", &T);
int cas = 1;
while(T--)
{
scanf("%d", &n);
idx = 1;
memset(v, 0, sizeof v);
memset(vis, 0, sizeof vis);
memset(h, -1, sizeof h);
for(int i = 1; i<=n; i++) scanf("%d", &v[i]);
scanf("%d", &m);
while(m -- )
{
int a,b,c;
scanf("%d %d", &a, &b);
c = v[b]-v[a];
add(a,b,c*c*c);
}
spfa();
int q,p;
scanf("%d", &q);
printf("Case %d:\n", cas++);
while(q--)
{
scanf("%d", &p);
// 若距离为INF 或 小于3 或该点被标记就到不了
if(d[p] == INF || d[p] < 3 || vis[p])
{
printf("?\n");
}else printf("%d\n", d[p]);
}
}
}