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. CardView官方教程

    Create Cards CardView extends the FrameLayout class and lets you show information inside cards that ...

  2. Image.FrameDimensionsList 属性备注

    Image.FrameDimensionsList 属性 .NET Framework 2.0   获取 GUID 的数组,这些 GUID 表示此 Image 中帧的维数. 命名空间:System.D ...

  3. launch genymotion simulator from command line

    Command to launch genymotion headless - player --vm-name Nexus_4 if player is not already added to p ...

  4. The dialect was not set. Set the property hibernate.dialect

    The   dialect   was   not   set.   Set   the   property   hibernate.dialect load hibernate.cfg.xml 出 ...

  5. Material Design 设计--阴影的重要性

    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_cont ...

  6. Linux常用设置

    1.文件夹操作 创建-->mkdir NAME 删除-->rm NAME -i 删除前逐一询问确认 -f 直接删除,不确认 -r 将目录即以下档案逐一删除 例:删除所有C语言程序文档,删除 ...

  7. IE8按F12不显示开发人员工具窗口

    转:http://www.cnblogs.com/micromouse/archive/2010/07/11/1775174.html 网上搜来的,记录一下,免得以后忘了 F12将开发人员工具启动后, ...

  8. K2 学习笔记

    转:http://www.cnblogs.com/kaixuanpisces/category/149223.html k2 简介 工作流介绍 k2流程设计简介 K2流程设计详细版(图文)一 K2流程 ...

  9. SPFile的使用

    转:http://blog.csdn.net/pclzr/article/details/7591741 SPFile对应于SharePoint对象模型中的文件,它的使用方法与SPFolder类大致相 ...

  10. 我的ECshop二次开发从零开始

    我是一个EC新手,EC就算做再多的模板,肯定也满足不了我们的需要,更何况各行有各行的门道,EC统一做出来的模板也不一定合适于我们这个行业用,因此,只有我们真正掌握了自己做模板,修改模板的功夫,才能真正 ...