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. pta—紧急救援 (dijkstra)

    题目连接:https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 题面: 作为一个城市的应急救援队伍 ...

  2. CodeForces 630Q Pyramids(数学公式)

    IT City administration has no rest because of the fame of the Pyramids in Egypt. There is a project ...

  3. B. Modular Equations

    Last week, Hamed learned about a new type of equations in his math class called Modular Equations. L ...

  4. Codeforces Round #687 (Div. 2, based on Technocup 2021 Elimination Round 2) D. XOR-gun (二进制,异或,前缀和)

    题意:给你一组非递减的数,你可以对两个连续的数进行异或,使其合并为一个数,问最少操作多少次使得这组数不满足非递减. 题解:首先,给出的这组数是非递减的,我们考虑二进制,对于三个连续的非递减的最高位相同 ...

  5. P3376 【模板】网络最大流——————Q - Marriage Match IV(最短路&最大流)

    第一道题是模板题,下面主要是两种模板,但都用的是Dinic算法(第二个题也是) 第一题: 题意就不需要讲了,直接上代码: vector代码: 1 //invalid types 'int[int]' ...

  6. 阿里云 MaxCompute(ODPS)

    大数据产品架构 BASE - Dataworks ODPS - MaxCompute ODPS 功能组成(Open Data Process Service) ODPS 是旧称,阿里云公有云服务中现称 ...

  7. codeforces 11B Jumping Jack

    Jack is working on his jumping skills recently. Currently he's located at point zero of the number l ...

  8. vue 自动注册全局组件

    vue 自动注册全局组件 vue 注册全局组件的方式 const plugins = { install(Vue) { const requireComponent = require.context ...

  9. The Weekly Web Dev Challenge: Emoji Ratings

    The Weekly Web Dev Challenge: Emoji Ratings /* DESCRIPTION: You job is to enable users to give a rat ...

  10. node.js & Unbuntu Linux & nvm & npm

    node.js & Unbuntu Linux & nvm & npm https://websiteforstudents.com/install-the-latest-no ...