HDU - 1317 XYZZY

题目:
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

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define MAX 105
#define NIL 0x3f3f3f3f
int n,c,k,x;
int mp[MAX][MAX],dis[MAX],vis[MAX];
bool inq[MAX];
bool spfa(int a)
{
    if(n == 1) return true;
    memset(vis,0,sizeof(vis));
    memset(dis,-NIL,sizeof(dis));
    memset(inq,false,sizeof(inq));
    dis[a] = 100;
    queue<int> q;
    q.push(a);
    inq[a] = true;
    while(!q.empty()){
        int flag = q.front();
        q.pop();
        vis[flag]++;
        inq[flag] = false;
        if(vis[flag] == n + 2) continue;
        if(vis[flag] == n + 1) dis[flag] = 100 * 100;
        for(int i = 1;i <= n;i++){
            if(mp[flag][i] == -NIL) continue;
            if(dis[i] < dis[flag] + mp[flag][i] && dis[flag] + mp[flag][i] > 0){
                dis[i] = dis[flag] + mp[flag][i];
                if(dis[n] > 0) return true;
                if(!inq[i]) q.push(i);
            }
        }
    }
    return false;
}
int main()
{
    while(~scanf("%d",&n) && (n != -1)){
        memset(mp,-NIL,sizeof(mp));
        for(int i = 1;i <= n;i++){
            cin >> c >> k;
            while(k--){
                cin >> x;
                mp[i][x] = c;
            }
        }
        bool b = spfa(1);
        if(b) cout << "winnable" << endl;
        else cout << "hopeless" <<endl;
    }
    return 0;
}

题意:
第一行输入n代表有n个点,随后输入n行,每一行第一个数代表权值,第二个数k代表与i点相连接的个数,之后输入连接点的序号。刚开始一个人在起点1有100点能量,每次走到其他点可能增加能量也可能失去能量,每次到达的条件一定要让能量大于0,问你是否那个人可以到达n点。
思路:
因为有负权边,所以这里用spfa算法,第一次写spfa还是看了别人的题解才勉强ac的,这里的spfa和不同的不一样,普通的一般是用来判断是否存在负权环的,但这里是来判断是否有正权环。

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值