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. 前、中、后序遍历随意两种是否能确定一个二叉树?理由? && 栈和队列的特点和区别

    前序和后序不能确定二叉树理由:前序和后序在本质上都是将父节点与子结点进行分离,但并没有指明左子树和右子树的能力,因此得到这两个序列只能明确父子关系,而不能确定一个二叉树. 由二叉树的中序和前序遍历序列 ...

  2. Java基础(第二期)

    数据类型扩展以及面试题讲解 整数拓展:进制 int i=10; int i2=010; //八进制0 int i3=0x10; //十六进制0x 0~9 A~F 16 相关进制转换自行学习,用的不多 ...

  3. Vulkan与DX11交互

    Demo演示地址07_wintest 有什么用 在android平台主流是用opengl es,android下vulkan与opengles纹理互通. 而在win平台,主流游戏还用的是DX11,如果 ...

  4. codeforces 875B

    B. Sorting the Coins time limit per test 1 second memory limit per test 512 megabytes input standard ...

  5. SPOJ LCS Longest Common Substring(后缀自动机)题解

    题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...

  6. hdu5303贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=5303 说一下题目大意.. 有一个长为L的环..你家在原点位置0,那么剩下L-1个点上种有一些树, 给你树的位置和 ...

  7. 手把手搭建一套基于 Sentry 的异常监控系统

    手把手搭建一套基于 Sentry 的异常监控系统 Sentry 开源版 DevOps refs https://github.com/getsentry/sentry sentry-anomaly-m ...

  8. 钓鱼教程 All In One

    钓鱼教程 All In One youtube https://www.youtube.com/results?search_query=钓鱼教程&sp=CAM%3D 钓鱼证 https:// ...

  9. CSS3 & transition & animation

    CSS3 & transition & animation https://developer.mozilla.org/en-US/docs/Web/CSS/transition-ti ...

  10. modal 弹框遮罩层,滚动穿透bug 解决方案

    modal 弹框遮罩层,滚动穿透bug 解决方案 parent component 动态设置 lock css const computedClassName = classNames( 'activ ...