传递闭包

在一个有向(无向)连通图中,如果节点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: 1000MS   Memory 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
-1

Sample 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");}elseprintf("hopeless\n");}
}

转载于:https://www.cnblogs.com/mypsq/p/4348118.html

传递闭包(Floyd+bellman-Fold POJ1932)相关推荐

  1. 算法小讲堂之最短路算法(Floyd+bellman+SPFA+Dijkstra)

    前言 如果你对图论相关知识一点也没有,那么建议您先去了解这些知识:https://acmer.blog.csdn.net/article/details/122310835,然后就可以快乐的学习最短路 ...

  2. POJ 3660 Cow Contest 传递闭包+Floyd

    原题链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  3. 作为一种实验的2050

    毫不意外,2021年的2050@2020&2021,又是一次幸福之旅.只是因为另有琐事缠身,我只参加了2天的2050,在幸福中不免留下了遗憾. 在第二天下午,与Elaine的交流给我很多收获, ...

  4. 大学计算机科学的14个知识领域

    到一个地方去旅游,最好研究一下当地的地图.准备学习编程的时候,最好了解计算机科学的概貌--拥有一份大图(big map).英国著名哲学家怀特海曾说过:『在中学阶段,学生伏案学习:在大学阶段,他需要站起 ...

  5. H - Cow Contest POJ - 3660(Floyd 传递闭包)

    H - Cow Contest POJ - 3660 题意: 有 n 头牛比赛,边 1 -> 2 代表 1 能赢 2 ,给你 m 条边,问能确定出多少头牛的名次? 思路: 如果 1->2 ...

  6. POJ 3660 Cow ContestCow(Floyd传递闭包)题解

    题意:给出m个关系,问你能确定机头牛的排名 思路:要确定排名那必须要把他和其他n-1头牛比过才行,所以Floyd传递闭包,如果赢的+输的有n-1就能确定排名. 代码: #include<cstd ...

  7. POJ 3275 Ranking the Cows (floyd传递闭包)

    Ranking the Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2248   Accepted: 1045 ...

  8. 【BZOJ5335】【TJOI2018】—智力竞赛(floyd传递闭包+二分图匹配)

    传送门 可以二分变成直接求最大链覆盖,因为可以重复经过一个点 floydfloydfloyd传递闭包就可以了 #include<bits/stdc++.h> using namespace ...

  9. poj 3660(Floyd求传递闭包)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9317   Accepted: 5249 Descr ...

最新文章

  1. 小插件 打开Android程序动画,android-单击小部件后如何启动活动?
  2. netmiko 自动判断设备类型python_Python模块-Netmiko入门
  3. 公共基础知识计算机,公共基础知识计算机基础知识试题
  4. 2012年总结,2013年的计划
  5. 19岁白帽子通过bug悬赏赚到一百万美元--转
  6. 百万级并发 Node.js也能行
  7. [vue] 怎么捕获组件vue的错误信息?
  8. linux基本命令-ls
  9. linux 项目同步,Linux项目系统,Linux控制台窗口,同步和附加到进程的Linux C ++工作负载改进...
  10. django1.11使用mysql_django 1.11.1 连接MySQL
  11. 【PHP基础】文件操作
  12. 来,教你写一手好SQL!
  13. 获取非行间样式和行间样式方法
  14. 前后端传参(二)之数组对象
  15. Adobe Photoshop CS5永久序列号
  16. SpringMVC对PathVariable的特殊字符.的处理默认是文件后缀
  17. AST反混淆实战:猿人学爬虫比赛第二题详细题解
  18. 【宋红康 MySQL数据库】【基础版】【15】存储过程与存储函数
  19. [POJ3384]Feng Shui(半平面交+凸包)
  20. 使用组件不渲染 Unknown custom element: <xxx> - did you register the component correctly? For recursiv

热门文章

  1. twemproxy 简介
  2. 《软件工程》总结——第十一章
  3. Windows Phone开发(27):隔离存储A
  4. HotSpot JVM 垃圾收集原理
  5. leetcode算法题--最长数对链
  6. axios vue 回调函数_Vue 02 —— Vue 入门小案例~使用 Axios 中的GET、POST请求
  7. python可变参数_Python 的四种共享传参详解
  8. 如何给图片赋值_医学数据的变量类型及在SPSS中的赋值方法(医学统计前的重要步骤)——【杏花开医学统计】...
  9. WCF单元测试遇到的问题
  10. 高速串行自同步方式介绍及原理