HDU 1317 XYZZY
2016-04-20 09:06
225 查看
XYZZY
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4153 Accepted Submission(s): 1151
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
In one line for each case, output "winnable" if it is possible for the player to win, otherwise output "hopeless".
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
hopeless hopeless winnable winnable
题意:出发点就有100点能量,到达一个新点能得到一个正或者负的能量,可走重复的点 ,所以如果有环,且环能得到总能量大于0,则必定可以到最终点
先用floyd扫描一遍图的连通性,再用bellman_ford做一遍最长路,如果终点能量大于0(达到终点),或者正环中的点(一直循环无限能量)能到达终点(floyd判断),则能到达终点,否则失败
#include <stdio.h> #include <string.h> #include <vector> using namespace std; int n; struct node { int u; int v; int w; } e; vector<node>map; bool a[105][105]; int dist[105]; void Floyd() { int i, j, k; for(k = 1; k <= n; k++) for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) a[i][j] = a[i][j] || (a[i][k] && a[k][j]); } bool Bellman_Ford() { int i, j; memset(dist, -0x3f, sizeof(dist)); dist[1] = 100; for(i = 1; i < n; i++) { for(j = 0; j < map.size(); j++) { e = map[j]; if(dist[e.u] + e.w > 0 && dist[e.v] < dist[e.u] + e.w) dist[e.v] = dist[e.u] + e.w; } } if(dist > 0) return true; for(j = 0; j < map.size(); j++) { e = map[j]; if(dist[e.u] + e.w > 0 && dist[e.v] < dist[e.u] + e.w && a[e.u] ) return true; } return false; } int main() { int i, w, t, v; while(scanf("%d", &n) != EOF && n != -1) { map.clear(); memset(a, false, sizeof(a)); for(i = 1; i <= n; i++) { scanf("%d%d", &w, &t); e.u = i; e.w = w; while(t--) { scanf("%d", &v); e.v = v; map.push_back(e); a[i][v] = true; } } Floyd(); if(Bellman_Ford()) printf("winnable\n"); else printf("hopeless\n"); } return 0; }
相关文章推荐
- mysql中GROUP BY结合GROUP_CONCAT的使用
- CI 笔记3 (easyui 的layout布局,最小化layout原型)
- java单例模式的几种写法
- JS实现消息来时让网页标题闪动效果的方法
- C# 基于StackExchange.Redis.dll利用Redis实现分布式Session
- poj2155 Matrix 【二维树状数组】
- php四种基础算法:冒泡,选择,插入和快速排序法
- php中数组中&的问题
- Linux下目录读权限与执行权限区别
- CoreGraphics Overview of Quartz 2D
- python中self的作用
- 浅谈Facebook的服务器架构
- scp不成功
- 053(三十六)
- Cordova - 使用Cordova开发iOS应用实战4(调用摄像头拍照,并编辑)
- Html和文本、编程语言学习的思维确别
- iOS版上传数据到阿里后台~ AliyunOSSiOS(转载自http://www.jianshu.com/p/27435fe627be)
- DIY自己的虚拟现实世界
- 可视化于机器学习
- C++构造函数与虚表覆盖