LightOJ 1074 Extended Traffic

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:

?

题意:不根据题意描述了,N组数据,每组数据有n个点,一开始给出n个点的权值,然后会有m条边,每条边给出两个点u,v,表示两个点可以连通,连通的权值是(v点的权值减去u点的权值的三次方),然后有Q个询问,每次询问是一个点,输出这个点到源点的距离。

首先是单源最短路,并且存在负边权,所以用SPFA,还会存在环,所以需要判断环。当是要求点是环,不连通,或者连通距离小于3就输出?,否则就输出距离。具体参考代码

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 205;
int cnt[maxn];
int dis[maxn];
int loop[maxn];
int book[maxn];
int head[maxn];
struct node{
	int e, w;
	int next;
	void Node(int x, int y, int z) {
		e = x; w = y; next = z;
	}
}e[40005];
void init(int n, int m) {
	memset(cnt, 0, sizeof(cnt));
	memset(dis, inf, sizeof(dis));
	memset(book, 0, sizeof(book));
	memset(loop, 0, sizeof(loop));
	memset(head, -1, sizeof(head));
}
void dfs(int x) {
	loop[x] = 1;
	for(int i = head[x]; i != -1; i = e[i].next) {
		if(!loop[e[i].e]) {
			dfs(e[i].e);
		}
	}
}
void SPFA_que(int n) {
	dis[1] = 0;
	int pre, nex;
	queue q;
	q.push(1);
	while(!q.empty()) {
		pre = q.front();
		q.pop();
		book[pre] = 0;
		if(loop[pre]) continue;     //
		for(int i = head[pre]; i != -1; i = e[i].next) {
			nex = e[i].e;
			if(loop[nex]) continue; //不加会T 
			if(dis[nex] > dis[pre] + e[i].w) {
				dis[nex] = dis[pre] + e[i].w;
				if(!book[nex]) {
					book[nex] = 1;
					q.push(nex);
					if(++cnt[nex] >= n) {
						dfs(nex);
					}
				}
			}
		}
	}
}
int mul(int x) {
	return x*x*x;
}
int main() {
	int N;
	int n, m;
	int u, v;
	int num[205];
	int Case = 1;
	scanf("%d", &N);
	while(N--) {
		scanf("%d", &n);
		for(int i = 1; i <= n; i++) {
			scanf("%d", &num[i]);
		}
		scanf("%d", &m);
		init(n , m);
		for(int i = 1; i <= m; i++) {
			scanf("%d %d", &u, &v);
			e[i].Node(v, mul(num[v]-num[u]), head[u]);
			head[u] = i;
		}
		SPFA_que(n);
		int Q, q[205];
		scanf("%d", &Q);
		for(int i = 0; i < Q; i++) {
			scanf("%d", &q[i]);
		}		
		printf("Case %d:\n", Case++);
		for(int i = 0; i < Q; i++) {
			if(dis[q[i]] < 3 || dis[q[i]] == inf)
			printf("?\n");
			else if(loop[q[i]]) printf("?\n");
			else printf("%d\n", dis[q[i]]);
		}
	}
	return 0;
}


你可能感兴趣的:(最短路)