LightOJ - 1074 Extended Traffic(spfa+dfs负环判断)

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 nintegers 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个点,m个单向边,有着特别处理的边权,从点1开始走,问能否到达目的地,看最小获得总额度能否大于3;

       特别注意的是存在负环~~(比如3-2-1-3);

       这样的话用spfa来处理,如果存在负环,那么就spfa一下,把所有负环能够到达的地方标记一下就好了。

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int head[205];
int v[205];
struct ***
{
	int to, ne, len;
}ed[1000005];
int tim[205];
int vis[205];
int cir[205];
int cnt, n, m;
void add(int from, int to, int len)
{
	ed[cnt].to = to;
	ed[cnt].len = len;
	ed[cnt].ne = head[from];
	head[from] = cnt++;
}
int d[205];
void init()
{
	for (int s = 0; s <= n; s++)
	{                                                                        
		head[s] = -1;
		cir[s] = 0;
	}
	cnt = 0;
}
void dfs(int x)
{
	cir[x] = 1;
	for (int s = head[x]; ~s; s = ed[s].ne)
	{
		if (!cir[ed[s].to])
		{
			dfs(ed[s].to);
		}
	}
}
void spfa()
{
	for (int s = 0; s <= n; s++)
	{
		tim[s] = 0;
		d[s] = 0x3f3f3f3f;
		vis[s] = 0;
	}
	queue<int>q;
	q.push(1);
	d[1] = 0;
	vis[1] = 1;
	tim[1]++;
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		vis[t] = 0;
		if (cir[t])
		{
			continue;
		}
		for (int s = head[t]; ~s; s = ed[s].ne)
		{
			if (d[ed[s].to] > d[t] + ed[s].len)
			{
				d[ed[s].to] = d[t] + ed[s].len;
				if (!vis[ed[s].to])
				{
					tim[ed[s].to]++;
					q.push(ed[s].to);
					vis[ed[s].to] = 1;
				}
				if (tim[ed[s].to] > n)
				{
					dfs(ed[s].to);
				}
			}
		}
	}
}
int main()
{
	int te,cas = 1;
	scanf("%d", &te);
	while (te--)
	{
		scanf("%d", &n);
		init();
		for (int s = 1; s <= n; s++)
		{
			scanf("%d", &v[s]);
		}
		scanf("%d", &m);
		while (m--)
		{
			int a, b;
			scanf("%d%d", &a, &b);
			add(a, b, (v[b] - v[a])*(v[b] - v[a])*(v[b] - v[a]));
		}
		spfa();
		scanf("%d", &m);
		printf("Case %d:\n", cas++);
		while (m--)
		{
			int t;
			scanf("%d", &t);
			if (cir[t] || d[t] < 3 || d[t] == 0x3f3f3f3f)
			{
				printf("?\n");
			}
			else
			{
				printf("%d\n", d[t]);
			}
		}
	}
	return 0;
}

 

全部评论

相关推荐

有工作后先养猫:太好了,是超时空战警,我们有救了😋
点赞 评论 收藏
分享
jack_miller:杜:你不用我那你就用我的美赞臣
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务