HDU 1317:XYZZY
Problem 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 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
题目大意:一个最多有100个节点的图,你需要从1走到n(注意哦,有向边)。初始有100个能量,到达一个点会使你的能量值变化,如果能在能量为正的情况下到达点n则输出“NOIP有希望了dalao%%%”,否则输出“NOIP翻车了好开心”。(233333~)
芒果君:之前我们做的都是最短路,现在要做最长路,改个符号就行了。这道题对于我的难点是——如何判环。在不断更新最长路时负环可以忽略,但遇到正环应该怎样处理?我看到有一种Floyd+SPFA的方法比较好理解,其实很像广搜。这道题数据比较小,那我们就先做一遍Floyd,在SPFA中记录一个点被放进队列的次数,如果这个次数很不科学(cnt==n),那么就说明它在正环里。But,存在正环不能说明到不了终点,相反,如果它与终点连通,一定能到达(如果你觉得正环到终点可能能量会耗尽,那么请你讲正环看成无敌聚能环233333再好好想想),所以我们要在之前处理连通性。
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstdlib> 7 #include<vector> 8 using namespace std; 9 vector<int>e[110]; 10 queue<int>Q; 11 int oc[110],vis[110],dis[110],map[110][110],n,w[110]; 12 int read() 13 { 14 int x=0,f=1; 15 char ch=getchar(); 16 while(ch<'0'||ch>'9'){ 17 if(ch=='-') f=-1; 18 ch=getchar(); 19 } 20 while(ch>='0'&&ch<='9'){ 21 x=x*10+ch-'0'; 22 ch=getchar(); 23 } 24 return x*f; 25 } 26 void floyd() 27 { 28 for(int k=1;k<=n;++k) 29 for(int i=1;i<=n;++i) 30 for(int j=1;j<=n;++j) 31 map[i][j]=map[i][j]||(map[i][k]&&map[k][j]); 32 } 33 bool spfa() 34 { 35 memset(dis,-127/3,sizeof(dis)); 36 memset(vis,0,sizeof(vis)); 37 memset(oc,0,sizeof(oc)); 38 dis[1]=100; 39 Q.push(1); 40 vis[1]=1; 41 oc[1]=1; 42 while(!Q.empty()){ 43 int x=Q.front(); 44 Q.pop(); 45 vis[x]=0; 46 if(oc[x]>=n) { 47 if(map[x][n]) return true; 48 else continue; 49 } 50 for(int i=0;i<e[x].size();++i){ 51 int v=e[x][i]; 52 if(dis[v]<dis[x]+w[v]&&dis[x]+w[v]>0){ 53 dis[v]=dis[x]+w[v]; 54 if(v==n) return true; 55 if(!vis[v]){ 56 oc[v]++; 57 vis[v]=1; 58 Q.push(v); 59 } 60 } 61 } 62 } 63 return false; 64 } 65 int main() 66 { 67 int cnt,t; 68 while(1){ 69 scanf("%d",&n); 70 if(n==-1) break; 71 memset(map,0,sizeof(map)); 72 for(int i=1;i<=n;++i){ 73 e[i].clear(); 74 w[i]=read();cnt=read(); 75 while(cnt--){ 76 t=read(); 77 e[i].push_back(t); 78 map[i][t]=1; 79 } 80 } 81 floyd(); 82 if(spfa()) printf("winnable\n"); 83 else printf("hopeless\n"); 84 } 85 return 0; 86 }