题意:有n块田,之间有m条无向边表示路径,权值表示走过需要花费的时间。有w对虫洞,虫洞是单向的,表示穿越一定时间到过去,并且回到虫洞指向的点,问一个人有没有可能通过虫洞回到某个起点,并且在从这个起点出发之前的时间,因为这样可以看到过去的自己。

解题:判断负圈,模板题。

//记录一下模板

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; int n,m,w;
struct node
{
int to;
int val;
};
vector<node>e[];
int num[];
int d[];
bool vis[]; bool spfa()
{
queue<int>que;
que.push();
d[]=;
vis[]=true;
num[]=;
while( que.size() )
{
int q=que.front();///q是当前结点
que.pop();
vis[q]=false;
int len=e[q].size();
for(int i=;i<len;i++)
{
int next=e[q][i].to;
int cost=e[q][i].val;
if( d[next] > d[q]+cost )
{
d[next]=d[q]+cost;
if( !vis[next] )///没有入队
{
num[next]++;
if( num[next]>=n )
return false;///入了这么多次,存在负圈
vis[next]=true;///入队标记
que.push(next);
} }
}
}
return true;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{ scanf("%d%d%d",&n,&m,&w);///点、边、负边
memset(d,inf,sizeof(d));
memset(num,,sizeof(num));
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)
e[i].clear(); ///正边+负边总数
int a,b,c;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);///起点,终点,权值
e[a].push_back({b,c});
e[b].push_back({a,c});
}
for(int i=;i<w;i++)
{
scanf("%d%d%d",&a,&b,&c);
e[a].push_back({b,-c}); }
if( spfa() )
printf("NO\n");
else
printf("YES\n"); }
return ;
}

spfa模板

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; struct edge
{
int from;
int to;
int val;
};
edge e[];///无向边*2+有向边
int d[];///各个点到起点的距离
int n,m,w,cnt;
int a,b,c; void add(int x,int y,int z)
{
e[cnt].from=x;
e[cnt].to=y;
e[cnt].val=z;
cnt++;
} bool Bellman_Ford()//贝尔曼福特
{
d[]=;///把1作为起点
for(int i=;i<n;i++)
{
for(int j=;j<cnt;j++)
{
if( d[ e[j].from ] > d[ e[j].to ] + e[j].val )
d[ e[j].from ] = d[ e[j].to ] + e[j].val;
}
}
for(int j=;j<cnt;j++)///第n+1次,还能对边松弛则说明存在负环
{
if( d[ e[j].from ] > d[ e[j].to ] + e[j].val )
return false;
}
return true;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(d,inf,sizeof(d));
scanf("%d%d%d",&n,&m,&w);///点、边、负边
cnt=;///正边+负边的总边数
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);///无向边
}
for(int i=;i<w;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,-c);
}
if( Bellman_Ford() )
printf("NO\n");
else
printf("YES\n"); }
return ;
}

Bellman_Ford模板

POJ3259-Wormholes-( spfa || Bellman_Ford )的更多相关文章

  1. POJ3259 :Wormholes(SPFA判负环)

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

  2. POJ3259 Wormholes(SPFA判断负环)

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

  3. POJ3259 Wormholes —— spfa求负环

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

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

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

  5. poj3259 Wormholes【Bellman-Ford或 SPFA判断是否有负环 】

    题目链接:poj3259 Wormholes 题意:虫洞问题,有n个点,m条边为双向,还有w个虫洞(虫洞为单向,并且通过时间为倒流,即为负数),问你从任意某点走,能否穿越到之前. 贴个SPFA代码: ...

  6. POJ3259 Wormholes 【spfa判负环】

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  7. POJ3259——Wormholes(Bellman-Ford+SPFA)

    Wormholes DescriptionWhile exploring his many farms, Farmer John has discovered a number of amazing ...

  8. POJ--3259 Wormholes (SPFA判负环)

    题目电波   3259 Wormholes #include<iostream> #include<cstring> #include<algorithm> #in ...

  9. poj3259 Wormholes (判负环)【spfa】(模板)

    <题目链接> 题目大意: John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从 ...

  10. poj-3259 Wormholes(无向、负权、最短路之负环判断)

    http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovere ...

随机推荐

  1. mysql中,手动提交事务

    1: 在mysql中,手动提交事务的案例:CREATE PROCEDURE tfer_funds       (from_account int, to_account int, tfer_amoun ...

  2. Qt 文件选项对话框弹出两次

    1 问题 ​ 在Qt 5.12.0 版本中,用 QFileDialog 类来做文件选择时候,发现当弹出对话框后,选择完文件后,又弹出文件选择对话框. 2 原因查找 2.1 代码 QFileDialog ...

  3. Go语言【数据结构】指针

    指针 本章围绕字符串.数字.数组.切片.map.channel.结构体与指针赋值及函数传参的应用剖析 字符串 字符串本身也是StringHeader的结构体,包含Data指针与字符串长度,如下 typ ...

  4. golang基础语法

    golang语言的常量定义: const  filename="abc.txt"; const filename String="abc.txt" golang ...

  5. LR编写grammar中的问题和解决方法

    本文主要说明LR解析过程中关于BNF的典型冲突如何在LR中解决 冲突一般分为两种: shift/reduce错误 redure/redure错误 下面分别解释两种冲突 1. shift/reduce错 ...

  6. 封装:Windows系统文件图标

    原文:封装:Windows系统文件图标 用途:用于获取文件系统默认图标 using System; using System.Collections.Generic; using System.Dra ...

  7. 批量修改Ms SqlServer 的default(默认值)

    原文:批量修改Ms SqlServer 的default(默认值) --1.取得数据库所有表的默认值: select t3.name as 表名,t1.name as 字段名,t2.text as 默 ...

  8. ASP.NET Core中如何显示[PII is hidden]的隐藏信息

    有时候我们在ASP.NET Core项目运行时,发生在后台程序中的错误会将关键信息隐藏为[PII is hidden]这种占位符,如下所示: 而知道这些关键信息,有时候对我们调试程序是非常重要的.所以 ...

  9. Java静态变量初始化的坑

    class SingleTon { private static SingleTon singleTon = new SingleTon(); public static int count1; pu ...

  10. WebApi自定义全局异常过滤器及返回数据格式化

    WebApi在这里就不多说了,一种轻量级的服务,应用非常广泛.我这这里主要记录下有关 WebApi的相关知识,以便日后使用. 当WebApi应用程序出现异常时,我们都会使用到异常过滤器进行日志记录,并 ...