Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
spfa/C++/224K 47MS#include <stdio.h> #include <memory.h> #include <string.h> #include <queue> #define MAXN 31 int vexnum,headlist[MAXN],visited[MAXN],Enter[MAXN]; char name[31][101]; using namespace std; typedef struct { int start,end; int next; double rate; }Edge; Edge de[10001]; double dis[MAXN]; int FindEdge(char s[]) { for(int i=1;i<=vexnum;++i) if(strcmp(s,name[i])==0) return i; return -1; } bool spfa(int k) { queue<int>q; int i,temp,x,y; memset(visited,0,sizeof(visited)); memset(dis,0,sizeof(dis)); memset(Enter,0,sizeof(Enter)); q.push(k),visited[k]=Enter[k]=1,dis[k]=1; while(!q.empty()) { temp=q.front(); q.pop(); visited[temp]=0; for(i=headlist[temp];i!=-1;i=de[i].next) { x=de[i].start,y=de[i].end; if(dis[y]<dis[x]*de[i].rate) { dis[y]=dis[x]*de[i].rate; if(!visited[y]) { visited[y]=1; Enter[y]++; if(Enter[y]>=vexnum) return true; q.push(y); } } } } return false; } int main() { int n,i,m,len=1; char start[101],end[101]; while(scanf("%d",&n)&&n!=0) { getchar(),vexnum=n; for(i=1;i<=n;++i) gets(name[i]); scanf("%d",&m); memset(headlist,-1,sizeof(headlist)); for(i=0;i<m;++i) { scanf(" %s %lf %s",start,&de[i].rate,end); de[i].start=FindEdge(start); de[i].end=FindEdge(end); de[i].next=headlist[de[i].start]; headlist[de[i].start]=i; } m=0; for(i=1;i<=vexnum;++i) { if(spfa(i)==true) { m=1; break; } } if(m) printf("Case %d: Yes\n",len++); else printf("Case %d: No\n",len++); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator