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. zoj3623 Battle Ships

    Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense ...

  2. Codeforces Round #669 (Div. 2) B. Big Vova (枚举)

    题意:有一个长度为\(n\)的序列,你需要对其重新排序,构造一个新数组\(c\),\(c_{i}=gcd(a_{1},...,a{i})\)并且使得\(c\)的字典序最小. 题解:直接跑\(n\)次, ...

  3. 牛客编程巅峰赛S1第6场 - 黄金&钻石&王者 B.牛牛摆放花 (贪心)

    题意;将一组数重新排序,使得相邻两个数之间的最大差值最小. 题解:贪心,现将所有数sort一下,然后正向遍历,将数分配到新数组的两端,然后再遍历一次维护一个最大值即可. 代码: class Solut ...

  4. 8.PowerShell DSC之Push

    前言 LCM的默认mode就是push,所以对于push模式,我们直接就三步走 以下是示例: 1.编写配置 Authoring Configuration WebsiteTest { # Import ...

  5. Chapter Zero 0.1.4 计算机上常用的计算单位

    0.1 计算机硬件 计算机上常用的计算单位 容量单位: 计算机对于数据的判断依据有没有通电来记录信息,对于每个记录而言, 他只认识0或1,而0/1这个二进制单位我们成为bit. 因为bit太小,所以存 ...

  6. C++11特性-右值引用

    什么是左值,什么是右值 常见的误区有 = 左边的是左值,右边的是右值. 左值:具有存储性质的对象,即lvalue对象,是指要实际占用内存空间.有内存地址的那些实体对象,例如:变量(variables) ...

  7. codeforces 6D

    D. Lizards and Basements 2 time limit per test 2 seconds memory limit per test 64 megabytes input st ...

  8. u-boot 移植 --->7、u-bootl流程粗线条梳理

    通过前面的调试了解到s5pv210这个芯片的启动流程是需要将u-boot分为两部分的分别为SPL和u-boot.这里我使用网上的方式不直接使用u-boot的SPL连接脚本单独生成SPL的image而是 ...

  9. H5 Funny Games All In One

    H5 Funny Games All In One H5 游戏 盖楼 游戏 https://iamkun.github.io/tower_game/ https://github.com/iamkun ...

  10. jest all in one

    jest all in one ES Modules & TypeScript & React https://github.com/xgqfrms/FEAT/tree/master/ ...