比赛 20110916 评测结果 AAAAAAAATTW
题目名称 找工作 最终得分 72
用户昵称 Citron酱 运行时间 2.215 s
代码语言 C++ 内存使用 0.59 MiB
提交时间 2011-09-16 21:57:55
显示代码纯文本
#include <fstream>
#include <climits>

#define I_F "jobhunt.in"
#define O_F "jobhunt.out"

const int MAXn=220;

int n,s,d;
long map[MAXn][MAXn];
bool flag[MAXn][MAXn]={{false}};
bool flight[MAXn][MAXn]={{false}};
bool flight_arrived[MAXn]={false};
bool arrived[MAXn]={false};
long maxl[MAXn];
long flight_maxl[MAXn];
long ans=0;

bool Ph(const int);
void Input();
long Dfs(const int, const long, const bool, const long);
void Output();

int main()
{
	Input();
	if (ans!=-1)
		ans=Dfs(s,d,false,0);
	Output();
	return 0;
}

bool Ph(const int x)
{
	arrived[x]=true;
	for (int i=0; i<n; i++)
		if ((flag[x][i])&&(arrived[i]))
			return true;
		else if (flag[x][i])
		{
			if (Ph(i))
				return true;
		}
	arrived[x]=false;
	return false;
}

void Input()
{
	int p,f;
	int x,y,t;
	std::ifstream fin(I_F);
	fin>>d>>p>>n>>f>>s;
	s--;
	for (int i=0; i<p; i++)
	{
		fin>>x>>y;
		x--;
		y--;
		map[x][y]=((map[x][y]<d)||(!flag[x][y]))?d:map[x][y];
		flag[x][y]=true;
	}
	if (Ph(s))
		ans=-1;
	for (int i=0; i<f; i++)
	{
		fin>>x>>y>>t;
		x--;
		y--;
		map[x][y]=((map[x][y]<d-t)||(!flag[x][y]))?(d-t):map[x][y];
		flag[x][y]=true;
		if (map[x][y]==d-t)
			flight[x][y]=true;
	}
	fin.close();
}

long Dfs(const int x, const long lnow, const bool ifflight, const long lmax)
{
	if ((ifflight)&&(flight_arrived[x])&&(lnow>flight_maxl[x]))
		return -1;
	long t, tlmax=lmax, tmaxl=maxl[x], tflight_maxl=flight_maxl[x];
	bool tarrived=arrived[x], tflight_arrived=flight_arrived[x];
	maxl[x]=(maxl[x]>lnow)?maxl[x]:lnow;
	if (ifflight)
		flight_maxl[x]=(flight_maxl[x]>lnow)?flight_maxl[x]:lnow;
	else
		flight_maxl[x]=LONG_MAX+1;
	flight_arrived[x]=ifflight;
	arrived[x]=true;
	for (int i=0; i<n; i++)
		if (flag[x][i])
			if ((!arrived[i])||(map[x][i]+lnow>maxl[i]))
			{
				t=Dfs(i,lnow+map[x][i],((ifflight)||(flight[x][i])),(tlmax>lnow+map[x][i])?tlmax:lnow+map[x][i]);
				if (t==-1)
					return -1;
				tlmax=(tlmax>t)?tlmax:t;
			}
	arrived[x]=tarrived;
	flight_arrived[x]=tflight_arrived;
	maxl[x]=tmaxl;
	flight_maxl[x]=tflight_maxl;
	return tlmax;
}

void Output()
{
	std::ofstream fout(O_F);
	fout<<ans<<std::endl;
	fout.close();
}