传递闭包(Floyd+bellman-Fold POJ1932)
传递闭包
在一个有向(无向)连通图中,如果节点i与k联通,k与j联通,则i和j联通,传递闭包就是把所有传递性的节点求出来,之后就知道了任意两个节点的连通性,只需枚举节点的联通情况即可,无需考虑最短路径:代码:memset(dis,-1,sizeof(dis));
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(g[i][k]&&g[k][j])
g[i][j]=1;
}
}
}XYZZY
Time
Limit: 1000MSMemory Limit: 30000K Total Submissions: 3344 Accepted: 963 Description
The prototypical computer adventure game, first designed by Will Crowther on the PDP-10 in the mid-1970s as an attempt at computer-refereed fantasy gaming, and expanded into a puzzle-oriented game by Don Woods at Stanford in 1976. (Woods had been one of the authors of INTERCAL.) Now better known as Adventure or Colossal Cave Adventure, but the TOPS-10 operating system permitted only six-letter filenames in uppercase. See also vadding, Zork, and Infocom. It has recently been discovered how to run open-source software on the Y-Crate gaming device. A number of enterprising designers have developed Advent-style games for deployment on the Y-Crate. Your job is to test a number of these designs to see which are winnable. 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.Input
The input consists of several test cases. Each test case begins with n, the number of rooms. The rooms are numbered from 1 (the start room) to n (the finish room). Input for the n rooms follows. The input for each room consists of one or more lines containing:
- 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.
Output
In one line for each case, output "winnable" if it is possible for the player to win, otherwise output "hopeless".Sample Input
5
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
-1Sample Output
hopeless
hopeless
winnable
winnable题意:给出一个单向连通图,和每个节点的能量,代表走到这个节点就可以获得,初始化能量为100,当到达某个节点后能量<=0则 不能继续走,问从1开始能不能到达n
分析:首先用floyd检验图的连通性即1能否到达n,若不能直接输出hopeless,如果1可以到达n,但是由于能量限制可能走不到n,如果从1可以到达一个正环(可以不断转圈获得能量)而且正环的点可以到达n,这种情况也是winnable,所以用bellman-Foyd判断正环,且正环上的点可以到达n,注意当到达某点的时候能量为非正,则不能从此点继续下去#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
#include"algorithm"
#include"string.h"
#include"string"
#include"math.h"
#include"vector"
#include"stack"
#include"map"
#define eps 1e-8
#define inf 0x3f3f3f3f
#define M 111
using namespace std;
int dis[M],g[M][M],energy[M],mp[M][M];
int main()
{
int n,i,j,k,m;
while(scanf("%d",&n),n!=-1)
{
memset(g,0,sizeof(g));
memset(dis,-1,sizeof(dis));
memset(mp,0,sizeof(mp));
for(i=1;i<=n;i++)
{
scanf("%d%d",&energy[i],&m);
while(m--)
{
scanf("%d",&j);
g[i][j]=1;
mp[i][j]=1;
}
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(g[i][k]&&g[k][j])
g[i][j]=1;
}
}
}
if(g[1][n]==0)
{
printf("hopeless\n");
continue;
}
dis[1]=100;
g[n][n]=1;//注意该连通性会用到
for(k=1;k<=n;k++)
{
int flag=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(mp[i][j]&&g[j][n]&&dis[j]<dis[i]+energy[j]&&dis[i]>0)
{
flag=0;
dis[j]=dis[i]+energy[j];
}
}
}
if(flag)
break;
}
if(k>n||dis[n]>=0)//如果存在1可以到达正环而正环可以到n或者1可以直接到n就是可以的
{
printf("winnable\n");
}
else
printf("hopeless\n");
}
}
传递闭包(Floyd+bellman-Fold POJ1932)的更多相关文章
- PKU 1932 XYZZY(Floyd+Bellman||Spfa+Floyd)
题目大意:原题链接 给你一张图,初始你在房间1,初始生命值为100,进入每个房间会加上那个房间的生命(可能为负),问是否能到达房间n.(要求进入每个房间后生命值都大于0) 解题思路: 解法一:Floy ...
- poj1860(Bellman—fold)
题目连接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...
- Treasure Exploration---poj2594(传递闭包Floyd+最小路径覆盖)
题目链接:http://poj.org/problem?id=2594 在外星上有n个点需要机器人去探险,有m条单向路径.问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过(这就是和单 ...
- POJ3660 传递闭包———floyd算法
POJ3660 Cow Contest 题目链接:http://poj.org/problem?id=3660 题意:农名约翰有些奶牛,约翰通过让他们决斗来决定他们的排名,约翰让这些奶牛一对一打完一定 ...
- POJ 3660 Cow Contest 传递闭包+Floyd
原题链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- 图论算法——最短路径Dijkstra,Floyd,Bellman Ford
算法名称 适用范围 算法过程 Dijkstra 无负权 从s开始,选择尚未完成的点中,distance最小的点,对其所有边进行松弛:直到所有结点都已完成 Bellman-Ford 可用有负权 依次对所 ...
- [bzoj2208][Jsoi2010]连通数_bitset_传递闭包floyd
连通数 bzoj-2208 Jsoi-2010 题目大意:给定一个n个节点的有向图,问每个节点可以到达的点的个数和. 注释:$1\le n\le 2000$. 想法:网上有好多tarjan+拓扑序dp ...
- poj 3660 Cow Contest(传递闭包 Floyd)
链接:poj 3660 题意:给定n头牛,以及某些牛之间的强弱关系.按强弱排序.求能确定名次的牛的数量 思路:对于某头牛,若比它强和比它弱的牛的数量为 n-1,则他的名次能够确定 #include&l ...
- poj 1975 Median Weight Bead(传递闭包 Floyd)
链接:poj 1975 题意:n个珠子,给定它们之间的重量关系.按重量排序.求确定肯定不排在中间的珠子的个数 分析:由于n为奇数.中间为(n+1)/2,对于某个珠子.若有至少有(n+1)/2个珠子比它 ...
随机推荐
- linux C gcc -lm
使用math.h中声明的库函数还有一点特殊之处,gcc命令行必须加-lm选项,因为数学函数位于libm.so库文件中(这些库文件通常位于/lib目录下),-lm选项告诉编译器,我们程序中用到的数学函数 ...
- epoll 简单介绍及例子
第一部分:Epoll简介 . 当select()返回时,timeout参数的状态在不同的系统中是未定义的,因此每次调用select()之前必须重新初始化timeout和文件描述符set.实际上,秒,然 ...
- nrf51822-添加DFU服务
以ble_app_uart例子为基础,在其上添加dfu服务. Sdk中的bootloader提供了两个方式来进入升级模式,一种是按键,另一种是手机点击升级. 在bootloader代码相关代码如下 如 ...
- js判断图片是否存在,并做处理
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 逻辑运算符——逻辑与&&、逻辑或||
一直以来,都是认为逻辑运算符返回的是布尔值,却突然发现:并不是这样. 对于||来说,如果条件判断结果为true就返回第一个操作数的值,如果为false就返回第二个操作数的值. &&则相 ...
- Solr分页与高亮(使用SolrNet实现)
Solr分页与高亮(使用SolrNet实现) 本节我们使用Asp.net MVC实现Solr客户端查询,建议使用SolrNet这个客户端,开源地址在:https://github.com/mausch ...
- MVC从服务器端返回js到客户端的方法(总结)
1.利用ViewBag,从服务器端创建一个显示js开关的ViewBag,然后到View中去做判断. Controller端 [HttpPost] public ActionResult Index(h ...
- ecshop transport.js 和 jquery 冲突解决办法
您提供一个简单的解决transport.js 和 jquery 方法: 在 page_header.lbi 库文件中加入如下代码,注意操作顺序: 1.先导入transport.js 文件 {inse ...
- Four Operations---hdu5938(暴力)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5938 题意:给一个不超过20位并且大于2位的数字字符串(只包含1-9)然后找到适当的位置依次放入&qu ...
- thinkPHP 接支付宝及时到账接口
支付宝及时到帐接口,现在整理以下: 1.先将支付宝提供的公共类库函数库文件防盗thinkPHP的Vender目录下建的一个alipay文件下,以便之后的调用. //四个文件我分别给他们改了下名字,因为 ...