hdu-1317-XYZZY-Bellman-Ford判环、Floyd算法

本文介绍了一种通过Bellman-Ford算法和Floyd算法来判断一款特定类型的游戏是否可赢的方法。具体而言,该方法首先确保起始房间与结束房间间存在路径连接,接着检查玩家是否能在不耗尽能量的情况下到达结束房间。

Link:

http://acm.hdu.edu.cn/showproblem.php?pid=1317

XYZZY

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

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

University of Waterloo Local Contest 2003.09.27

解释

    上图我们分别考虑n=6和n=7两种情况这两种特例。

    我们考虑一下首先必须从1可以到达n,暂且不考虑能量问题,也就是说首先1,n必须连通,然后我们考虑如果是无环的话,必须到达
    n点能量仍然保持正数。如果有环,负环的话我们完全可以不用考虑,如果有正环并且可以到达,那么只要环上的点可以到达n,那么必然可以,我们
    便想到了Bellman-Ford算法和Floyd算法。

Code

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn(105);
const int INF(0x3f3f3f3f);
int map[maxn][maxn];/******用于判断图上的任意两个端点是否连通*******/
int n;
int room_energy[maxn];
int edge_index;/*****存储这个图开始时给出的边的个数******/
struct edge
{
    int from,to;
} edges[maxn*maxn];
void init()
{
    memset(map,0,sizeof(map));
    for(int i=1; i<=n; i++)
        map[i][i]=1;
    edge_index=0;
}
void input()
{
    int cnt_next,_next;
    for(int i=1; i<=n; i++)
    {
        scanf("%d%d",room_energy+i,&cnt_next);
        while(cnt_next--)
        {
            scanf("%d",&_next);
            map[i][_next]=1;
            ++edge_index;
            edges[edge_index].from=i;
            edges[edge_index].to=_next;
        }
    }
}
void floyd()/******Floyd算法用于判定任意两点的连通**********/
{
    for(int i=1; i<=n; i++)/******Floyd算法注意i,j,k的位置,不能颠倒*******/
    {
        for(int j=1; j<=n; j++)
        {
            for(int k=1; k<=n; k++)
            {
                map[i][j]=map[i][j]||(map[i][k]&&map[k][j]);
            }
        }
    }
}
bool bellman_ford()
{
    int dis[maxn];
    for(int i=2; i<=n; i++)
    {
        dis[i]=-INF;
    }
    dis[1]=100;
    int from,to;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=edge_index; j++)
        {
            from=edges[j].from;
            to=edges[j].to;
            /****注意此处dis[from]+room_energy[to]>0的意思是还没有run out of energy****/
            if(dis[to]<dis[from]+room_energy[to]&&dis[from]+room_energy[to]>0)
            {
                dis[to]=dis[from]+room_energy[to];
            }
        }
    }
    for(int i=1; i<=edge_index; i++)
    {
        from=edges[i].from;
        to=edges[i].to;
        if(dis[to]<dis[from]+room_energy[to]&&dis[from]+room_energy[to]>0&&map[to][n])
            return 1;/*****若有环的地方可以与终点连通那么必然可以******/
    }
    return dis[n]>0;/****没遇到环但是能量没有耗光****/
}
int main()
{
    while(scanf("%d",&n),n!=-1)
    {
        init();
        input();
        floyd();
        if(!map[1][n])/********如果开始点与最后的点都不连通,那么必定不可能**********/
        {
            puts("hopeless");
            continue;
        }
        if(bellman_ford())
            puts("winnable");
        else
            puts("hopeless");
    }
    return 0;
}
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值