Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

spfa/C++/224K 47MS

Posted by 15211160230 at 2016-09-01 19:58:20 on Problem 2240
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator