hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】

 

hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】

分类: acm 解题报告 hdu 最短路 图论   272人阅读  评论(0)  收藏  举报

目录(?)[+]

链接:



XYZZY

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1701    Accepted Submission(s): 419


Problem Description
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
 

Source
 

Recommend
Eddy
 

题意:


    有 N 个房间, 编号从 1 到 N 。
    每次进入一个房间, 能量值可能增加也可能减少
    问:从第一个房间开始走, 给你 100 个能量值。
        问你是否能走到第 N 个房间。


    第一行 N  输入房间的个数
    然后下面 N 行数据:
        第 i  行数据的第一个表示进入该房间得到的能量【可正可负】
        第二个表示从该房间出发能到达的房间个数 num
        剩下 num 个数表示可以到达的房间编号

算法:Bellman_ford() 判断正环


注意:有向图,
      然后建图的时候要注意下, 边是没有权的。。。
      点有权

思路:


      其实开始没看题目的时候,没有看到群里的吐槽也不会想到用 Bellman_ford()
      如果图中存在正环, 那么就可以不停的走这个环来增加能量,
      如果环中的点能到达 N 那么肯定是赢了。。。
      但是由于这个先入为主的思想,开始很容易的就让我忽略了,这题的本质是到达 N 的时候还有能量。
      然后就是各种不注意,各种 WA 的血泪史。。。。然后网上各种高深的题解。
      直到看到了一篇用 Bellman_ford 写的


      加边建图的过程同时记录连通性, 先判断 1 与 N 在不考虑能量的时候是否连通【不判断也可以,只是个无关紧要的优化】
      然后就是套用 Bellman_ford() 判断是否有正环


      注意当不存在正环的时候, 不要像以前用这个算法时直接跳出
            因为我们的主要目的不是判断正环,而是要使得到达 N 还有能量。



      那么赢的可能性就两种了:
      1.没有正环, 但是通过Bellman_ford() 的松弛操作, 到 N 的能量值 > 0
      2.存在正环, 正环中的点, 能到达终点。

code:

[cpp]  view plain copy
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. #include<iostream>  
  4. using namespace std;  
  5.   
  6. const int maxn = 110;  
  7. const int INF = 10000000000;  
  8.   
  9. int w[maxn][maxn]; // 判断图的连通性  
  10. int en[maxn]; //进入该点的能量值  
  11. int d[maxn];  //每一点的能量值  
  12. int n,m; //n 个点, m 条边  
  13.   
  14. struct Edge{  
  15.     int u,v;  
  16. }edge[maxn*maxn];  
  17.   
  18. void floyd() // 有向图的传递闭包  
  19. {  
  20.     for(int k = 1; k <= n; k++)  
  21.         for(int i = 1; i <= n; i++)  
  22.             for(int j = 1; j <= n; j++)  
  23.                 w[i][j] = w[i][j] || (w[i][k] && w[k][j]);  //不要写错了 WA 的都是泪。。。  
  24. }  
  25.   
  26. bool Bellman_ford()  
  27. {  
  28.     for(int i = 1; i <= n; i++) d[i] = -INF;  
  29.     d[1] = 100; // 初始第一个点有 100 个能量  
  30.   
  31.     for(int i = 1; i < n; i++)  
  32.     {  
  33.         for(int j = 0; j < m; j++)  
  34.         {  
  35.             int u = edge[j].u;  
  36.             int v = edge[j].v;  
  37.   
  38.             if(d[v] < d[u]+en[v] && d[u]+en[v] > 0) //松弛  
  39.                 d[v] = d[u]+en[v]; //是加上点的权。。。  
  40.         } //注意:不能像以前一样不能松弛了,就直接返回 false 因为判断正环的目的是使 d[n] > 0  
  41.     }  
  42.   
  43.     for(int i = 0; i < m; i++)  
  44.     {  
  45.         int u = edge[i].u;  
  46.         int v = edge[i].v;  
  47.   
  48.         if(d[v] < d[u]+en[v] && d[u]+en[v] > 0) //如果存在正环  
  49.             if(w[v][n]) //正环中的点能够到达终点  
  50.                 return true;  
  51.     }  
  52.   
  53.     return d[n]>0; // 不存在正环, 判断能否依靠 100 个能量值到达终点  
  54. }  
  55.   
  56. int main()  
  57. {  
  58.     while(scanf("%d", &n) != EOF)  
  59.     {  
  60.         if(n == -1) break;  
  61.   
  62.         m = 0; // 初始化边  
  63.         memset(w, 0, sizeof(w));  
  64.         memset(en, 0, sizeof(en));  
  65.         for(int i = 1; i <= n; i++) w[i][i] = 1;  
  66.   
  67.         int num;  
  68.         for(int i = 1; i <= n; i++)  
  69.         {  
  70.             int v;  
  71.             scanf("%d%d", &en[i], &num);  
  72.             while(num--) //注意是单向的  
  73.             {  
  74.                 scanf("%d", &v);  
  75.                 edge[m].u = i;  
  76.                 edge[m++].v = v;  
  77.                 w[i][v] = 1; // 有向图, 不要傻逼的加上 w[v][i] = 1  
  78.             }  
  79.         }  
  80.   
  81.         floyd(); // 考查有向图的连通性  
  82.         /* 
  83.         if(!w[1][n]) 
  84.         { 
  85.             printf("hopeless\n"); continue; 
  86.         }*/  
  87.   
  88.         if(Bellman_ford()) printf("winnable\n");  
  89.         else printf("hopeless\n");  
  90.     }  
  91.     return 0;  
  92. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值