比赛 寒假归来,刮刮油 评测结果 AAAAAAAAAA
题目名称 分糖果 最终得分 100
用户昵称 ミント 运行时间 2.729 s
代码语言 C++ 内存使用 20.97 MiB
提交时间 2016-02-25 20:14:52
显示代码纯文本
#include <fstream>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
//#include <set>

using namespace std;

ifstream fin("dividecandy.in");
ofstream fout("dividecandy.out");

const int Maxn = 100000 + 100;
const int Maxp = 1000010 + 100;
bool Vis[Maxn];
//bool Dist[Maxn][Maxn];
int N, P, C, M;
int U, V, Kount = 1;
int Ans = 0;

class poi
{
    public:
		int Next, To;
}Edge[Maxp*2];
int Head[Maxp*2];

void Init()
{
	//memset(Dist, false, sizeof(Dist));
	memset(Vis, false, sizeof(Vis));
	memset(Head, -1, sizeof(Head));
	memset(Edge, 0, sizeof(Edge));
	
	return ;
}
void Add_Edge(int u, int v)
{
	Edge[Kount].To = v;
	Edge[Kount].Next = Head[u];
	Head[u] = Kount;
	
	Kount++;
}
void Read()
{
	fin>>N>>P>>C>>M;
	for(int i=1;i<=P;i++)
	{
		fin>>U>>V;
		
		Add_Edge(U, V);
		Add_Edge(V, U);
	}
	return ;
}
void BFS()
{
	queue<int> q, Dist;
	//set<int> s;
	
	q.push(C);
	Dist.push(1);
	//s.insert(C);
	Vis[C] = true;
	
	while(!q.empty())
	{
		int Now = q.front();
		int Now_Dist = Dist.front();
		
		
		
		//if(s.size()==N)break ;
		for(int i=Head[Now];i!=-1;i=Edge[i].Next)
			if(!Vis[Edge[i].To])
			{
				Vis[Edge[i].To] = true;
				q.push(Edge[i].To);
				Dist.push(Now_Dist+1);
			}
		//Ans += 1;
		Ans = Dist.front();
		
		q.pop();
		Dist.pop();
	}
	
	Ans += M;
	return ;
}
void Write()
{
	fout<<Ans<<endl;
	
	return ;
}
int main()
{
	Init();
	
	Read();
	
	BFS();
	
	Write();
	
	fin.close();
	fout.close();
	
	return 0;
}