LightOJ1074 Extended Traffic【SPFA+DFS 標記負環的結點】

Enjoy_process發表於2018-10-20

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 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個點,每個點有權值ai,從i到j的邊權為:(aj−ai)^3。問從1到達k的最短路,不能到達和路徑長小於3輸出? 其它輸出長度

解題思路:可能出現負環,使用SPFA演算法,出現負環時,負環可達的結點都應該輸出?,入隊次數超過n次,標記它是負環中的結點,從此節點進行DFS,它所能達的結點的最短路徑也為負無窮

AC的C++程式:

#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>

using namespace std;

const int N=205;
const int INF=0x3f3f3f3f;
int dist[N],b[N];
int g[N][N];
bool vis[N];
bool f[N];//標記結點是否在負環中 
int cnt[N];
int n;

void dfs(int u)
{
	f[u]=true;
	for(int v=1;v<=n;v++)
	  if(!f[v]&&v!=u&&g[u][v]!=INF)
	    dfs(v);
}

void spfa(int s)
{
	memset(dist,INF,sizeof(dist));
	memset(vis,false,sizeof(vis));
	memset(f,false,sizeof(f));
	memset(cnt,0,sizeof(cnt));
	queue<int>q;
	vis[s]=true;
	cnt[s]=1;
	dist[s]=0;
	q.push(s);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		vis[u]=false;
		if(!f[u]){//如果u不是負環中的點 
			for(int v=1;v<=n;v++)
			  if(!f[v]&&v!=u&&g[u][v]!=INF&&dist[v]>dist[u]+g[u][v]){
			  	dist[v]=dist[u]+g[u][v];
			  	if(!vis[v]){
			  		vis[v]=true;
			  		cnt[v]++;
			  		if(cnt[v]>n)//v是負環中的點 
			  		  dfs(v);//標記v可達的點也是負環中的點 
			  		else
			  		  q.push(v);
				  }
			  }
		}
	}
}

int main()
{
	int T,m,u,v,q,x;
	scanf("%d",&T);
	for(int t=1;t<=T;t++){
		scanf("%d",&n);
		memset(g,INF,sizeof(g));
		for(int i=1;i<=n;i++)
		  scanf("%d",&b[i]);
		scanf("%d",&m);
		while(m--){
			scanf("%d%d",&u,&v);
			g[u][v]=(b[v]-b[u])*(b[v]-b[u])*(b[v]-b[u]);
		}
		spfa(1);
		scanf("%d",&q);
		printf("Case %d:\n",t);
		while(q--){
			scanf("%d",&x);
			if(f[x]||dist[x]==INF||dist[x]<3)
			  printf("?\n");
			else
			  printf("%d\n",dist[x]);
		}
	}
	return 0;
}
 

 

相關文章