题目大意:每一个城市都有一定的繁荣度,然后给出m条有向边i->j,定义这条边的权值为pow(arr[j]-arr[i],3),然后给你q个询问,每个询问输入一个x。

然后问你点1到x的距离,如果小与3或者不可到达,那么输出?,否则的话就输出dis[x]。

题解:如果说这是一个无向图,那么如果这个图内存在负环,那么输出一定是?,因为点y假设可以到打1,那么就可以通过负环无限减小到y的距离,这样的话一定是小于3的。但这是个有向图,该怎么操作呢?我们可以把与负环相连接的元素给他打上标记,另外,如果说点z和负环相连,也就没必要对z进行松弛了...

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=1e18+;
const ll N=1e5+;
struct stu{
ll to,nxt;
ll weight;
}edge[N];
ll time1=;
ll n;
ll cnt=,head[N];
ll dis[N];
bool mark[N],cir[N];
ll num[N];
ll value[N];
void add(ll x,ll y,ll weight){
edge[cnt].to=y;
edge[cnt].weight=weight;
edge[cnt].nxt=head[x];
head[x]=cnt++;
}
void dfs(int v){
cir[v]=;
for(int i=head[v];i!=-;i=edge[i].nxt){
v=edge[i].to;
if(!cir[v]) dfs(v);
}
}
bool spfa(){
for(ll i=;i<=n;i++) dis[i]=INF;
memset(cir,,sizeof cir);
memset(mark,,sizeof mark);
memset(num,,sizeof num);
dis[]=;
queue<ll>que;
que.push();
num[]++;
mark[]=;
while(que.size()){
ll u=que.front();
que.pop();
mark[u]=;
for(ll i=head[u];i!=-;i=edge[i].nxt){
ll v=edge[i].to;
if(cir[v]) continue ;
if(dis[v]>dis[u]+edge[i].weight){
dis[v]=dis[u]+edge[i].weight;
if(!mark[v]) {
num[v]++;
mark[v]=;
que.push(v);
if(num[v]>=n) dfs(v);
}
}
}
}
return ;
}
void solve(){
memset(head,-,sizeof head);
cnt=;
cin>>n;
for(ll i=;i<=n;i++) cin>>value[i];
ll m,x,y;
cin>>m;
for(ll i=;i<=m;i++){
cin>>x>>y;
add(x,y,(value[y]-value[x])*(value[y]-value[x])*(value[y]-value[x]));
}
spfa();
ll problem;
cin>>problem;
ll time=;
printf("Case %d:\n",time1++);
while(problem--){
ll q;
cin>>q;
if(cir[q]||dis[q]<||dis[q]==INF){
cout<<"?"<<endl;
}
else{
cout<<dis[q]<<endl;
}
}
}
int main(){
ll t;
cin>>t;
while(t--) solve();
return ;
}

Extended Traffic LightOJ - 1074 (经典SPFA问题)的更多相关文章

  1. Extended Traffic LightOJ - 1074 spfa判断负环

    //判断负环 在负环内的城市输出? #include <iostream> #include <queue> #include <cstdio> #include ...

  2. Extended Traffic LightOJ - 1074

    题目链接:https://vjudge.net/problem/LightOJ-1074 思路:(busyness of destination - busyness of source)3 可能会是 ...

  3. LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)

    Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...

  4. lightoj 1074【spfa判负环】

    题意: 给你一幅图,dis(u->v)的权值就是(w[v]-w[u])*(w[v]-w[u])*(w[v]-w[u]),所以有可能是负的,给你n个询问,给出最短路,长度<3或者不可达输出& ...

  5. LightOJ 1074 - Extended Traffic (SPFA)

    http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic   PDF (English) Stati ...

  6. Light OJ 1074:Extended Traffic(spfa判负环)

    Extended Traffic 题目链接:https://vjudge.net/problem/LightOJ-1074 Description: Dhaka city is getting cro ...

  7. lightoj 1074 spfa判断负环

     Extended Traffic Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Sub ...

  8. LightOJ 1074 - Extended Traffic 【SPFA】(经典)

    <题目链接> 题目大意:有n个城市,每一个城市有一个拥挤度Ai,从一个城市I到另一个城市J的时间为:(A(v)-A(u))^3.问从第一个城市到达第k个城市所花的时间,如果不能到达,或者时 ...

  9. LightOj 1074 Extended Traffic (spfa+负权环)

    题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...

随机推荐

  1. hdu3367最大伪森林(并查集)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3367/ 题目要求一个连通图的最大伪森林,伪森林是一个最多有一个回路的图.我们只要用Kruskal最大生成树的策略 ...

  2. MySQL----DML(增删改表中数据)

    ##DML:增删改表中的数据 1.添加数据 *语法: *  insert into 表名(列名1,列名2,...列名n) values (值1,值2,...值n); *注意: 1.列名和值要一一对应. ...

  3. spring-boot-plus-v2.0发布了-让天下没有难写的代码

    spring-boot-plus是易于使用,快速,高效,功能丰富,开源的spring boot脚手架 前后端分离,专注于后端服务 目标 每个人都可以独立.快速.高效地开发项目! GITHUB | GI ...

  4. 企业级自动化部署方案——ansible实现tomcat自动安装和配置

    共耗时10多个小时 思路一 总体设计 ansible-playbook目录结构 [root@ansible ~]# tree /etc/ansible/roles/tomcat /etc/ansibl ...

  5. Selenium系列(九) - 针对alert窗口的处理(警告框、确认框、对话框)

    如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...

  6. SpringBoot 整合 MyCat 实现读写分离

    MyCat一个彻底开源的,面向企业应用开发的大数据库集群.基于阿里开源的Cobar产品而研发.能满足数据库数据大量存储:提高了查询性能.文章介绍如何实现MyCat连接MySQL实现主从分离,并集成Sp ...

  7. Mysql获取系统时间,年,月,日

      Mysql数据库中获取系统时间,年,月,日单个获取 获取当前系统日期时间:select SYSDATE() AS 系统日期时间; 获取当前系统年月日:select current_date AS ...

  8. Building Applications with Force.com and VisualForce (DEV401) (二二):Visualforce Componets (Tags) Library Part II

    Dev401-023:Visualforce Pages: Visualforce Componets (Tags) Library Part II   Apex:pageBlockTable1.A ...

  9. Spring-Cloud-Alibaba Nacos 启动失败,窗口一闪而过

    下载及启动 Nacos 下载地址:https://github.com/alibaba/nacos/releases 在Windows下,进入bin目录,双击 startup.cmd 即可运行 启动出 ...

  10. vscode下搭建typescript时提示"无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称"错误的解决方法

    根据网上的教程,安装了node.js后,再安装了typescript,,,这时候编译生成或者在vscode的终端里调用npm或者tsc --version时,总是提示 npm : 无法将"n ...