LightOJ 1074 Extended Traffic SPFA 消负环
分析:一看就是求最短路,然后用dij,果断错了一发,发现是3次方,有可能会出现负环
然后用spfa判负环,然后标记负环所有可达的点,被标记的点答案都是“?”
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=+;
const int INF=0x7fffffff;
struct Edge{
int v;
LL w;
int next;
bool operator<(const Edge &e)const{
return w>e.w;
}
}edge[N*N];
int head[N],val[N],tot,n,m,p;
LL d[N];
void add(int u,int v,LL w){
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
bool inq[N];
queue<int>q;
int cnt[N];
bool c[N];
void dfs(int u){
c[u]=;
for(int i=head[u];~i;i=edge[i].next)
if(!c[edge[i].v])dfs(edge[i].v);
}
void spfa(int s){
for(int i=;i<=n;++i)
c[i]=cnt[i]=inq[i]=,d[i]=INF;
q.push(s),cnt[s]=,inq[s]=,d[s]=;
while(!q.empty()){
int u=q.front();
q.pop();
inq[u]=;
if(c[u])continue;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(c[v])continue;
if(d[v]>d[u]+edge[i].w){
d[v]=d[u]+edge[i].w;
if(inq[v])continue;
q.push(v);
++cnt[v];
inq[v]=;
if(cnt[v]>n)dfs(v);
}
}
}
}
int main(){
int T,cas=;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=;i<=n;++i)scanf("%d",&val[i]);
scanf("%d",&m);
memset(head,-,sizeof(head)),tot=;
for(int i=;i<m;++i){
int u,v;
scanf("%d%d",&u,&v);
LL w=(val[v]-val[u]);
w=w*w*w;
add(u,v,w);
}
spfa();
scanf("%d",&p);
printf("Case %d:\n",++cas);
for(int i=;i<=p;++i){
int u;
scanf("%d",&u);
if(c[u]||d[u]==INF||d[u]<)printf("?\n");
else printf("%d\n",d[u]);
}
}
return ;
}
LightOJ 1074 Extended Traffic SPFA 消负环的更多相关文章
- LightOJ - 1074 Extended Traffic(标记负环)
题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市u到另一个城市v的时间为:(au-av)^3,存在负环.问从第一个城市到达第k个城市所话的时间,如果不能到达,或者时间小于3输出?否则输出所花的 ...
- LightOj 1074 Extended Traffic (spfa+负权环)
题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...
- LightOJ 1074 Extended Traffic(spfa+dfs标记负环上的点)
题目链接:https://cn.vjudge.net/contest/189021#problem/O 题目大意:有n个站点,每个站点都有一个busyness,从站点A到站点B的花费为(busynes ...
- LightOJ - 1074 Extended Traffic (SPFA+负环)
题意:N个点,分别有属于自己的N个busyness(简称b),两点间若有边,则边权为(ub-vb)^3.Q个查询,问从点1到该点的距离为多少. 分析:既然是差的三次方,那么可能有负边权的存在,自然有可 ...
- LightOJ 1074 - Extended Traffic (SPFA)
http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic PDF (English) Stati ...
- LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)
Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...
- lightoj 1074 - Extended Traffic(spfa+负环判断)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J ...
- SPFA(负环) LightOJ 1074 Extended Traffic
题目传送门 题意:收过路费.如果最后的收费小于3或不能达到,输出'?'.否则输出到n点最小的过路费 分析:关键权值可为负,如果碰到负环是,小于3的约束条件不够,那么在得知有负环时,把这个环的点都标记下 ...
- (简单) LightOJ 1074 Extended Traffic,SPFA+负环。
Description Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked ...
随机推荐
- spring 中的<aop:advisor>和<aop:aspect>的区别
在AOP中有几个概念: — 方面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象.事务管理是J2EE应用中一个很好的横切关注点例子.方面用Spring的Advisor或拦截器实 ...
- .NET平台开源项目速览-最快的对象映射组件Tiny Mapper之项目实践
心情小札:近期换了工作,苦逼于22:00后下班,房间一篇狼藉~ 小翠鄙视到:"你就适合生活在垃圾堆中!!!" 晚上浏览博客园 看到一篇非常实用的博客:.NET平台开源项目速览(14 ...
- 读书计划——javascript dom 编程艺术(一)
用脑图把基础体系再捋清楚一边.
- 基于等待队列及poll机制的按键驱动代码分析和测试代码
按键驱动分析: #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> ...
- C++ 实现设计模式之观察者模式
1. 什么是观察者模式? 观察者模式(有时又被称为发布-订阅Subscribe>模式.模型-视图View>模式.源-收听者Listener>模式或从属者模式)是软件设计模式的一种.在 ...
- 【jpa】 引用包的问题
Hibernate使用Annotation(注解)需加 hibernate-jpa-2.0-api-1.0.0.Final.jar Hibernate3 ...
- The partner transaction manager has disabled its support for remote/network transactions.
http://technet.microsoft.com/en-us/library/cc753510(WS.10).aspx
- hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://poj.org/problem?id=1077 Eight Time Limit: 1000 ...
- oracle----修改表中的数据
1. 修改表中的数据:UPDATE语句: 语法: UPDTAE table_name SET column1 = value1,... [WHERE conditions] (2),无条件的更新(没有 ...
- maven 项目编译时候提示:Error building POM (may not be this project's POM).
编译时候提示Error building POM (may not be this project's POM)的错误,具体信息如下: [0] 'dependencies.dependency.ver ...