LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)
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+标记负环点)的更多相关文章
- LightOJ 1074 - Extended Traffic (SPFA)
http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic PDF (English) Stati ...
- LightOj 1074 Extended Traffic (spfa+负权环)
题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...
- LightOJ 1074 Extended Traffic SPFA 消负环
分析:一看就是求最短路,然后用dij,果断错了一发,发现是3次方,有可能会出现负环 然后用spfa判负环,然后标记负环所有可达的点,被标记的点答案都是“?” #include<cstdio> ...
- LightOJ - 1074 Extended Traffic(标记负环)
题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市u到另一个城市v的时间为:(au-av)^3,存在负环.问从第一个城市到达第k个城市所话的时间,如果不能到达,或者时间小于3输出?否则输出所花的 ...
- Wormholes---poj3259(最短路 spfa 判断负环 模板)
题目链接:http://poj.org/problem?id=3259 题意是问是否能通过虫洞回到过去: 虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts. 我们把虫洞看成是一条负权路,问 ...
- POJ 3259 Wormholes【最短路/SPFA判断负环模板】
农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径 ...
- LightOJ 1074 Extended Traffic(spfa+dfs标记负环上的点)
题目链接:https://cn.vjudge.net/contest/189021#problem/O 题目大意:有n个站点,每个站点都有一个busyness,从站点A到站点B的花费为(busynes ...
- lightoj 1074 - Extended Traffic(spfa+负环判断)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J ...
- LightOJ 1074 - Extended Traffic 【SPFA】(经典)
<题目链接> 题目大意:有n个城市,每一个城市有一个拥挤度Ai,从一个城市I到另一个城市J的时间为:(A(v)-A(u))^3.问从第一个城市到达第k个城市所花的时间,如果不能到达,或者时 ...
随机推荐
- BZOJ 2301 Problem b(莫比乌斯函数)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2301 题意:每次给出a,b,c,d,K.求有多少数对(x,y)满足a<=x< ...
- [HDOJ2874]Connections between cities(LCA, 离线tarjan)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 这题有不连通的情况,特别注意. 觉得是存query的姿势不对,用前向星存了一遍,还是T…… /* ...
- 1002: A+B for Input-Output Practice (II)
问题描述: http://acm.wust.edu.cn/problem.php?id=1002&soj=0 代码实现: import java.util.Scanner; public cl ...
- ogre, dx, opengl坐标矩阵
opengl 右手坐标系 列向量 左乘 列主序存储矩阵osg 右手坐标系 行向量 右乘 行主序存储矩阵d3d 左手坐标系 行向量 右乘 行主序存储矩阵ogre 右手坐标系 列向量 ...
- 在Windows下通过命令行或者.bat文件统计一个目录中文件数量
在Windows下面怎样通过命令行统计一个目录中文件的数量,或者说,如果在一个.bat文件中,统计一个目录中的文件数量? 我原来以为是不可能的,要编一个vbs程序什么的,后来到网上找了下,发现还真是可 ...
- Discuz 7.2 /faq.php SQL注入漏洞
测试方法: 提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! Discuz 7.2 /faq.php SQL注入漏洞 http://www.xxx.com/faq.php?a ...
- sql2005数据库转换成sql2000
第一步:在SQL2005中生成脚本文件 ① 在2005中选中要进行转换的那个数据库,鼠标“右键”选择—“属性”—“选项”:修改“兼容级别”为“SQL Server 2000 (80)”: ② ...
- poj 2773 Happy 2006
// 题意 :给你两个数 m(10^6),k(10^8) 求第k个和m互质的数是什么这题主要需要知道这样的结论gcd(x,n)=1 <==> gcd(x+n,n)=1证明 假设 gcd(x ...
- db2数据库sql报错信息
sqlcode sqlstate 说明 000 00000 SQL语句成功完成 01xxx SQL语句成功完成,但是有警告 +012 01545 未限定的列名被解释为一个有相互关系的引用 +098 ...
- Java Error和Exception区别
Error和Exception都继承自Throwable: 二者不同之处: Exception: 1.可以是可被控制(checked)或者不可控制(unchecked): 2.表示一个由程序员导致的错 ...