Extended Traffic

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Appoint description: 
System Crawler  (2016-05-03)

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:

?

题意: 有n个城市,m条路,每条路连接2个城市,费用为a[y] - a[x]的3次方。问对于每个点从1出发的费用。

思路:这题存在负环,所以可以用spfa解决,对于负环内的点我们要进行标记,下次遇到的时候就可以直接跳过

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<time.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
struct node
{
int to;
ll val;
int next;
}edge[MAXN*MAXN*];
int ind,pre[MAXN],vis[MAXN],n,m,dis[MAXN],a[MAXN],num[MAXN];
void add(int x,int y,int z)
{
edge[ind].to = y;
edge[ind].val = z;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void spfa()
{
for(int i = ; i <= n; i++){
dis[i] = INF;
vis[i] = ;
num[i] = ;
}
num[] = ;
vis[] = ;
dis[] = ;
queue<int>q;
q.push();
while(!q.empty()){
int tp = q.front();
q.pop();
vis[tp] = ;
for(int i = pre[tp]; i != -; i = edge[i].next){
int t = edge[i].to;
if(num[t] > n)continue;
if(dis[t] > dis[tp] + edge[i].val){
dis[t] = dis[tp] + edge[i].val;
if(!vis[t]){
num[t] ++;
vis[t] = ;
q.push(t);
}
}
}
}
}
int power(int x)
{
return x * x * x;
}
int main()
{
int t,ff = ;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i = ; i <= n; i++){
scanf("%d",&a[i]);
}
scanf("%d",&m);
ind = ;
memset(pre,-,sizeof(pre));
int x,y;
for(int i = ; i <= m; i++){
scanf("%d%d",&x,&y);
add(x,y,power(a[y] - a[x]));
}
spfa();
int q;
scanf("%d",&q);
printf("Case %d:\n",++ff);
while(q--){
scanf("%d",&x);
if(dis[x] < || dis[x] >= INF || num[x] > n){
printf("?\n");
}
else {
printf("%d\n",dis[x]);
}
}
}
return ;
}

lightoj 1074 spfa判断负环的更多相关文章

  1. Extended Traffic LightOJ - 1074 spfa判断负环

    //判断负环 在负环内的城市输出? #include <iostream> #include <queue> #include <cstdio> #include ...

  2. POJ 3259 Wormholes【最短路/SPFA判断负环模板】

    农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径 ...

  3. spfa判断负环

    会了spfa这么长时间竟然不会判断负环,今天刚回.. [例题]poj3259 题目大意:当农场主 John 在开垦他的农场时,他发现了许多奇怪的昆虫洞.这些昆虫洞是单向的,并且可以把你从入口送到出口, ...

  4. spfa 判断负环 (转载)

    当然,对于Spfa判负环,实际上还有优化:就是把判断单个点的入队次数大于n改为:如果总的点入队次数大于所有点两倍 时有负环,或者单个点的入队次数大于sqrt(点数)有负环.这样时间复杂度就降了很多了. ...

  5. Wormholes---poj3259(最短路 spfa 判断负环 模板)

    题目链接:http://poj.org/problem?id=3259 题意是问是否能通过虫洞回到过去: 虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts. 我们把虫洞看成是一条负权路,问 ...

  6. POJ 3259 Wormholes ( SPFA判断负环 && 思维 )

    题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...

  7. Wormholes POJ - 3259 spfa判断负环

    //判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...

  8. UVA 558 SPFA 判断负环

    这个承认自己没看懂题目,一开始以为题意是形成环路之后走一圈不会产生负值就输出,原来就是判断负环,用SPFA很好用,运用队列,在判断负环的时候,用一个数组专门保存某个点的访问次数,超过了N次即可断定有负 ...

  9. POJ3259 Wormholes(SPFA判断负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

随机推荐

  1. 第15章 设备无关位图_15.3 DIB和DDB的结合

    第15章 设备相关位图_15.3 DIB和DDB的结合 15.3.1 从DIB创建DDB (1)hBitmap =CreateDIBitmap(…)——注意这名称会误导,实际上创建的是DDB 参数 说 ...

  2. Hibernate中saveOrUpdate()和merge()的区别

    this.getSession().merge(obj); this.getSession().saveOrUpdate(obj); saveOrUpdate(): saveOrUpdate()基本上 ...

  3. java 24 - 3 GUI之添加按钮

    需求:把按钮添加到窗体,并对按钮添加一个点击事件. A:创建窗体对象 B:创建按钮对象 C:把按钮添加到窗体 D:窗体显示 注意:这里对按钮添加点击事件,同样使用监听器. 但是,这里的按钮是组件,所以 ...

  4. Mysql备份系列(2)--mysqldump备份(全量+增量)方案操作记录

    在日常运维工作中,对mysql数据库的备份是万分重要的,以防在数据库表丢失或损坏情况出现,可以及时恢复数据. 线上数据库备份场景:每周日执行一次全量备份,然后每天下午1点执行MySQLdump增量备份 ...

  5. 浅析jQuery删除节点的三个方法

    jQuery提供了三种删除节点的方法,即remove(),detach()和empty().测试所用HTML代码:[html] view plaincopy<p title="选择你最 ...

  6. Linux虚拟机突然不能上网了

    之前是可以的,然后这次打开突然不能上网了. 更改配置后就好了: 配置如下: 我的问题是打开打开之后变成了OFF不是ON了.然后不管怎么改变O都失败了. 改为: 这样虚拟机这边就好了. 我们看下wind ...

  7. Codevs 1051 二叉树最大宽度和高度

    1501 二叉树最大宽度和高度  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver       题目描述 Description 给出一个二叉树,输出它的最大宽 ...

  8. 关于在线预览word,excel,ppt,pdf的需求处理方法。

    参考文档:http://www.cnblogs.com/wolf-sun/p/3574278.html 我选用的方案:先用office com组件生成pdf,然后使用pdf.js在线预览pdf文档.在 ...

  9. Linux下使用automake、autoconf生成configure文件

    一.生成configure过程中各文件之间的关系图 二.详细介绍 autoscan: 扫描源代码以搜寻普通的可移植性问题,比如检查编译器,库,头文件等,生成文件configure.scan,它是con ...

  10. rpc框架: thrift/avro/protobuf 之maven插件生成java类

    thrift.avro.probobuf 这几个rpc框架的基本思想都差不多,先定义IDL文件,然后由各自的编译器(或maven插件)生成目标语言的源代码,但是,根据idl生成源代码这件事,如果每次都 ...