Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3836 | Accepted: 1109 |
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 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
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
题意:根据给出的关系图,判断是否存在一条从1到n的路径,且最终的cost值为正值,初始值为100。中间各个room的值有正有负。但在求路径的时候,任何一点的value都不能小于或者等于零,否则这条路就不能通。当然,如果有正环,并且可以从1到n是连通的,那么就一定winnable。
思路:
这题,不用构边,因为他是加上到达点的权值的, 因此, 这题最长路直接写就好了, 不用权值为负,跑最短路了
首先明确,要求到n的时候是 >= 0的, 所以就是求最长路,只要有一个符合,就符合题意,所以求最长路, 然后就是要求每个点都 > 0, 就在spfa里加一句话, 如果dis[u] + val[to] > 0,才更新, 如果不是,就不更新让他继续是-inf,这样如果有 <= 0的点存在就到达不了终点,这是根据题意得来的,那如果存在正环呢?存在正环说明他的值可以很大很大, 这样如果1,n连通, 肯定就是可以到达的, 如果不联通,只在一个联通块有正环, 那还是到达不了,为了解决这个问题,有两种解法:
1: 在做spfa时候, 如果出线正环,直接把他的值变成INF, 这样后面的值肯定都可以更新~这样正环里所有的值都是INF, 如果1,n能通过这个正环连接,dis【n】的值肯定>0(被更新过了),但要注意可能有多个正环,INF开太大容易炸,可以开long long 并且第二遍正环的时候continue掉, 不过这题数据水,没啥事;
2.可以先做spfa, 然后弗洛伊德,判断每个正环里的点,可不可以连接1,n
方法1代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 105;
const int INF = 2e9;
vector<int> v[maxn];
int book[maxn], cnt[maxn], dis[maxn], val[maxn], n, x;
void spfa()
{
queue<int> q;
memset(book, 0, sizeof(book));
memset(cnt, 0, sizeof(cnt));
for(int i = 0; i < maxn; i++) dis[i] = -INF;
dis[1] = val[1];
q.push(1);
book[1] = 1;
cnt[1] = 1;
while(!q.empty())
{
int u = q.front();
q.pop();
book[u] = 0;
if(cnt[u] > n) dis[u] = INF;
for(int i = 0; i < v[u].size(); i++)
{
int to = v[u][i];
if(dis[u] + val[to] > dis[to] && dis[u] + val[to] >= 0)
{
dis[to] = val[to] + dis[u];
if(cnt[to] > n) continue;
if(!book[to])
{
book[to] = 1;
q.push(to);
cnt[to]++;
}
}
}
}
if(dis[n] > 0) printf("winnable\n");
else printf("hopeless\n");
}
int main()
{
while(~scanf("%d", &n), n != -1)
{
for(int i = 0; i < maxn; i++)
v[i].clear();
for(int i = 1; i <= n; i++)
{
scanf("%d", &val[i]);
int len;
scanf("%d", &len);
for(int j = 1; j <= len; j++)
{
scanf("%d", &x);
v[i].push_back(x);
}
}
val[1] = 100;
spfa();
}
return 0;
}
方法2代码:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int INF=-9999999;
const int MAXN=111;//注孤 - -|||
bool map[MAXN][MAXN];
int val[MAXN],dis[MAXN];
int n;
bool SPFA()
{
for(int i=1;i<=n;i++)
dis[i]=INF;
bool vis[MAXN]={0};
int cnt[MAXN]={0};
queue<int> q;
q.push(1);
vis[1]=true;
dis[1]=100;
cnt[1]=1;
while(!q.empty())
{
int cur=q.front();
q.pop();
if(cnt[cur] > n)
break;
vis[cur]=false;
for(int i=1;i<=n;i++)
{
if(map[cur][i]==true && dis[cur] + val[i] > dis[i]
&& dis[cur] + val[i] > 0 )
{
dis[i]=dis[cur] + val[i];
if(!vis[i])
{
q.push(i);
vis[i]=true;
cnt[i]++;
}
}
}
}
if(dis[n]>0)
return true;
else
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(map[i][k] && map[k][j])
map[i][j]=true;
for(int i=1;i<=n;i++)
if(cnt[i]>n && map[1][i] && map[i][n]) //忘记要从i到N了
return true;
return false;
}
}
int main()
{
while(~scanf("%d",&n),n!=-1)
{
memset(map,0,sizeof(map));
memset(val,0,sizeof(val));
for(int i=1;i<=n;i++)
{
int cnt,temp;
scanf("%d %d",&val[i],&cnt);
for(int j=1;j<=cnt;j++)
{
scanf("%d",&temp);
map[i][temp]=true;
}
}
if(SPFA())
puts("winnable");
else
puts("hopeless");
}
return 0;
}