首页 > ACM题库 > HDU-杭电 > HDU 1317 XYZZY-图论-[解题报告] C++
2013
12-09

HDU 1317 XYZZY-图论-[解题报告] C++

XYZZY

问题描述 :

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.

输入:

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.

输出:

In one line for each case, output “winnable” if it is possible for the player to win, otherwise output “hopeless”.

样例输入:

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

样例输出:

hopeless
hopeless
winnable
winnable

这道题纠结了俩小时,总觉得网上的解法存在点问题(也可能是我自己理解的问题),不存在环的情况自然不用说,就是求最长路,对于存在环时候的情况,有一种说法是只要存在正环,直接判断1到n是否连通即可,还有一种说法是找到正环以后,从发现正环的点出发dfs看是否和n连通,如果是就可以到达,不是就直接不可以到达,我感觉不太对,比较支持某位前辈的说法,找到所有的正环,然后判断能否到达n,但是最近比较懒…鉴于这种方法实现比较复杂,最后还是用了个偷懒的方法><

先做一次n-1次循环的Bellman-Ford,因为前面做了n-1次操作,所以后面松弛操作成功的点有两种情况。1.该点本身在正环中,2.该点本身不在正环中,但是和正环连通。然后可以做无限次松弛操作,player在此点可获得的最大能量值赋值为INF,然后把该位置可获得能量值改成负数(如果该点在环中,就相当于去掉了环,不在环中也不影响结果),直到某次循环没有松弛操作则跳出。

现在只需要检查player在n点的最大能量值的正负就可以直到结果了。

#include<iostream>
 #include<vector>
 #include<queue>
 using namespace std;
 #define INF 1000000000
 vector<int>road[120];

 int dis[120],va[120],mark[120];
 int main()
 {
     int n,num,i,j,k,e;
     while(scanf("%d",&n)!=EOF)
     {
         if(n==-1)
             break;
         for(i=1;i<=n;i++)
         {
             road[i].clear();
             dis[i]=0;
             mark[i]=0;
         }
         dis[1]=100;
         for(i=1;i<=n;i++)
         {
             scanf("%d%d",&va[i],&num);
             while(num--)
             {
                 scanf("%d",&e);
                 road[i].push_back(e);
             }
         }
         mark[1]=1;
         int flag=1;
         for(i=1;i<n;i++)
         {
             flag=0;
             for(j=1;j<=n;j++)
             {
                 if(!mark[j])
                     continue;
                 int size=road[j].size();
                 for(k=0;k<size;k++)
                 {
                     int e=road[j][k];
                     if(dis[j]+va[e]>dis[e])
                     {
                         flag=1;
                         dis[e]=dis[j]+va[e];
                         mark[e]=1;
                     }
                 }
             }
             if(!flag) break;
         }
         while(1)
         {
             flag=0;
             for(j=1;j<=n;j++)
             {
                 if(!mark[j])
                     continue;
                 int size=road[j].size();
                 for(k=0;k<size;k++)
                 {
                     int e=road[j][k];
                     if(dis[j]+va[e]>dis[e])
                     {
                         flag=1;
                         dis[e]=INF;
                         mark[e]=1;
                         va[e]=-100;
                     }
                 }
             }
             if(!flag)
                 break;
         }
         if(dis[n]>0)
             printf("winnable\n");
         else
             printf("hopeless\n");
     }
 return 0;
 }

 

 

解题报告转自:http://www.cnblogs.com/SolarWings/archive/2013/03/16/2963961.html


  1. bottes vernies blanches

    I appreciate the efforts you men and women place in to share blogs on such sort of matters, it was certainly useful. Keep Posting!