[hdu1317]spfa
题意:给一个有向图,每个点有一个权值,从1个点出发,初始能量有100,每到达新的点,能量就会加上那个点的权值,当能量大于0时才能继续走,可以多次进入同一点。问能否到达目标点
思路:如果没正权环,则直接优先队列bfs模拟走的过程即可,因为先到不会比后到的能量少,那过程其实就和dijkstra差不多,但根据题目的意思,是可能存在正权环的,所以dijkstra行不通,于是考虑spfa。一旦某个点入队了n次,就可判定这个点在正权环上,通过在正权环上不断走来获得无限的能量,于是将这个点的能量值设为无穷大,并让它再入队一次后丢弃这个点(因为能量值变为了无穷大,需要用它来更新邻点,更新完了它就没有存在的意义了),同时判断这个点是否与目标点连通,如果连通,那么毫无疑问,目标点肯定可以顺利到达。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | #include <map> #include <set> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <vector> #include <cstdio> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define X first #define Y second #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() #define fillchar(a, x) memset(a, x, sizeof(a)) typedef long long ll; typedef pair< int , int > pii; typedef unsigned long long ull; #ifndef ONLINE_JUDGE void RI(vector< int >&a, int n){a.resize(n); for ( int i=0;i<n;i++) scanf ( "%d" ,&a[i]);} void RI(){} void RI( int &X){ scanf ( "%d" ,&X);} template < typename ...R> void RI( int &f,R&...r){RI(f);RI(r...);} void RI( int *p, int *q){ int d=p<q?1:-1; while (p!=q){ scanf ( "%d" ,p);p+=d;}} void print(){cout<<endl;} template < typename T> void print( const T t){cout<<t<<endl;} template < typename F, typename ...R> void print( const F f, const R...r){cout<<f<< ", " ;print(r...);} template < typename T> void print(T*p, T*q){ int d=p<q?1:-1; while (p!=q){cout<<*p<< ", " ;p+=d;}cout<<endl;} #endif template < typename T> bool umax(T&a, const T&b){ return b<=a? false :(a=b, true );} template < typename T> bool umin(T&a, const T&b){ return b>=a? false :(a=b, true );} template < typename T> void V2A(T a[], const vector<T>&b){ for ( int i=0;i<b.size();i++)a[i]=b[i];} template < typename T> void A2V(vector<T>&a, const T b[]){ for ( int i=0;i<a.size();i++)a[i]=b[i];} const double PI = acos (-1.0); const int INF = 1e9 + 7; /* -------------------------------------------------------------------------------- */ const int maxn = 107; struct Graph { vector<vector< int > > G; void clear() { G.clear(); } void resize( int n) { G.resize(n + 2); } void add( int u, int v) { G[u].push_back(v); } vector< int > & operator [] ( int u) { return G[u]; } }; Graph G; bool vis[maxn], flag[maxn]; int n; int cnt[maxn], d[maxn], p[maxn]; bool dfs( int s, int t) { if (s == t) return true ; vis[s] = true ; for ( int i = 0; i < G[s].size(); i ++) { int v = G[s][i]; if (!vis[v]) if (dfs(v, t)) return true ; } return false ; } bool relax( int u, int v) { if (d[u] + p[v] > d[v]) { d[v] = d[u] + p[v]; return true ; } return false ; } bool work() { queue< int > Q; Q.push(1); fillchar(d, 0); fillchar(flag, 0); fillchar(cnt, 0); d[1] = 100; flag[1] = true ; while (!Q.empty()) { int u = Q.front(); Q.pop(); flag[u] = false ; if (u == n) return true ; if (d[u] >= 1e8) { fillchar(vis, 0); if (dfs(u, n)) return true ; } int sz = G[u].size(); for ( int i = 0; i < sz; i ++) { int v = G[u][i]; if (relax(u, v)) { if (!flag[v]) { flag[v] = true ; if (cnt[v] > n) continue ; if (cnt[v] == n) d[v] = INF; Q.push(v); cnt[v] ++; } } } } return false ; } int main() { #ifndef ONLINE_JUDGE freopen ( "in.txt" , "r" , stdin); //freopen("out.txt", "w", stdout); #endif // ONLINE_JUDGE int m, v; while (cin >> n, ~n) { G.clear(); G.resize(n); for ( int i = 1; i <= n; i ++) { scanf ( "%d%d" , p + i, &m); for ( int j = 0; j < m; j ++) { scanf ( "%d" , &v); G.add(i, v); } } fillchar(vis, 0); if (!dfs(1, n)) puts ( "hopeless" ); else puts (work()? "winnable" : "hopeless" ); } return 0; } |