题意:就是给你一个n,m,t   n代表有多少个点。m代表有多少个双向的边  t代表的是虫洞。如今要你判读是否还能够穿越到过去的点

虫洞的意思是给你的边是单向的,而且是负权值(输入的时候是正数)

思路:能否够穿越回过去的点,即有没有负环。果断套用模板,dijkstra算法不能检測负环

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
#define maxn 520
const int INF = 0x3fffffff;
struct Edge
{
int from,to,dist;
};
struct BellmanFord
{
int n,m;
vector<Edge> edges;
vector<int> G[maxn];
bool inq[maxn];
int d[maxn];
int p[maxn];
int cnt[maxn];
Edge e;
void init(int n)
{
this->n=n;
for(int i=0;i<n;i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int dist)
{
edges.push_back((Edge){from,to,dist});
m=edges.size();
G[from].push_back(m-1);
}
bool negativeCycle()
{
queue<int >Q;
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
for(int i=0;i<n;i++)
{
d[i]=INF;
inq[0]=true;
Q.push(i);
}
d[0] = 0;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
inq[u]=false; for(int i=0;i<G[u].size();i++)
{
Edge& e=edges[G[u][i]];
if(d[e.to]>d[u]+e.dist)
{
d[e.to]=d[u]+e.dist;
p[e.to]=G[u][i];
if(!inq[e.to])
{
Q.push(e.to);
inq[e.to]=true;
if(++cnt[e.to]>n)
return true;
}
}
} }
return false;
}
};
int main()
{
int a,b,c,i,node,m,t,case1,k;
bool j;
scanf("%d",&case1);
while(case1--)
{
scanf("%d %d %d",&node,&m,&t);
if(node==0&&m==0)break;
BellmanFord tu;
tu.init(node);
for(i=0;i<m;i++)
{
scanf("%d %d %d",&a,&b,&c);
a--;b--;
tu.AddEdge(a,b,c);
tu.AddEdge(b,a,c);
}
for(k=0;k<t;k++)
{
scanf("%d %d %d",&a,&b,&c);
a--;
b--;
tu.AddEdge(a,b,-c);
}
j=tu.negativeCycle();
if(j)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

HDU 3259 Wormholes的更多相关文章

  1. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  2. 最短路(Bellman_Ford) POJ 3259 Wormholes

    题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...

  3. POJ 3259 Wormholes (Bellman_ford算法)

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

  4. poj 3259 Wormholes

    题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...

  5. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

  6. POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告

    三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...

  7. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

  8. POJ 3259 Wormholes(最短路,判断有没有负环回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24249   Accepted: 8652 Descri ...

  9. POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】

    链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

随机推荐

  1. activiti集成spring异常(DbSqlSession)

    在eclipse配置一个简单的activiti项目,配置的是mysql数据库,报错如下: SLF4J: Failed to load class "org.slf4j.impl.Static ...

  2. easyui源码翻译1.32--PropertyGrid(属性表格)

    前言 继承自$.fn.datagrid.defaults.使用$.fn.propertygrid.defaults重写默认值对象.下载该插件翻译源码 属性表格提供The propertygrid pr ...

  3. Linux内核学习笔记: uid之ruid,euid,suid

    转自: http://www.linuxidc.com/Linux/2011-09/43194.htm 看UNIX相关的书时经常能遇到这几个概念,但一直没有好好去理清这几个概念,以致对这几个概念一直一 ...

  4. C调用OPENSSL做REST服务客户端的例子

    //SSL-Client.c #include <stdio.h> #include <errno.h> #include <unistd.h> #include ...

  5. 第九章 Mass Storage设备

    9.1 Mass Storage设备介绍 USB的Mass Storage类是USB大容量储存设备类(Mass Storage Device Class).专门用于大容量存储设备,比如U盘.移动硬盘. ...

  6. float 和 real

      用于表示浮点数值数据的大致数值数据类型.浮点数据为近似值:因此,并非数据类型范围内的所有值都能精确地表示. 注意: real 的 SQL-92 同义词为 float(24). 数据类型 范围 存储 ...

  7. Android Binder设计与实现 - 设计篇

    要 Binder是Android系统进程间通信(IPC)方式之一.Linux已经拥有管道,system V IPC,socket等IPC手段,却还要倚赖Binder来实现进程间通信,说明Binder具 ...

  8. RxJava开发精要8 – 与REST无缝结合-RxJava和Retrofit

    原文出自<RxJava Essentials> 原文作者 : Ivan Morgillo 译文出自 : 开发技术前线 www.devtf.cn 转载声明: 本译文已授权开发者头条享有独家转 ...

  9. chrome插件 postman插件 接口测试、API & HTTP 请求调试工具

    Postman 是一个非常棒的Chrome扩展,提供功能强大的API & HTTP 请求调试. 它能够发送任何类型的HTTP requests (GET, HEAD, POST, PUT..) ...

  10. WCF - Consuming WCF Service

    WCF services allow other applications to access or consume them. A WCF service can be consumed by ma ...