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个城市所花的时间,如果不能到达,或者时 ...
随机推荐
- jQuery $.post $.ajax用法
jQuery $.post $.ajax用法 jQuery.post( url, [data], [callback], [type] ) :使用POST方式来进行异步请求 参数: url (Stri ...
- cmd修改系统时间
time 11:15:00 修改时间 date 2015/11/25 修改日期
- JAVA中获取项目文件路径
在java中获得文件的路径在我们做上传文件操作时是不可避免的. web 上运行 1:this.getClass().getClassLoader().getResource("/" ...
- 高斯消元 分析 && 模板 (转载)
转载自:http://hi.baidu.com/czyuan_acm/item/dce4e6f8a8c45f13d7ff8cda czyuan 先上模板: /* 用于求整数解得方程组. */ #inc ...
- treap完全版模板
这是我综合poj1442 3481 2352的treap操作 得到treap完全版模板.(经测AC) 结构体Tree { int key; //键值 int size; //该子树总节点个数 int ...
- Bitset小结 (POJ2443 & HDU4920)
学了下bitset用法,从网上找的一些bitset用法,并从中调出一些常用的用法. 构造函数bitset<n> b; b有n位,每位都为0.参数n可以为一个表达式.如bitset<5 ...
- cd /d %~dp0
cd /d %~dp0 注册服务的bat要用到,普通的执行没关系,但是用“管理员角色”执行,默认的起始目录会到 c:\windows\system 下,用上面一句可以回到当前执行bat的目录
- bzoj2245: [SDOI2011]工作安排
费用流. 这道题的模型比较明显,拆点也是很容易看出来的. #include<cstdio> #include<algorithm> #include<cstring> ...
- WDF模型驱动程序开发
WDF驱动程序开发 1. 引言 设备驱动程序是硬件设备连接到计算机系统的软件接口,任何设备都必须有相应的驱动程序才能在计算机系统上正常工作.设备驱动程序的优劣直接关系到整个系统的性能和稳定性,因此,设 ...
- Run busybox httpd with php, sqlite
/*********************************************************************************** * Run busybox h ...