【最短路】【负圈可达点】【用while(!Q.empty())】【优先队列优化】【dij】

本文深入探讨了信息技术领域的各个方面,包括前端开发、后端开发、移动开发、游戏开发等细分领域。从HTML、CSS、JavaScript等基础技术,到PHP、Python、Java等高级语言的应用,再到大数据开发、开发工具、嵌入式硬件等复杂场景,全面覆盖了信息技术的广泛范围。同时,文章还涵盖了图像处理、音视频技术、AI处理、测试、基础运维等热门话题,为读者提供了一个全面的技术视野。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Time Limit: 2 second(s)Memory Limit: 32 MB

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 nrespectively. 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

Output for 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

Case 1:

3

4

Case 2:

?





lightoj 1074

#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
    
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mp push_back
int T,n,m,q;
const int maxn = 210;
int H[maxn];
struct Edge
{
	int v,c,next;
}edge[maxn*maxn];

struct Qnode
{
	int idx,dis;
	Qnode(){};
	Qnode(int _i,int _d) : idx(_i),dis(_d){};
	bool operator< (const Qnode &x) const
	{
		return dis > x.dis;
	}
};
int val[maxn];
int d[maxn];
bool ok[maxn];
bool cir[maxn];
int top;

int dis(int f,int t)
{
	return (t-f)*(t-f)*(t-f);
}

void addedge(int u,int v,int c)
{
	edge[top].v = v;
	edge[top].c = c;
	edge[top].next = H[u];

	H[u] = top++;
}

void dfs(int u)
{
	cir[u] = true;
	for(int ei=H[u];ei!=-1;ei=edge[ei].next)
	{
		Edge e = edge[ei];
		if(!cir[e.v])
			dfs(e.v);
	}
}
void dij(int st)
{
	for(int i=1;i<=n;i++)
	{
		d[i] = 0x3f3f3f3f;
		ok[i] = false;
		cir[i] = false;
	} 

	d[st] = 0;
	priority_queue<Qnode> pq;
	pq.push(Qnode(st,0));

	while(!pq.empty())
	{
		Qnode cur = pq.top();
		pq.pop();

		ok[cur.idx] = true;
		for(int ei=H[cur.idx];ei!=-1;ei=edge[ei].next)
		{
			Edge e = edge[ei];
			if(cir[e.v]) continue;
			if(d[e.v] > cur.dis + e.c)
			{
				if(ok[e.v])
				{
					dfs(e.v);
				}
				else
				{
					d[e.v] = cur.dis + e.c;
					pq.push(Qnode(e.v,d[e.v]));
				}
			}
		}
	}

}
int main()
{
	int	 C = 1;
    scanf("%d",&T);
	while(T--)
	{
		top = 0;
		memset(H,-1,sizeof(H));
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&val[i]);
		}

		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			int c = dis(val[u],val[v]);
			addedge(u,v,c);
		}
		dij(1);	
		scanf("%d",&q);
		printf("Case %d:\n",C++);
		while(q--)
		{
			int v;
			scanf("%d",&v);
			if(cir[v] || d[v] < 3 || d[v] == 0x3f3f3f3f)
			{
				puts("?");
			}
			else
			{
				printf("%d\n",d[v]);
			}
		}
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值