传递闭包(Floyd+bellman-Fold POJ1932)
生活随笔
收集整理的這篇文章主要介紹了
传递闭包(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
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
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");}elseprintf("hopeless\n");} }轉載于:https://www.cnblogs.com/mypsq/p/4348118.html
總結
以上是生活随笔為你收集整理的传递闭包(Floyd+bellman-Fold POJ1932)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于网站域名的配置过程
- 下一篇: Checked ==true ? Y:N