Extended Traffic

题目链接:

http://acm.hust.edu.cn/vjudge/contest/122685#problem/O

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

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

</big>

##Sample Output
<big>
Case 1:
3
4
Case 2:
?
</big> ##Hint
<big>
</big> <br/>
##题意:
<big>
对图中的N点,给出q个询问,输出起点到询问点之间的最短路.
若不可达或最短路小于3,输出?.
</big> <br/>
##题解:
<big>
本来看题目数据这么小,直接floyd就可以了,结果发现不太对.
如果图中存在负环,那么若询问负环中的点,都需要输出'?'.
所以在spfa中要增加一个环节,当判断到存在负环时,需要用dfs标记出当前负环上的所有点.
做题要仔细. 最短路要多考虑负环、重边、不联通.
</big> <br/>
##代码:
``` cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 5010
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int m,n,k;
int dis[maxn]; struct Edge
{
int v,cost;
Edge(int _v = 0, int _cost = 0)
{
v = _v;
cost = _cost;
}
};
vector<Edge>E[maxn];
void addedge(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
} bool circle[maxn];
void dfs(int u) {
circle[u] = 1;
for(int i = 0;i < E[u].size();i++) {
if(!circle[E[u][i].v]) dfs(E[u][i].v);
}
} queue<int> q;
bool inq[maxn];
int inq_cnt[maxn];
void spfa(int s) {
memset(inq, 0, sizeof(inq));
memset(inq_cnt, 0, sizeof(inq_cnt));
for(int i=1; i<=n; i++) dis[i] = inf; dis[s] = 0;
while(!q.empty()) q.pop();
q.push(s); inq_cnt[s]++; while(!q.empty()) {
int p = q.front(); q.pop();
inq[p] = 0;
for(int i = 0;i < E[p].size();i++){
int v = E[p][i].v;
if(circle[v]) continue;
if(dis[v] > dis[p] + E[p][i].cost){
dis[v] = dis[p] + E[p][i].cost;
if(!inq[v]) {
q.push(v);
inq[v] = 1;
inq_cnt[v]++;
if(inq_cnt[v] > n) {
dfs(v);
//return 0;
}
}
}
}
} //return 1;
} int num[maxn]; int main(void)
{
//IN; int t; cin >> t; int ca=1;
while(t--)
{
cin >> n;
memset(circle, 0, sizeof(circle));
//memset(first, -1, sizeof(first));
//edges = 0;
for(int i = 1;i <= n;i++)
E[i].clear();
for(int i=1; i<=n; i++) scanf("%d",&num[i]);
cin >> m;
for(int i=1; i<=m; i++) {
int u,v; scanf("%d %d",&u,&v);
int d = num[v] - num[u];
addedge(u,v,d*d*d);
}
spfa(1);
int q; cin >> q;
printf("Case %d:\n", ca++);
while(q--) {
int x; scanf("%d",&x);
if(circle[x] || dis[x]<3 || dis[x]==inf) printf("?\n");
else printf("%d\n", dis[x]);
}
} return 0;
}

LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)的更多相关文章

  1. LightOJ 1074 - Extended Traffic (SPFA)

    http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic   PDF (English) Stati ...

  2. LightOj 1074 Extended Traffic (spfa+负权环)

    题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...

  3. LightOJ 1074 Extended Traffic SPFA 消负环

    分析:一看就是求最短路,然后用dij,果断错了一发,发现是3次方,有可能会出现负环 然后用spfa判负环,然后标记负环所有可达的点,被标记的点答案都是“?” #include<cstdio> ...

  4. LightOJ - 1074 Extended Traffic(标记负环)

    题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市u到另一个城市v的时间为:(au-av)^3,存在负环.问从第一个城市到达第k个城市所话的时间,如果不能到达,或者时间小于3输出?否则输出所花的 ...

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

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

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

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

  7. LightOJ 1074 Extended Traffic(spfa+dfs标记负环上的点)

    题目链接:https://cn.vjudge.net/contest/189021#problem/O 题目大意:有n个站点,每个站点都有一个busyness,从站点A到站点B的花费为(busynes ...

  8. lightoj 1074 - Extended Traffic(spfa+负环判断)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J ...

  9. LightOJ 1074 - Extended Traffic 【SPFA】(经典)

    <题目链接> 题目大意:有n个城市,每一个城市有一个拥挤度Ai,从一个城市I到另一个城市J的时间为:(A(v)-A(u))^3.问从第一个城市到达第k个城市所花的时间,如果不能到达,或者时 ...

随机推荐

  1. cdev_init函数

    linux-2.6.22/include/linux/cdev.hstruct cdev {   struct kobject kobj;          // 每个 cdev 都是一个 kobje ...

  2. 面试题_103_to_124_关于 OOP 和设计模式的面试题

    这部分包含 Java 面试过程中关于 SOLID 的设计原则,OOP 基础,如类,对象,接口,继承,多态,封装,抽象以及更高级的一些概念,如组合.聚合及关联.也包含了 GOF 设计模式的问题. 103 ...

  3. gulp browser-sync自动刷新插件

    很久没弄gulp了,都快忘了,今天又来温习下browser-sync 自动刷新插件,在安装的时候出现以下提示: $ npm install browser-sync --save-dev> ws ...

  4. 【转载】Morris遍历二叉树 & BST(二叉搜索树) Traverse & 空间O(1) 时间O(n)

    因为做一道Leetcode的题目(前面博客有:link),需要用Space O(1)空间复杂度来中序遍历树, 看了Discuss,也上网搜了一下,发现空间O(1)可以用 Morris遍历的方法.方法介 ...

  5. ASP.NET MVC 学习4、Controller中添加SearchIndex页面,实现简单的查询功能

    参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-method ...

  6. FileZilla无法确定拖放操作的目标,由于shell未正确安装

    天有不测风云,突然间,用filezilla下载ftp上的文件到桌面的时候,提示"无法确定拖放操作目标.由于shell未正确安装" 解决办法很简单,执行如下几步就OK了 1.在CMD ...

  7. C++类的构造、拷贝构造、析构函数等

    1: 一个空的class在C++编译器处理过后就不再为空,编译器会自动地为我们声明一些member function,如果你写 class A{}; 编译器处理后,就相当于: class A{ pub ...

  8. zoj 2723 Semi-Prime

    // 题意都不好理解 我以为是求 一个数被分成2个素数和 然后是求分成2个素数积// 坑爹 忘记写 !=EOF 然后一直超时 然后换了几种 还是超时 一看别人代码 速度明显比我慢// 然后发现被自己坑 ...

  9. CKEditor如何统计文字数量

    今天在修改v5后台的比赛系统时,发现文本框需要限制输入字数.我们这个系统用的是3.6.3版本的,前台代码是这样的 <script> //编辑器 CKEDITOR.replace('matc ...

  10. 我的web前端之路 分享些前端的好书(转)

    WEB前端研发工程师,在国内算是一个朝阳职业,这个领域没有学校的正规教育,大多数人都是靠自己自学成才.本文主要介绍自己从事web开发以来 (从大二至今)看过的书籍和自己的成长过程,目的是给想了解Jav ...