题目大意:
农民约翰在农场散步的时候发现农场有大量的虫洞,这些虫洞是非常特别的因为它们都是单向通道,为了方便现在把约翰的农田划分成N快区域,M条道路,W的虫洞。
约翰是时空旅行的粉丝,他希望这样做,在一个区域开始,经过一些道路和虫洞然后回到他原来所在的位置,这样也许他就能见到他自己了。
穿越虫洞会回到以前。。。。。(穿越者约翰)
/////////////////////////////////////////////////////////////
很明显这是一个很扯淡的故事,不过为了出一道带负权值的题目也是难为了出题人,迪杰斯特拉算法不能处理带有负权值的问题,佛洛依德倒是可以,不过复杂度很明显太高,所以spfa是不错的选择,只需要判断出发点时间是否变小.
#include<algorithm>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std; const int maxn = ;
const int oo = 0xfffffff; struct node
{
    int y, time;
    node(int y, int t):y(y), time(t){}
};
vector<node> G[maxn];
int v[maxn]; int spfa(int s)
{
    queue<int> Q;
    Q.push(s);     while(Q.size())
    {
        s = Q.front();Q.pop();
        int len = G[s].size();         for(int i=; i<len; i++)
        {
            node q = G[s][i];             if(v[s]+q.time < v[q.y])
            {
                v[q.y] = v[s] + q.time;
                Q.push(q.y);
            }
        }         if(v[] < )
            return ;
    }     return ;
} int main()
{
    int T;     scanf("%d", &T);     while(T--)
    {
        int N, M, W, i, a, b, c;         scanf("%d%d%d", &N, &M, &W);         for(i=; i<=N; i++)
        {
            v[i] = oo;
            G[i].clear();
        }
        v[] = ;         for(i=; i<M; i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            G[a].push_back(node(b, c));
            G[b].push_back(node(a, c));
        }         for(i=; i<W; i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            G[a].push_back(node(b, -c));
        }         int ans = spfa();         if(ans == )
            printf("YES\n");
        else
            printf("NO\n");
    }     return ;

}

F - Wormholes的更多相关文章

  1. 【算法系列学习】SPFA邻接表最短路 [kuangbin带你飞]专题四 最短路练习 F - Wormholes

    https://vjudge.net/contest/66569#problem/F 题意:判断图中是否存在负权回路 首先,介绍图的邻接表存储方式 数据结构:图的存储结构之邻接表 邻接表建图,类似于头 ...

  2. Mysql_以案例为基准之查询

    查询数据操作

  3. POJ 3259 Wormholes (判负环)

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

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

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

  5. Wormholes

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

  6. poj 3259 Wormholes 判断负权值回路

    Wormholes Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   Java ...

  7. Wormholes(Bellman-ford)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 33008   Accepted: 12011 Descr ...

  8. poj3259 bellman——ford Wormholes解绝负权问题

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35103   Accepted: 12805 Descr ...

  9. Wormholes 分类: POJ 2015-07-14 20:21 21人阅读 评论(0) 收藏

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35235   Accepted: 12861 Descr ...

随机推荐

  1. sql 作业+游标 自动备份数据库

    前言 昨天有个同事在客户的服务器上面弄数据库,不小心执行了一条 sql 语句 TRUNCATE TABLE xxx 碉堡了吧,数据全没了  - - ,然后就是在网上拼命的搜索关于数据恢复的软件,搞了一 ...

  2. Spot light工具集

    Spot light on UNIX 安装没什么问题 Spot light on Oracle  必须安装32位的客户端,不然搞死你 两者的界面都是吊炸天啊

  3. 受限玻尔兹曼机(RBM)

    能量模型 RBM用到了能量模型. 简单的概括一下能量模型.假设一个孤立系统(总能量$E$一定,粒子个数$N$一定),温度恒定为1,每个粒子有$m$个可能的状态,每个状态对应一个能量$e_i$.那么,在 ...

  4. 258. Add Digits(C++)

    258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...

  5. html良好结构-之豆瓣风格

    良好HTML结构 1 结构层次 unit> hd+ unit-wrap>section>item2 语议化的结构 html5 html 语议 ck-box-unit ck-hd-wr ...

  6. Unity3D中的Coroutine详解

    Unity中的coroutine是通过yield expression;来实现的.官方脚本中到处会看到这样的代码. 疑问: yield是什么? Coroutine是什么? unity的coroutin ...

  7. A Statistical View of Deep Learning (I): Recursive GLMs

    A Statistical View of Deep Learning (I): Recursive GLMs Deep learningand the use of deep neural netw ...

  8. AVR单片机RC触摸

    RC电容触摸感应按键1:RC感应原理 RC采样原理就是通过测量感应极电容的微小变化,来感知人体对电容式感应器(按键.轮键或者滑条)的感应.电极电容(C)通过一个固定的电阻(R)周期性地充放电.(原文件 ...

  9. VS在Release模式下,难道还可以Debug?

    就是这段代码: int main(int argc, char *argv[]) { QApplication a(argc, argv); cxcxsdee w; w.show(); QString ...

  10. autotrace显示Statistics很多信息为0(转)

    一朋友使用autotrace查看数据库执行计划发现结果如下,Statistics中很多信息为0,这个肯定是不正常现象,什么都可以为0,consistent gets也不可能为0. SQL> se ...