Cod sursa(job #170024)

Utilizator razvi9Jurca Razvan razvi9 Data 2 aprilie 2008 12:30:11
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include<cstdio>
#include<vector>
int n,m,x,y,max,min,viz[250001];
char s[100],*p;
std::vector<std::pair<int,int> > a[250001];
#pragma region Read
inline void read1()
{
	gets(s);
	p=s;
	while(*p==' ') p++;
	n=0;
	while(*p>='0' && *p<='9') {
		n=n*10+*p-'0';
		p++;}
	while(*p==' ') p++;
	m=0;
	while(*p>='0' && *p<='9') {
		m=m*10+*p-'0';
		p++;}
	while(*p==' ') p++;
	x=0;
	while(*p>='0' && *p<='9') {
		x=x*10+*p-'0';
		p++;}
	while(*p==' ') p++;
	y=0;
	while(*p>='0' && *p<='9') {
		y=y*10+*p-'0';
		p++;}
}
inline void read()
{
	int x,y,c;
	gets(s);
	p=s;
	while(*p==' ') p++;
	x=0;
	while(*p>='0' && *p<='9'){
		x=x*10+*p-'0';
		p++;}
	while(*p==' ') p++;
	y=0;
	while(*p>='0' && *p<='9'){
		y=y*10+*p-'0';
		p++;}
	while(*p==' ') p++;
	c=0;
	while(*p>='0' && *p<='9'){
		c=c*10+*p-'0';
		p++;}
	if(x!=y){
		a[x].push_back(std::make_pair(y,c));
		if(c>max)  max=c;
		if(c<min)  min=c;}
}
#pragma endregion

int DF(int vf,int max)
{
	if(vf==y) return 1;
	viz[vf]=max;
	for(int i=0;i<a[vf].size();i++)
		if(a[vf][i].second<=max && viz[a[vf][i].first]!=max)
			if(DF(a[vf][i].first,max)) return 1;
	return 0;
}
int search(int st,int dr)
{
	if(st>dr) return 0;
	if(st==dr) 
		if(DF(x,st)) return st;
		else return 0;
	int mij=(st+dr)>>1;
	if(DF(x,mij)){
		int aux=search(st,mij-1);
		if(aux) return aux;
		else return mij;}
	else
		return search(mij+1,dr);
}

int main()
{
	freopen("pscnv.in","r",stdin);
	freopen("pscnv.out","w",stdout);
	read1();
	max=0;min=1001;
	for(int i=1;i<=m;i++)
		read();
	if(max==min) printf("%d\n",min);
	else
		printf("%d\n",search(min,max));
	fclose(stdout);
	return 0;
}