POJ 3259

17 篇文章 0 订阅
先翻译一下题目吧,这个题读的难度比做的难度大一点....
John有F个农场,这块农场有N块地方,共有M条路径和W个虫洞。通过路径要消耗一定时间,通过虫洞会回到过去,减少一定的时间。同时,路径是双向的,虫洞是单项的。
请判断john是否可以在农场随便转转再回到出发点的时间可以为负(可以回到过去),其实只要判断有没有负环就可以了。
注意题中对f m w的范围限制的描述,可以作答。

这道题我的时间是1000+ms,实在是太慢了,估计是用了new的缘故,以后new还是少用一点

#include<iostream>
#include "malloc.h" 
using namespace std;


//int dist[501];

struct edg{
	int s;
	int e;
	int t;
};
edg* edge[5200];
int dist[501];
bool case1[6];

int main()
{
	int f;
	int i,j,k;
	int n,m,w;
	int dist[501];
	edg* te;
	int ts0,te0,tt0;
	memset(case1,0,sizeof(case1));
	cin>>f;
	int all=0;
	for(i=0;i<f;i++)
	{
		cin>>n>>m>>w;
		//initialize
		
		//dist=(int*)malloc(n*sizeof(int));
		//edge=(edg**)malloc((m+w+1)*sizeof(edg*));
		all=0;
		memset(dist,999999,sizeof(dist));
		for(k=0;k<5200;k++)
			edge[k]=NULL;
		dist[1]=0;


		for(j=0;j<m;j++)
		{
			te=new edg;
			cin>>ts0>>te0>>tt0;
			te->s=ts0;
			te->e=te0;
			te->t=tt0;
			edge[all]=te;
			all++;
		
			te=new edg;
			te->e=ts0;
			te->s=te0;
			te->t=tt0;
			edge[all]=te;
			all++;
		}

		for(j=0;j<w;j++)
		{
			
			te=new edg;
			cin>>te->s>>te->e>>te->t;
			te->t=-te->t;
			edge[all]=te;
			all++;
		}

		for(k=0;k<m+w-1;k++)
		{
			for(j=0;edge[j]!=NULL;j++)
			{
				te=edge[j];
				if(dist[te->e]>dist[te->s]+te->t)
					dist[te->e]=dist[te->s]+te->t;
			}
		}


		for(j=0;edge[j]!=NULL;j++)
		{
			te=edge[j];
			if(dist[te->e]>dist[te->s]+te->t)
			{
				case1[i]=true;
				break;
			}
		}
	}
	for(i=0;i<f;i++)
	{
		if(case1[i]==true)
		{
			cout<<"YES"<<endl;
		}
		else
		{
			cout<<"NO"<<endl;
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值