Extended Traffic LightOJ - 1074

2020-10-28 11:26:13 浏览数 (1)

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:

? 这个题是问你什么点可以到达什么点不能到达,不被负环松弛且边权大于3的点可以到达,这里直接SPFA跑就可以最后把负环标记一下,负环出队N次,而且每个点最多连N-1条入边,即在N次出队时,可处理点都已处理完,剩下的都是不能处理的,即到达不了的。

代码语言:javascript复制
#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>
#define Swap(a,b) a^=b^=a^=b
#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define speed ios_base::sync_with_stdio(0)
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
#define mem(n,x) memset(n,x,sizeof(n))
#define INF  0x3f3f3f3f
#define maxn  100010
#define Ege 100000000
#define Vertex 1005
#define esp  1e-9
#define mp(a,b) make_pair(a,b)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
struct Node
{
    int to, lat, val; //边的右端点,边下一条边,边权
};
Node edge[1000005];
int head[1005],tot,dis[1005],N,M,vis[1005],V[1005];
int val[1005];
void add(int from, int to, int dis)
{
	edge[  tot].lat = head[from];
	edge[tot].to = to;
	edge[tot].val = dis;
	head[from] = tot;

}
void spfa(int s)
{
	memset(dis, 0x3f, sizeof(dis));
	dis[0]=0;
	memset(vis, 0, sizeof(vis));
	memset(V, 0, sizeof(V));
	vis[s] = 1;
	dis[s] = 0;
	queue<int>Q;
	Q.push(s);
	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		vis[u] = 0;
		V[u]  ;
		if(V[u]>N)
        {
            dis[u]=-1;
            while (!Q.empty()) {
            int u = Q.front();
                dis[u]=-1;
                Q.pop();
            }
            break;
        }
		for (int i = head[u];i;i = edge[i].lat) {
			int to = edge[i].to;
			int di = edge[i].val;
			if (dis[to]>dis[u]   di) {
				dis[to] = dis[u]   di;
				if (!vis[to]) {
					vis[to] = 1;
					Q.push(to);
				}
			}
		}
	}

}
int main()
{
    int t, x;
    scanf("%d", &t);
    for(int cnt=1;cnt<=t;cnt  )
    {
        memset(head, 0, sizeof(head));
        cini(N);
        for(int i=0;i<N;i  ) cin>> val[i 1];
        cini(M);
        while (M--)
        {
            int a, b, dis;
            scanf("%d %d", &a, &b);
            dis=val[b]-val[a];
            add(a, b, dis*dis*dis);
        }
        spfa(1);
        cini(N);
        for(int i=0;i<N;i  ) cin>>val[i];
        printf("Case %d:n",cnt);
        for(int i=0;i<N;i  )
        {
            if(dis[val[i]]<3||dis[val[i]]==INF) cout<<'?'<<endl;
            else cout<<dis[val[i]]<<endl;
        }
    }
    return 0;
}

0 人点赞