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-204K 0ms

Posted by 15211160230 at 2016-08-30 13:00:35 on Problem 1860
对spfa有深入理解了,判断是否含有源点的正权回路,因为各个边的权大小直接或间接取决于源点的权值,如果有正权回路,那么一定会与源点连通。
#include <stdio.h>
#include <memory.h>
#include <queue>
#define MAXV 101
using namespace std;
typedef struct
{	int start,end;
	double rate,comm;
	int next;
}Edge;
Edge de[2001];
int headlist[MAXV],vexnum,edgenum;
int Enter[MAXV],visited[MAXV];
double dis[MAXV];
void Insert(int a,int b,double a1,double a2)
{	de[edgenum].start=a;
	de[edgenum].end=b;
	de[edgenum].rate=a1;
	de[edgenum].comm=a2;
	de[edgenum].next=headlist[a];
	headlist[a]=edgenum;
	edgenum++;
}
bool spfa(int k,double V)
{	queue<int>q;
	int i,temp,x,y;
	double t;
	q.push(k);
	dis[k]=V,visited[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]<(t=(double)(dis[x]-de[i].comm)*de[i].rate))
			{	dis[y]=t;
				if(!visited[y])
				{	visited[y]=1;
					q.push(y);
					Enter[y]++;
					if(Enter[y]>=vexnum)
						return true;					
				}
			}
		}
	}
	return false;
}
int main()
{	int N,M,S;
	double V;
	int a,b;
	double a1,b1,a2,b2;
	while(scanf("%d %d %d %lf",&N,&M,&S,&V)!=EOF)
	{	vexnum=N,edgenum=0;
		memset(headlist,-1,sizeof(headlist));
		memset(dis,0,sizeof(dis));
		memset(visited,0,sizeof(visited));
		memset(Enter,0,sizeof(Enter));
		while(M--)
		{	scanf("%d %d %lf %lf %lf %lf",&a,&b,&a1,&b1,&a2,&b2); 
			Insert(a,b,a1,b1);
			Insert(b,a,a2,b2);
		}
		if(spfa(S,V)==true)		puts("YES");
		else					puts("NO");	
	}
	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