From Evernote: |
uva 10557 |
SPFA加點的權值超級活用,需要多加練習
[sourcecode language="cpp"]
//============================================================================
// Name : XYZZY.cpp
// Date : 2013 2013/2/23 下午9:14:33
// Author : GCA
//============================================================================
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 110
#define PII pair<int,int>
#define PB push_back
#define oo INT_MAX
#define Set_oo 0x3f
#define Is_debug true
#define debug(...) if(Is_debug)printf("DEBUG: "),printf(__VA_ARGS__)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
int weight[M];
int can_arrive[M];
int mz[M][M];
int q[M*M*M];
int n;
int vis[M];
void dfs(int x){
can_arrive[x]=1;
vis[x]=1;
for(int i=1;i<=n;i++){
if(mz[i][x]&&!vis[i]){
dfs(i);
}
}
}
void bf(){
int dist[M];
int front,rear,inqueue[M];
Set(dist,0);
Set(inqueue,0);
front=rear=0;
dist[1]=100;
q[front++]=1;
inqueue[1]=1;
int k=0,fg=0;
while(front>rear){
// printf("k%d\n",k);
int u=q[rear++];
for(int i=1;i<=n;i++){
if(can_arrive[i]&&mz[u][i]&&dist[u]+weight[i]>dist[i]){
dist[i]=dist[u]+weight[i];
// printf("dist%d=%d\n",i,dist[i]);
if(!inqueue[i]){
inqueue[i]=1;
q[front++]=i;
}
}
}
if(k++>=n-1){
fg=1;
break;
}
inqueue[u]=0;
}
if(fg||dist[n]>0)printf("winnable\n");
else printf("hopeless\n");
}
void solve(){
Set(can_arrive,0);
Set(vis,0);
dfs(n);
if(can_arrive[1])
bf();
else printf("hopeless\n");
}
int main() {
ios_base::sync_with_stdio(0);
while(~scanf("%d",&n)&&n!=-1){
Set(mz,0);
for(int i=1;i<=n;i++){
int k;
scanf("%d%d",&weight[i],&k);
for(int j=0;j<k;j++){
int v;
scanf("%d",&v);
mz[i][v]=1;
}
}
solve();
}
}
[/sourcecode]
留言
張貼留言