您的位置:首页 > 其它

Extended Traffic(spfa判负环)

2015-10-30 19:09 309 查看
Description

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:

?

一张图,u到v的距离定义变为两个城市值差的立方,然后求最短路,如果没有输出?

首先边的权值变了,然后还会有负环,用spfa判断一下,设置一个数组,表示该点入队多少次,一旦大于n次,那就肯定是负环上的点了。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int head[205],d[205],a[205],n,ans[205];
bool leap[205];
struct Node
{
int u,v,w,next;
}node[80050];
void add_edge(int u,int v,int w,int tot)
{
node[tot].u = u;node[tot].v = v;
node[tot].w = w;
node[tot].next = head[u];
head[u] = tot;
}
void spfa()
{
for(int i = 1;i <= n;i++)leap[i] = false;
for(int i = 1;i <= n;i++)d[i] = inf;
for(int i = 1;i <= n;i++)ans[i] = 0;
d[1] = 0;leap[1] = true;ans[1] = 1;
queue<int>p;
p.push(1);
while(p.size())
{
int u = p.front();p.pop();
leap[u] = false;
for(int i = head[u];i != -1;i = node[i].next)
{
int v = node[i].v;
if(d[v] > d[u] + node[i].w)
{
d[v] = d[u] + node[i].w;
if(!leap[v])
{
ans[v]++;
if(ans[v] <= n)
{
leap[v] = true;p.push(v);
}
else
{
leap[v] = true;
}
}
}
}
}
}
int main()
{
#ifdef LOCAL
freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
#endif // LOCAL
int t,kase = 1;
scanf("%d",&t);
while(t--)
{
int cnt = 1;
memset(head,-1,sizeof(head));
scanf("%d",&n);
for(int i = 1;i <= n;i++)
scanf("%d",&a[i]);
int m;
scanf("%d",&m);
for(int i = 1;i <= m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
int w = (a[v] - a[u])*(a[v] - a[u])*(a[v] - a[u]);
add_edge(u,v,w,cnt++);
}
spfa();
int q;
scanf("%d",&q);
printf("Case %d:\n",kase++);
for(int i = 1;i <= q;i++)
{
int x;
scanf("%d",&x);
if(d[x] == inf || leap[x] || d[x] < 3)
printf("?\n");
else
printf("%d\n",d[x]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: