http://acm.hdu.edu.cn/showproblem.php?pid=1317

题意:

给出一个有向图,每到达一个点,都会加上或减去一些能量,我们要做的就是判断从1出发是否能到达n。初始能量有100,行走的途中能量不能小于等于0。

思路:

首先我们用floyd来判断一下1和n之间是否有通路。

其次就是bellman_ford算法来判正环了。

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
using namespace std; const int maxn = + ;
const int INF = 0x3f3f3f3f; int n, m;
int power[];
int d[][];
int en[];
int cnt; struct node
{
int s, e;
}edge[maxn]; void floyd()
{
for (int k = ; k <= n; k++)
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
{
d[i][j] = d[i][j] || (d[i][k] && d[k][j]);
}
} bool bellman_ford()
{
for (int i = ; i <= n; i++) en[i] = -INF;
en[] = ;
for (int i = ; i < n; i++)
{
bool flag = false;
for (int j = ; j <cnt; j++)
{
if (en[edge[j].e] < en[edge[j].s] + power[edge[j].e] && en[edge[j].s]+power[edge[j].e]>)
{
en[edge[j].e] = en[edge[j].s] + power[edge[j].e];
flag = true;
}
}
if (!flag) break;
}
for (int j = ; j < cnt; j++)
{
//这儿需要注意一下,判断正环的时候,这个正环必须能到达终点
if (en[edge[j].e] < en[edge[j].s] + power[edge[j].e] && en[edge[j].s] + power[edge[j].e]> && d[edge[j].e][n]) return true;
}
if (en[n] <= ) return false;
else return true;
} int main()
{
//freopen("D:\\input.txt", "r", stdin);
int v;
while (cin >> n && n != -)
{
memset(d, , sizeof(d));
cnt = ; for (int i = ; i <= n; i++)
{
scanf("%d%d", &power[i], &m);
while (m--)
{
scanf("%d", &v);
edge[cnt].s = i;
edge[cnt].e = v;
d[i][v] = ;
cnt++;
}
}
floyd(); //首先判断1到n是否连通
if (!d[][n])
{
printf("hopeless\n");
continue;
}
if (bellman_ford())
printf("winnable\n");
else
printf("hopeless\n");
}
}

HDU 1317 XYZZY(floyd+bellman_ford判环)的更多相关文章

  1. HDU 1317 XYZZY【Bellman_Ford判断正环】

    题意:给出n个房间,初始在房间1有100的能量值,每次进入一个房间,能量值可能增加也可能减小,(是点权,不是边权),问能否到达终点的时候能量值还为正 这题自己写的时候wa--wa-- 后来看了题解,还 ...

  2. hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  3. hdu 1317 XYZZY

    http://acm.hdu.edu.cn/showproblem.php?pid=1317 #include <cstdio> #include <queue> #inclu ...

  4. [HDU 1317]XYZZY[SPFA变形][最长路]

    题意: 一个图, 点权代表走到该点可获得的能量值. 可正可负. 一个人从1 号出发,带有100点能量. 问是否有一种方案可使人在能量值>0的时候走到n. 思路: 这个题首先要注意点权. 其实就是 ...

  5. hdu1317 XYZZY Floyd + Bellman_Ford

    这题,我在学搜索的时候做过.不过好像不叫这名字. 1.先用Floyd算法判断图的连通性.如果1与n是不连通的,输出hopeless. 2.用Bellman_Ford算法判断是否有正圈,如果某点有正圈, ...

  6. HDU 4514并查集判环+最长路

    点击打开链接 题意:中文题...... 思路:先推断是否能成环,之前以为是有向图,就用了spfa推断,果断过不了自己出的例子,发现是无向图.并查集把,两个点有公共的父节点,那就是成环了,之后便是求最长 ...

  7. HDU 5154 Harry and Magical Computer 有向图判环

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5154 题解: 有向图判环. 1.用dfs,正在访问的节点标记为-1,已经访问过的节点标记为1,没有访 ...

  8. floyd判环算法(龟兔赛跑算法)

    floyd判环算法(龟兔赛跑算法) 注意,这个算法是用来判断一条链+一条环的图,环的长度或者环与链的交界处的,所以此floyd非彼floyd(虽然都是一个人想出来的). (图不是我的) 如果只要求环的 ...

  9. HDU 1317:XYZZY

    Problem Description It has recently been discovered how to run open-source software on the Y-Crate g ...

随机推荐

  1. 【黑金ZYNQ7000系列原创视频教程】07.自定义IP——定制RTC IP实验

    黑金论坛地址: http://www.heijin.org/forum.php?mod=viewthread&tid=36641&extra=page%3D1 爱奇艺地址: http: ...

  2. URL上的中文编码

    参考:http://www.chinaz.com/web/2013/0226/293639.shtml 一 URL上拼接中文会进行编码 在URL上拼接中文时,比如www.abc.com?season= ...

  3. angular -- ng-class该如何使用?

    ng-class是一个判断是否给某一个元素添加类名的属性: 例如:下面是判断 是否添加 aHover 这个类名: <ul class="nav fl w120 o"> ...

  4. DFS判断正环

    hdu1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. postgresql----唯一索引,表达式索引,部分索引

    一.唯一索引 唯一索引字面上理解就是在索引上增加唯一约束,不允许出现索引值相同的行,目前只有Btree索引可以声明唯一索引,唯一键会自动创建唯一索引. 测试表: test=# create table ...

  6. codeforces#505--C Plasticine Zebra

    C. Plasticine zebra time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. Mysql EXPLAIN 相关疑问: Using temporary ; Using filesort

    一.什么是Using temporary ; Using filesort 1. using filesort filesort主要用于查询数据结果集的排序操作,首先MySQL会使用sort_buff ...

  8. Redhat 7改动默认执行级别方法 --RHEL7使用systemd创建符号链接指向默认执行级别

    今天装了下正式版的RHEL7,发现熟悉的inittab中没有了改动默认执行级别,打开inittab例如以下 [root@localhost init.d]# vi /etc/inittab # ini ...

  9. Kconfig文件说明

    Kconfig的格式 下面截取/drivers/net下的Kconfig文件中的部分内容: # Network device configuration menuconfig NETDEVICES d ...

  10. PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]

    1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...