您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: