题意:

给你一幅图,dis(u->v)的权值就是(w[v]-w[u])*(w[v]-w[u])*(w[v]-w[u]),所以有可能是负的,给你n个询问,给出最短路,长度<3或者不可达输出"?";

思路:

spfa判个负环就好了;

但是。。。。没有考虑与负环相连的所有都是会越来越来,所以每次有负环,要搜一下,把整块拿掉

100

6

11 12 9 8 7 10

6

5 6

1 2

2 5

3 1

5 4

4 3

100

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int>PII;
const double eps=1e-5;
const double pi=acos(-1.0);
//const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const int N=4e4+10; struct Edge{
int w;
int to;
int next;
};
Edge q[N];
int head[N],tol;
int dis[210],n;
int w[210],num[210];
bool vis[210]; bool in[210];
void dfs(int u)
{
in[u]=1;
for(int i=head[u];i!=-1;i=q[i].next)
{
int to=q[i].to;
if(!in[to])
dfs(to);
}
} queue<int>que;
void spfa()
{
while(!que.empty())
que.pop();
for(int i=1;i<=n;i++)
{
vis[i]=false;
num[i]=0;
dis[i]=INF;
}
vis[1]=true;
dis[1]=0;
num[1]++;
que.push(1);
while(!que.empty())
{
int u=que.front();
que.pop();
vis[u]=false; for(int i=head[u];i!=-1;i=q[i].next)
{
int v=q[i].to;
if(in[v])
continue;
if(dis[v]>dis[u]+q[i].w)
{
dis[v]=dis[u]+q[i].w;
if(!vis[v])
{
vis[v]=true;
num[v]++;
if(num[v]>n)
{
dfs(v);
continue;
}
que.push(v);
}
}
}
}
} void add(int u,int v,int w)
{
q[tol].w=w;
q[tol].to=v;
q[tol].next=head[u];
head[u]=tol++;
} void init()
{
tol=0;
memset(head,-1,sizeof(head));
memset(in,0,sizeof(in));
} int main()
{
int m,Q;
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d",&w[i]);
init();
scanf("%d",&m);
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v,(w[v]-w[u])*(w[v]-w[u])*(w[v]-w[u]));
}
spfa();
printf("Case %d:\n",cas++);
scanf("%d",&Q);
while(Q--)
{
int u;
scanf("%d",&u);
if(in[u]||dis[u]==INF||dis[u]<3)
puts("?");
else
printf("%d\n",dis[u]);
}
}
return 0;
}

lightoj 1074【spfa判负环】的更多相关文章

  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. LightOj 1221 - Travel Company(spfa判负环)

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

  4. POJ 3259 Wormholes(SPFA判负环)

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

  5. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

  6. spfa判负环

    bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...

  7. poj 1364 King(线性差分约束+超级源点+spfa判负环)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14791   Accepted: 5226 Description ...

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

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

  9. BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划

    BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...

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

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

随机推荐

  1. MVC入门——删除页

    添加Action DeleteUserInfo using System; using System.Collections.Generic; using System.Linq; using Sys ...

  2. Creating Tabbed Applications

    新建一个空工程,如图 新建类 using System; using UIKit; namespace TabbedApplication { public class TabController : ...

  3. LeetCode(15)题解--3Sum

    https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c i ...

  4. 2016/08/18 select

    1.//得到select项的个数 2.jQuery.fn.size = function(){ 3. return jQuery(this).get(0).options.length; 4.} 5. ...

  5. Golang RPC 性能测试

    Golang RPC 性能测试 | KDF5000 http://kdf5000.com/2017/03/28/Golang-RPC-性能测试/

  6. Kotlin基本语法笔记2之类型检测及自动类型转换、循环

    类型检测及自动类型转换 is运算符用于检测一个表达式是否为某类型的一个实例检测出为某类型后,检测后的分支中可以直接当作该类型使用,无需显示转换 fun getStringLength(obj: Any ...

  7. Machine Learning in Action(3) 朴素贝叶斯算法

    贝叶斯决策一直很有争议,今年是贝叶斯250周年,历经沉浮,今天它的应用又开始逐渐活跃,有兴趣的可以看看斯坦福Brad Efron大师对其的反思,两篇文章:“Bayes'Theorem in the 2 ...

  8. Impala 安装笔记1一Cloudera CDH4.3.0安装

    Impala是Cloudera在受到Google的Dremel启发下开发的实时交互SQL大数据查询工具,Impala没有再使用缓慢的Hive+MapReduce批处理,而是通过使用与商用并行关系数据库 ...

  9. govendor

    cd  到工程目录. govendor init : 初始化 govendor fetch : 拉取包 go 1.6以后编译go代码会优先从vendor目录先寻找依赖包: controllers\ar ...

  10. 【 spring配置文件详解】

    转自: http://book.51cto.com/art/201004/193743.htm Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的 ...