题意:N个顶点, M条双向边, W条权值为负的单向边。求是否存在负环。

思路:首先你要懂bellman-ford或spfa。。这是基础的spfa判断是否存在负环的题,存在负环的节点会重复入队(因为最短路在不断变小), 所以只要有节点重复入队超过n次,即可判断存在负环(即开一个数组记录节点入队次数)。

总结:本来是想求出每对节点之间的最短路,看是否存在负的,结果果断TLE。后来才想起spfa可以处理负环云云~~

AC代码:

 #include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<vector>
#include<climits>
#include<cstring>
using namespace std;
#define maxn 600
#define INF 10000000
struct node
{
int to, dist;
};
vector<node> g[maxn];
int n, m, w;
int cnt[maxn], d[maxn];
void input()
{
scanf("%d%d%d", &n, &m, &w);
for(int i = ; i < n+; i++) g[i].clear();
for(int i = ; i < m; i++){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
g[a].push_back((node){b, c});
g[b].push_back((node){a, c});
}
for(int i = ; i < w; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
c = -c;
g[a].push_back((node) {b, c});
}
}
bool spfa(int s)
{
for(int i = ; i <= n; i++) d[i] = INF, cnt[i] = ;
d[s] = ;
cnt[s] = ;
queue<int> q;
q.push(s);
bool inq[maxn]; memset(inq, , sizeof(inq));
inq[s] = true;
while(!q.empty()){
int t = q.front(); q.pop();
inq[t] = false;
int l = g[t].size();
for(int i = ; i < l; i++){
int to = g[t][i].to, dist = g[t][i].dist;
if(d[to] > d[t] + dist) {
d[to] = d[t] + dist;
if(!inq[to]){
inq[to] = ;
cnt[to]++;
if(cnt[to] >= n) return true;
q.push(to);
}
}
}
}
return false;
}
void work()
{
input();
if(spfa()) printf("YES\n");
else printf("NO\n");
} int main()
{
int t ; cin>>t;
while(t--){
work();
}
return ;
}

poj-3259-wormholes-spfa-判负环的更多相关文章

  1. POJ 3259 Wormholes(SPFA判负环)

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

  2. Wormholes POJ 3259(SPFA判负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  3. POJ 3259 Wormholes (判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...

  4. POJ 3259 Wormholes ( SPFA判断负环 && 思维 )

    题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...

  5. POJ 3259 Wormholes( bellmanFord判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36425   Accepted: 13320 Descr ...

  6. poj 3621 二分+spfa判负环

    http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...

  7. [poj3259]Wormholes(spfa判负环)

    题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...

  8. POJ 3259 Wormholes 最短路+负环

    原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...

  9. POJ3259 :Wormholes(SPFA判负环)

    POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...

  10. Poj 3259 Wormholes(spfa判负环)

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

随机推荐

  1. javascript 内部函数的定义及调用

    内部函数:定义在另一个函数中的函数 例如: <script> function outer(){ function inner(){ } } </script> inner() ...

  2. configure: error: C++ compiler cannot create executables

    今天装虚拟机LNMP环境 安装报错:configure: error: C++ compiler cannot create executables 这是因为 gcc 组件不完整,执行安装 yum i ...

  3. Java基础之序列化对象——反序列化对象(DeserializeObjects)

    控制台程序,使用如下代码能读入包含Junk对象的文件: import java.io.*; import java.nio.file.*; class DeserializeObjects { pub ...

  4. 使用javap反编译class文件

    一个普通的Java类: package org.ccnt.concurrence; public class VolatileTest { public static volatile int rac ...

  5. p++ ++p

    1.P++是先使用这个变量,使用完了再加1,你的例子就是,先输出,再加一++P是先加一,在使用变量 eg: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...

  6. .NET: C#: StopWatch

    StopWatch class is used for calculate the timespan for that procedure. In Debug Mode it will be very ...

  7. java中Date的getTime() 方法奇葩问题

    今天遇到了一个奇葩问题: 从数据库中读取了3个Date类型的数据: DATE1:2015-03-12 12:10:42 DATE2:2015-03-12 12:04:40 DATE3:2015-03- ...

  8. DataTable 筛选数据

    //使用聚合函数 max ,sum  ,count .... private void ComputeBySalesSalesID(DataSet dataSet) {     // Presumes ...

  9. Oracle的分页查询

    --1:无ORDER BY排序的写法.(效率最高)--(经过测试,此方法成本最低,只嵌套一层,速度最快!即使查询的数据量再大,也几乎不受影响,速度依然!) SELECT * FROM (SELECT ...

  10. 夺命雷公狗---微信开发59----在线点播电影网1之ckplayer播放器

    我们节课程就要开始写一个小项目了,这项目主要是写一个在线点播电影影网的,我们用到的播放器是ckplayer ckplayer基本介绍: ckplayer的全称是:超酷flv播放器,他是一款用于网页上播 ...