题意: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. linux:磁盘的分割、检验、格式化与挂载

    新增一颗磁碟: 1.对磁碟进行分割,以建立可用的partition 2.对该分割槽partition进行格式化(format),以建立系统可用的filesystem 3.若要仔细点,可对刚刚建立的fi ...

  2. InterProScan 5.17-56.0 安装和使用

    InterProScan 5.18-57.0 安装和使用,目前最新版的interproscan 引用自 每日一生信--interproscan安装及使用(终结版)原文官网:http://code.go ...

  3. 机器学习笔记:Gradient Descent

    机器学习笔记:Gradient Descent http://www.cnblogs.com/uchihaitachi/archive/2012/08/16/2642720.html

  4. FAQ: C++中定义类的对象:用new和不用new有何区别?

    C++用new创建对象和不用new创建对象的区别解析 作者: 字体:[增加 减小] 类型:转载 时间:2013-07-26 我要评论 在C++用new创建对象和不用new创建对象是有区别的,不知你是否 ...

  5. AJAX简单的数据增删改与分页应用

    运行截图: PageBar.js: /* * 说明: * 整体思想,1.第一页时不显示:首页,上一页, * 2.最后一页时不显示:下一页,尾页 * 3.中间有 5 页导航, * 若:3.1.(总页数& ...

  6. csu oj 1330 字符识别?

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1330 1330: 字符识别? Time Limit: 1 Sec  Memory Limit: 1 ...

  7. [摘录]quarts:feature

    Features of Quartz Runtime Environments Quartz can run embedded within another free standing applica ...

  8. .NET C#: NameValueCollection

    NameValueCollection class is in System.Collection.Specialized assembly. Unlike with HashTable, NameV ...

  9. 使用git做服务器端代码的部署

    传统部署方案     windows 远程桌面     FTP/SFTP     登录服务器pull github代码     Phing(PHP专业部署工具) git 自动部署流程图   服务器端准 ...

  10. 转:装完Centos7提示Initial setup of CentOS Linux 7 (core)

    在用U盘装完CentOS后,重新开机启动后显示: Initial setup of CentOS Linux 7 (core) 1) [x] Creat user 2) [!] License inf ...