日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

传递闭包(Floyd+bellman-Fold POJ1932)

發(fā)布時(shí)間:2025/7/25 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 传递闭包(Floyd+bellman-Fold POJ1932) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
傳遞閉包 在一個(gè)有向(無向)連通圖中,如果節(jié)點(diǎn)i與k聯(lián)通,k與j聯(lián)通,則i和j聯(lián)通,傳遞閉包就是把所有傳遞性的節(jié)點(diǎn)求出來,之后就知道了任意兩個(gè)節(jié)點(diǎn)的連通性,只需枚舉節(jié)點(diǎn)的聯(lián)通情況即可,無需考慮最短路徑: 代碼: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題意:給出一個(gè)單向連通圖,和每個(gè)節(jié)點(diǎn)的能量,代表走到這個(gè)節(jié)點(diǎn)就可以獲得,初始化能量為100,當(dāng)?shù)竭_(dá)某個(gè)節(jié)點(diǎn)后能量<=0則 不能繼續(xù)走,問從1開始能不能到達(dá)n分析:首先用floyd檢驗(yàn)圖的連通性即1能否到達(dá)n,若不能直接輸出hopeless,如果1可以到達(dá)n,但是由于能量限制可能走不到n,如果從1可以到達(dá)一個(gè)正環(huán)(可以不斷轉(zhuǎn)圈獲得能量)而且正環(huán)的點(diǎn)可以到達(dá)n,這種情況也是winnable,所以用bellman-Foyd判斷正環(huán),且正環(huán)上的點(diǎn)可以到達(dá)n,注意當(dāng)?shù)竭_(dá)某點(diǎn)的時(shí)候能量為非正,則不能從此點(diǎn)繼續(xù)下去#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;//注意該連通性會(huì)用到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可以到達(dá)正環(huán)而正環(huán)可以到n或者1可以直接到n就是可以的{printf("winnable\n");}elseprintf("hopeless\n");} }


轉(zhuǎn)載于:https://www.cnblogs.com/mypsq/p/4348118.html

總結(jié)

以上是生活随笔為你收集整理的传递闭包(Floyd+bellman-Fold POJ1932)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。