题目:
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和不同的不一样,普通的一般是用来判断是否存在负权环的,但这里是来判断是否有正权环。