poj 1932(最长路Bellman_Ford

传送门

XYZZY

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 3820 Accepted: 1103

Description

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. 

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

Source

Waterloo local 2003.09.27 题意:n个操作。三个数代表经过这条边能量的变化量,多少条边,具体哪个点和他相连。初始在1位置能量为100。如果能在到达终点能量都能>=0的话就winnable 不能就hopeless 比如样例:

5
0 1 2
-60 1 3
-60 1 4
20 1 5
0 0

建边1-2距离为0 2-3距离为-60 3-4距离为-60 4-5距离为20 当走到4的时候就-20能量了。 走不动了hopeless 可能会出现正环的情况,如最后个样例,所以用最长路。(Bellman_Ford

还得判一下图是否连通(floyd 

//china no.1
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define rand() srand(time(0));
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0);
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,-1,-1,1,1};
const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxn=1e3+5;
const int maxx=1e3+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while

int n,x,y,k,m,ni,ki,b,c,t;
char oi[5];
struct edge
{
    int u, v, w;
};

vector <edge> G;
int d[maxx],inq[maxx],num[maxx],out[maxx],dis[maxx];
int a[maxn][maxn];
void floyd()
{
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                a[i][j] = a[i][j] || (a[i][k] && a[k][j]);

}
bool Bellman_Ford()
{
    for(int i = 1; i <= n; i++)
        dis[i] = -999999999;
    dis[1] = 100;
    for(int i = 1; i < n; i++)
    {
        for(int j = 0; j < G.size(); j++)
        {
            edge e = G[j];
            if(dis[e.v] < dis[e.u] + e.w && dis[e.u] + e.w > 0)
                dis[e.v] = dis[e.u] + e.w;
        }
    }
    //printf("%d\n", dis[n]);
    if(dis[n] > 0)
        return true;
    for(int i = 0; i < G.size(); i++)
    {
        edge e = G[i];
        if(dis[e.v] < dis[e.u] + e.w && dis[e.u] + e.w > 0)
        {
            //puts("sss");
            dis[e.v] = dis[e.u] + e.w;
            if(a[e.v][n])
                return true;
        }
    }
    return false;
}
int main()
{
    W(cin>>n)
    {
        //init();
        G.clear();
        me(a);
        if(n==-1) return 0;
        //m=Scan();
        FOR(1,n,i)
        {
            cin>>c>>t;
            W(t--)
            {
                cin>>b;
                G.push_back((edge){i,b,c});
                //E[i].push_back(make_pair(b,c));
                a[i][b]=1;
            }

        }
        floyd();
        if(Bellman_Ford())
        {
            puts("winnable");
            continue;
        }
        else puts("hopeless");
    }
}
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/qq_36553623/article/details/75096382
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞