Extended Traffic LightOJ - 1074 (经典SPFA问题)
题目大意:每一个城市都有一定的繁荣度,然后给出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问题)的更多相关文章
- Extended Traffic LightOJ - 1074 spfa判断负环
//判断负环 在负环内的城市输出? #include <iostream> #include <queue> #include <cstdio> #include ...
- Extended Traffic LightOJ - 1074
题目链接:https://vjudge.net/problem/LightOJ-1074 思路:(busyness of destination - busyness of source)3 可能会是 ...
- LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)
Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...
- lightoj 1074【spfa判负环】
题意: 给你一幅图,dis(u->v)的权值就是(w[v]-w[u])*(w[v]-w[u])*(w[v]-w[u]),所以有可能是负的,给你n个询问,给出最短路,长度<3或者不可达输出& ...
- LightOJ 1074 - Extended Traffic (SPFA)
http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic PDF (English) Stati ...
- Light OJ 1074:Extended Traffic(spfa判负环)
Extended Traffic 题目链接:https://vjudge.net/problem/LightOJ-1074 Description: Dhaka city is getting cro ...
- lightoj 1074 spfa判断负环
Extended Traffic Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Sub ...
- LightOJ 1074 - Extended Traffic 【SPFA】(经典)
<题目链接> 题目大意:有n个城市,每一个城市有一个拥挤度Ai,从一个城市I到另一个城市J的时间为:(A(v)-A(u))^3.问从第一个城市到达第k个城市所花的时间,如果不能到达,或者时 ...
- LightOj 1074 Extended Traffic (spfa+负权环)
题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...
随机推荐
- hdu3973 AC's String 线段树+字符串hash
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3973/ 题意是:给出一个模式串,再给出一些串组成一个集合,操作分为两种,一种是替换模式串中的一个字符,还有一种是 ...
- SpringMVC最详细笔记partⅠ
一.springMVC-quickStar 解决maven加载项目过慢 archetypeCatalog internal 导入依赖 <!-- 版本锁定 --> <propertie ...
- mybatis入门二-----增删改查
一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: <?xml version="1.0&quo ...
- shell大全
1.shell判断文件是否存在 http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html
- Java并发基础04. 线程技术之死锁问题
我们知道,使用 synchronized 关键字可以有效的解决线程同步问题,但是如果不恰当的使用 synchronized 关键字的话也会出问题,即我们所说的死锁.死锁是这样一种情形:多个线程同时被阻 ...
- Oracle给权限和同义词
在同一个DB下,用户A创建了一个Table(student),用户B无法访问.如果B想要访问,就需要A赋予B权限. 登录用户A执行下面语句: GRANT SELECT, INSERT, UPDATE, ...
- 1058 A+B in Hogwarts (20分)(水)
If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- a ...
- PTA数据结构与算法题目集(中文) 7-31
PTA数据结构与算法题目集(中文) 7-31 7-31 笛卡尔树 (25 分) 笛卡尔树是一种特殊的二叉树,其结点包含两个关键字K1和K2.首先笛卡尔树是关于K1的二叉搜索树,即结点左子树的所有 ...
- JUnit 3.8.1 源码学习简记
先记录一个整理的流程 1.首先使用TestSuite获取一个TestCase中的所有测试方法(方法名以test开头的方法),为每个方法生成一个TestCase实例并保存,实例中有个字段保存对应的方法名 ...
- C++模板心得
C++模板心得 我开始学模板的时候一脸懵逼,真的看不懂模板是怎么作用的.因为大多数人的代码把模板声明和函数.类的声明分行写,让我以为模板的作用是全局的,实际上应该像如下理解. 函数模板 templat ...