zoukankan      html  css  js  c++  java
  • uva- 10557 XYZZY

    题目大意就是找从起点到终点是否能有一条大于0的路。题目中有回路可正可负。。

    一开始用dfs写当有回路的时候判断是否大于0,如果大于0则把这条路设为INF , 从这条路一定可以到终点大于0。。但是一直wa。之后改用spfa设置最大次数10000才AC了

    题目:

    Problem D: XYZZY


    ADVENT: /ad�vent/, n.
    

    The prototypical computer adventure game, first designed by Will Crowther on the PDP-10 in the mid-1970s as an attempt at computer-refereed fantasy gaming, and expanded into a puzzle-oriented game by Don Woods at Stanford in 1976. (Woods had been one of the authors of INTERCAL.) Now better known as Adventure or Colossal Cave Adventure, but the TOPS-10 operating system permitted only six-letter filenames in uppercase. See also vadding, Zork, and Infocom.


    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".

    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
    

    Output for Sample Input

    hopeless
    hopeless
    winnable
    winnable
    


    代码:
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <vector>
      4 #include <memory.h>
      5 #include <iterator>
      6 #include <queue>
      7 using namespace std;
      8 #define INF 100000
      9 vector<int> G[200];
     10 int value[200];
     11 int D[200];
     12 int vis[200];
     13 int N;
     14 bool flag= false;
     15 //int dfs(int u,int life)
     16 //{
     17 //    life+= value[u];
     18 ////    cout<<u<<"   "<<life<<endl;
     19 //    vis[u]=1;
     20 //    if(life<0)return -1;
     21 //    if(u==N-1 && life>0)
     22 //    {
     23 //        flag =true;
     24 //        cout<<"winnable"<<endl;
     25 //        return 0;
     26 //    }
     27 //    for(vector<int>::iterator iv = G[u].begin(); iv!=G[u].end(); iv++)
     28 //    {
     29 //        if(!vis[*iv])
     30 //        {
     31 //            dfs(*iv, life );
     32 //        }
     33 //        else if(vis[*iv])
     34 //        {
     35 //            if(life + value[*iv]> value[*iv])
     36 //            {
     37 //                life=INF;
     38 //            }
     39 //        }
     40 //    }
     41 //
     42 //
     43 //    return 0;
     44 //}
     45 
     46 
     47 int spfa()
     48 {
     49     queue<int> v;
     50     v.push(0);
     51     for(int i=0;i<N;i++)
     52     {
     53         D[i]=0;
     54     }
     55     D[0]=100;
     56     while(!v.empty())
     57     {
     58         int  u = v.front();
     59          for(vector<int>::iterator iv = G[u].begin(); iv!=G[u].end(); iv++)
     60          {
     61                 if(D[*iv] <D[u] + value[*iv])
     62                 {
     63                     D[*iv] = D[u]+value[*iv];
     64                     vis[*iv]++;
     65                     if(vis[*iv]<10000)
     66                     v.push(*iv);
     67                 }
     68          }
     69          v.pop();
     70     }
     71     if(D[N-1]>0)cout<<"winnable"<<endl;
     72     else cout<<"hopeless"<<endl;
     73 
     74 
     75 }
     76 
     77 int main()
     78 {
     79     freopen("in.txt","r",stdin);
     80     while(cin>>N &&N!=-1)
     81     {
     82         for(int i=0;i<N;i++)
     83         {
     84             int n;
     85             cin>>value[i]>>n;
     86             for(int j=0;j<n;j++)
     87             {
     88                 int temp;
     89                 cin>>temp;
     90                 temp--;
     91                 G[i].push_back(temp);
     92             }
     93         }
     94 //        dfs(0,100);
     95 
     96         spfa();
     97 
     98 //
     99 //        if(!flag )
    100 //            cout<<"hopeless"<<endl;
    101 
    102 
    103         flag =false;
    104         memset(G,0,sizeof(G));
    105         memset(value,0,sizeof(value));
    106         memset(vis,0,sizeof(vis));
    107         memset(D,0,sizeof(D));
    108     }
    109 
    110 
    111     return 0;
    112 }
  • 相关阅读:
    2016孤独重视
    什么时候有空写一篇影评 《含泪活着》
    登录页面总结
    心跳机制
    心跳机制详解
    mysql数据库字符编码修改
    mysql20170404代码实现
    2017全面JAVA面试经历总结
    Oracle经典入门教程
    MySQL基础教程-绝对推荐
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3474348.html
Copyright © 2011-2022 走看看