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 ...
随机推荐
- jQuery事件绑定和委托
可以用多种方法实现,on() . bind() . live() . delegate() ,还有one(). 有时我们可能会像下面这样绑定一个事件: $("#div1"). ...
- C#序列化/反序列化
序列化:将实体类以二进制或者XML的形式保存到磁盘或者内存中. 反序列化:将序列化的二进制文件和XML文件解析成实体类. 例如下面的二进制序列化与反系列化: using System; using S ...
- IOC----LightInject
开源项目 引入 LightInject.cs 默认服务 new ServiceContainer 注册跟获取获取服务 container.Register<IFoo, Foo>();con ...
- find grep
grep grep -rn "hello,world!" * #递归查找当前目录下所有包含hello,world的文件 grep -C number pattern files : ...
- Binding的源和路径
书上写着:Binding的源也就是数据的源头.Binding对于源的要求很简单-只要他是一个对象!并且通过属性(Property)公开自己的数据,它就可以作为Binding的源了.就像上一篇我写的那个 ...
- log4j记录运行日志
1.在工程中导入log4j-1.2.15.jar的jar包2.新建测试类 package control; import org.apache.log4j.Logger; import org.apa ...
- 【算法】桶排->冒泡->快排
啊哈 算法 http://pan.baidu.com/s/1jGGl2SI http://pan.baidu.com/s/15C1oq 1 节 最快最简单的排序——桶排序 在我们生活的这个世界中到处都 ...
- .net entity framework 泛型 更新与增加记录
static public bool SaveOrUpdate<T>(T entity) where T: class { bool result = false; using (wech ...
- hdu 1423
最长公共上升子序列:O(n*m)的算法: #include<cstdio> #include<cstring> #define maxn 1000 using namespac ...
- javascript pattern
bing:javascript pattern baidu:module pattern javascript高级程序设计 姊妹篇:ajax高级程序设计 http://wenku.baidu.com/ ...