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