题意:

给你一幅图,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. 02 svn 文件提交与目录结构

    一:文件操作给svn服务器提交程序文件: ① 在被提交文件的身上点击右键------> tortoiseSVN----->add ② 在被提交文件身上点击右键------> comm ...

  2. linux下修改主机名hostname方法(转载)

    查看主机名: 在终端输入hostname 永久修改 第一步:#hostname oratest 第二步:修改/etc/sysconfig/network中的hostname 第三步:修改/etc/ho ...

  3. 《学习opencv》笔记——矩阵和图像操作——cvConvertScale,cvConvertScaleAbs,cvCopy and cvCountNonZero

    矩阵和图像的操作 (1)cvConvertScale函数 其结构: void cvConvertScale( //进行线性变换,将src乘scale加上shift保存到dst const CvArr* ...

  4. php判断某字符串是否不以数字或其他特殊字符开头

    if(preg_match("/^[^\d-.,:]/",$addr)){ echo $addr.'不是数字或其他特殊字符开头'; }

  5. iOS开发-14款状态栏(StatusBar)开源软件

    本文转载至 http://mobile.51cto.com/hot-418125.htm 之前逛街看到移动做推广,有一个定位应用挺好的,合理的利用了状态栏,做了一些消息提醒和隐藏动画,自己回家就做了一 ...

  6. iframe调用父页面js函数 方法 元素

    在一个页面中添加iframe,但是有时需要与父页面进行通信,传递参数. 网上总结有以下方法: 一.iframe标签中   src属性传参 <iframe src="test.jsp?i ...

  7. Go Concurrency Patterns: Timing out, moving on

    https://blog.golang.org/go-concurrency-patterns-timing-out-and

  8. 调用远程service aidl接口定义

    Android studio 查看aidl定义的文件:当你进入你的AIDL文件并编写好了之后,点击AS上方菜单栏中的Build->Make Project,之后便可以在当前工程的app/buil ...

  9. nyoj 1030 hihocoder 1338

    题目链接1: 点这里打开. 题目链接2:   点击打开链接 思路:dp,dp[i][j] 表示某个人在区间 i,j上的得分. sum数组表示前 n 项和, num 数组用来存输入的数字. 因为取数字是 ...

  10. coeforces 665D D. Simple Subset(最大团orsb题)

    题目链接: D. Simple Subset time limit per test 1 second memory limit per test 256 megabytes input standa ...