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:

?

二、思路

  • 2个点之间的边为单向边, 权值为(目的点的权 - 源点权)3
  • 由于有负权边。可以用spfa进行求最短路。

三、代码

#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
const int N = 205, M = 4e5 + 5, INF = 0x3f3f3f3f;
int t, n, m, k, u, v, w, len, p[N], h[N], d[N], cnt[N];//p[i] 记录每个点的权值 
bool vis[N];
struct E {
	int v, w, next;
} e[M];
void add(int u, int v, int w) {
	e[len].v = v;
	e[len].w = w;
	e[len].next = h[u];
	h[u] = len++;	
}
bool spfa() {
	memset(d, 0x3f, sizeof(d));
	memset(cnt, 0, sizeof(cnt));
	memset(vis, false, sizeof(vis));
	d[1] = 0;
 	queue<int> q;
 	q.push(1);
 	while (!q.empty()) {
		int u = q.front();
		q.pop();
		vis[u] = false;
		for (int j = h[u]; j; j = e[j].next) {
			int v = e[j].v;
			int w = e[j].w + d[u];
			if (d[v] > w) {
				d[v] = w;
				cnt[v] = cnt[u] + 1;
				if (cnt[v] >= n) return true;
 				if (!vis[v]) vis[v] = true, q.push(v);
			}
		}
	}
	return false;
} 
int main() {
	int cas = 0;
	scanf("%d", &t);
	while (t--) {
		memset(h, 0, sizeof(h)); len = 1;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
		scanf("%d", &m);
		while (m--) {
			scanf("%d%d", &u, &v);
			w = p[v] - p[u]; //目的地-源点 
			add(u, v, w*w*w);
		}
		spfa();
		printf("Case %d:\n", ++cas);
		scanf("%d", &k);
		while (k--) {
			scanf("%d", &u);
			// 小于3(可能由于负环导致) || 无解  
			if (d[u] < 3 || d[u] > INF / 2) printf("?\n");
			else printf("%d\n", d[u]);
		}
	}
	return 0;
} 
发布了414 篇原创文章 · 获赞 380 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104199999