Extended Traffic

LightOJ-1074

  • 这题因为涉及到减法和三次方,所以可能会出现负圈。
  • 这里使用的算法叫做SPFA算法,这个可以用来判负圈和求解最短路。Bellman-Ford算法和SPFA算法很相似。
  • 这里要注意的是cnt出现次数应该要在哪里加。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=202;
int d[maxn];
int n,m;
int a[maxn];
int cnt[maxn];
bool vis[maxn];
bool circle[maxn];
struct edge{
int to;
int cost;
};
vector<edge>edges[maxn];
void SPFA(int s){
memset(d,INF,sizeof(d));
memset(vis,0,sizeof(vis));
memset(cnt,0,sizeof(cnt));
memset(circle,0,sizeof(circle));
d[s]=0;
queue<int> q;
q.push(s);
vis[s]=1;
while(!q.empty()){
int v=q.front();
q.pop();
vis[v]=0;
for(int i=0;i<edges[v].size();i++){
int u=edges[v][i].to;
int cost=edges[v][i].cost;
if(circle[u])
continue;
if(d[u]>d[v]+cost){
d[u]=d[v]+cost;
if(!vis[u]){
q.push(u);
vis[u]=1;
cnt[u]++;
}
if(cnt[u]>n)
circle[u]=1;
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
int k=0;
while(t--){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
edges[i].clear();
}
cin>>m;
for(int i=0;i<m;i++){
int from,to;
cin>>from>>to;
int diff=(a[to]-a[from])*(a[to]-a[from])*(a[to]-a[from]);
edges[from].push_back({to,diff});
}
SPFA(1);
int q;
cin>>q;
cout<<"Case "<<++k<<":"<<endl;
for(int i=0;i<q;i++){
int ter;
cin>>ter;
if(d[ter]<3||d[ter]==INF||circle[ter]){
cout<<"?"<<endl;
}else cout<<d[ter]<<endl;
}
}
return 0;
}

LightOJ-1074(SPFA判负圈+Bellman-Ford算法)的更多相关文章

  1. lightoj 1074 spfa判断负环

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

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

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

  3. UVA 11090 判负圈问题

    题目链接http://vjudge.net/problem/viewProblem.action?id=34650 题目大意: 给定n个点m条边的加权有向图,求平均权值最小的回路.平均权值=路径权值之 ...

  4. [HNOI2009]最小圈 分数规划 spfa判负环

    [HNOI2009]最小圈 分数规划 spfa判负环 题面 思路难,代码简单. 题目求圈上最小平均值,问题可看为一个0/1规划问题,每个边有\(a[i],b[i]\)两个属性,\(a[i]=w(u,v ...

  5. 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)

    传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...

  6. LightOj 1221 - Travel Company(spfa判负环)

    1221 - Travel Company PDF (English) Statistics problem=1221" style="color:rgb(79,107,114)& ...

  7. [P1768]天路(分数规划+SPFA判负环)

    题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于出现在本次试题上了 ...

  8. [poj3259]Wormholes(spfa判负环)

    题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...

  9. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

随机推荐

  1. poj2923 Relocation

    Description Emma and Eric are moving to their new house they bought after returning from their honey ...

  2. PowerShell随笔1---背景

    既然是随笔,那就想到什么说什么,既会分享主题知识,也会分享一些其他技巧和个人学习方法,供交流. 我一般学习一个东西,我都会问几个问题: 这东西是什么? 这东西有什么用,为什么会出现,出现是为了解决什么 ...

  3. CF1397-C. Multiples of Length

    CF1397-C. Multiples of Length 题意: 给出一个长度为\(n\)的序列,让你进行下面操作三次使得整个序列全部变为\(0\): ​ 在序列中选中一段序列\((l, r)\), ...

  4. Django的settings配置文件

    一.邮件配置 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.qq.com' EMAI ...

  5. 缓冲区溢出实验 2 sizeof/strlen

    实验环境.代码.及准备 https://www.cnblogs.com/lqerio/p/12870834.html vul2 下面只给出有漏洞部分的代码.Vul2问题为sizeof函数在计算时会考虑 ...

  6. 计算机网络 part2

    一.UDP协议 1.概述 UDP提供不可靠的服务,无连接(不存在建立连接的时延),首部开销相对TCP小,没有拥塞控制,提供最大努力交付,面向报文(无论多长的报文UDP也只加一个头部就往下发:TCP面向 ...

  7. 洛谷p1981 表达式求值

    #include <iostream> #include <cstdio> #include <cstring> using namespace std; char ...

  8. 记一次getshell

    水文涉及的知识点: Oday的挖掘 可以执行命令,但是有WAF , 命令执行的绕过 机器不出网,无法反弹 Echo写文件,发现只要写入php文件,后缀就重名为*,如1.php 变成1.* 通过上传 l ...

  9. KafkaBroker 简析

    Kafka 依赖 Zookeeper 来维护集群成员的信息: Kafka 使用 Zookeeper 的临时节点来选举 controller Zookeeper 在 broker 加入集群或退出集群时通 ...

  10. Keras 报错: Error when checking target: expected dense_4...

    笔者此处是一个回归任务, 最后一层是: ... pred = Dense(1)(x) 在最后一个Dense层前加上x = Flatten()(x)即可.