POJ3259(虫洞)
题目大意:给你一张图,先输入m条双向边(权值为正),再输入w条单向边(权值为负),判断是否有负环
题目思路:bellman-ford或者SPFA都行,我用的是SPFA(因为和POJ1860类似,就不加详细注释了)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
using namespace std;
#define gamma 0.5772156649015328606065120 //欧拉常数
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 1001000
#define maxn 300000
typedef long long LL;
typedef pair<int,int> PII; int n;
struct Node
{
int over; ///终点
int v; ///权值
int next;
} node[5500];
int n_cnt,l[505]; ///头节点
int vis[505],d[505],cnt[505]; void init()
{
memset(l,-1,sizeof(l));
memset(d,inf,sizeof(d)); ///到节点需要的花费
memset(vis,0,sizeof(vis)); ///是否在队列中
memset(cnt,0,sizeof(cnt)); ///判断有负环的标记(入队超过n次就有负环)n是题目所给节点数
n_cnt=0;
} void add(int &x,int &y,int &v)
{
node[n_cnt].over=y;
node[n_cnt].v=v;
node[n_cnt].next=l[x];
l[x]=n_cnt++;
} int SPFA()
{
int i,j;
queue<int>q;
q.push(1);
d[1]=0;
while(!q.empty())
{
int i=q.front();
q.pop();
vis[i]=0;
if(++cnt[i]>n) return 1;
for(j=l[i]; j!=-1; j=node[j].next)
{
int pos=node[j].over;
if(d[pos]>d[i]+node[j].v)
{
d[pos]=d[i]+node[j].v;
if(!vis[pos])
{
q.push(pos);
vis[pos]=1;
}
}
}
} return 0;
} int main()
{
int i,j,group,m,w,x,y,v;
//freopen("lxx.txt","r",stdin);
scanf("%d",&group);
while(group--)
{
init();
scanf("%d%d%d",&n,&m,&w);
for(i=0; i<m; ++i)
{
scanf("%d%d%d",&x,&y,&v);
add(x,y,v);
add(y,x,v);
}
for(i=0; i<w; ++i)
{
scanf("%d%d%d",&x,&y,&v);
v=-v;
add(x,y,v);
}
if(SPFA())printf("YES\n");
else printf("NO\n");
}
return 0;
}
POJ3259(虫洞)的更多相关文章
- POJ-3259 Wormholes---SPFA判断有无负环
题目链接: https://vjudge.net/problem/POJ-3259 题目大意: 农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的 ...
- poj3259 Wormholes(Bellman-Ford判断负圈)
https://vjudge.net/problem/POJ-3259 一开始理解错题意了,以为从A->B一定得走路,B->A一定得走虫洞.emmm其实回来的时候可以路和虫洞都可以走,只要 ...
- POJ3259 :Wormholes(SPFA判负环)
POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...
- SPFA穿越虫洞——负权回路得判断
poj3259 题目大意:穿越虫洞可以回到过去(时间--)所以能不能让时间倒流呢,就是判断有没有负权回路这次尝试用SPFA算法,也可以复习一下链式前向星 准备工作,队列q,spfa算法得有点就在于这个 ...
- poj3259 Wormholes【Bellman-Ford或 SPFA判断是否有负环 】
题目链接:poj3259 Wormholes 题意:虫洞问题,有n个点,m条边为双向,还有w个虫洞(虫洞为单向,并且通过时间为倒流,即为负数),问你从任意某点走,能否穿越到之前. 贴个SPFA代码: ...
- POJ3259 Wormholes 【spfa判负环】
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- hzwer模拟赛 虫洞
[题目描述] N个虫洞,M条单向跃迁路径.从一个虫洞沿跃迁路径到另一个虫洞需要消耗一定量的燃料和1单位时间.虫洞有白洞和黑洞之分.设一条跃迁路径两端的虫洞质量差为delta. 1.从白洞跃迁到黑洞,消 ...
- poj3259 bellman——ford Wormholes解绝负权问题
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35103 Accepted: 12805 Descr ...
- poj3259 最短路判环
题意:有一些点.一些道路和一些虫洞,道路是双向的,连接两点,花费正的时间,而虫洞是单向的,连接两点,可以使时间倒退,求是否能够回到过去. 只要明确回到过去其实就是当出现一个负环的时候,不断沿这个环走, ...
随机推荐
- [HTML5] Render Hello World Text with Custom Elements
Custom elements are fun technology. In this video, you will learn how to set one up and running in l ...
- HTML 中图片的隐藏与显示
<html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Co ...
- LeetCode 格雷码序列的生成
问题概述:在一组数的编码中,若随意两个相邻的代码仅仅有一位二进制数不同.则称这样的编码为格雷码. 2位数的格雷码序列:00 : 001 : 111 : 310 : 2找规律:假设要求n位的格雷码,先要 ...
- 在github上建立jekyll blog
1.git常用命令(简易介绍见http://rogerdudler.github.io/git-guide/index.zh.html) git init #创建新的git仓库 git clo ...
- Atitit. Toast alert loading js控件 atiToast v2新特性
Atitit. Toast alert loading js控件 atiToast v2新特性 1. 连续多个txt追加的原理 var txt = document.createElement(& ...
- gcc支持的标签
#include <stdio.h> #include <time.h> int main(/*int argc, char const *argv[]*/) { void * ...
- linux系统之free命令详解
total used free shared buffers cached Mem: -/+ buffers/cache: Swap: 上面是free命令的执行结果,下面我来详细说说其中的含义: Me ...
- 验证用户名,密码,验证码,发送alax请求进行登录代码
//html代码 <div class="layui-form" id="larry_form"> <div class="layu ...
- c++ how to make your own class a valid key type for std::map?
In Java, if you want your own class to be a valid key type of the container, you just need to make i ...
- CentOS统的7个运行级别的含义
原文: http://blog.csdn.net/liansehai/article/details/45370965 CentOS系统有7个运行级别(runlevel) 运行级别就是操作系统当前正在 ...