标签:ges color uri struct nis ems process vector number
1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<vector> 5 #include<queue> 6 #include<cstdlib> 7 #include<cstring> 8 #include<cstdio> 9 #include<cmath> 10 using namespace std; 11 const int maxn=105; 12 const int maxm=10005; 13 const int INF=0x3f3f3f3f; 14 struct edgenode { 15 int to,w,next; 16 }edges[maxm]; 17 bool vis[maxn]; 18 int dist[maxn],du[maxn],head[maxn]; 19 int n,cnt; 20 void init() { 21 for(int i=0;i<maxn;++i) head[i]=-1; 22 for(int i=0;i<maxm;++i) edges[i].next=-1; 23 cnt=0; 24 } 25 void addedge(int u, int v, int w) { 26 edges[cnt].to=v; 27 edges[cnt].w=w; 28 edges[cnt].next=head[u]; 29 head[u]=cnt++; 30 } 31 bool spfa() { 32 memset(vis,false,sizeof(vis)); 33 memset(du,0,sizeof(du)); 34 for(int i=0;i<maxn;++i) dist[i]=-INF; 35 queue<int> q; 36 dist[1]=100;vis[1]=true; 37 q.push(1); 38 while(!q.empty()) { 39 int now=q.front();q.pop(); 40 vis[now]=false; 41 du[now]++; 42 if(du[now]>n) continue; 43 if(du[now]==n) dist[now]=INF; 44 for(int i=head[now];~i;i=edges[i].next) { 45 if(dist[edges[i].to]<dist[now]+edges[i].w&&dist[now]+edges[i].w>0) { 46 dist[edges[i].to]=dist[now]+edges[i].w; 47 if(edges[i].to==n) return true; 48 if(!vis[edges[i].to]) { 49 vis[edges[i].to]=true; 50 q.push(edges[i].to); 51 } 52 } 53 } 54 } 55 return false; 56 } 57 int main() { 58 while(scanf("%d",&n)&&n!=-1) { 59 int w,num,id; 60 init(); 61 for(int i=1;i<=n;++i) { 62 scanf("%d%d",&w,&num); 63 for(int j=1;j<=num;++j) { 64 scanf("%d",&id); 65 addedge(i,id,w); 66 } 67 } 68 if(spfa()) printf("winnable\n"); 69 else printf("hopeless\n"); 70 } 71 return 0; 72 }
HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)
标签:ges color uri struct nis ems process vector number
原文地址:http://www.cnblogs.com/lemonbiscuit/p/7775976.html