Pluto_Rabbit's Blog

蒟蒻妹纸一枚,轻拍

VIJOS-P1053 Easy sssp

Jacinth posted @ 2015年8月07日 14:11 in VIJOS with tags VIJOS SPFA 图论 , 810 阅读

一道最短路的题,SPFA,然而很水。。。连我都A了

题目: https://vijos.org/p/1053

SPFA,SPFA,SPFA(重要的事情说三遍~~~)

有些坑爹的地方要判一下。。。然后稍微小小小小地优化一下(然而并不知道有没有用)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;

queue <int>  que;
int dis[1010],l[1010][1010],p[1010],v[1010][1010],a[1010];
bool f,used[1010];
int n,m,s,x,y,z;

void SPFA(int xx)
{
	for (int i=1;i<=n;++i) dis[i]=0xfffffff;
	memset(used,0,sizeof(used));
	memset(p,0,sizeof(p));
	int h=1;
	int t=1; 
	used[xx]=1;
	p[xx]=1; dis[xx]=0; que.push(xx);
	while (!que.empty())
	{
		int head=que.front();
		que.pop();
		used[head]=1;
		for (int i=1;i<=l[head][0];++i)
		{
			int pr=l[head][i];
			if (dis[pr]>dis[head]+v[head][pr])
			{
				dis[pr]=dis[head]+v[head][pr];
				if (!used[pr])
				{
					used[pr]=1;
					t++;
					que.push(pr);
					p[pr]++;
					if (p[pr]>n)
					{
						f=1;
						return;
					}
				}
			}
		}
		h++;
		used[head]=0;
		if (dis[xx]<0)
		{
			f=1;
			return;
		}
	}
	return;
}

int main()
{
	scanf("%d%d%d",&n,&m,&s);
	for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) v[i][j]=0xfffffff;
	for (int i=1;i<=n;++i) v[i][i]=0;
	for (int i=1;i<=m;++i)
	{
		scanf("%d%d%d",&x,&y,&z);
		if (z<v[x][y]) 
		{
			v[x][y]=z;
			l[x][++l[x][0]]=y;
		}
	}
	f=0;
	SPFA(s);
	if (f) 
	{
		puts("-1");
		return 0;
	}
	memcpy(a,dis,sizeof(long)*(n+1));
	for (int i=1;i<=n;++i)
	{
		if (a[i]==0xfffffff)
		{
			f=0;
			SPFA(i);
		 	if (f)
		 	{
	 			puts("-1");
	 			return 0;
	 		}
		}
	}
	for (int i=1;i<=n;++i)
	{
		if (a[i]==0xfffffff)
		{
			puts("NoPath");
		}
		else printf("%d\n",a[i]);
	}
	return 0;
}

其实变量名在第一遍写的时候打错是一件很恶心的事,因为调一调就要老久

PS. 今天最后一题文件名忘记改了,导致少了25分 不然rank就可以上去很多... 不爽。。。

Avatar_small
Assam HSLC Model Pap 说:
2022年8月27日 15:16

Every year Students Struggle to acquire the Pass marks in 10th Examination. Student Download Assam HSLC Question Paper 2023 After Regular Prepare to Participate in must take a printout of Assam Board HSLC Model Paper 2023 available our Website. Student’s use Previous Paper and Start Preparation Assam HSLC Final Exam 2023 Every year Students Struggle to acquire the Pass marks in 10th Examination. Assam HSLC Model Paper Student Download Assam HSLC Question Paper 2023 After Regular Prepare to Participate in must take a printout of Assam Board HSLC Model Paper 2023 available our Website. Student’s use Previous Paper and Start Preparation Assam HSLC Final Exam 2023

Avatar_small
AP SSC Hindi Model P 说:
2022年9月19日 00:28

Every student of class 10 can learn Hindi easily and this is or National language, Hindi is the most important language for every student, AP SSC Hindi Model Paper and the Board of Secondary Education, Andhra Pradesh (BSEAP) class 10th of SSC students also can download the AP 10th Hindi Model Paper 2023 Pdf with Solved question paper in chapter wise for all lessons of the course as AP SSC Hindi Model Paper 2023. Every student of class 10 can learn Hindi easily and this is or National language, Hindi is the most important language for every student.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter