hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】
链接:
XYZZY
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1701 Accepted Submission(s): 419
Each game consists of a set of up to 100 rooms. One of the rooms is the start and one of the rooms is the finish. Each room has an energy value between -100 and +100. One-way doorways interconnect pairs of rooms.
The player begins in the start room with 100 energy points. She may pass through any doorway that connects the room she is in to another room, thus entering the other room. The energy value of this room is added to the player's energy. This process continues until she wins by entering the finish room or dies by running out of energy (or quits in frustration). During her adventure the player may enter the same room several times, receiving its energy each time.
the energy value for room i
the number of doorways leaving room i
a list of the rooms that are reachable by the doorways leaving room i
The start and finish rooms will always have enery level 0. A line containing -1 follows the last test case.
0 1 2
-60 1 3
-60 1 4
20 1 5
0 0
5
0 1 2
20 1 3
-60 1 4
-60 1 5
0 0
5
0 1 2
21 1 3
-60 1 4
-60 1 5
0 0
5
0 1 2
20 2 1 3
-60 1 4
-60 1 5
0 0
-1
hopeless
winnable
winnable
题意:
有 N 个房间, 编号从 1 到 N 。
每次进入一个房间, 能量值可能增加也可能减少
问:从第一个房间开始走, 给你 100 个能量值。
问你是否能走到第 N 个房间。
第一行 N 输入房间的个数
然后下面 N 行数据:
第 i 行数据的第一个表示进入该房间得到的能量【可正可负】
第二个表示从该房间出发能到达的房间个数 num
剩下 num 个数表示可以到达的房间编号
算法:Bellman_ford() 判断正环
注意:是有向图,
然后建图的时候要注意下, 边是没有权的。。。
点有权。
思路:
其实开始没看题目的时候,没有看到群里的吐槽也不会想到用 Bellman_ford()
如果图中存在正环, 那么就可以不停的走这个环来增加能量,
如果环中的点能到达 N 那么肯定是赢了。。。
但是由于这个先入为主的思想,开始很容易的就让我忽略了,这题的本质是到达 N 的时候还有能量。
然后就是各种不注意,各种 WA 的血泪史。。。。然后网上各种高深的题解。
加边建图的过程同时记录连通性, 先判断 1 与 N 在不考虑能量的时候是否连通【不判断也可以,只是个无关紧要的优化】
注意:当不存在正环的时候, 不要像以前用这个算法时直接跳出
因为我们的主要目的不是判断正环,而是要使得到达 N 还有能量。
那么赢的可能性就两种了:
1.没有正环, 但是通过Bellman_ford() 的松弛操作, 到 N 的能量值 > 0
2.存在正环, 正环中的点, 能到达终点。
code:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std; const int maxn = 110;
const int INF = 10000000000; int w[maxn][maxn]; // 判断图的连通性
int en[maxn]; //进入该点的能量值
int d[maxn]; //每一点的能量值
int n,m; //n 个点, m 条边 struct Edge{
int u,v;
}edge[maxn*maxn]; void floyd() // 有向图的传递闭包
{
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
w[i][j] = w[i][j] || (w[i][k] && w[k][j]); //不要写错了 WA 的都是泪。。。
} bool Bellman_ford()
{
for(int i = 1; i <= n; i++) d[i] = -INF;
d[1] = 100; // 初始第一个点有 100 个能量 for(int i = 1; i < n; i++)
{
for(int j = 0; j < m; j++)
{
int u = edge[j].u;
int v = edge[j].v; if(d[v] < d[u]+en[v] && d[u]+en[v] > 0) //松弛
d[v] = d[u]+en[v]; //是加上点的权。。。
} //注意:不能像以前一样不能松弛了,就直接返回 false 因为判断正环的目的是使 d[n] > 0
} for(int i = 0; i < m; i++)
{
int u = edge[i].u;
int v = edge[i].v; if(d[v] < d[u]+en[v] && d[u]+en[v] > 0) //如果存在正环
if(w[v][n]) //正环中的点能够到达终点
return true;
} return d[n]>0; // 不存在正环, 判断能否依靠 100 个能量值到达终点
} int main()
{
while(scanf("%d", &n) != EOF)
{
if(n == -1) break; m = 0; // 初始化边
memset(w, 0, sizeof(w));
memset(en, 0, sizeof(en));
for(int i = 1; i <= n; i++) w[i][i] = 1; int num;
for(int i = 1; i <= n; i++)
{
int v;
scanf("%d%d", &en[i], &num);
while(num--) //注意是单向的
{
scanf("%d", &v);
edge[m].u = i;
edge[m++].v = v;
w[i][v] = 1; // 有向图, 不要傻逼的加上 w[v][i] = 1
}
} floyd(); // 考查有向图的连通性
/*
if(!w[1][n])
{
printf("hopeless\n"); continue;
}*/ if(Bellman_ford()) printf("winnable\n");
else printf("hopeless\n");
}
return 0;
}
hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】的更多相关文章
- HDU 1317 XYZZY(floyd+bellman_ford判环)
http://acm.hdu.edu.cn/showproblem.php?pid=1317 题意: 给出一个有向图,每到达一个点,都会加上或减去一些能量,我们要做的就是判断从1出发是否能到达n.初始 ...
- [HDU 1317]XYZZY[SPFA变形][最长路]
题意: 一个图, 点权代表走到该点可获得的能量值. 可正可负. 一个人从1 号出发,带有100点能量. 问是否有一种方案可使人在能量值>0的时候走到n. 思路: 这个题首先要注意点权. 其实就是 ...
- Currency Exchange POJ - 1860 (spfa判断正环)
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- poj1860 兑换货币(bellman ford判断正环)
传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...
- Currency Exchange POJ - 1860 spfa判断正环
//spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...
- HDU 1317 XYZZY【Bellman_Ford判断正环】
题意:给出n个房间,初始在房间1有100的能量值,每次进入一个房间,能量值可能增加也可能减小,(是点权,不是边权),问能否到达终点的时候能量值还为正 这题自己写的时候wa--wa-- 后来看了题解,还 ...
- HDU 1317(Floyd判断连通性+spfa判断正环)
XYZZY Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)
XYZZY Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- poj 1932 XYZZY(spfa最长路+判断正环+floyd求传递闭包)
XYZZY Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4154 Accepted: 1185 Description ...
随机推荐
- Inspiron 14 7000 系列 (7447) 游匣14 拆机图
Inspiron 14 7000 系列 (7447) 游匣14 拆机图 游匣配置不多说,i5起步,标配4G GTX850M显卡,这么霸道的配置给我玩扫雷肯定不卡.配置高功耗就大,不过游匣的散热 ...
- POJ 1961 Period KMP算法next数组的应用
题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...
- Swift2.0新特性
随着刚刚结束的 WWDC 2015 苹果发布了一系列更新,这其中就包括了令人振奋的 Swift 2.0. 这是对之前语言特性的一次大幅的更新,加入了很多实用和方便的元素,下面我们就一起来看看这次更新都 ...
- Xcode6 Xcode7 Xcode 官方链接 --备用
Xcode 6 官方下载链接: http://adcdownload.apple.com//wwdc_2014/xcode_6_beta_ie8g3n/xcode_6_beta.dmg Xcode ...
- JVM 学习笔记
1. JAVA类分为三类: 1.1 系统类 (用系统类加载器加载bootstrap ClassLoader) 1.2 扩展类 (用扩展类加载器加载Ext ClassLoader) 1. ...
- Java 8 的 JVM 有多快?Fork-Join 性能基准测试
Java 8 已经发布一段时间了,许多开发者已经开始使用 Java 8.本文也将讨论最新发布在 JDK 中的并发功能更新.事实上,JDK 中已经有多处java.util.concurrent 改动,但 ...
- Best Sequence
poj1699:http://poj.org/problem?id=1699 题意:给你nge串,让你求出这些串组成的最小的串重叠部分只算一次. 题解:我的做法是DFS,因为数据范围只有10,就算是n ...
- Web API路由与动作(三)
本章包括三个小节 如果你输入了mvc的路由规则 这个可以粗略过一遍即可 内容说明有点繁琐 原文地址:http://www.asp.net/web-api/overview/web-api-rout ...
- Android ListView中 每一项都有不同的布局
实现代码 Adapter的代码 其中:ViewHolder分别是三个不同的布局,也就是ListView中每一项的布局 TYPE_1...是三种类型. 在使用不同布局的时候,getItemViewTyp ...
- [译]GotW #1: Variable Initialization 续
Answer 2. 下面每行代码都做了什么? 在Q2中,我们创建了一个vector<int>且传了参数10和20到构造函数中,第一种情况下(10,20),第二种情况是{10, 20}. 它 ...