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


PDF (English) Statistics Forum
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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
    
#define rep(i,a,n) for (int i=a;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 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


你可能感兴趣的:(ACM_图论,ACM_template)